Ejemplo n.º 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"));
  }
Ejemplo n.º 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);
  }
Ejemplo n.º 3
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"));
  }