Skip to content

Instantly share code, notes, and snippets.

@nickman
Created January 13, 2012 21:46
Show Gist options
  • Select an option

  • Save nickman/1608875 to your computer and use it in GitHub Desktop.

Select an option

Save nickman/1608875 to your computer and use it in GitHub Desktop.
Dumping Inner Closures
import org.helios.gmx.classloading.*;
import org.codehaus.groovy.control.*
def instrumentation = ByteCodeRepository.getInstance().agentInstrumentation.getInstrumentation();
class ByteCodeNet implements java.lang.instrument.ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer) {
if(ByteCodeRepository.getInstance().isGeneratedClosure(classfileBuffer)) {
println "Generated Closure: $className";
}
return classfileBuffer;
}
}
cft = new ByteCodeNet();
cu = new CompilationUnit();
instrumentation.addTransformer(cft, true);
cu.addSource("ClosureFactory.groovy", "public class ClosureFactory3 { public Closure getClosure() { return { it.getDeclaredMethods().each() { println it.toGenericString(); } } } }");
cu.compile();
Class.forName("ClosureFactory3").newInstance().getClosure()(cft.getClass());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment