Exemplo n.º 1
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"));
  }
Exemplo n.º 2
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"));
  }
Exemplo n.º 3
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());
 }
Exemplo n.º 4
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"));
  }
Exemplo n.º 5
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"));
 }
Exemplo n.º 6
0
 public void testConstructorName() throws Exception {
   CtClass cc = sloader.get("test2.Construct");
   CtConstructor[] cons = cc.getDeclaredConstructors();
   assertEquals("Construct", cons[0].getName());
   assertEquals("Construct", cons[1].getName());
   assertEquals("<clinit>", cc.getClassInitializer().getName());
 }
Exemplo n.º 7
0
 public void testObjectSuper() throws Exception {
   CtClass cc = sloader.get("java.lang.Object");
   try {
     cc.addMethod(CtNewMethod.make("public int foo(){ return super.hashCode(); }", cc));
     fail("could access the super of java.lang.Object");
   } catch (CannotCompileException e) {
   }
 }
Exemplo n.º 8
0
 public void testSuperInterface() throws Exception {
   CtClass cc = sloader.makeClass("test2.SuperInterface3");
   CtClass cc2 = sloader.get("test2.SuperInterface2");
   cc.addInterface(cc2);
   cc.addField(new CtField(cc2, "inner", cc));
   CtMethod m = CtNewMethod.make("public int getAge() { return inner.getAge(); }", cc);
   cc.addMethod(m);
   cc.writeFile();
 }
Exemplo n.º 9
0
 private void testRemove4(CtClass cc, String desc) throws Exception {
   CtConstructor c = cc.getConstructor(desc);
   cc.removeConstructor(c);
   try {
     CtConstructor c2 = cc.getConstructor(desc);
     fail("the removed method still exists");
   } catch (NotFoundException e) {
   }
 }
Exemplo n.º 10
0
 private void testRemove3(CtClass cc, String methodName) throws Exception {
   CtMethod m = cc.getDeclaredMethod(methodName);
   cc.removeMethod(m);
   try {
     CtMethod m2 = cc.getDeclaredMethod(methodName);
     fail("the removed method still exists");
   } catch (NotFoundException e) {
   }
 }
Exemplo n.º 11
0
 private void testRemove2(CtClass cc, String fieldName) throws Exception {
   CtField f = cc.getField(fieldName);
   cc.removeField(f);
   try {
     CtField f2 = cc.getField(fieldName);
     fail("the removed field still exists");
   } catch (NotFoundException e) {
   }
 }
Exemplo n.º 12
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"));
 }
Exemplo n.º 13
0
 public void testWhere() throws Exception {
   CtClass cc = sloader.get("test2.Where");
   CtConstructor cons = cc.getClassInitializer();
   cons.instrument(
       new ExprEditor() {
         public void edit(MethodCall m) throws CannotCompileException {
           System.out.println(m.where().getName());
         }
       });
 }
Exemplo n.º 14
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"));
 }
Exemplo n.º 15
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"));
 }
Exemplo n.º 16
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"));
  }
Exemplo n.º 17
0
 public void testDeclaringClass2() throws Exception {
   CtClass out = sloader.get("test2.Anon");
   CtClass inner = sloader.get("test2.Anon$1");
   if (System.getProperty("java.vm.version").startsWith("1.4"))
     assertTrue(inner.getEnclosingMethod() == null);
   else {
     assertEquals("make", inner.getEnclosingMethod().getName());
     assertEquals(out, inner.getDeclaringClass());
     assertEquals(out, inner.getEnclosingMethod().getDeclaringClass());
   }
 }
Exemplo n.º 18
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"));
 }
Exemplo n.º 19
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]);
 }
Exemplo n.º 20
0
  public void testStaticArrays() throws Exception {
    CtClass cc = sloader.makeClass("StaticArrays");
    CtField f = new CtField(sloader.get("test2.StaticArraysMem[]"), "myStaticField", cc);

    f.setModifiers(Modifier.STATIC);
    cc.addField(f);
    CtConstructor init = cc.makeClassInitializer();
    String body = "{\n";
    body += ("myStaticField = new test2.StaticArraysMem[2];\n");
    body += ("\n}");
    init.setBody(body);
  }
Exemplo n.º 21
0
 public void testNullArg() throws Exception {
   CtClass cc = sloader.makeClass("test2.NullArgTest");
   CtMethod m1 =
       CtNewMethod.make(
           "public Object foo(Object[] obj, int idx) throws Throwable {" + "    return null; }",
           cc);
   cc.addMethod(m1);
   CtMethod m2 = CtNewMethod.make("public void bar() { this.foo(null, 0); }", cc);
   cc.addMethod(m2);
   CtMethod m3 = CtNewMethod.make("public void bar2() { this.foo((Object[])null, 0); }", cc);
   cc.addMethod(m3);
   cc.writeFile();
 }
Exemplo n.º 22
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"));
 }
Exemplo n.º 23
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");
 }
Exemplo n.º 24
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"));
 }
Exemplo n.º 25
0
  public void testInheritance() throws Exception {
    ClassPool pool = ClassPool.getDefault();
    String classname = "test2.Inherit";
    CtClass target = pool.get(classname);
    String src =
        "public void sampleMethod() {"
            + "  test2.Inherit i = new test2.Inherit();"
            + "  test2.Inherit2 i2 = i;"
            + "  test2.Inherit3 i3 = i;"
            + "  i3.foo2(); i3.foo2(); i2.foo1(); }";

    CtMethod newmethod = CtNewMethod.make(src, target);
    target.addMethod(newmethod);
    target.writeFile();
  }
Exemplo n.º 26
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"));
  }
Exemplo n.º 27
0
  public void testPrune() throws Exception {
    CtClass cc = sloader.get("test2.Prune");
    cc.stopPruning(false);
    System.out.println(cc);
    cc.addField(new CtField(CtClass.intType, "f", cc));
    cc.toBytecode();
    try {
      cc.defrost();
      fail("can call defrost()");
    } catch (RuntimeException e) {
      assertTrue(e.getMessage().indexOf("prune") >= 0);
    }

    System.out.println(cc);
  }
Exemplo n.º 28
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"));
  }
Exemplo n.º 29
0
  public void testGetURL() throws Exception {
    CtClass cc = sloader.get("java.lang.String");
    String url = cc.getURL().toString();
    System.out.println(url);
    assertTrue(url.startsWith("jar:file:"));
    assertTrue(url.endsWith(".jar!/java/lang/String.class"));

    cc = sloader.get("int");
    try {
      URL u = cc.getURL();
      fail();
    } catch (NotFoundException e) {
      assertEquals("int", e.getMessage());
    }
  }
Exemplo n.º 30
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"));
  }