Created
May 21, 2014 14:06
-
-
Save melix/3739470fb13cf83926e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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