示例#1
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) {
   }
 }
示例#2
0
  public void testRemove() throws Exception {
    CtClass cc = sloader.get("test2.Remove");
    testRemove2(cc, "f1");
    testRemove2(cc, "f6");
    testRemove2(cc, "f3");
    CtField p = cc.getField("p");
    try {
      cc.removeField(p);
      fail("non-existing field has been removed");
    } catch (NotFoundException e) {
    }

    testRemove3(cc, "bar");
    testRemove3(cc, "bar2");
    testRemove4(cc, "(I)V");
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(7, invoke(obj, "foo"));
  }
示例#3
0
  public void testConstField() throws Exception {
    CtClass cc = sloader.get("test2.ConstField");
    CtField f;
    f = cc.getField("b");
    assertEquals(true, ((Boolean) f.getConstantValue()).booleanValue());
    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("j");
    assertEquals(7L, ((Long) f.getConstantValue()).longValue());
    f = cc.getField("f");
    assertEquals(8.0F, ((Float) f.getConstantValue()).floatValue(), 0.0);
    f = cc.getField("d");
    assertEquals(9.0, ((Double) f.getConstantValue()).doubleValue(), 0.0);
    f = cc.getField("s");
    assertEquals("const", f.getConstantValue());
    f = cc.getField("obj");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("integer");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());

    cc.getClassFile().prune();

    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());
  }