Skip to content

Instantly share code, notes, and snippets.

@melix
Created May 21, 2014 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/3739470fb13cf83926e0 to your computer and use it in GitHub Desktop.
Save melix/3739470fb13cf83926e0 to your computer and use it in GitHub Desktop.
def "lazy constant evaluation"() {
given:
def config = new CompilerConfiguration()
config.optimizationOptions.indy = true
def shell = new GroovyShell(config)
when:
def method = shell.evaluate '''import groovy.transform.CompileStatic
import java.lang.invoke.CallSite
import java.lang.invoke.ConstantCallSite
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
import static groovyjarjarasm.asm.Opcodes.*
@CompileStatic
class Helper {
public static CallSite bootstrap(MethodHandles.Lookup lookup, String callType, MethodType type, String methodName) {
def mh = lookup.findStatic(Helper, methodName, type)
new ConstantCallSite(mh)
}
static int foo() {
666
}
}
@groovyx.ast.bytecode.Bytecode
int testConstant() {
invokedynamic 'experiment', '()I', [H_INVOKESTATIC, 'Helper', 'bootstrap', [CallSite, MethodHandles.Lookup, String, MethodType, String]], 'foo'
ireturn
}
this.&testConstant'''
then:
assert method() == 666
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment