예제 #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 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);
  }
예제 #3
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);
  }
예제 #4
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"));
  }
예제 #5
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);
  }