java.lang.AssertionError thrown by compiler when adding generated method with parameters
Asked Answered
B

6

8

I am modifying an existing class using internal sun APIs (com.sun.tools.javac) with an annotation processor. I am able to generate and add a MethodDecl to a ClassDecl using the following code:

JCTree.JCClassDecl classDecl = ...
JCTree.JCMethodDecl methodDecl = ...
JCTree[] trees = new JCTree[classDecl.defs.length() + 1];
trees[classDecl.defs.length()] = methodDecl;
classDecl.defs = List.from(trees);

This works fine until I try to add parameters to the method declaration. I use the following code to generate a new MethodDecl:

Map<String, Class<?>> paramMap = ...
List<JCVariableDecl> parameters = List.from(paramMap.entrySet().stream()
        .map(e -> treeMaker.VarDef(treeMaker.Modifiers(Flags.PARAMETER), elements.getName(e.getKey()), getType(e.getValue()), null))
        .toArray(JCVariableDecl[]::new));
...
JCMethodDecl methodDecl = treeMaker.MethodDef(modifiers, methodName, returnType, genericParameters, parameters, throwables, methodBody, null);

I get the following stacktrace when compiling:

An exception has occurred in the compiler (9). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError
    at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
    at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
    at jdk.compiler/com.sun.tools.javac.util.Bits.incl(Bits.java:186)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.initParam(Flow.java:1931)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitMethodDef(Flow.java:1880)
    at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:866)
    at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:393)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1451)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitClassDef(Flow.java:1822)
    at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:774)
    at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:393)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:1451)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.analyzeTree(Flow.java:2521)
    at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.analyzeTree(Flow.java:2504)
    at jdk.compiler/com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:212)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1389)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1363)
    at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:959)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:100)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:142)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:96)
    at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:90)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:49)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:36)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:99)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:52)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:37)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:35)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25)
    at org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:204)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:189)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:121)
    at jdk.internal.reflect.GeneratedMethodAccessor170.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:179)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:135)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:122)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:59)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:128)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecuter.java:43)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:46)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
    at org.gradle.initialization.DefaultGradleLauncher$ExecuteTasks.run(DefaultGradleLauncher.java:314)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.initialization.DefaultGradleLauncher.runTasks(DefaultGradleLauncher.java:204)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:134)
    at org.gradle.initialization.DefaultGradleLauncher.executeTasks(DefaultGradleLauncher.java:109)
    at org.gradle.internal.invocation.GradleBuildController$1.call(GradleBuildController.java:78)
    at org.gradle.internal.invocation.GradleBuildController$1.call(GradleBuildController.java:75)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:152)
    at org.gradle.internal.invocation.GradleBuildController.doBuild(GradleBuildController.java:100)
    at org.gradle.internal.invocation.GradleBuildController.run(GradleBuildController.java:75)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:53)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.tooling.internal.provider.ValidatingBuildActionRunner.run(ValidatingBuildActionRunner.java:32)
    at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner$1.run(RunAsBuildOperationBuildActionRunner.java:43)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner.run(RunAsBuildOperationBuildActionRunner.java:40)
    at org.gradle.tooling.internal.provider.SubscribableBuildActionRunner.run(SubscribableBuildActionRunner.java:51)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:47)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:30)
    at org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:39)
    at org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:25)
    at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:80)
    at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:53)
    at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:57)
    at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:32)
    at org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:36)
    at org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:25)
    at org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:43)
    at org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:29)
    at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:64)
    at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:29)
    at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:59)
    at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:44)
    at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:45)
    at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:30)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:67)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:55)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:62)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:82)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:295)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.base/java.lang.Thread.run(Thread.java:844)

This code compiles perfectly when I replace the parameters with List.nil(), this is why I believe the issue is related to the parameters.

The following code is what throws the exception:

public void incl(int x) {
    Assert.check(currentState != BitsState.UNKNOWN);
    Assert.check(x >= 0); // <-- this line
    sizeTo((x >>> wordshift) + 1);
    bits[x >>> wordshift] = bits[x >>> wordshift] |
        (1 << (x & wordmask));
    currentState = BitsState.NORMAL;
}

visitMethodDef is called (from the Flow class):

//important part in method visitMethodDef
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
    JCVariableDecl def = l.head;
    scan(def);
    Assert.check((def.sym.flags() & PARAMETER) != 0, "Method parameter without PARAMETER flag");
    /*  If we are executing the code from Gen, then there can be
     *  synthetic or mandated variables, ignore them.
     */
    initParam(def);
}

which calls initParam:

protected void initParam(JCVariableDecl def) {
    inits.incl(def.sym.adr);
    uninits.excl(def.sym.adr);
}

So if I understand correctly, the parameter's sym.adr is < 0. Manually creating the Symbol and setting the adr field to something greater than 0 does not work.

The generated code (MethodDecl#toString) is valid:

public static java.lang.String test(java.lang.String arg) {
    System.out.println("Running test method!");
    return new java.lang.String("Hello!");
}

So to summarize: A java.lang.AssertionError is thrown while compiling a generated method with parameters and I'm unsure how to fix it.

Bronson answered 22/10, 2017 at 12:34 Comment(5)
Your question is kind of meshy, with what you did and what you are trying and other irrelevant stuffs, which confusing. You need to just specify what is working code and what is not and point out the different (even not required) and post the stacktrace.Whet
You forgot an important tenant. Do not use com.sun.* libraries, they can break at any time. I would look at a third party library like javaparser.org/index.html Another option would be vogella.com/tutorials/EclipseJDT/article.htmlSporades
incl is called from initParam method. Add the code for initParam method.Pathan
@RohitGaikwad added the codeBronson
@RomainHippeau I am aware of this, but I don't think Eclipse JDT support the javac compiler and I cannot get JavaParser to workBronson
C
4

I've managed to get generated, functional parameters by creating a MethodSymbol, using that with TreeMaker::MethodDef(MethodSymbol, JCTree.JCBlock) to create a new JCTree.JCMethodDecl, and them iterating over the parameters to set their adr to 0 (which is accepted, and nothing in the spec mentioned by @Piotr Wilkin leads me to believe this shouldn't be valid).

// maker is a TreeMaker instance
// sym is a Symbol.MethodSymbol instance
// body is a JCTree.JCBlock instance
JCTree.JCMethodDecl decl = maker.MethodDef(sym, body);
decl.params.forEach(p -> {
    p.sym.adr = 0;
});

I suppose the reason this works, is because javac will not attempt to create new symbols (since one already exists), and the VarSymbols (sym field of JCTree.JCVariableDecl) are mutable. Moreover, searching the javac code shows little attempt to override this anyway.

Corrigendum answered 2/11, 2017 at 0:43 Comment(0)
H
6

After reading the source code of javac, I finally find the solution.

The key point is that the value of sym.adr is -1, and then it will be assigned by newVar():

// Flow$AssignAnalyzer
void newVar(JCVariableDecl varDecl) {
    ...
    sym.adr = nextadr;
}

But the newVar() is called by visitVarDef() only if tree.sym is trackable:

// Flow$AssignAnalyzer
public void visitVarDef(JCVariableDecl tree) {
    ...
    boolean track = trackable(tree.sym);
    if (track && tree.sym.owner.kind == MTH) {
        newVar(tree);
    }
    ...
}
// Flow$AssignAnalyzer
protected boolean trackable(VarSymbol sym) {
    return
        sym.pos >= startPos &&
        ((sym.owner.kind == MTH ||
        isFinalUninitializedField(sym)));
}

So we should ensure that sym.pos >= startPos is true while generating our method.

In conclusion, we should set the TreeMaker.pos carefully before we call TreeMaker.MethodDef() like this:

// ensure the current pos is valid
treeMaker.at(jcClassDecl.pos);

treeMaker.MethodDef(...);
Higbee answered 15/1, 2019 at 11:32 Comment(3)
This works for me on Java 17. The prior answers didn't work, because you can't set the .sym or the .sym.adr without it being overwritten elsewhere (one other comment said it gets cleared?). This seems to force the javac to populate the values and the execution succeeds.Rolf
The most explained and architecturally correct answerWoodhouse
This worked for me, using JDK 11.Track
C
4

I've managed to get generated, functional parameters by creating a MethodSymbol, using that with TreeMaker::MethodDef(MethodSymbol, JCTree.JCBlock) to create a new JCTree.JCMethodDecl, and them iterating over the parameters to set their adr to 0 (which is accepted, and nothing in the spec mentioned by @Piotr Wilkin leads me to believe this shouldn't be valid).

// maker is a TreeMaker instance
// sym is a Symbol.MethodSymbol instance
// body is a JCTree.JCBlock instance
JCTree.JCMethodDecl decl = maker.MethodDef(sym, body);
decl.params.forEach(p -> {
    p.sym.adr = 0;
});

I suppose the reason this works, is because javac will not attempt to create new symbols (since one already exists), and the VarSymbols (sym field of JCTree.JCVariableDecl) are mutable. Moreover, searching the javac code shows little attempt to override this anyway.

Corrigendum answered 2/11, 2017 at 0:43 Comment(0)
L
3

Actually, the JavaDoc for the Symbol class tells you what the adr field is used for:

The variable's address. Used for different purposes during flow analysis, translation and code generation. Flow analysis: If this is a blank final or local variable, its sequence number. Translation: If this is a private field, its access number. Code generation: If this is a local variable, its logical slot number.

Its default value is -1, which is basically the base-types equivalent of null, so what you're getting is semantically (although not technically) a NullPointerException. You need to fill in the parameter position according to the above documentation (and probably some reverse engineering as well, since you're using internal APIs).

Laddy answered 31/10, 2017 at 10:11 Comment(1)
If I understand correctly I need to manually set the value of adr, but I am unsure how I would do this since the sym field gets overwritten at compile time.Bronson
I
1

The solutions above didn't solve my problem.

To solve this, we need to use TreeMaker.Param(), and not TreeMaker.VarDef(). Here is the solution that helped me:

Wrong solution:

return treeMaker.MethodDef(
            modifiers,
            elementUtils.getName(methodName),
            treeMaker.Type(new Type.JCVoidType()),
            List.nil(),
            List.of(treeMaker.VarDef(treeMaker.Modifiers(0), paramName, treeMaker.Type(type), null)),
            List.nil(),
            body,
            null
    );

Working solution:

return treeMaker.MethodDef(
                modifiers,
                elementUtils.getName(methodName),
                treeMaker.Type(new Type.JCVoidType()),
                List.nil(),
                List.of(treeMaker.Param(paramName, type, null)),
                List.nil(),
                body,
                null
        );
Israel answered 18/11, 2020 at 20:52 Comment(0)
S
1

I followed the @fashare2015 idea. Here is my code

public static JCTree.JCMethodDecl createSetMethod(JCTree.JCVariableDecl field) {
    JCTree.JCFieldAccess thisX = TreeMaker.Select(TreeMaker.Ident(NameApi.fromString("this")), field.name);
    JCTree.JCAssign assign = TreeMaker.Assign(thisX, TreeMaker.Ident(field.name));
    JCTree.JCExpression methodType = TreeMaker.Type(new Type.JCVoidType());
    JCTree.JCBlock methodBody = TreeMaker.Block(0, List.of(TreeMaker.Exec(assign)));
    Name methodName = NameApi.fromString(StringUtils.generateMethodName("set", field.getName().toString()));
    JCTree.JCVariableDecl param = TreeMaker.VarDef(TreeMaker.Modifiers(Flags.PARAMETER), field.name, field.vartype, null);
    param.pos = field.pos; // <---- This lines I used
    
    return TreeMaker.MethodDef(TreeMaker.Modifiers(Flags.PUBLIC), methodName, methodType,
            List.nil(), List.of(param), List.nil(), methodBody, null);
}

I use JDK version 14.

You can pull my demo here: link

Stark answered 9/2, 2021 at 7:31 Comment(0)
J
1

I find the solution:

before execute create getter method ,add this code :

treeMaker.pos = jcVariableDecl.pos; 

for example :

//add this code ,redefine the position of parameters in the syntax tree:

treeMaker.pos = jcVariableDecl.pos;

treeMaker.MethodDef(treeMaker.Modifiers(Flags.PUBLIC),
        getNewMethodName(jcVariableDecl.getName(), "set"),
        methodType,
        List.nil(),
        parameters,
        List.nil(),
        block,
        null);
Jaal answered 29/3, 2021 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.