Search in sources :

Example 6 with RewriteResult

use of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult in project spring-loaded by spring-projects.

the class SystemClassReflectionRewriterTests method jlClass_getDeclaredField.

@Test
public void jlClass_getDeclaredField() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Two");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Two", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Two", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Two v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Two.java null\n" + "FIELD 0x0000() s Ljava/lang/String;\n" + "FIELD 0x0009(public static) __sljlcgdf Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n" + "METHOD: 0x0001(public) f(Ljava/lang/String;)Ljava/lang/reflect/Field; java/lang/NoSuchFieldException\n" + "METHOD: 0x000a(private static) __sljlcgdf(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field; java/lang/NoSuchFieldException\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:field?java.lang.String system.Two.s nsfe", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper2", Class.class, String.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgdf).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:field?null nsfe", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETDECLAREDFIELD) != 0);
    assertTrue((rr.bits & ~JLC_GETDECLAREDFIELD) == 0);
    assertEquals(2, callcount);
    assertEquals(2, events.size());
    assertEquals("helper2(system.Two,s)", events.get(0));
    assertEquals("helper2(system.Two,foo)", events.get(1));
    assertEquals("Class.getDeclaredField()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 7 with RewriteResult

use of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult in project spring-loaded by spring-projects.

the class SystemClassReflectionRewriterTests method jlClass_getMethod.

@Test
public void jlClass_getMethod() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Six");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Six", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Six", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Six v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Six.java null\n" + "FIELD 0x0001(public) s Ljava/lang/String;\n" + "FIELD 0x0009(public static) __sljlcgm Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n" + "METHOD: 0x0001(public) m(Ljava/lang/String;)Ljava/lang/reflect/Method; java/lang/NoSuchMethodException\n" + "METHOD: 0x008a(private static) __sljlcgm(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method; java/lang/NoSuchMethodException\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:method?public java.lang.String system.Six.runIt() throws java.lang.Exception nsme", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper4", Class.class, String.class, Class[].class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgm).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:method?null unexpectedly_didn't_fail", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETMETHOD) != 0);
    assertTrue((rr.bits & ~JLC_GETMETHOD) == 0);
    assertEquals(2, callcount);
    assertEquals(2, events.size());
    assertEquals("helper4(system.Six,runIt)", events.get(0));
    assertEquals("helper4(system.Six,foo)", events.get(1));
    assertEquals("Class.getMethod()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 8 with RewriteResult

use of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult in project spring-loaded by spring-projects.

the class SystemClassReflectionRewriterTests method jlrField_getLong.

@Test
public void jlrField_getLong() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Thirteen");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Thirteen", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Thirteen", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Thirteen v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Thirteen.java null\n" + "FIELD 0x0001(public) foo J\n" + "FIELD 0x0009(public static) __sljlcgf Ljava/lang/reflect/Method;\n" + "FIELD 0x0009(public static) __sljlrfgl Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n" + "METHOD: 0x0001(public) gf()Ljava/lang/Long; java/lang/Exception\n" + "METHOD: 0x000a(private static) __sljlcgf(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field; java/lang/NoSuchFieldException\n" + "METHOD: 0x000a(private static) __sljlrfgl(Ljava/lang/reflect/Field;Ljava/lang/Object;)J java/lang/IllegalAccessException java/lang/IllegalArgumentException\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:value=42", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRFGL_0", Class.class, String.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgf).set(null, m);
    m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRFGL", Field.class, Object.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlrfGetLongMember).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:value=0", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & (JLRF_GETLONG | JLC_GETFIELD)) != 0);
    assertTrue((rr.bits & ~(JLRF_GETLONG | JLC_GETFIELD)) == 0);
    assertEquals(1, callcount);
    assertEquals("Class.getField() Field.getLong()", rr.summarize());
}
Also used : Field(java.lang.reflect.Field) RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 9 with RewriteResult

use of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult in project spring-loaded by spring-projects.

the class SystemClassReflectionRewriterTests method jlClass_getMethods.

@Test
public void jlClass_getMethods() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Nine");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Nine", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Nine", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Nine v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Nine.java null\n" + "FIELD 0x0009(public static) __sljlcgms Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String;\n" + "METHOD: 0x0001(public) ms()[Ljava/lang/reflect/Method;\n" + "METHOD: 0x000a(private static) __sljlcgms(Ljava/lang/Class;)[Ljava/lang/reflect/Method;\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:methods:null?false methods:size=11", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperGMs", Class.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgms).set(null, m);
    m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperGMs", Class.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgms).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:methods:null?true", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETMETHODS) != 0);
    assertTrue((rr.bits & ~JLC_GETMETHODS) == 0);
    assertEquals(1, callcount);
    assertEquals("Class.getMethods()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 10 with RewriteResult

use of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult in project spring-loaded by spring-projects.

the class SystemClassReflectionRewriterTests method jlClass_getDeclaredMethods.

@Test
public void jlClass_getDeclaredMethods() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Four");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Four", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Four", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Four v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Four.java null\n" + "FIELD 0x0009(public static) __sljlcgdms Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String;\n" + "METHOD: 0x0001(public) ms()[Ljava/lang/reflect/Method;\n" + "METHOD: 0x000a(private static) __sljlcgdms(Ljava/lang/Class;)[Ljava/lang/reflect/Method;\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:methods:null?false methods:size=3", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper3", Class.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgdms).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:methods:null?true", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETDECLAREDMETHODS) != 0);
    assertTrue((rr.bits & ~JLC_GETDECLAREDMETHODS) == 0);
    assertEquals(1, callcount);
    assertEquals("Class.getDeclaredMethods()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

RewriteResult (org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult)14 Method (java.lang.reflect.Method)13 Test (org.junit.Test)13 Field (java.lang.reflect.Field)2 IsReloadableTypePlugin (org.springsource.loaded.IsReloadableTypePlugin)1 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)1 Plugin (org.springsource.loaded.Plugin)1 ReloadableType (org.springsource.loaded.ReloadableType)1 TypeRegistry (org.springsource.loaded.TypeRegistry)1 ReloadableTypeNameDecision (org.springsource.loaded.TypeRegistry.ReloadableTypeNameDecision)1