Skip to content

Commit

Permalink
include nested structures in search of annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrimatti committed Dec 29, 2023
1 parent d1d7e85 commit a46f466
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.solita.utils</groupId>
<artifactId>meta-utils</artifactId>
<version>0.12.25</version>
<version>0.12.26</version>
<build>
<resources>
<resource>
Expand Down Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>fi.solita.utils</groupId>
<artifactId>functional-utils</artifactId>
<version>0.12.39</version>
<version>0.12.42</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public boolean accept(Element candidate) {
//msg.printMessage(Kind.OTHER, "Skipping due to being generated element: " + candidate.getSimpleName());
return false;
}
if (!includeAllByAnnotation.accept(candidate) && !withAnnotations(includesAnnotation()).accept(candidate)) {
if (!includeAllByAnnotation.accept(candidate) && !withAnnotations(includesAnnotation(), true).accept(candidate)) {
//msg.printMessage(Kind.OTHER, "Skipping due to not included by annotation: " + candidate.getSimpleName());
return false;
}
if (withAnnotations(excludesAnnotation()).accept(candidate)) {
if (withAnnotations(excludesAnnotation(), true).accept(candidate)) {
//msg.printMessage(Kind.OTHER, "Skipping due to being excluded by annotation: " + candidate.getSimpleName());
return false;
}
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/fi/solita/utils/meta/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static fi.solita.utils.functional.Functional.filter;
import static fi.solita.utils.functional.Functional.head;
import static fi.solita.utils.functional.Functional.map;
import static fi.solita.utils.functional.Functional.flatMap;
import static fi.solita.utils.functional.Functional.reduce;
import static fi.solita.utils.functional.Functional.zip;
import static fi.solita.utils.functional.FunctionalC.reverse;
Expand Down Expand Up @@ -740,7 +741,7 @@ public String transform(VariableElement source) {
return map(join, zip(argCasts, map(simpleName, parameters)));
}

public static final Predicate<Element> withAnnotations(final String classNames) {
public static final Predicate<Element> withAnnotations(final String classNames, final boolean includeNested) {
final Set<String> className = newSet(classNames.split(","));
return new Predicate<Element>() {
@Override
Expand All @@ -753,13 +754,44 @@ private boolean acc(final Element e, final Set<Element> visited) {
return false;
}
visited.add(e);

Iterable<AnnotationMirror> constructorAnnotations;
Iterable<AnnotationMirror> methodAnnotations;
Iterable<AnnotationMirror> fieldAnnotations;
if (includeNested) {
for (TypeElement n: element2NestedClasses.apply(e)) {
if (acc(n, visited)) {
// annotation exists on a nested class
return true;
}
}
constructorAnnotations = flatMap(new Transformer<ExecutableElement,List<? extends AnnotationMirror>>() {
@Override
public List<? extends AnnotationMirror> transform(ExecutableElement source) {
return source.getAnnotationMirrors();
}}, element2Constructors.apply(e));
methodAnnotations = flatMap(new Transformer<ExecutableElement,List<? extends AnnotationMirror>>() {
@Override
public List<? extends AnnotationMirror> transform(ExecutableElement source) {
return source.getAnnotationMirrors();
}}, element2Methods.apply(e));
fieldAnnotations = flatMap(new Transformer<VariableElement,List<? extends AnnotationMirror>>() {
@Override
public List<? extends AnnotationMirror> transform(VariableElement source) {
return source.getAnnotationMirrors();
}}, element2Fields.apply(e));
} else {
constructorAnnotations = Collections.emptyList();
methodAnnotations = Collections.emptyList();
fieldAnnotations = Collections.emptyList();
}
return exists(new Predicate<AnnotationMirror>() {
@Override
public boolean accept(AnnotationMirror m) {
Element elem = m.getAnnotationType().asElement();
return className.contains(qualifiedName.apply(elem)) || !elem.equals(e) && acc(elem, visited);
}
}, e.getAnnotationMirrors());
}, concat(e.getAnnotationMirrors(), constructorAnnotations, methodAnnotations, fieldAnnotations));
}
};
}
Expand Down

0 comments on commit a46f466

Please sign in to comment.