Skip to content

Commit

Permalink
Fix code that was ensuring unnamed module can be read by compiled mod…
Browse files Browse the repository at this point in the history
…ules + refactoring (#4)
  • Loading branch information
nikita-tkachenko-datadog authored Jun 25, 2024
1 parent 10a4206 commit c47bd0a
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 311 deletions.
17 changes: 9 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@ def launcher8 = javaToolchains.launcherFor { languageVersion.set(JavaLanguageVer
def javaHome8 = launcher8.map { it.metadata.installationPath.asFile }
def jvm8 = Jvm.forHome(javaHome8.get())

apply from: "$rootDir/gradle/java8-compile.gradle"
apply from: "$rootDir/gradle/multi-release.gradle"
apply from: "$rootDir/gradle/publishing.gradle"
apply from: "$rootDir/gradle/test.gradle"

dependencies {
implementation files(jvm8.toolsJar)

api project(":dd-javac-plugin-client")
api "org.burningwave:core:12.62.7"

java9Implementation "org.burningwave:core:12.65.1"

testImplementation "org.junit.jupiter:junit-jupiter-api:5.9.2"
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2'
testImplementation 'commons-io:commons-io:2.11.0'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.9.2"
testImplementation "commons-io:commons-io:2.11.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.9.2"
}

apply from: "$rootDir/gradle/java8-compile.gradle"
apply from: "$rootDir/gradle/multi-release.gradle"
apply from: "$rootDir/gradle/publishing.gradle"
apply from: "$rootDir/gradle/test.gradle"
1 change: 1 addition & 0 deletions gradle/multi-release.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sourceSets {
java {
srcDir 'src/main/java9'
}
compileClasspath += sourceSets.main.output
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
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 src/main/java/datadog/compiler/AnnotationsInjectingClassVisitor.java
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));
}
}
9 changes: 9 additions & 0 deletions src/main/java/datadog/compiler/CompilerModuleOpener.java
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.
}

}
Loading

0 comments on commit c47bd0a

Please sign in to comment.