}
@Test
public void jlrMethod_Invoke() throws Exception {
byte[] classbytes = loadBytesForClass("system.Eleven");
RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Eleven", classbytes);
byte[] newbytes = rr.bytes;
Class<?> clazz = loadit("system.Eleven", newbytes);
// Check the new field and method are in the type:
//@formatter:off
assertEquals(
"CLASS: system/Eleven v50 0x0021(public synchronized) super java/lang/Object\n"+
"SOURCE: Eleven.java null\n"+
"FIELD 0x0009(public static) __sljlcgdm Ljava/lang/reflect/Method;\n"+
"FIELD 0x0009(public static) __sljlrmi Ljava/lang/reflect/Method;\n"+
"METHOD: 0x0001(public) <init>()V\n"+
"METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n"+
"METHOD: 0x0089(public static) invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; java/lang/Exception\n"+
"METHOD: 0x0001(public) foo(ILjava/lang/String;)Ljava/lang/String;\n"+
"METHOD: 0x008a(private static) __sljlcgdm(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method; java/lang/NoSuchMethodException\n"+
"METHOD: 0x000a(private static) __sljlrmi(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; java/lang/IllegalAccessException java/lang/reflect/InvocationTargetException\n"+
"\n",
toStringClass(newbytes));
//@formatter:on
Object value = run(clazz, "runIt");
// Check that without the field initialized, things behave as expected
assertEquals("complete:obj=i=12:s=abc", value);
assertEquals(0, callcount);
// Set the field
Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRMI_0", Class.class, String.class, Class[].class);
assertNotNull(m);
clazz.getDeclaredField(jlcgdm).set(null, m);
m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRMI", Method.class, Object.class, Object[].class);
assertNotNull(m);
clazz.getDeclaredField(jlrmInvokeMember).set(null, m);
// Now re-run, should be intercepted to call our helper
value = run(clazz, "runIt");
assertEquals("complete:obj=null", value);
// Check the correct amount of rewriting went on
assertTrue((rr.bits & (JLC_GETDECLAREDMETHOD|JLRM_INVOKE)) != 0);
assertTrue((rr.bits & ~(JLC_GETDECLAREDMETHOD|JLRM_INVOKE)) == 0);
assertEquals(1, callcount);
assertEquals("Class.getDeclaredMethod() Method.invoke()", rr.summarize());
}