-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix code that was ensuring unnamed module can be read by compiled mod…
…ules + refactoring (#4)
- Loading branch information
1 parent
10a4206
commit c47bd0a
Showing
12 changed files
with
339 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ sourceSets { | |
java { | ||
srcDir 'src/main/java9' | ||
} | ||
compileClasspath += sourceSets.main.output | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
113 changes: 113 additions & 0 deletions
113
src/main/java/datadog/compiler/AnnotationsInjectingClassVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package datadog.compiler; | ||
|
||
import com.sun.source.tree.BlockTree; | ||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.LineMap; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.util.TreeScanner; | ||
import com.sun.tools.javac.code.Flags; | ||
import com.sun.tools.javac.tree.EndPosTable; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import com.sun.tools.javac.tree.TreeMaker; | ||
import com.sun.tools.javac.util.List; | ||
import com.sun.tools.javac.util.Name; | ||
import com.sun.tools.javac.util.Names; | ||
import com.sun.tools.javac.util.Position; | ||
|
||
public class AnnotationsInjectingClassVisitor extends TreeScanner<Void, Void> { | ||
private final TreeMaker maker; | ||
private final Names names; | ||
private final JCTree.JCAnnotation sourcePathAnnotation; | ||
private final JCTree.JCExpression methodLinesAnnotationType; | ||
private final boolean methodAnnotationDisabled; | ||
private final LineMap lineMap; | ||
private final EndPosTable endPositions; | ||
|
||
AnnotationsInjectingClassVisitor(TreeMaker maker, | ||
Names names, | ||
JCTree.JCAnnotation sourcePathAnnotation, | ||
JCTree.JCExpression methodLinesAnnotationType, | ||
boolean methodAnnotationDisabled, | ||
LineMap lineMap, | ||
EndPosTable endPositions) { | ||
this.maker = maker; | ||
this.names = names; | ||
this.sourcePathAnnotation = sourcePathAnnotation; | ||
this.methodLinesAnnotationType = methodLinesAnnotationType; | ||
this.methodAnnotationDisabled = methodAnnotationDisabled; | ||
this.lineMap = lineMap; | ||
this.endPositions = endPositions; | ||
} | ||
|
||
@Override | ||
public Void visitClass(ClassTree node, Void aVoid) { | ||
JCTree.JCClassDecl classDeclaration = (JCTree.JCClassDecl) node; | ||
for (JCTree.JCAnnotation annotation : classDeclaration.mods.annotations) { | ||
if (annotation.annotationType.toString().endsWith("SourcePath")) { | ||
// The method is already annotated with @SourcePath. | ||
// This can happen, for instance, when code-generation tools are used | ||
// that copy annotations from interface to class | ||
return super.visitClass(node, aVoid); | ||
} | ||
} | ||
|
||
if (node.getSimpleName().length() > 0) { | ||
classDeclaration.mods.annotations = classDeclaration.mods.annotations.prepend(sourcePathAnnotation); | ||
} | ||
return super.visitClass(node, aVoid); | ||
} | ||
|
||
public Void visitMethod(MethodTree node, Void aVoid) { | ||
if (!methodAnnotationDisabled && (node instanceof JCTree.JCMethodDecl)) { | ||
JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) node; | ||
|
||
for (JCTree.JCAnnotation annotation : methodDecl.mods.annotations) { | ||
if (annotation.annotationType.toString().endsWith("MethodLines")) { | ||
// The method is already annotated with @MethodLines. | ||
// This can happen, for instance, when code-generation tools are used | ||
// that copy annotations from interface methods to class methods | ||
return super.visitMethod(node, aVoid); | ||
} | ||
} | ||
|
||
JCTree.JCModifiers modifiers = methodDecl.getModifiers(); | ||
if ((modifiers.flags & Flags.PUBLIC) != 0) { | ||
|
||
int startPosition = modifiers.getStartPosition(); | ||
if (startPosition == Position.NOPOS) { | ||
startPosition = methodDecl.getStartPosition(); | ||
} | ||
|
||
int endPosition = methodDecl.getEndPosition(endPositions); | ||
if (endPosition == Position.NOPOS) { | ||
BlockTree methodBody = node.getBody(); | ||
if (methodBody != null) { | ||
JCTree methodBodyTree = (JCTree) methodBody; | ||
endPosition = methodBodyTree.getEndPosition(endPositions); | ||
} | ||
} | ||
|
||
int startLine = (int) lineMap.getLineNumber(startPosition); | ||
int endLine = (int) lineMap.getLineNumber(endPosition); | ||
|
||
Name startName = names.fromString("start"); | ||
JCTree.JCIdent startIdent = maker.Ident(startName); | ||
JCTree.JCLiteral startLiteral = maker.Literal(startLine); | ||
JCTree.JCAssign startAssign = maker.Assign(startIdent, startLiteral); | ||
|
||
Name endName = names.fromString("end"); | ||
JCTree.JCIdent endIdent = maker.Ident(endName); | ||
JCTree.JCLiteral endLiteral = maker.Literal(endLine); | ||
JCTree.JCAssign endAssign = maker.Assign(endIdent, endLiteral); | ||
|
||
JCTree.JCAnnotation methodLinesAnnotation = annotation(maker, methodLinesAnnotationType, startAssign, endAssign); | ||
methodDecl.mods.annotations = methodDecl.mods.annotations.prepend(methodLinesAnnotation); | ||
} | ||
} | ||
return super.visitMethod(node, aVoid); | ||
} | ||
|
||
private static JCTree.JCAnnotation annotation(TreeMaker maker, JCTree type, JCTree.JCExpression... arguments) { | ||
return maker.Annotation(type, List.from(arguments)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package datadog.compiler; | ||
|
||
public class CompilerModuleOpener { | ||
|
||
public static void setup() { | ||
// this is a stub used only for JDK 8. It does nothing. See corresponding class in JDK 9+ sources. | ||
} | ||
|
||
} |
Oops, something went wrong.