Skip to content

Commit

Permalink
Dependencies to self are supported on class annotations
Browse files Browse the repository at this point in the history
Issue: #136
Signed-off-by: Alen Kosanović <alen.kosanovic@svgroup.hr>
  • Loading branch information
Alen Kosanović committed Jan 9, 2020
1 parent 3b34444 commit ddf8e24
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import java.util.Map;

import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.HasDescription;
import com.tngtech.archunit.base.Optional;
import com.tngtech.archunit.core.domain.properties.HasAnnotations;
import com.tngtech.archunit.core.domain.properties.HasOwner;
import com.tngtech.archunit.core.domain.properties.HasType;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaAnnotationBuilder;
Expand Down Expand Up @@ -139,5 +137,6 @@ public <A extends Annotation> A as(Class<A> annotationType) {
return AnnotationProxy.of(annotationType, this);
}

public interface JavaAnnotatedElement extends HasAnnotations, HasDescription {}
// TODO support annotation description

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public class JavaClass implements HasName.AndFullName, HasAnnotations, HasModifi
private final Set<JavaClass> subClasses = new HashSet<>();
private Optional<JavaClass> enclosingClass = Optional.absent();
private Optional<JavaClass> componentType = Optional.absent();
private Supplier<Map<String, JavaAnnotation>> annotations =
Suppliers.ofInstance(Collections.<String, JavaAnnotation>emptyMap());
private Map<String, JavaAnnotation> annotations = Collections.emptyMap();
private Supplier<Set<JavaMethod>> allMethods;
private Supplier<Set<JavaConstructor>> allConstructors;
private Supplier<Set<JavaField>> allFields;
Expand Down Expand Up @@ -244,13 +243,13 @@ public boolean isAnnotatedWith(Class<? extends Annotation> annotationType) {
@Override
@PublicAPI(usage = ACCESS)
public boolean isAnnotatedWith(String annotationTypeName) {
return annotations.get().containsKey(annotationTypeName);
return annotations.containsKey(annotationTypeName);
}

@Override
@PublicAPI(usage = ACCESS)
public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate) {
return CanBeAnnotated.Utils.isAnnotatedWith(annotations.get().values(), predicate);
return CanBeAnnotated.Utils.isAnnotatedWith(annotations.values(), predicate);
}

@Override
Expand All @@ -268,7 +267,7 @@ public boolean isMetaAnnotatedWith(String typeName) {
@Override
@PublicAPI(usage = ACCESS)
public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate) {
return CanBeAnnotated.Utils.isMetaAnnotatedWith(annotations.get().values(), predicate);
return CanBeAnnotated.Utils.isMetaAnnotatedWith(annotations.values(), predicate);
}

/**
Expand All @@ -294,7 +293,7 @@ public JavaAnnotation getAnnotationOfType(String typeName) {
@Override
@PublicAPI(usage = ACCESS)
public Set<JavaAnnotation> getAnnotations() {
return ImmutableSet.copyOf(annotations.get().values());
return ImmutableSet.copyOf(annotations.values());
}

/**
Expand All @@ -316,7 +315,7 @@ public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type)
@Override
@PublicAPI(usage = ACCESS)
public Optional<JavaAnnotation> tryGetAnnotationOfType(String typeName) {
return Optional.fromNullable(annotations.get().get(typeName));
return Optional.fromNullable(annotations.get(typeName));
}

@PublicAPI(usage = ACCESS)
Expand Down Expand Up @@ -897,12 +896,7 @@ void completeMembers(final ImportContext context) {
.addAll(methods)
.addAll(constructors)
.build();
this.annotations = Suppliers.memoize(new Supplier<Map<String, JavaAnnotation>>() {
@Override
public Map<String, JavaAnnotation> get() {
return context.createAnnotations(JavaClass.this);
}
});
annotations = context.createAnnotations(JavaClass.this);
}

CompletionProcess completeFrom(ImportContext context) {
Expand Down Expand Up @@ -1108,8 +1102,7 @@ private Set<Dependency> constructorParameterDependenciesToSelf() {
private Iterable<? extends Dependency> annotationDependenciesToSelf() {
Set<Dependency> result = new HashSet<>();
for (JavaAnnotation annotation : getAnnotationsWithTypeOfSelf()) {
// TODO see what to fill with
result.add(Dependency.fromAnnotation(null, annotation, null));
result.add(Dependency.fromAnnotation(annotation.getOwner(), annotation, annotation.getOwner()));
}
return result;
}
Expand Down Expand Up @@ -1162,7 +1155,7 @@ private static class MemberDependenciesOnClass {
this.methodsWithThrowsDeclarationTypeOfClass = ImmutableSet.copyOf(methodsWithThrowsDeclarationTypeOfClass);
this.constructorsWithParameterTypeOfClass = ImmutableSet.copyOf(constructorsWithParameterTypeOfClass);
this.constructorsWithThrowsDeclarationTypeOfClass = ImmutableSet.copyOf(constructorsWithThrowsDeclarationTypeOfClass);
this.annotationsWithTypeOfClass= ImmutableSet.copyOf(annotationsWithTypeOfClass);
this.annotationsWithTypeOfClass = ImmutableSet.copyOf(annotationsWithTypeOfClass);
}

Set<JavaField> getFieldsWithTypeOfClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ void registerConstructors(Set<JavaConstructor> constructors) {
}

void registerAnnotations(Collection<JavaAnnotation> annotations) {
for (JavaAnnotation annotation : annotations) {
annotationTypeDependencies.put(annotation.getType(), annotation);
}
}

Set<JavaField> getFieldsOfType(JavaClass javaClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,8 @@ public void direct_dependencies_from_self_by_annotation() {
.from(ClassWithAnnotationDependencies.class)
.to(WithType.class)
.inLineNumber(0))
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(MetaAnnotation.class)
.inLineNumber(0))
.areAtLeastOne(annotationMemberOfTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(B.class)
.inLineNumber(0))
;
// TODO test meta annotations and member annotations
// TODO test that annotation dependencies do not lead to a infinite loop
}

Expand Down Expand Up @@ -523,35 +516,14 @@ public void direct_dependencies_to_self_by_member_declarations() {

@Test
public void direct_dependencies_to_self_by_annotation() {
JavaClass javaClass = importClasses(ClassWithAnnotationDependencies.class, OnClass.class)
.get(ClassWithAnnotationDependencies.class);
JavaClasses javaClasses = importClasses(ClassWithAnnotationDependencies.class, OnClass.class);

assertThat(javaClass.getDirectDependenciesToSelf())
assertThat(javaClasses.get(OnClass.class).getDirectDependenciesToSelf())
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(OnClass.class)
.inLineNumber(0))
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(OnField.class)
.inLineNumber(0))
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(OnConstructor.class)
.inLineNumber(0))
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(OnMethod.class)
.inLineNumber(0))
.areAtLeastOne(annotationTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(WithType.class)
.inLineNumber(0))
.areAtLeastOne(annotationMemberOfTypeDependency()
.from(ClassWithAnnotationDependencies.class)
.to(B.class)
.inLineNumber(0))
;
.inLineNumber(0));
// TODO needs support for rest of annotations
}

@Test
Expand Down

0 comments on commit ddf8e24

Please sign in to comment.