0

I want to run some dynamic script with help of Groovyshell. But, i don't want to write new keyword in dynamic script. So, i thought of adding a CompilerConfiguration with Newify keyword. But, things are not working as expected.

CompilerConfiguration configuration = new CompilerConfiguration()
 configuration.addCompilationCustomizers(
            new ASTTransformationCustomizer(
                    [pattern: "[A-Za-z0-9].*"],
                    Newify
            ))
GroovyShell shell = new GroovyShell(profile, configuration)

Still i am getting error

Cannot find matching method sample#BoundingRegion(int, int, int, int)

where BoundingRegion is a class

Sanket Bajoria
  • 748
  • 6
  • 18

1 Answers1

0

Perhaps you need to provide more information. This works for me:

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

def script = '''
class Main {
    static main(args) {
        assert BigInteger.new(42).toString() == '42' // Ruby style
        assert BigDecimal('3.14').toString() == '3.14' // Python style matching regex
    }
}
'''

def configuration = new CompilerConfiguration()
configuration.addCompilationCustomizers(
        new ASTTransformationCustomizer(
                [pattern: "[A-Za-z0-9].*"],
                Newify
        ))

new GroovyShell(configuration).evaluate(script)
Paul King
  • 627
  • 4
  • 6