Search in sources :

Example 11 with RewriteResult

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

the class SystemClassReflectionRewriterTests method jlClass_getDeclaredConstructor.

@Test
public void jlClass_getDeclaredConstructor() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Seven");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Seven", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Seven", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Seven v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Seven.java null\n" + "FIELD 0x0009(public static) __sljlcgdc Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) <init>(Ljava/lang/String;)V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n" + "METHOD: 0x0081(public) m([Ljava/lang/Class;)Ljava/lang/reflect/Constructor; java/lang/NoSuchMethodException\n" + "METHOD: 0x008a(private static) __sljlcgdc(Ljava/lang/Class;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor; 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:defaultctor?public system.Seven() stringctor?public system.Seven(java.lang.String) nsme", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper5", Class.class, Class[].class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgdc).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:defaultctor?null stringctor?null nsme", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETDECLAREDCONSTRUCTOR) != 0);
    assertTrue((rr.bits & ~JLC_GETDECLAREDCONSTRUCTOR) == 0);
    assertEquals(3, callcount);
    assertEquals(3, events.size());
    assertEquals("helper5(system.Seven)", events.get(0));
    assertEquals("helper5(system.Seven)", events.get(1));
    assertEquals("helper5(system.Seven)", events.get(2));
    assertEquals("Class.getDeclaredConstructor()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 12 with RewriteResult

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

the class SystemClassReflectionRewriterTests method jlClass_getField.

@Test
public void jlClass_getField() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Three");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Three", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Three", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Three v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Three.java null\n" + "FIELD 0x0001(public) s Ljava/lang/String;\n" + "FIELD 0x0009(public static) __sljlcgf 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) __sljlcgf(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?public java.lang.String system.Three.s nsfe", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper2", Class.class, String.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgf).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_GETFIELD) != 0);
    assertTrue((rr.bits & ~JLC_GETFIELD) == 0);
    assertEquals(2, callcount);
    assertEquals(2, events.size());
    assertEquals("helper2(system.Three,s)", events.get(0));
    assertEquals("helper2(system.Three,foo)", events.get(1));
    assertEquals("Class.getField()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 13 with RewriteResult

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

the class SystemClassReflectionRewriterTests method jlClass_getDeclaredMethod.

@Test
public void jlClass_getDeclaredMethod() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Five");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Five", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Five", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Five v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Five.java null\n" + "FIELD 0x0000() s Ljava/lang/String;\n" + "FIELD 0x0009(public static) __sljlcgdm 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) __sljlcgdm(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.Five.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(jlcgdm).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:method?null nsme", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETDECLAREDMETHOD) != 0);
    assertTrue((rr.bits & ~JLC_GETDECLAREDMETHOD) == 0);
    assertEquals(2, callcount);
    assertEquals(2, events.size());
    assertEquals("helper4(system.Five,runIt)", events.get(0));
    assertEquals("helper4(system.Five,foobar)", events.get(1));
    assertEquals("Class.getDeclaredMethod()", rr.summarize());
}
Also used : RewriteResult(org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 14 with RewriteResult

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

the class SystemClassReflectionRewriterTests method jlClass_getModifiers.

@Test
public void jlClass_getModifiers() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Eight");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Eight", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Eight", newbytes);
    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals("CLASS: system/Eight v50 0x0021(public synchronized) super java/lang/Object\n" + "SOURCE: Eight.java null\n" + "INNERCLASS: system/Eight$Inner system/Eight Inner 2\n" + "FIELD 0x0009(public static) __sljlcgmods Ljava/lang/reflect/Method;\n" + "METHOD: 0x0001(public) <init>()V\n" + "METHOD: 0x0001(public) <init>(Ljava/lang/String;)V\n" + "METHOD: 0x0001(public) runIt()Ljava/lang/String; java/lang/Exception\n" + "METHOD: 0x0001(public) m(Ljava/lang/Class;)I\n" + "METHOD: 0x000a(private static) __sljlcgmods(Ljava/lang/Class;)I\n" + "\n", toStringClass(newbytes));
    //@formatter:on
    Object value = run(clazz, "runIt");
    // Check that without the field initialized, things behave as expected
    assertEquals("complete:mods?1 mods?0 mods?2", value);
    assertEquals(0, callcount);
    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helper6", Class.class);
    //		m = ReflectiveInterceptor.class.getDeclaredMethod("jlClassGetDeclaredField", Class.class, String.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgmods).set(null, m);
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:mods?1 mods?0 mods?2", value);
    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & JLC_GETMODIFIERS) != 0);
    assertTrue((rr.bits & ~JLC_GETMODIFIERS) == 0);
    assertEquals(3, callcount);
    assertEquals(3, events.size());
    assertEquals("helper6(system.Eight)", events.get(0));
    assertEquals("helper6(system.DefaultVis)", events.get(1));
    assertEquals("helper6(system.Eight$Inner)", events.get(2));
    assertEquals("Class.getModifiers()", 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