Examples of RewriteResult


Examples of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult

  }
 
  @Test
  public void jlrField_get() throws Exception {
    byte[] classbytes = loadBytesForClass("system.Twelve");
    RewriteResult rr = SystemClassReflectionRewriter.rewrite("system.Twelve", classbytes);
    byte[] newbytes = rr.bytes;
    Class<?> clazz = loadit("system.Twelve", newbytes);

    // Check the new field and method are in the type:
    //@formatter:off
    assertEquals(
        "CLASS: system/Twelve v50 0x0021(public synchronized) super java/lang/Object\n"+
        "SOURCE: Twelve.java null\n"+
        "FIELD 0x0001(public) foo Ljava/lang/String;\n"+
        "FIELD 0x0009(public static) __sljlcgf Ljava/lang/reflect/Method;\n"+
        "FIELD 0x0009(public static) __sljlrfg 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/Object; 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) __sljlrfg(Ljava/lang/reflect/Field;Ljava/lang/Object;)Ljava/lang/Object; 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=abc", value);
    assertEquals(0, callcount);

    // Set the field
    Method m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRFG_0", Class.class, String.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlcgf).set(null, m);
   
    m = SystemClassReflectionRewriterTests.class.getDeclaredMethod("helperJLRFG", Field.class, Object.class);
    assertNotNull(m);
    clazz.getDeclaredField(jlrfGetMember).set(null, m);
   
    // Now re-run, should be intercepted to call our helper
    value = run(clazz, "runIt");
    assertEquals("complete:value=null", value);

    // Check the correct amount of rewriting went on
    assertTrue((rr.bits & (JLRF_GET | JLC_GETFIELD)) != 0);
    assertTrue((rr.bits & ~(JLRF_GET | JLC_GETFIELD)) == 0);

    assertEquals(1, callcount);
    assertEquals("Class.getField() Field.get()", rr.summarize());
  }
View Full Code Here

Examples of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult

  }
 
  @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());
  }
View Full Code Here

Examples of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult

  }

  @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());
  }
View Full Code Here

Examples of org.springsource.loaded.SystemClassReflectionRewriter.RewriteResult

    if (typeRegistry == null) { // A null type registry indicates nothing is being made reloadable for the classloader
      if (classLoader == null && slashedClassName != null) { // Indicates loading of a system class
        if (systemClassesContainingReflection.contains(slashedClassName)) {
          try {
            // TODO [perf] why are we not using the cache here, is it because the list is so short?
            RewriteResult rr = SystemClassReflectionRewriter.rewrite(slashedClassName, bytes);
            if (GlobalConfiguration.verboseMode && log.isLoggable(Level.INFO)) {
              log.info("System class rewritten: name="+slashedClassName+" rewrite summary="+rr.summarize());
            }
            systemClassesRequiringInitialization.put(slashedClassName, rr.bits);
            return rr.bytes;
          } catch (Exception re) {
            re.printStackTrace();
          }
         
        }
        else if (slashedClassName.equals("java/lang/invoke/InnerClassLambdaMetafactory")) {
          bytes = Java8.enhanceInnerClassLambdaMetaFactory(bytes);
          return bytes;
        } else if ((GlobalConfiguration.investigateSystemClassReflection || GlobalConfiguration.rewriteAllSystemClasses) &&
            SystemClassReflectionInvestigator.investigate(slashedClassName, bytes, GlobalConfiguration.investigateSystemClassReflection) > 0) {
          // This block can help when you suspect there is a system class using reflection and that
          // class isn't on the 'shortlist' (in systemClassesContainingReflection). Basically turn on the
          // options to trigger this investigation then add them to the shortlist if it looks like they need rewriting.
          RewriteResult rr = SystemClassReflectionRewriter.rewrite(slashedClassName, bytes);
          if (GlobalConfiguration.rewriteAllSystemClasses) {
            systemClassesRequiringInitialization.put(slashedClassName, rr.bits);
            return rr.bytes;
          }
          else {
            System.err.println("Type " + slashedClassName + " rewrite summary: " + rr.summarize());
            return bytes;
          }
        }
      }
      return bytes;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.