Skip to content

Commit

Permalink
Suppliers are added back to annotations in JavaClasses and JavaMembers
Browse files Browse the repository at this point in the history
Issue: TNG#136
  • Loading branch information
Alen Kosanović committed Mar 26, 2019
1 parent dbad841 commit b5936ac
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ static Dependency from(JavaAccess<?> access) {
}

static Dependency fromInheritance(JavaClass origin, JavaClass targetSuperType) {
String originDescription = origin.getDescription();
String originType = origin.isInterface() ? "Interface" : "Class";
String originDescription = originType + " " + bracketFormat(origin.getName());

String dependencyType = !origin.isInterface() && targetSuperType.isInterface() ? "implements" : "extends";

String targetType = targetSuperType.isInterface() ? "interface" : "class";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public class JavaClass implements HasName.AndFullName, HasAnnotations, HasModifi
private final Set<JavaClass> interfaces = new HashSet<>();
private final Set<JavaClass> subClasses = new HashSet<>();
private Optional<JavaClass> enclosingClass = Optional.absent();
private Map<String, JavaAnnotation> annotations = Collections.emptyMap();
private Supplier<Map<String, JavaAnnotation>> annotations =
Suppliers.ofInstance(Collections.<String, JavaAnnotation>emptyMap());
private Supplier<Set<JavaMethod>> allMethods;
private Supplier<Set<JavaConstructor>> allConstructors;
private Supplier<Set<JavaField>> allFields;
Expand Down Expand Up @@ -121,8 +122,7 @@ public SourceCodeLocation getSourceCodeLocation() {

@Override
public String getDescription() {
String originType = isInterface() ? "Interface" : "Class";
return originType + " <" + getName() + ">";
return "Class <" + getName() + ">";
}

@Override
Expand Down Expand Up @@ -191,12 +191,12 @@ public boolean isAnnotatedWith(Class<? extends Annotation> annotationType) {

@Override
public boolean isAnnotatedWith(String annotationTypeName) {
return annotations.containsKey(annotationTypeName);
return annotations.get().containsKey(annotationTypeName);
}

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

@Override
Expand All @@ -211,7 +211,7 @@ public boolean isMetaAnnotatedWith(String typeName) {

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

/**
Expand All @@ -234,7 +234,7 @@ public JavaAnnotation getAnnotationOfType(String typeName) {

@Override
public Set<JavaAnnotation> getAnnotations() {
return ImmutableSet.copyOf(annotations.values());
return ImmutableSet.copyOf(annotations.get().values());
}

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

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

CompletionProcess completeFrom(ImportContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Set;

import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.tngtech.archunit.Internal;
import com.tngtech.archunit.PublicAPI;
Expand All @@ -31,9 +32,9 @@
import com.tngtech.archunit.core.domain.properties.HasDescriptor;
import com.tngtech.archunit.core.domain.properties.HasModifiers;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;
import com.tngtech.archunit.core.domain.properties.HasOwner;
import com.tngtech.archunit.core.domain.properties.HasOwner.Functions.Get;
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;
import com.tngtech.archunit.core.importer.DomainBuilders.JavaMemberBuilder;

import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -48,7 +49,7 @@ public abstract class JavaMember implements

private final String name;
private final String descriptor;
private final Map<String, JavaAnnotation> annotations;
private final Supplier<Map<String, JavaAnnotation>> annotations;
private final JavaClass owner;
private final SourceCodeLocation sourceCodeLocation;
private final Set<JavaModifier> modifiers;
Expand All @@ -64,7 +65,7 @@ public abstract class JavaMember implements

@Override
public Set<JavaAnnotation> getAnnotations() {
return ImmutableSet.copyOf(annotations.values());
return ImmutableSet.copyOf(annotations.get().values());
}

/**
Expand All @@ -91,7 +92,7 @@ public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type)

@Override
public Optional<JavaAnnotation> tryGetAnnotationOfType(String typeName) {
return Optional.fromNullable(annotations.get(typeName));
return Optional.fromNullable(annotations.get().get(typeName));
}

@Override
Expand All @@ -101,12 +102,12 @@ public boolean isAnnotatedWith(Class<? extends Annotation> type) {

@Override
public boolean isAnnotatedWith(String typeName) {
return annotations.containsKey(typeName);
return annotations.get().containsKey(typeName);
}

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

@Override
Expand All @@ -121,7 +122,7 @@ public boolean isMetaAnnotatedWith(String typeName) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ JavaClasses complete() {
ensureCallTargetsArePresent();
ensureClassHierarchies();
completeMembers();
completeAnnotations();
for (RawAccessRecord.ForField fieldAccessRecord : importRecord.getRawFieldAccessRecords()) {
tryProcess(fieldAccessRecord, AccessRecord.Factory.forFieldAccessRecord(), processedFieldAccessRecords);
}
Expand Down Expand Up @@ -142,6 +143,12 @@ private void completeMembers() {
}
}

private void completeAnnotations() {
for (JavaClass javaClass : classes.getAll().values()) {
javaClass.getAnnotations();
}
}

private <T extends AccessRecord<?>, B extends RawAccessRecord> void tryProcess(
B rawRecord,
AccessRecord.Factory<B, T> factory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ public String getName() {
return name;
}

public Map<String, JavaAnnotation> getAnnotations() {
return buildAnnotations(owner, annotations, importedClasses);
public Supplier<Map<String, JavaAnnotation>> getAnnotations() {
return Suppliers.memoize(new Supplier<Map<String, JavaAnnotation>>() {
@Override
public Map<String, JavaAnnotation> get() {
return buildAnnotations(owner, annotations, importedClasses);
}
});
}

public String getDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void Dependency_from_annotation_on_class() {
assertThat(dependency.getOriginClass()).matches(ClassWithDependencyOnAnnotation.class);
assertThat(dependency.getTargetClass()).matches(annotationClass);
assertThat(dependency.getDescription()).as("description")
.contains("Class <" + origin.getName() + "> has annotation <" + annotationClass.getName() + ">");
.contains("Class <" + origin.getName() + "> is annotated with <" + annotationClass.getName() + ">");

origin = importClassesWithContext(InterfaceWithDependencyOnAnnotation.class, SomeAnnotation.class)
.get(InterfaceWithDependencyOnAnnotation.class);
Expand All @@ -86,7 +86,7 @@ public void Dependency_from_annotation_on_class() {
assertThat(dependency.getOriginClass()).matches(InterfaceWithDependencyOnAnnotation.class);
assertThat(dependency.getTargetClass()).matches(annotationClass);
assertThat(dependency.getDescription()).as("description")
.contains("Interface <" + origin.getName() + "> has annotation <" + annotationClass.getName() + ">");
.contains("Class <" + origin.getName() + "> is annotated with <" + annotationClass.getName() + ">");
}

@Test
Expand All @@ -102,7 +102,7 @@ public void Dependency_from_annotation_on_member() {
assertThat(dependency.getOriginClass()).matches(ClassWithAnnotatedField.class);
assertThat(dependency.getTargetClass()).matches(annotationClass);
assertThat(dependency.getDescription()).as("description")
.contains(origin.getDescription() + " has annotation <" + annotationClass.getName() + ">");
.contains(origin.getDescription() + " is annotated with <" + annotationClass.getName() + ">");
}

@Test
Expand Down

0 comments on commit b5936ac

Please sign in to comment.