Skip to content

Commit

Permalink
Review: Replace Supplier<Map> annotations by a simple Map
Browse files Browse the repository at this point in the history
By completing the annotations in a later step we can avoid the complexity of the annotations Supplier. Since we have to access this supplier anyway for the import process, there is no value in creating annotations lazily.

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 f99c992 commit 1230b64
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static void completeMembers(JavaClass javaClass, ImportContext importCont
javaClass.completeMembers(importContext);
}

public static void completeAnnotations(JavaClass javaClass, ImportContext importContext) {
javaClass.completeAnnotations(importContext);
}

public static <T extends HasDescription> JavaAnnotation<T> createJavaAnnotation(T owner, JavaAnnotationBuilder builder) {
return new JavaAnnotation<>(owner, builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Utils.toAnnotationOfType;
import static com.tngtech.archunit.core.domain.properties.HasName.Functions.GET_NAME;
import static com.tngtech.archunit.core.domain.properties.HasType.Functions.GET_RAW_TYPE;
import static java.util.Collections.emptyMap;

public class JavaClass implements HasName.AndFullName, HasAnnotations<JavaClass>, HasModifiers, HasSourceCodeLocation {
private final Optional<Source> source;
Expand All @@ -78,8 +79,7 @@ public class JavaClass implements HasName.AndFullName, HasAnnotations<JavaClass>
private final Set<JavaClass> subClasses = new HashSet<>();
private Optional<JavaClass> enclosingClass = Optional.absent();
private Optional<JavaClass> componentType = Optional.absent();
private Supplier<Map<String, JavaAnnotation<JavaClass>>> annotations =
Suppliers.ofInstance(Collections.<String, JavaAnnotation<JavaClass>>emptyMap());
private Map<String, JavaAnnotation<JavaClass>> annotations = emptyMap();
private Supplier<Set<JavaMethod>> allMethods;
private Supplier<Set<JavaConstructor>> allConstructors;
private Supplier<Set<JavaField>> allFields;
Expand Down Expand Up @@ -240,13 +240,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 @@ -264,7 +264,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 @@ -290,7 +290,7 @@ public JavaAnnotation<JavaClass> getAnnotationOfType(String typeName) {
@Override
@PublicAPI(usage = ACCESS)
public Set<JavaAnnotation<JavaClass>> getAnnotations() {
return ImmutableSet.copyOf(annotations.get().values());
return ImmutableSet.copyOf(annotations.values());
}

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

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

void completeAnnotations(final ImportContext context) {
this.annotations = context.createAnnotations(this);
}

CompletionProcess completeFrom(ImportContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ private void completeMembers() {

private void completeAnnotations() {
for (JavaClass javaClass : classes.getAll().values()) {
// By invoking the .get method of the annotations supplier, annotations are registered into the ImportContext
javaClass.getAnnotations();
DomainObjectCreationContext.completeAnnotations(javaClass, this);
for (JavaMember member : javaClass.getFields()) {
memberDependenciesByTarget.registerMemberAnnotations(member);
}
Expand Down

0 comments on commit 1230b64

Please sign in to comment.