Skip to content

Commit

Permalink
JavaAccessCondition now ignores accesses from self to self. In partic…
Browse files Browse the repository at this point in the history
…ular, self-accesses don't have to be manually ignored anymore, when onlyAccessedBy().. is checked. This resolves #3, since for any smarter ignores, it would be necessary to talk about better fitting objects, than just classes filtered by any criteria.
  • Loading branch information
codecholeric committed May 20, 2017
1 parent ebdc4a7 commit fbca4c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class JavaAccessCondition extends ArchCondition<JavaAccess<?>> {

@Override
public void check(JavaAccess<?> item, ConditionEvents events) {
events.add(new SimpleConditionEvent<>(item, predicate.apply(item), item.getDescription()));
if (!item.getOriginOwner().equals(item.getTargetOwner())) {
events.add(new SimpleConditionEvent<>(item, predicate.apply(item), item.getDescription()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.core.domain.properties.HasType;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.syntax.elements.testclasses.access.ClassAccessingOtherClass;
import com.tngtech.archunit.lang.syntax.elements.testclasses.accessed.ClassBeingAccessedByOtherClass;
import com.tngtech.archunit.lang.syntax.elements.testclasses.anotheraccess.YetAnotherClassAccessingOtherClass;
Expand Down Expand Up @@ -530,6 +531,15 @@ public void byClassesThat_predicate() {
ClassImplementingSomeInterface.class, ClassBeingAccessedByClassImplementingSomeInterface.class);
}

@Test
public void accesses_by_the_class_itself_are_ignored() {
ClassesShouldConjunction rule = classes().should().onlyBeAccessed()
.byClassesThat(classWithNameOf(ClassAccessingClassAccessingItself.class));

rule.check(new ClassFileImporter().importClasses(
ClassAccessingItself.class, ClassAccessingClassAccessingItself.class));
}

private DescribedPredicate<HasName> classWithNameOf(Class<?> type) {
return GET_NAME.is(equalTo(type.getName()));
}
Expand Down Expand Up @@ -642,6 +652,19 @@ private static class ClassExtendingClass extends ClassImplementingSomeInterface
}

private static class ClassBeingAccessedByClassImplementingSomeInterface {
}

private static class ClassAccessingItself {
private String field;

ClassAccessingItself(String field) {
this.field = field;
}
}

private static class ClassAccessingClassAccessingItself {
void call() {
new ClassAccessingItself("foo");
}
}
}

0 comments on commit fbca4c9

Please sign in to comment.