public void testRemoveAnnotatino() throws Exception {
    CtClass cc = sloader.get("test5.RemoveAnnotation");
    AnnotationsAttribute aa =
        (AnnotationsAttribute) cc.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa.removeAnnotation("test5.RemoveAnno1"));
    AttributeInfo ai = cc.getClassFile().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod foo = cc.getDeclaredMethod("foo");
    AnnotationsAttribute aa2 =
        (AnnotationsAttribute) foo.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa2.removeAnnotation("test5.RemoveAnno1"));

    CtMethod bar = cc.getDeclaredMethod("bar");
    AnnotationsAttribute aa3 =
        (AnnotationsAttribute) bar.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertFalse(aa3.removeAnnotation("test5.RemoveAnno1"));
    assertTrue(aa3.removeAnnotation("test5.RemoveAnno2"));
    AttributeInfo ai2 = bar.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai2.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod run = cc.getDeclaredMethod("run");
    AttributeInfo ai3 = run.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertNull(ai3);

    CtField baz = cc.getDeclaredField("baz");
    AttributeInfo ai4 = baz.getFieldInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai4.getName(), AnnotationsAttribute.invisibleTag);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(3, invoke(obj, "run"));
  }
Exemple #2
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());
  }
  public void testJIRA256() throws Exception {
    // CtClass ec = sloader.get("test5.Entity");

    CtClass cc = sloader.makeClass("test5.JIRA256");
    ClassFile ccFile = cc.getClassFile();
    ConstPool constpool = ccFile.getConstPool();

    AnnotationsAttribute attr =
        new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    javassist.bytecode.annotation.Annotation entityAnno =
        new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
    // = new javassist.bytecode.annotation.Annotation(constpool, ec);

    entityAnno.addMemberValue(
        "value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
    attr.addAnnotation(entityAnno);
    ccFile.addAttribute(attr);

    cc.writeFile();
    Object o = make(cc.getName());
    assertTrue(o.getClass().getName().equals("test5.JIRA256"));

    java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
    assertEquals(1, annotations.length);
  }
 private void findAndRemoveMethod(CtClass ctClass, String methodName) throws NotFoundException {
   try {
     CtMethod ctMethod = ctClass.getDeclaredMethod(methodName);
     ctClass.getClassFile().getMethods().remove(ctMethod.getMethodInfo());
   } catch (Exception e) {
   }
 }
 public void testTypeAnno() throws Exception {
   CtClass cc = sloader.get("test5.TypeAnno");
   cc.getClassFile().compact();
   cc.writeFile();
   Object obj = make(cc.getName());
   TypeVariable<?> t = obj.getClass().getTypeParameters()[0];
   Annotation[] annos = t.getAnnotations();
   assertEquals("@test5.TypeAnnoA()", annos[0].toString());
 }
  private void findAndRemoveMethod(CtClass ctClass, CtField ctField, String className) {

    try {
      CtMethod ctMethod =
          ctClass.getDeclaredMethod(
              ctField.getName(), new CtClass[] {ctClass.getClassPool().get(className)});
      ctClass.getClassFile().getMethods().remove(ctMethod.getMethodInfo());
    } catch (Exception e) {
    }
  }
 public void testInnerClassAttributeRemove() throws Exception {
   CtClass cc = sloader.get("test5.InnerClassRemove");
   ClassFile cf = cc.getClassFile();
   InnerClassesAttribute ica = (InnerClassesAttribute) cf.getAttribute(InnerClassesAttribute.tag);
   String second = ica.innerClass(1);
   String secondName = ica.innerName(1);
   String third = ica.innerClass(2);
   String thirdName = ica.innerName(2);
   assertEquals(3, ica.remove(3));
   assertEquals(2, ica.remove(0));
   assertEquals(second, ica.innerClass(0));
   assertEquals(secondName, ica.innerName(0));
   assertEquals(third, ica.innerClass(1));
   assertEquals(thirdName, ica.innerName(1));
   assertEquals(1, ica.remove(1));
   assertEquals(second, ica.innerClass(0));
   assertEquals(secondName, ica.innerName(0));
   cc.writeFile();
   Object obj = make(cc.getName());
   assertEquals(1, invoke(obj, "run"));
 }