diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/javassist/JvstTest3.java | 3 | ||||
-rw-r--r-- | src/test/javassist/JvstTest5.java | 48 | ||||
-rw-r--r-- | src/test/test5/RemoveAnnotation.java | 20 |
3 files changed, 70 insertions, 1 deletions
diff --git a/src/test/javassist/JvstTest3.java b/src/test/javassist/JvstTest3.java index f399d077..056df5c2 100644 --- a/src/test/javassist/JvstTest3.java +++ b/src/test/javassist/JvstTest3.java @@ -1086,7 +1086,8 @@ public class JvstTest3 extends JvstTestRoot { CtMethod m3 = CtMethod.make("public void foo3() {}", cc); try { cc.addMethod(m3); - fail(); + if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_8) + fail(); } catch (CannotCompileException e) { // System.out.println(e); diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java index 41b640c7..9d22c47c 100644 --- a/src/test/javassist/JvstTest5.java +++ b/src/test/javassist/JvstTest5.java @@ -4,6 +4,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.TypeVariable; import javassist.bytecode.AnnotationsAttribute; +import javassist.bytecode.AttributeInfo; import javassist.bytecode.ClassFile; import javassist.bytecode.ConstPool; import javassist.bytecode.InnerClassesAttribute; @@ -241,5 +242,52 @@ public class JvstTest5 extends JvstTestRoot { cp.appendClassPath(new LoaderClassPath(new Loader())); assertNotNull(cp.get(Object.class.getName())); assertNotNull(cp.get(this.getClass().getName())); + + public void testAddDefaultMethod() throws Exception { + CtClass cc = sloader.makeInterface("test5.AddDefaultMethod"); + cc.addMethod(CtNewMethod.make("static int foo() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("public static int foo1() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("public int foo2() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("int foo3() { return 1; }", cc)); + try { + cc.addMethod(CtNewMethod.make("private int foo4() { return 1; }", cc)); + fail(); + } catch (CannotCompileException e) {} + try { + cc.addMethod(CtNewMethod.make("private static int foo5() { return 1; }", cc)); + fail(); + } catch (CannotCompileException e) {} + } + + 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")); } } diff --git a/src/test/test5/RemoveAnnotation.java b/src/test/test5/RemoveAnnotation.java new file mode 100644 index 00000000..97f75253 --- /dev/null +++ b/src/test/test5/RemoveAnnotation.java @@ -0,0 +1,20 @@ +package test5; + +@interface RemoveAnno1 {} + +@interface RemoveAnno2 { + int foo() default 3; +} + +@RemoveAnno1 public class RemoveAnnotation { + @RemoveAnno1 @RemoveAnno2(foo=4) + int foo() { return 1; } + + @RemoveAnno2 + int bar() { return 2; } + + @RemoveAnno1 + int baz = 10; + + public int run() { return foo() + bar(); } +} |