예제 #1
0
  public void testMethodInInner2() throws Exception {
    CtClass inner = sloader.get("test2.Nested3$Inner");
    CtClass outer = sloader.get("test2.Nested3");
    String src =
        "public int f() {"
            + "  int k = 0;"
            + "  test2.Nested3 n = new test2.Nested3(3);"
            + "  k += n.geti();"
            + "  n = new test2.Nested3();"
            + "  k += n.geti();"
            + "  n = new test2.Nested3(\"foo\");"
            + "  k += n.geti();"
            + "  return k; }";

    outer.stopPruning(true);
    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make(src, inner);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();

    CtMethod m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    inner.writeFile();
    outer.writeFile();

    Object iobj = make(inner.getName());
    assertEquals(6, invoke(iobj, "f"));
  }
예제 #2
0
  public void testCodeGen() throws Exception {
    CtClass cc = sloader.get("test2.CodeGen");
    CtMethod m1 = cc.getDeclaredMethod("run");
    m1.insertBefore(
        "{ double d = true ? 1 : 0.1; "
            + "  d = d > 0.5 ? 0.0 : - 1.0; "
            + "  System.out.println(d); "
            + "  String s = \"foo\"; "
            + "  s = 1 + 2 + s + \"bar\"; "
            + "  s += \"poi\" + 3 + seven() + seven(\":\" + ' '); "
            + "  s += .14; "
            + "  msg = s; "
            + "  System.out.println(s); }");

    // recursive type check is done if $proceed is used.
    CtMethod m2 =
        CtNewMethod.make(
            "public int test() {"
                + "  String s = $proceed(\"int\" + (3 + 0.14)) + '.'; "
                + "  System.out.println(s); return s.length(); }",
            cc,
            "this",
            "seven");
    cc.addMethod(m2);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(19, invoke(obj, "run"));
    assertEquals(9, invoke(obj, "test"));
  }
예제 #3
0
  public void testInsertLocal() throws Exception {
    CtClass cc = sloader.get("test2.InsertLocal");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.insertBefore("{ i = s.length(); d = 0.14; }");
    m1.insertAfter("{ field = i; }");

    CtMethod m2 = cc.getDeclaredMethod("run2");
    m2.insertAt(22, "{ s = \"12\"; k = 5; }");

    CtMethod m3 = cc.getDeclaredMethod("run3");
    m3.instrument(
        new ExprEditor() {
          public void edit(NewExpr n) throws CannotCompileException {
            n.replace("{ i++; $_ = $proceed($$); }");
          }

          public void edit(FieldAccess f) throws CannotCompileException {
            f.replace("{ i++; $_ = $proceed($$); }");
          }

          public void edit(MethodCall m) throws CannotCompileException {
            m.replace("{ i++; $_ = $proceed($$); }");
          }
        });

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(317, invoke(obj, "run"));
    assertEquals(7, invoke(obj, "run2"));
    assertEquals(3, invoke(obj, "run3"));
  }
예제 #4
0
 public void testArrayLen() throws Exception {
   CtClass cc = sloader.get("test2.ArrayLenTest");
   cc.addMethod(CtNewMethod.make("public int foo(){ return this.length; }", cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "foo"));
 }
예제 #5
0
  public void testNewArray() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          int dim[] = {1, 2, 2, 1, 2, 2, 3};
          int cdim[] = {1, 1, 2, 1, 1, 2, 2};
          int counter = 0;

          public void edit(NewArray expr) throws CannotCompileException {
            try {
              CtClass str = sloader.get("java.lang.String");
              if (counter < 3) assertEquals(str, expr.getComponentType());
              else assertEquals(CtClass.intType, expr.getComponentType());

              assertEquals(dim[counter], expr.getDimension());
              assertEquals(cdim[counter], expr.getCreatedDimensions());
              expr.replace("{ i += $1; $_ = $proceed($$); }");
              ++counter;
            } catch (NotFoundException e) {
              throw new CannotCompileException(e);
            }
          }
        };

    CtClass cc = sloader.get("test2.NewArray");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(48, invoke(obj, "run"));
  }
예제 #6
0
  public void testCodeGen2() throws Exception {
    CtClass cc = sloader.makeClass("test2.CodeGen2");

    CtMethod m1 =
        CtNewMethod.make(
            "public int test() {"
                + "  int len;"
                + "  String s = \"foo\" + \"bar\" + 3;"
                + "  System.out.println(s); len = s.length();"
                + "  len = -3 + len; len = len - (7 - 2 + -1);"
                + "  int k = 3; len += ~k - ~3;"
                + "  return len; }",
            cc);
    cc.addMethod(m1);

    CtMethod m2 =
        CtNewMethod.make(
            "public int test2() {"
                + "  double d = 0.2 - -0.1;"
                + "  d += (0.2 + 0.3) * 1.0;"
                + "  return (int)(d * 10); }",
            cc);
    cc.addMethod(m2);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(0, invoke(obj, "test"));
    assertEquals(8, invoke(obj, "test2"));
  }
예제 #7
0
 public void testAddCatchForConstructor() throws Exception {
   CtClass cc = sloader.get("test2.AddCatchForConstructor");
   CtConstructor m1 = cc.getDeclaredConstructors()[0];
   m1.addCatch("return;", sloader.get("java.lang.Exception"));
   cc.writeFile();
   Object obj = make(cc.getName());
 }
예제 #8
0
 public void testReplaceClassName() throws Exception {
   String oldName = "test2.ReplaceClassName2";
   String newName = "test2.ReplaceClassName3";
   CtClass cc = sloader.get("test2.ReplaceClassName");
   cc.replaceClassName(oldName, newName);
   cc.writeFile();
   CtClass cc2 = dloader.get(cc.getName());
   CtMethod m = cc2.getDeclaredMethod("foo");
   assertEquals(newName, m.getParameterTypes()[0].getName());
 }
예제 #9
0
 public void testArrayLength() throws Exception {
   CtClass cc = sloader.makeClass("test2.ArrayLength");
   CtMethod m2 =
       CtNewMethod.make(
           "public int f() { String[] s = new String[3]; " + "return s.length; }", cc);
   cc.addMethod(m2);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(3, invoke(obj, "f"));
 }
예제 #10
0
 public void testLocalVar() throws Exception {
   CtClass cc = sloader.get("test2.LocalVar");
   CtMethod m = cc.getDeclaredMethod("toString");
   m.addLocalVariable("var", CtClass.booleanType);
   m.insertBefore("{var = true; }");
   m.insertAfter("{if (var) hashCode(); }", false);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(3, invoke(obj, "foo"));
 }
예제 #11
0
 public void testAddLocalVar() throws Exception {
   CtClass cc = sloader.get("test2.AddLocalVar");
   CtMethod m1 = cc.getDeclaredMethod("foo");
   m1.addLocalVariable("i", CtClass.intType);
   m1.insertBefore("i = 3;");
   m1.insertAfter("$_ = i + 1;");
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(4, invoke(obj, "foo"));
 }
예제 #12
0
 public void testConstBody() throws Exception {
   CtClass cc = sloader.get("test2.ConstBody");
   CtConstructor cons =
       new CtConstructor(
           new CtClass[] {sloader.get("java.lang.String"), sloader.get("java.lang.Integer")}, cc);
   cons.setBody("super((String)$1, (Integer)$2);");
   cc.addConstructor(cons);
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "bar"));
 }
예제 #13
0
 public void testSetExceptions() throws Exception {
   CtClass cc = sloader.get("test2.SetExceptions");
   CtMethod m = cc.getDeclaredMethod("f");
   CtClass ex = m.getExceptionTypes()[0];
   assertEquals("java.lang.Exception", ex.getName());
   m.setExceptionTypes(null);
   assertEquals(0, m.getExceptionTypes().length);
   m.setExceptionTypes(new CtClass[0]);
   assertEquals(0, m.getExceptionTypes().length);
   m.setExceptionTypes(new CtClass[] {ex});
   assertEquals(ex, m.getExceptionTypes()[0]);
 }
예제 #14
0
  public void testMethodInInner() throws Exception {
    CtClass inner = sloader.get("test2.Nested2$Inner");
    CtClass outer = sloader.get("test2.Nested2");
    String src = "public int f(test2.Nested2 n) {" + "  n.i = 1; n.i++; n.i += 2; return n.i; }";

    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make(src, inner);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();

    CtMethod m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    src =
        "public int g(test2.Nested2 n) {"
            + "  n.d = 1.0; n.d++; n.d += 2.0;"
            + "  return n.d == 4.0 ? 7 : 8; }";
    m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    src =
        "public int h(test2.Nested2 n) {"
            + "  n.s = \"poi\";"
            + "return n.s.length() + f(n) + g(n); }";
    m = CtNewMethod.make(src, inner);
    inner.addMethod(m);

    inner.writeFile();
    outer.writeFile();

    Object nobj = make(outer.getName());
    Object iobj = make(inner.getName());
    Method mth = iobj.getClass().getMethod("h", new Class[] {nobj.getClass()});
    Object resobj = mth.invoke(iobj, new Object[] {nobj});
    int res = ((Integer) resobj).intValue();
    assertEquals(14, res);
  }
예제 #15
0
 public void testRemoveCall() throws Exception {
   CtClass cc = sloader.get("test2.RemoveCall");
   CtMethod m1 = cc.getDeclaredMethod("bar");
   m1.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           m.replace("{ $_ = ($r)null; }");
         }
       });
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(0, invoke(obj, "bar"));
 }
예제 #16
0
 public void testSuperCall() throws Exception {
   CtClass cc = sloader.get("test2.SuperCall");
   CtMethod m1 = cc.getDeclaredMethod("foo");
   m1.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           m.replace("{ $_ = $proceed($$); }");
         }
       });
   cc.writeFile();
   Object obj = make(cc.getName());
   invoke(obj, "bar");
 }
예제 #17
0
 public void testBrennan() throws Exception {
   CtClass cc = sloader.get("test2.Brennan");
   cc.addMethod(
       CtNewMethod.make(
           "public int foo(){"
               + "  java.text.SimpleDateFormat df;"
               + "  if((df = (java.text.SimpleDateFormat)format) == null)"
               + "    df = new java.text.SimpleDateFormat(\"yyyyMMdd\");"
               + "  return 1;}",
           cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "foo"));
 }
예제 #18
0
  public void testIncOp() throws Exception {
    CtClass target = sloader.makeClass("test2.IncOp");
    String src =
        "public int sample() {"
            + "    int ia[] = new int[50];"
            + "    ia[0] = 1;"
            + "    int v = ++(ia[0]);"
            + "    return v; }";

    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);
    target.writeFile();
    Object obj = make(target.getName());
    assertEquals(2, invoke(obj, "sample"));
  }
예제 #19
0
  public void testDeclaringClass() throws Exception {
    try {
      CtClass cc = sloader.get("test2.NotExistingClass");
    } catch (NotFoundException e) {
      System.out.println(e);
    }
    CtClass inner = sloader.get("test2.Nested$Inner");
    CtClass outer = sloader.get("test2.Nested");
    assertEquals(outer, inner.getDeclaringClass());
    assertEquals(null, outer.getDeclaringClass());
    assertEquals(null, CtClass.intType.getDeclaringClass());

    CtClass inner3 = sloader.get("test2.Nested$Inner3");
    outer.writeFile();
    try {
      CtMethod m = CtNewMethod.make("public void f(test2.Nested n) { return n.geti(); }", inner3);
      fail();
    } catch (RuntimeException e) {
    }
    outer.defrost();
    CtMethod m =
        CtNewMethod.make(
            "public int f(test2.Nested n) { "
                + "return n.geti() + test2.Nested.getj(1) + f() + g(); } ",
            inner3);
    inner3.addMethod(m);
    inner3.writeFile();
    outer.writeFile();

    Object nobj = make(outer.getName());
    Object iobj = make(inner3.getName());
    Method mth = iobj.getClass().getMethod("f", new Class[] {nobj.getClass()});
    Object resobj = mth.invoke(iobj, new Object[] {nobj});
    int res = ((Integer) resobj).intValue();
    assertEquals(6, res);
  }
예제 #20
0
  public void testNewExprInTry() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          public void edit(NewExpr expr) throws CannotCompileException {
            expr.replace("$_ = new test2.HashMapWrapper($1, 1);");
          }
        };

    CtClass cc = sloader.get("test2.NewExprInTry");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(1, invoke(obj, "run"));
  }
예제 #21
0
 public void testImportPackage() throws Exception {
   CtClass cc2 = sloader.makeClass("test2.Imported");
   cc2.writeFile();
   CtClass cc = sloader.makeClass("test2.Importer");
   sloader.importPackage("test2");
   cc.addMethod(
       CtNewMethod.make(
           "public int foo(){ "
               + "  Imported obj = new Imported();"
               + "  return obj.getClass().getName().length(); }",
           cc));
   sloader.clearImportedPackages();
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(14, invoke(obj, "foo"));
 }
예제 #22
0
  private void testDotClass4(String cname, boolean java5) throws Exception {
    CtClass cc = sloader.makeClass(cname);
    if (java5) cc.getClassFile2().setVersionToJava5();

    CtMethod m =
        CtNewMethod.make(
            "public int test() {"
                + "  String s = Object.class.getName()"
                + "      + Object[].class.getName();"
                + "  return s.length(); }",
            cc);
    cc.addMethod(m);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(35, invoke(obj, "test"));
  }
예제 #23
0
  public void testStaticFinal() throws Exception {
    CtClass cc = sloader.makeClass("test2.StaticFinal");
    CtField f = new CtField(CtClass.intType, "sff1", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "5");
    assertEquals(new Integer(5), f.getConstantValue());

    f = new CtField(CtClass.longType, "sff2", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "6");
    assertEquals(new Long(6), f.getConstantValue());

    f = new CtField(CtClass.floatType, "sff3", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "7");
    assertEquals(new Float(7.0F), f.getConstantValue());

    f = new CtField(CtClass.floatType, "sff4", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "8.0");
    assertEquals(new Float(8.0F), f.getConstantValue());

    f = new CtField(CtClass.doubleType, "sff5", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "9");
    assertEquals(new Double(9.0), f.getConstantValue());

    f = new CtField(CtClass.doubleType, "sff6", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "10.0");
    assertEquals(new Double(10.0), f.getConstantValue());

    f = new CtField(sloader.get("java.lang.String"), "sff7", cc);
    f.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(f, "\"test\"");
    assertEquals("test", f.getConstantValue());

    f = new CtField(sloader.get("java.lang.String"), "sff8", cc);
    f.setModifiers(Modifier.STATIC);
    cc.addField(f, "\"static\"");
    assertEquals(null, f.getConstantValue());

    cc.addMethod(CtNewMethod.make("public int foo(){ return sff1 + sff7.length(); }", cc));
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(9, invoke(obj, "foo"));
  }
예제 #24
0
 public void testMakeStaticMethod() throws Exception {
   CtClass cc = sloader.makeClass("test2.MakeStaticMethod");
   CtMethod m =
       CtNewMethod.make(
           Modifier.PUBLIC | Modifier.STATIC,
           CtClass.intType,
           "create",
           new CtClass[] {CtClass.intType},
           null,
           "{ return $1; }",
           cc);
   cc.addMethod(m);
   cc.addMethod(CtNewMethod.make("public int test() { return create(13); }", cc));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(13, invoke(obj, "test"));
 }
예제 #25
0
  private void testDotClass2(String cname, boolean java5) throws Exception {
    CtClass cc = sloader.makeClass(cname);
    CtClass cc3 = sloader.makeClass("test2.DotClass3");
    if (java5) cc.getClassFile2().setVersionToJava5();

    CtMethod m =
        CtNewMethod.make(
            "public int test() {" + "  return test2.DotClass3.class.getName().length(); }", cc);
    cc.addMethod(m);
    cc.writeFile();
    // don't execute cc3.writeFile();
    Object obj = make(cc.getName());
    try {
      assertEquals(15, invoke(obj, "test"));
    } catch (java.lang.reflect.InvocationTargetException e) {
      Throwable t = e.getCause();
      assertTrue(t instanceof java.lang.NoClassDefFoundError);
    }
  }
예제 #26
0
  public void testRemove() throws Exception {
    CtClass cc = sloader.get("test2.Remove");
    testRemove2(cc, "f1");
    testRemove2(cc, "f6");
    testRemove2(cc, "f3");
    CtField p = cc.getField("p");
    try {
      cc.removeField(p);
      fail("non-existing field has been removed");
    } catch (NotFoundException e) {
    }

    testRemove3(cc, "bar");
    testRemove3(cc, "bar2");
    testRemove4(cc, "(I)V");
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(7, invoke(obj, "foo"));
  }
예제 #27
0
  public void testNewExprTry() throws Exception {
    ExprEditor ed =
        new ExprEditor() {
          public void edit(NewExpr expr) throws CannotCompileException {
            StringBuffer code = new StringBuffer(300);
            code.append("{ try ");
            code.append("{ $_ = $proceed($$); }");
            code.append("catch (OutOfMemoryError e) {}}");
            expr.replace(code.toString());
          }
        };

    CtClass cc = sloader.get("test2.NewExprTry");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    m1.instrument(ed);
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(16, invoke(obj, "run"));
  }
예제 #28
0
  public void testSetSuper() throws Exception {
    CtClass cc = sloader.makeClass("test2.SetSuper");
    CtClass cc2 = sloader.makeClass("test2.SetSuperParent");
    CtClass intf = sloader.makeInterface("test2.SetSuperIntf");
    CtClass remote = sloader.get("java.rmi.Remote");

    cc.setSuperclass(cc2);
    cc.setInterfaces(new CtClass[] {intf});
    intf.setSuperclass(remote);
    intf.writeFile();
    cc2.writeFile();
    cc.writeFile();

    assertEquals(cc2, cc.getSuperclass());
    assertEquals(intf, cc.getInterfaces()[0]);
    assertEquals(sloader.get("java.lang.Object"), intf.getSuperclass());
    assertEquals(remote, intf.getInterfaces()[0]);

    make(cc.getName());
  }
예제 #29
0
  public void testStaticMember2() throws Exception {
    CtClass cc = sloader.get("test2.StaticMember2");

    cc.addMethod(
        CtNewMethod.make(
            "public int run() {"
                + "  return test2.StaticMember2.k + test2.StaticMember2.seven()"
                + "         + (test2.StaticMember2.f + f)"
                + "         + test2.StaticMember2.f + f; }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run1() {"
                + "  long j = 1L;"
                + "  return (int)(j + (test2.StaticMember2.fj + fj)"
                + "         + test2.StaticMember2.fj + fj); }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run2() {"
                + "  double x = 1.0;"
                + "  double d = x + test2.StaticMember2.fd + fd"
                + "             + (test2.StaticMember2.fd + fd);"
                + "  return (int)(d * 10); }",
            cc));

    cc.addMethod(
        CtNewMethod.make(
            "public int run3() {" + "  return (test2.StaticMember2.fb & fb) ? 1 : 0; }", cc));

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(54, invoke(obj, "run"));
    assertEquals(53, invoke(obj, "run1"));
    assertEquals(958, invoke(obj, "run2"));
    assertEquals(0, invoke(obj, "run3"));
  }
예제 #30
0
  public void testMakeNestedClass() throws Exception {
    CtClass outer = sloader.get("test2.Nested4");
    try {
      CtClass inner = outer.makeNestedClass("Inner", false);
      fail();
    } catch (RuntimeException e) {
      print(e.getMessage());
    }

    CtClass nested = outer.makeNestedClass("Inner", true);
    outer.stopPruning(true);
    outer.writeFile();
    outer.defrost();
    String src = "public int f() { return test2.Nested4.value; }";

    CtMethod m = CtNewMethod.make(src, nested);
    nested.addMethod(m);
    nested.writeFile();
    outer.writeFile();

    Object iobj = make(nested.getName());
    assertEquals(6, invoke(iobj, "f"));
  }