diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/DomainObjectCreationContext.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/DomainObjectCreationContext.java index 0bddbaed8b..22ddc552ce 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/DomainObjectCreationContext.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/DomainObjectCreationContext.java @@ -26,6 +26,7 @@ import com.google.common.base.Suppliers; import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Multimap; import com.google.common.collect.SetMultimap; import com.tngtech.archunit.Internal; import com.tngtech.archunit.base.Function; @@ -190,62 +191,87 @@ void finish() { } private Supplier> getFieldAccessesTo(final JavaField field) { - return newAccessSupplier(field.getOwner(), fieldAccessTargetResolvesTo(field)); + return Suppliers.memoize(new AccessSupplier<>(field.getOwner(), fieldAccessTargetResolvesTo(field))); } private Function> fieldAccessTargetResolvesTo(final JavaField field) { - return new Function>() { - @Override - public Set apply(JavaClass input) { - Set result = new HashSet<>(); - for (JavaFieldAccess access : fieldAccessesByTarget.get(input)) { - if (access.getTarget().resolveField().asSet().contains(field)) { - result.add(access); - } - } - return result; - } - }; + return new ClassToFieldAccessesToSelf(fieldAccessesByTarget, field); } private Supplier> getMethodCallsOf(final JavaMethod method) { - return newAccessSupplier(method.getOwner(), methodCallTargetResolvesTo(method)); + return Suppliers.memoize(new AccessSupplier<>(method.getOwner(), methodCallTargetResolvesTo(method))); } private Function> methodCallTargetResolvesTo(final JavaMethod method) { - return new Function>() { - @Override - public Set apply(JavaClass input) { - Set result = new HashSet<>(); - for (JavaMethodCall call : methodCallsByTarget.get(input)) { - if (call.getTarget().resolve().contains(method)) { - result.add(call); - } + return new ClassToMethodCallsToSelf(methodCallsByTarget, method); + } + + private static class ClassToFieldAccessesToSelf implements Function> { + private final Multimap fieldAccessesByTarget; + private final JavaField field; + + ClassToFieldAccessesToSelf(SetMultimap fieldAccessesByTarget, JavaField field) { + this.fieldAccessesByTarget = fieldAccessesByTarget; + this.field = field; + } + + @Override + public Set apply(JavaClass input) { + Set result = new HashSet<>(); + for (JavaFieldAccess access : fieldAccessesByTarget.get(input)) { + if (access.getTarget().resolveField().asSet().contains(field)) { + result.add(access); } - return result; } - }; + return result; + } } - private Supplier> newAccessSupplier(final JavaClass owner, final Function> doWithEachClass) { + private static class ClassToMethodCallsToSelf implements Function> { + private final Multimap methodCallsByTarget; + private final JavaMethod method; + + ClassToMethodCallsToSelf(SetMultimap methodCallsByTarget, JavaMethod method) { + this.methodCallsByTarget = methodCallsByTarget; + this.method = method; + } - return Suppliers.memoize(new Supplier>() { - @Override - public Set get() { - ImmutableSet.Builder result = ImmutableSet.builder(); - for (final JavaClass javaClass : getPossibleTargetClassesForAccess()) { - result.addAll(doWithEachClass.apply(javaClass)); + @Override + public Set apply(JavaClass input) { + Set result = new HashSet<>(); + for (JavaMethodCall call : methodCallsByTarget.get(input)) { + if (call.getTarget().resolve().contains(method)) { + result.add(call); } - return result.build(); } + return result; + } + } - private Set getPossibleTargetClassesForAccess() { - return ImmutableSet.builder() - .add(owner) - .addAll(owner.getAllSubClasses()) - .build(); + private static class AccessSupplier> implements Supplier> { + private final JavaClass owner; + private final Function> mapToAccesses; + + AccessSupplier(JavaClass owner, Function> mapToAccesses) { + this.owner = owner; + this.mapToAccesses = mapToAccesses; + } + + @Override + public Set get() { + ImmutableSet.Builder result = ImmutableSet.builder(); + for (final JavaClass javaClass : getPossibleTargetClassesForAccess()) { + result.addAll(mapToAccesses.apply(javaClass)); } - }); + return result.build(); + } + + private Set getPossibleTargetClassesForAccess() { + return ImmutableSet.builder() + .add(owner) + .addAll(owner.getAllSubClasses()) + .build(); + } } } } diff --git a/archunit/src/main/java/com/tngtech/archunit/core/importer/DomainBuilders.java b/archunit/src/main/java/com/tngtech/archunit/core/importer/DomainBuilders.java index ccb0876ceb..37820e555d 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/importer/DomainBuilders.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/importer/DomainBuilders.java @@ -156,12 +156,7 @@ public String getName() { } public Supplier> getAnnotations() { - return Suppliers.memoize(new Supplier>() { - @Override - public Map get() { - return buildAnnotations(annotations, importedClasses); - } - }); + return Suppliers.memoize(new AnnotationsSupplier(annotations, importedClasses)); } public String getDescriptor() { @@ -182,6 +177,21 @@ public final OUTPUT build(JavaClass owner, ClassesByTypeName importedClasses) { this.importedClasses = importedClasses; return construct(self(), importedClasses); } + + private static class AnnotationsSupplier implements Supplier> { + private final Set annotations; + private final ClassesByTypeName importedClasses; + + AnnotationsSupplier(Set annotations, ClassesByTypeName importedClasses) { + this.annotations = annotations; + this.importedClasses = importedClasses; + } + + @Override + public Map get() { + return buildAnnotations(annotations, importedClasses); + } + } } @Internal