Skip to content

Commit

Permalink
Review: Unify treatment of class and member annotations
Browse files Browse the repository at this point in the history
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 <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric authored and Alen Kosanović committed Jan 9, 2020
1 parent 06b7c64 commit 4795986
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,5 @@ public interface ImportContext {

Set<JavaAnnotation<?>> getAnnotationsWithParameterOfType(JavaClass javaClass);

Set<JavaMember> getMembersAnnotatedWithType(JavaClass javaClass);

Set<JavaMember> getMembersWithParametersOfType(JavaClass javaClass);

JavaClass resolveClass(String fullyQualifiedClassName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class JavaClassDependencies {
private final Set<ThrowsDeclaration<JavaConstructor>> constructorsWithThrowsDeclarationTypeOfClass;
private final Set<JavaAnnotation<?>> annotationsWithTypeOfClass;
private final Set<JavaAnnotation<?>> annotationsWithParameterTypeOfClass;
private final Set<JavaMember> membersWithAnnotationTypeOfClass;
private final Set<JavaMember> membersWithAnnotationParameterTypeOfClass;
private final Supplier<Set<Dependency>> directDependenciesFromClass;
private final Supplier<Set<Dependency>> directDependenciesToClass;

Expand All @@ -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();
}
Expand Down Expand Up @@ -139,18 +135,6 @@ Set<JavaAnnotation<?>> getAnnotationsWithTypeOfClass() {
return annotationsWithTypeOfClass;
}

private Set<JavaAnnotation<?>> getAnnotationsWithParameterTypeOfClass() {
return annotationsWithParameterTypeOfClass;
}

private Set<JavaMember> getMembersWithAnnotationTypeOfClass() {
return membersWithAnnotationTypeOfClass;
}

private Set<JavaMember> getMembersWithAnnotationParameterTypeOfClass() {
return membersWithAnnotationParameterTypeOfClass;
}

private Set<Dependency> dependenciesFromAccesses(Set<JavaAccess<?>> accesses) {
ImmutableSet.Builder<Dependency> result = ImmutableSet.builder();
for (JavaAccess<?> access : filterNoSelfAccess(accesses)) {
Expand Down Expand Up @@ -320,18 +304,10 @@ private Set<Dependency> constructorParameterDependenciesToSelf() {

private Iterable<? extends Dependency> annotationDependenciesToSelf() {
Set<Dependency> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -237,16 +237,6 @@ public Set<JavaAnnotation<?>> getAnnotationsWithParameterOfType(JavaClass javaCl
return memberDependenciesByTarget.getAnnotationsWithParameterOfType(javaClass);
}

@Override
public Set<JavaMember> getMembersAnnotatedWithType(JavaClass javaClass) {
return memberDependenciesByTarget.getMembersAnnotatedWithType(javaClass);
}

@Override
public Set<JavaMember> getMembersWithParametersOfType(JavaClass javaClass) {
return memberDependenciesByTarget.getMembersWithParametersOfType(javaClass);
}

private <T extends AccessTarget, B extends DomainBuilders.JavaAccessBuilder<T, B>>
B accessBuilderFrom(B builder, AccessRecord<T> record) {
return builder
Expand Down Expand Up @@ -330,8 +320,6 @@ private static class MemberDependenciesByTarget {
private final SetMultimap<JavaClass, ThrowsDeclaration<JavaConstructor>> constructorThrowsDeclarationDependencies = HashMultimap.create();
private final SetMultimap<JavaClass, JavaAnnotation<?>> annotationTypeDependencies = HashMultimap.create();
private final SetMultimap<JavaClass, JavaAnnotation<?>> annotationParameterTypeDependencies = HashMultimap.create();
private final SetMultimap<JavaClass, JavaMember> memberAnnotatedWithTypeDependencies = HashMultimap.create();
private final SetMultimap<JavaClass, JavaMember> memberAnnotatedWithParameterOfTypeDependencies = HashMultimap.create();

void registerFields(Set<JavaField> fields) {
for (JavaField field : fields) {
Expand Down Expand Up @@ -395,37 +383,11 @@ private void registerAnnotationParameter(JavaAnnotation<?> annotation, Object va
}
}

void registerMemberWithAnnotations(JavaMember member) {
void registerMemberAnnotations(JavaMember member) {
Set<? extends JavaAnnotation<? extends JavaMember>> annotations = member.getAnnotations();
for (JavaAnnotation<?> annotation : annotations) {
memberAnnotatedWithTypeDependencies.put(annotation.getRawType(), member);
registerMemberAnnotationParameters(member, annotation);
}
}

void registerMemberAnnotationParameters(JavaMember member, JavaAnnotation<?> annotation) {
for (Map.Entry<String, Object> 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);
}
}

Expand Down Expand Up @@ -460,14 +422,5 @@ Set<JavaAnnotation<?>> getAnnotationsOfType(JavaClass javaClass) {
Set<JavaAnnotation<?>> getAnnotationsWithParameterOfType(JavaClass javaClass) {
return annotationParameterTypeDependencies.get(javaClass);
}

Set<JavaMember> getMembersAnnotatedWithType(JavaClass javaClass) {
return memberAnnotatedWithTypeDependencies.get(javaClass);
}

Set<JavaMember> getMembersWithParametersOfType(JavaClass javaClass) {
return memberAnnotatedWithParameterOfTypeDependencies.get(javaClass);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -441,16 +440,6 @@ public Set<JavaAnnotation<?>> getAnnotationsWithParameterOfType(JavaClass javaCl
return Collections.emptySet();
}

@Override
public Set<JavaMember> getMembersAnnotatedWithType(JavaClass javaClass) {
return Collections.emptySet();
}

@Override
public Set<JavaMember> getMembersWithParametersOfType(JavaClass javaClass) {
return Collections.emptySet();
}

@Override
public JavaClass resolveClass(String fullyQualifiedClassName) {
throw new UnsupportedOperationException("Override me where necessary");
Expand Down

0 comments on commit 4795986

Please sign in to comment.