You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/01/28 21:15:00 UTC

[jira] [Comment Edited] (GROOVY-9807) Static compile of dynamically generated code produces different output in a top-level class and a nested class

    [ https://issues.apache.org/jira/browse/GROOVY-9807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17273996#comment-17273996 ] 

Eric Milles edited comment on GROOVY-9807 at 1/28/21, 9:14 PM:
---------------------------------------------------------------

You are actually quite close on your solution.  The callX factory method does not set the implicitThis property and so it defaults to true.  Setting it to false and the inner class method works properly.

Here is a self-contained script that can be tried in the Groovy Console or Groovy Shell or wherever:
{code:groovy}
import static groovyjarjarasm.asm.Opcodes.*
import static org.codehaus.groovy.ast.tools.GeneralUtils.*

import org.codehaus.groovy.ast.*
import org.codehaus.groovy.control.*
import org.codehaus.groovy.control.customizers.*

import groovy.transform.CompileStatic

GroovyClassLoader load(ClassNode type) {
  CompilerConfiguration config = new CompilerConfiguration()
  config.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic))
  new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config).tap { loader ->
    SourceUnit source = new SourceUnit("${type.nameWithoutPackage}.groovy", null, config, loader, null)
    ModuleNode module = new ModuleNode(source)
    module.setPackageName(type.packageName)
    module.addClass(type)
    for (def iter = type.getInnerClasses(); iter.hasNext(); ) {
      module.addClass(iter.next())
    }
    CompilationUnit unit = new CompilationUnit(config, null, loader)
    unit.getAST().addModule(module)
    unit.compile(Phases.CLASS_GENERATION)
    for (groovyClass in unit.getClasses()) {
      defineClass(groovyClass.name, groovyClass.bytes)
    }
  }
}



ClassNode topLevelClass = ClassHelper.make('test.TopLevelClass')
ClassNode stringBuilderClass = ClassHelper.make(StringBuilder.class)
org.codehaus.groovy.ast.expr.VariableExpression var = varX('sb', stringBuilderClass)
//topLevelClass.addMethod('test', ACC_PUBLIC, stringBuilderClass, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
//  block(
//    declS(var, callX(ctorX(stringBuilderClass), 'append', args(constX('testString')))),
//    returnS(varX(var))
//  )
//)
ClassNode nestedClass = new InnerClassNode(topLevelClass, topLevelClass.name + '$NestedClass', ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE)
nestedClass.addMethod('test', ACC_PUBLIC, stringBuilderClass, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
  block(
    declS(
      var,
      callX(
        ctorX(stringBuilderClass),
        'append',
        args(constX('testString'))
      ).tap {
        setImplicitThis(false) // XXXX: this is the critical bit missing from callX
      }
    ),
    returnS(varX(var))
  )
)

Class<?> compiledClass = Class.forName('test.TopLevelClass$NestedClass', true, load(topLevelClass))
StringBuilder result = compiledClass.newInstance().test()
print result
{code}


was (Author: emilles):
You are actually quite close on your solution.  The callX factory method does not set the implicitThis property and so it defaults to true.  Setting it to false and the inner class method works properly.

Here is a self-contained script that can be tried in the Groovy Console or Groovy Shell or wherever:
{code:groovy}
import static groovyjarjarasm.asm.Opcodes.*
import static org.codehaus.groovy.ast.tools.GeneralUtils.*

import org.codehaus.groovy.ast.*
import org.codehaus.groovy.control.*
import org.codehaus.groovy.control.customizers.*

import groovy.transform.CompileStatic

GroovyClassLoader load(ClassNode type) {
  CompilerConfiguration config = new CompilerConfiguration()
  config.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic))
  new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config).tap { loader ->
    SourceUnit source = new SourceUnit("${type.nameWithoutPackage}.groovy", null, config, loader, null)
    ModuleNode module = new ModuleNode(source)
    module.setPackageName(type.packageName)
    module.addClass(type)
    for (def iter = type.getInnerClasses(); iter.hasNext(); ) {
      module.addClass(iter.next())
    }
    CompilationUnit unit = new CompilationUnit(config, null, loader)
    unit.getAST().addModule(module)
    unit.compile(Phases.CLASS_GENERATION)
    for (groovyClass in unit.getClasses()) {
      defineClass(groovyClass.name, groovyClass.bytes)
    }
  }
}



ClassNode topLevelClass = ClassHelper.make('test.TopLevelClass')
ClassNode stringBuilderClass = ClassHelper.make(StringBuilder.class)
org.codehaus.groovy.ast.expr.VariableExpression var = varX('sb', stringBuilderClass)
//topLevelClass.addMethod('test', ACC_PUBLIC, stringBuilderClass, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
//  block(
//    declS(var, callX(ctorX(stringBuilderClass), 'append', args(constX('testString')))),
//    returnS(varX(var))
//  )
//)
ClassNode nestedClass = new InnerClassNode(topLevelClass, topLevelClass.name + '$NestedClass', ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE)
nestedClass.addMethod('test', ACC_PUBLIC, stringBuilderClass, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
  block(
    declS(var, callX(ctorX(stringBuilderClass), 'append', args(constX('testString')))),
    returnS(varX(var))
  )
)
// XXXX: this is the critical bit missing from callX
nestedClass.getMethods('test')[0].getCode().getStatements()[0].getExpression().getRightExpression().setImplicitThis(false)

Class<?> compiledClass = Class.forName('test.TopLevelClass$NestedClass', true, load(topLevelClass))
StringBuilder result = compiledClass.newInstance().test()
print result
{code}

> Static compile of dynamically generated code produces different output in a top-level class and a nested class
> --------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-9807
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9807
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 3.0.6
>         Environment: openjdk version "1.8.0_242"
> OpenJDK Runtime Environment (build 1.8.0_242-8u242-b08-0ubuntu3~18.04-b08)
> OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
>            Reporter: Lyuben Atanasov
>            Assignee: Eric Milles
>            Priority: Critical
>
> I have encountered an issue when trying to generate code, containing chained method calls, at runtime (without source code) and compiling using CompileStatic.
> What I am trying to achieve is produce code similar to:
> {code:java}
> public StringBuilder test() {
>     StringBuilder sb = new StringBuilder().append("testString");
>     return sb;
> }
> {code}
> I create the appropriate AST nodes, statements, expressions, etc.
> Everything works fine if I add a method like the one shown in a top-level class (the class is also dynamically generated at runtime). However, if I try to add the exact same method to a nested class, it seems like the groovy compiler gets somehow confused and, judging by the decompiler output, produces something like:
> {code:java}
> public StringBuilder test() {
>     StringBuilder sb = ((StringBuilder)((Closure)this).getThisObject()).append("testString");
>     return sb;
> }
> {code}
> Naturally, this does not work.
> Here's how I try to generate the method:
> {code:java}
> ClassNode topLevelClass = ClassHelper.make(
>     "com.example.groovycompiler.test.TopLevelClass");
> InnerClassNode nestedClass = new InnerClassNode(
>     topLevelClass,
>     "com.example.groovycompiler.test.TopLevelClass$NestedClass",
>     Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
>     ClassHelper.OBJECT_TYPE);
> ClassNode stringBuilderClass = ClassHelper.make(StringBuilder.class);
> nestedClass.addMethod(
>     "test",
>     Opcodes.ACC_PUBLIC,
>     stringBuilderClass,
>     Parameter.EMPTY_ARRAY,
>     ClassNode.EMPTY_ARRAY,
>     block(
>         declS(
>             varX("sb", stringBuilderClass),
>             callX(ctorX(stringBuilderClass), "append",
>                 args(constX("testString")))),
>         returnS(
>             varX("sb", stringBuilderClass))
>     )
> );
> {code}
> Perhaps I am missing something, in which case I will be very happy to receive some pointers.
> I have created a github repo with a maven project containing a unit test that demonstrates the issue: https://github.com/l-atanasov/groovy-static-compile-issue
> Note, that I have extended the CompilationUnit a little to allow to run the static compiler without source.
> Also, if I am not using the static compiler, everything works as expected.
> Update: reformatted code for readability



--
This message was sent by Atlassian Jira
(v8.3.4#803005)