From 47959861dde72bb12f2f71c0e4f37708933b7332 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Mon, 4 Nov 2019 08:09:07 +0100 Subject: [PATCH] Review: Unify treatment of class and member annotations Since we now have a consistent approach to finding annotation dependencies, where we handle as well class as member owners, we do not need to distinguish anymore before. Issue: #136 Signed-off-by: Peter Gafert --- .../archunit/core/domain/ImportContext.java | 4 -- .../core/domain/JavaClassDependencies.java | 28 +-------- .../core/importer/ClassGraphCreator.java | 59 ++----------------- .../core/importer/ImportTestUtils.java | 11 ---- 4 files changed, 8 insertions(+), 94 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/ImportContext.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/ImportContext.java index af770e2656..3b5b4fff9d 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/ImportContext.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/ImportContext.java @@ -61,9 +61,5 @@ public interface ImportContext { Set> getAnnotationsWithParameterOfType(JavaClass javaClass); - Set getMembersAnnotatedWithType(JavaClass javaClass); - - Set getMembersWithParametersOfType(JavaClass javaClass); - JavaClass resolveClass(String fullyQualifiedClassName); } diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClassDependencies.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClassDependencies.java index 6d0701900a..1fd539f325 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClassDependencies.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClassDependencies.java @@ -42,8 +42,6 @@ class JavaClassDependencies { private final Set> constructorsWithThrowsDeclarationTypeOfClass; private final Set> annotationsWithTypeOfClass; private final Set> annotationsWithParameterTypeOfClass; - private final Set membersWithAnnotationTypeOfClass; - private final Set membersWithAnnotationParameterTypeOfClass; private final Supplier> directDependenciesFromClass; private final Supplier> directDependenciesToClass; @@ -57,8 +55,6 @@ class JavaClassDependencies { this.constructorsWithThrowsDeclarationTypeOfClass = context.getConstructorThrowsDeclarationsOfType(javaClass); this.annotationsWithTypeOfClass = context.getAnnotationsOfType(javaClass); this.annotationsWithParameterTypeOfClass = context.getAnnotationsWithParameterOfType(javaClass); - this.membersWithAnnotationTypeOfClass = context.getMembersAnnotatedWithType(javaClass); - this.membersWithAnnotationParameterTypeOfClass = context.getMembersWithParametersOfType(javaClass); this.directDependenciesFromClass = getDirectDependenciesFromClassSupplier(); this.directDependenciesToClass = getDirectDependenciesToClassSupplier(); } @@ -139,18 +135,6 @@ Set> getAnnotationsWithTypeOfClass() { return annotationsWithTypeOfClass; } - private Set> getAnnotationsWithParameterTypeOfClass() { - return annotationsWithParameterTypeOfClass; - } - - private Set getMembersWithAnnotationTypeOfClass() { - return membersWithAnnotationTypeOfClass; - } - - private Set getMembersWithAnnotationParameterTypeOfClass() { - return membersWithAnnotationParameterTypeOfClass; - } - private Set dependenciesFromAccesses(Set> accesses) { ImmutableSet.Builder result = ImmutableSet.builder(); for (JavaAccess access : filterNoSelfAccess(accesses)) { @@ -320,18 +304,10 @@ private Set constructorParameterDependenciesToSelf() { private Iterable annotationDependenciesToSelf() { Set result = new HashSet<>(); - for (JavaAnnotation annotation : javaClass.getAnnotationsWithTypeOfSelf()) { - result.add(Dependency.fromAnnotation(annotation)); - } - for (JavaAnnotation annotation : getAnnotationsWithParameterTypeOfClass()) { - result.add(Dependency.fromAnnotationMember(annotation, javaClass)); - } - for (JavaMember member : getMembersWithAnnotationTypeOfClass()) { - JavaAnnotation annotation = member.getAnnotationOfType(javaClass.getName()); + for (JavaAnnotation annotation : annotationsWithTypeOfClass) { result.add(Dependency.fromAnnotation(annotation)); } - for (JavaMember member : getMembersWithAnnotationParameterTypeOfClass()) { - JavaAnnotation annotation = member.getAnnotationOfType(javaClass.getName()); + for (JavaAnnotation annotation : annotationsWithParameterTypeOfClass) { result.add(Dependency.fromAnnotationMember(annotation, javaClass)); } return result; diff --git a/archunit/src/main/java/com/tngtech/archunit/core/importer/ClassGraphCreator.java b/archunit/src/main/java/com/tngtech/archunit/core/importer/ClassGraphCreator.java index ed2b8ee6df..6ab3195710 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/importer/ClassGraphCreator.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/importer/ClassGraphCreator.java @@ -148,13 +148,13 @@ private void completeAnnotations() { // By invoking the .get method of the annotations supplier, annotations are registered into the ImportContext javaClass.getAnnotations(); for (JavaMember member : javaClass.getFields()) { - memberDependenciesByTarget.registerMemberWithAnnotations(member); + memberDependenciesByTarget.registerMemberAnnotations(member); } for (JavaMember member : javaClass.getMethods()) { - memberDependenciesByTarget.registerMemberWithAnnotations(member); + memberDependenciesByTarget.registerMemberAnnotations(member); } for (JavaMember member : javaClass.getConstructors()) { - memberDependenciesByTarget.registerMemberWithAnnotations(member); + memberDependenciesByTarget.registerMemberAnnotations(member); } } } @@ -237,16 +237,6 @@ public Set> getAnnotationsWithParameterOfType(JavaClass javaCl return memberDependenciesByTarget.getAnnotationsWithParameterOfType(javaClass); } - @Override - public Set getMembersAnnotatedWithType(JavaClass javaClass) { - return memberDependenciesByTarget.getMembersAnnotatedWithType(javaClass); - } - - @Override - public Set getMembersWithParametersOfType(JavaClass javaClass) { - return memberDependenciesByTarget.getMembersWithParametersOfType(javaClass); - } - private > B accessBuilderFrom(B builder, AccessRecord record) { return builder @@ -330,8 +320,6 @@ private static class MemberDependenciesByTarget { private final SetMultimap> constructorThrowsDeclarationDependencies = HashMultimap.create(); private final SetMultimap> annotationTypeDependencies = HashMultimap.create(); private final SetMultimap> annotationParameterTypeDependencies = HashMultimap.create(); - private final SetMultimap memberAnnotatedWithTypeDependencies = HashMultimap.create(); - private final SetMultimap memberAnnotatedWithParameterOfTypeDependencies = HashMultimap.create(); void registerFields(Set fields) { for (JavaField field : fields) { @@ -395,37 +383,11 @@ private void registerAnnotationParameter(JavaAnnotation annotation, Object va } } - void registerMemberWithAnnotations(JavaMember member) { + void registerMemberAnnotations(JavaMember member) { Set> annotations = member.getAnnotations(); for (JavaAnnotation annotation : annotations) { - memberAnnotatedWithTypeDependencies.put(annotation.getRawType(), member); - registerMemberAnnotationParameters(member, annotation); - } - } - - void registerMemberAnnotationParameters(JavaMember member, JavaAnnotation annotation) { - for (Map.Entry entry : annotation.getProperties().entrySet()) { - Object value = entry.getValue(); - if (value.getClass().isArray()) { - if (!value.getClass().getComponentType().isPrimitive()) { - Object[] values = (Object[]) value; - for (Object o : values) { - registerMemberAnnotationParameter(member, o); - } - } - } else { - registerMemberAnnotationParameter(member, value); - } - } - } - - private void registerMemberAnnotationParameter(JavaMember member, Object value) { - if (value instanceof JavaClass) { - memberAnnotatedWithParameterOfTypeDependencies.put((JavaClass) value, member); - } else if (value instanceof JavaAnnotation) { - JavaAnnotation memberAnnotation = (JavaAnnotation) value; - memberAnnotatedWithParameterOfTypeDependencies.put(memberAnnotation.getRawType(), member); - registerMemberAnnotationParameters(member, memberAnnotation); + annotationTypeDependencies.put(annotation.getRawType(), annotation); + registerAnnotationParameters(annotation); } } @@ -460,14 +422,5 @@ Set> getAnnotationsOfType(JavaClass javaClass) { Set> getAnnotationsWithParameterOfType(JavaClass javaClass) { return annotationParameterTypeDependencies.get(javaClass); } - - Set getMembersAnnotatedWithType(JavaClass javaClass) { - return memberAnnotatedWithTypeDependencies.get(javaClass); - } - - Set getMembersWithParametersOfType(JavaClass javaClass) { - return memberAnnotatedWithParameterOfTypeDependencies.get(javaClass); - } - } } diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportTestUtils.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportTestUtils.java index 8503e963fb..a5ad7a5a7b 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportTestUtils.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/ImportTestUtils.java @@ -32,7 +32,6 @@ import com.tngtech.archunit.core.domain.JavaEnumConstant; import com.tngtech.archunit.core.domain.JavaField; import com.tngtech.archunit.core.domain.JavaFieldAccess; -import com.tngtech.archunit.core.domain.JavaMember; import com.tngtech.archunit.core.domain.JavaMethod; import com.tngtech.archunit.core.domain.JavaMethodCall; import com.tngtech.archunit.core.domain.JavaModifier; @@ -441,16 +440,6 @@ public Set> getAnnotationsWithParameterOfType(JavaClass javaCl return Collections.emptySet(); } - @Override - public Set getMembersAnnotatedWithType(JavaClass javaClass) { - return Collections.emptySet(); - } - - @Override - public Set getMembersWithParametersOfType(JavaClass javaClass) { - return Collections.emptySet(); - } - @Override public JavaClass resolveClass(String fullyQualifiedClassName) { throw new UnsupportedOperationException("Override me where necessary");