Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more
  • Loading branch information
msridhar committed May 10, 2025
commit d8e29f2db6ec77587f8b79dc57a50a2316259913
18 changes: 17 additions & 1 deletion jdk-javac-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ plugins {
id 'java'
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs += [
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.source.tree=ALL-UNNAMED",
]
}

//jar {
// manifest {
// attributes(
Expand All @@ -15,4 +31,4 @@ plugins {
// from(sourceSets.main.resources) {
// include 'META-INF/services/com.sun.source.util.Plugin'
// }
//}
//}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.uber.nullaway.javacplugin;

import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.type.TypeMirror;

public class HelloPlugin implements Plugin {

Expand All @@ -14,18 +25,103 @@ public String getName() {

@Override
public void init(JavacTask task, String... args) {
Trees trees = Trees.instance(task);
task.addTaskListener(
new TaskListener() {
new com.sun.source.util.TaskListener() {
@Override
public void started(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE) {
System.out.println("[HelloPlugin] Analyzing: " + e.getTypeElement());
}
}
public void started(com.sun.source.util.TaskEvent e) {}

@Override
public void finished(TaskEvent e) {
// no-op
public void finished(com.sun.source.util.TaskEvent e) {
if (e.getKind() != com.sun.source.util.TaskEvent.Kind.ANALYZE) {
return;
}
CompilationUnitTree cu = e.getCompilationUnit();
new TreePathScanner<Void, Void>() {

@Override
public Void visitClass(ClassTree classTree, Void unused) {
Name simpleName = classTree.getSimpleName();
if (simpleName.contentEquals("")) {
return null; // skip anonymous
}

ClassSymbol classSym = (ClassSymbol) trees.getElement(getCurrentPath());
if (classSym.getModifiers().contains(Modifier.PRIVATE)) {
return null; // skip private classes
}

TypeMirror classType = trees.getTypeMirror(getCurrentPath());
boolean hasNullMarked =
hasAnnotation(classSym, "org.jspecify.annotations.NullMarked");
boolean hasNullUnmarked =
hasAnnotation(classSym, "org.jspecify.annotations.NullUnmarked");

System.out.println(
"Class: "
+ simpleName
+ " Type: "
+ classType
+ " @NullMarked="
+ hasNullMarked
+ " @NullUnmarked="
+ hasNullUnmarked);

// class type‐parameters
for (TypeParameterTree tp : classTree.getTypeParameters()) {
System.out.println(formatTypeParam(tp));
}

return super.visitClass(classTree, null);
}

@Override
public Void visitMethod(MethodTree methodTree, Void unused) {
MethodSymbol mSym = (MethodSymbol) trees.getElement(getCurrentPath());
if (mSym == null || mSym.getModifiers().contains(Modifier.PRIVATE)) {
return null; // skip private methods
}

boolean hasNullMarked = hasAnnotation(mSym, "org.jspecify.annotations.NullMarked");
boolean hasNullUnmarked =
hasAnnotation(mSym, "org.jspecify.annotations.NullUnmarked");

System.out.println(" Method: " + mSym);
System.out.println(
" @NullMarked=" + hasNullMarked + " @NullUnmarked=" + hasNullUnmarked);

// method type‐parameters (for generic methods)
for (TypeParameterTree tp : methodTree.getTypeParameters()) {
System.out.println(" " + formatTypeParam(tp));
}

return null;
}

/** Helper to format a TypeParameterTree with annotations and bounds */
private String formatTypeParam(TypeParameterTree tp) {
String anns =
tp.getAnnotations().stream()
.map(Object::toString)
.collect(Collectors.joining(" "));
String bounds =
tp.getBounds().stream()
.map(Object::toString)
.collect(Collectors.joining(" & "));
return " TypeParameter: <"
+ (anns.isEmpty() ? "" : anns + " ")
+ tp.getName()
+ (bounds.isEmpty() ? "" : " extends " + bounds)
+ ">";
}

private boolean hasAnnotation(com.sun.tools.javac.code.Symbol sym, String fqn) {
return sym.getAnnotationMirrors().stream()
.map(AnnotationMirror::getAnnotationType)
.map(Object::toString)
.anyMatch(fqn::equals);
}
}.scan(cu, null);
}
});
}
Expand Down