Skip to content

Commit

Permalink
Look at annotations on method returns and params (#160)
Browse files Browse the repository at this point in the history
Also, make sure this works for anonymous inner classes.
  • Loading branch information
illicitonion authored Mar 8, 2023
1 parent ed064ec commit 9586f4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public Void visitMethod(com.sun.source.tree.MethodTree m, Void v) {
checkFullyQualifiedType(m.getReturnType());
}

handleAnnotations(m.getModifiers().getAnnotations());

// Check to see if we have a main method
if (m.getName().toString().equals("main")
&& m.getModifiers().getFlags().containsAll(Set.of(STATIC, PUBLIC))
Expand All @@ -228,15 +230,19 @@ public Void visitMethod(com.sun.source.tree.MethodTree m, Void v) {
}

// Check the parameters for the method
// Identifier or Member Select -> May be a fully qualifyed type name
// Parameterized Type -> The generics values may be qualified.
// Arry -> The array may be a fully qul
for (VariableTree param : m.getParameters()) {
checkFullyQualifiedType(param.getType());
handleAnnotations(param.getModifiers().getAnnotations());
}
return super.visitMethod(m, v);
}

private void handleAnnotations(List<? extends AnnotationTree> annotations) {
for (AnnotationTree annotation : annotations) {
checkFullyQualifiedType(annotation.getAnnotationType());
}
}

@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void v) {
if (node.getMethodSelect() instanceof MemberSelectTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ public void testFullyQualifieds() throws IOException {
assertEquals(expected, parser.getUsedTypes());
}

@Test
public void testAnonymousInnerClass() throws IOException {
List<? extends JavaFileObject> files =
List.of(
testFiles.get(
"/workspace/com/gazelle/java/javaparser/generators/AnonymousInnerClass.java"));
parser.parseClasses(files);

Set<String> expected =
Set.of(
"java.util.HashMap", "javax.annotation.Nullable", "org.jetbrains.annotations.Nullable");
assertEquals(expected, parser.getUsedTypes());
}

private <T> TreeSet<T> treeSet(T... values) {
TreeSet<T> set = new TreeSet<>();
for (T value : values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package workspace.com.gazelle.java.javaparser.generators;

import java.util.HashMap;

public class AnonymousInnerClass {
public static final HashMap<String, String> map = new HashMap<>() {
@javax.annotation.Nullable
@Override
public boolean containsValue(@org.jetbrains.annotations.Nullable Object value) {
return true;
}
};
}

0 comments on commit 9586f4e

Please sign in to comment.