示例#1
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());
   }
 }
示例#2
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);
  }