Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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());
 }
Exemplo n.º 6
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.º 7
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.º 8
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.º 9
0
  public void testMethodCall() throws Exception {
    CtClass cc = sloader.get("test2.MethodCall");

    CtMethod m1 = cc.getDeclaredMethod("bar");
    m1.instrument(
        new ExprEditor() {
          public void edit(MethodCall m) throws CannotCompileException {
            if ("clone".equals(m.getMethodName())) methodCallData = m.getClassName();
          }
        });

    cc.writeFile();
    assertEquals("java.lang.String[]", methodCallData);

    assertEquals("java.lang.String[]", sloader.get("[Ljava/lang/String;").getName());
    assertEquals("int[][]", sloader.get("[[I").getName());
  }
Exemplo n.º 10
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"));
  }
Exemplo n.º 11
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"));
  }
Exemplo n.º 12
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.º 13
0
  public void testNewOp() throws Exception {
    CtClass cc = sloader.get("test2.NewOp");

    CtMethod add =
        CtNewMethod.make(
            "public test2.NewOp2 "
                + "    addMonitoringRemoteEventListener("
                + "        test2.NewOp2 listener)"
                + "        throws java.rmi.RemoteException {"
                + "    $0.listenerList.addElement(listener);"
                + "    return new test2.NewOp2(0L, this, null, 0L);"
                + "}",
            cc);

    add.setModifiers(Modifier.PUBLIC);
    cc.addMethod(add);
    /*
        CtMethod type= CtNewMethod.make(
           "public test2.Where getNewType() { return new test2.Where(); }",
            cc);
        cc.addMethod(type);
    */
    cc.writeFile();
  }
Exemplo n.º 14
0
  public void testInsertAt() throws Exception {
    CtClass cc = sloader.get("test2.InsertAt");
    CtMethod m1 = cc.getDeclaredMethod("foo");
    int line = 6;
    int ln = m1.insertAt(line, false, null);
    int ln2 = m1.insertAt(line, "counter++;");
    assertEquals(ln, ln2);
    assertEquals(7, ln2);

    line = 8;
    ln = m1.insertAt(line, false, null);
    ln2 = m1.insertAt(line, "counter++;");
    assertEquals(ln, ln2);
    assertEquals(8, ln2);

    CtMethod m2 = cc.getDeclaredMethod("bar2");
    int ln3 = m2.insertAt(20, "{ int m = 13; j += m; }");
    assertEquals(20, ln3);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(7, invoke(obj, "foo"));
    assertEquals(25, invoke(obj, "bar"));
  }
Exemplo n.º 15
0
 public void testArrayAndNull() throws Exception {
   CtClass cc = sloader.get("test2.ArrayAndNull");
   CtMethod m = cc.getDeclaredMethod("test");
   m.insertAfter("if ($_ == null) $_ = new int[0];");
 }