diff options
author | chibash <chiba@javassist.org> | 2014-11-19 05:49:27 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2014-11-19 05:49:27 +0900 |
commit | 0ba6f9efdc82e386e8716d8f2b1f25a1b95580a8 (patch) | |
tree | a026009c0c5399659b9c09f43643800d76a1eb05 | |
parent | a0f4d935a20ffeefcfe4c66730ec702b7fff0928 (diff) | |
download | javassist-0ba6f9efdc82e386e8716d8f2b1f25a1b95580a8.tar.gz javassist-0ba6f9efdc82e386e8716d8f2b1f25a1b95580a8.zip |
for fixing JASSIST-236, I've added getDeclaredMethods(String) to CtClass.
-rw-r--r-- | src/main/javassist/ClassPool.java | 19 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 16 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 14 | ||||
-rw-r--r-- | src/test/javassist/JvstTest4.java | 34 | ||||
-rw-r--r-- | src/test/test4/AnnoLoad.java | 5 | ||||
-rw-r--r-- | src/test/test4/DeclMethodsList.java | 7 |
6 files changed, 93 insertions, 2 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 4804b593..0493c6db 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -815,7 +815,7 @@ public class ClassPool { /** * Creates a new public nested class. - * This method is called by CtClassType.makeNestedClass(). + * This method is called by {@link CtClassType#makeNestedClass()}. * * @param classname a fully-qualified class name. * @return the nested class. @@ -858,6 +858,23 @@ public class ClassPool { } /** + * Creates a new annotation. + * If there already exists a class/interface with the same name, + * the new interface overwrites that previous one. + * + * @param name a fully-qualified interface name. + * Or null if the annotation has no super interface. + * @param superclass the super interface. + * @throws RuntimeException if the existing interface is frozen. + * @since 3.19 + */ + public CtClass makeAnnotation(String name, CtClass superclass) throws RuntimeException { + CtClass cc = makeInterface(name, superclass); + cc.setModifiers(cc.getModifiers() | Modifier.ANNOTATION); + return cc; + } + + /** * Appends the system search path to the end of the * search path. The system search path * usually includes the platform library, extension diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 9e4ca20c..79d7efcb 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -26,10 +26,10 @@ import java.io.OutputStream; import java.net.URL; import java.security.ProtectionDomain; import java.util.Collection; + import javassist.bytecode.ClassFile; import javassist.bytecode.Descriptor; import javassist.bytecode.Opcode; -import javassist.bytecode.SignatureAttribute; import javassist.expr.ExprEditor; /* Note: @@ -993,6 +993,20 @@ public abstract class CtClass { } /** + * Retrieves methods with the specified name among the methods + * declared in the class. Multiple methods with different parameters + * may be returned. + * + * <p>Note: this method does not search the superclasses.</p> + * + * @param name method name. + * @since 3.19 + */ + public CtMethod[] getDeclaredMethods(String name) throws NotFoundException { + throw new NotFoundException(name); + } + + /** * Retrieves the method with the specified name among the methods * declared in the class. If there are multiple methods with * the specified name, then this method returns one of them. diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 4df382a9..7ed43759 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -1205,6 +1205,20 @@ class CtClassType extends CtClass { return cms; } + public CtMethod[] getDeclaredMethods(String name) throws NotFoundException { + CtMember.Cache memCache = getMembers(); + CtMember mth = memCache.methodHead(); + CtMember mthTail = memCache.lastMethod(); + ArrayList<CtMethod> methods = new ArrayList<CtMethod>(); + while (mth != mthTail) { + mth = mth.next(); + if (mth.getName().equals(name)) + methods.add((CtMethod)mth); + } + + return methods.toArray(new CtMethod[methods.size()]); + } + public CtMethod getDeclaredMethod(String name) throws NotFoundException { CtMember.Cache memCache = getMembers(); CtMember mth = memCache.methodHead(); diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java index d92384b4..13784fd7 100644 --- a/src/test/javassist/JvstTest4.java +++ b/src/test/javassist/JvstTest4.java @@ -5,7 +5,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashSet; +import java.util.List; + import javassist.bytecode.*; +import javassist.bytecode.annotation.Annotation; import javassist.expr.*; public class JvstTest4 extends JvstTestRoot { @@ -1038,4 +1041,35 @@ public class JvstTest4 extends JvstTestRoot { assertEquals("test4.AnnoArg$B", a.value().getName()); System.out.println(a.value().getName()); } + + public void testDeclaredMethods() throws Exception { + CtClass cc = sloader.get("test4.DeclMethodsList"); + CtMethod[] meth = cc.getDeclaredMethods("foo"); + assertEquals(2, meth.length); + assertEquals("()V", meth[0].getSignature()); + assertEquals("(I)I", meth[1].getSignature()); + meth = cc.getDeclaredMethods("bar"); + assertEquals(1, meth.length); + assertEquals("()V", meth[0].getSignature()); + meth = cc.getDeclaredMethods("baz"); + assertEquals(0, meth.length); + } + + public void testAnnotationLoader() throws Exception { + CtClass anno = sloader.makeAnnotation("test4.AnnoLoadAnno", null); + anno.debugWriteFile(); + CtClass cc = sloader.get("test4.AnnoLoad"); + CtMethod m = cc.getDeclaredMethod("foo"); + ClassFile cf = cc.getClassFile(); + ConstPool cp = cf.getConstPool(); + AnnotationsAttribute attr + = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); + Annotation a = new Annotation(anno.getName(), cp); + attr.setAnnotation(a); + m.getMethodInfo().addAttribute(attr); + cc.writeFile(); + Object obj = m.getAnnotations()[0]; + String name = obj.getClass().getName(); + assertEquals(anno.getName(), name); + } } diff --git a/src/test/test4/AnnoLoad.java b/src/test/test4/AnnoLoad.java new file mode 100644 index 00000000..fba397cb --- /dev/null +++ b/src/test/test4/AnnoLoad.java @@ -0,0 +1,5 @@ +package test4; + +public class AnnoLoad { + public int foo() { return 0; } +} diff --git a/src/test/test4/DeclMethodsList.java b/src/test/test4/DeclMethodsList.java new file mode 100644 index 00000000..e762e4cc --- /dev/null +++ b/src/test/test4/DeclMethodsList.java @@ -0,0 +1,7 @@ +package test4; + +public class DeclMethodsList { + public void foo() {} + int foo(int i) { return i; } + public void bar() {} +} |