From a690c8bf872c9e5fe4e14b6095ddb81de01a34c4 Mon Sep 17 00:00:00 2001 From: chiba Date: Fri, 26 Jun 2009 15:29:48 +0000 Subject: [PATCH] fixed JASSIST-84 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@484 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- Readme.html | 4 +- src/main/javassist/CtBehavior.java | 41 +++++++++++++++ src/main/javassist/CtClass.java | 26 ++++++++++ src/main/javassist/CtClassType.java | 77 +++++++++++++++++++++++++++++ src/main/javassist/CtField.java | 38 ++++++++++++++ src/main/javassist/CtMember.java | 25 ++++++++++ 6 files changed, 210 insertions(+), 1 deletion(-) diff --git a/Readme.html b/Readme.html index 009fa5ed..6317e262 100644 --- a/Readme.html +++ b/Readme.html @@ -283,7 +283,9 @@ see javassist.Dump.

-version 3.11

-version 3.10 on March 5, 2009 diff --git a/src/main/javassist/CtBehavior.java b/src/main/javassist/CtBehavior.java index f3677f8d..7c26d093 100644 --- a/src/main/javassist/CtBehavior.java +++ b/src/main/javassist/CtBehavior.java @@ -145,6 +145,47 @@ public abstract class CtBehavior extends CtMember { methodInfo.setAccessFlags(AccessFlag.of(mod)); } + /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return true if the annotation is found, + * otherwise false. + * @since 3.11 + */ + public boolean hasAnnotation(Class clz) { + MethodInfo mi = getMethodInfo2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.visibleTag); + return CtClassType.hasAnnotationType(clz, + getDeclaringClass().getClassPool(), + ainfo, ainfo2); + } + + /** + * Returns the annotation if the class has the specified annotation class. + * For example, if an annotation @Author is associated + * with this method/constructor, an Author object is returned. + * The member values can be obtained by calling methods on + * the Author object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise null. + * @since 3.11 + */ + public Object getAnnotation(Class clz) throws ClassNotFoundException { + MethodInfo mi = getMethodInfo2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.visibleTag); + return CtClassType.getAnnotationType(clz, + getDeclaringClass().getClassPool(), + ainfo, ainfo2); + } + /** * Returns the annotations associated with this method or constructor. * diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 97f7b426..9f17aa8b 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -474,6 +474,32 @@ public abstract class CtClass { return 0; } + /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return true if the annotation is found, otherwise false. + * @since 3.11 + */ + public boolean hasAnnotation(Class clz) { + return false; + } + + /** + * Returns the annotation if the class has the specified annotation class. + * For example, if an annotation @Author is associated + * with this class, an Author object is returned. + * The member values can be obtained by calling methods on + * the Author object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise null. + * @since 3.11 + */ + public Object getAnnotation(Class clz) throws ClassNotFoundException { + return null; + } + /** * Returns the annotations associated with this class. * For example, if an annotation @Author is associated diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 7ddee72d..46a7361f 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -434,6 +434,83 @@ class CtClassType extends CtClass { cf.setAccessFlags(AccessFlag.of(mod)); } + public boolean hasAnnotation(Class clz) { + ClassFile cf = getClassFile2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.visibleTag); + return hasAnnotationType(clz, getClassPool(), ainfo, ainfo2); + } + + static boolean hasAnnotationType(Class clz, ClassPool cp, + AnnotationsAttribute a1, AnnotationsAttribute a2) + { + Annotation[] anno1, anno2; + + if (a1 == null) + anno1 = null; + else + anno1 = a1.getAnnotations(); + + if (a2 == null) + anno2 = null; + else + anno2 = a2.getAnnotations(); + + String typeName = clz.getName(); + if (anno1 != null) + for (int i = 0; i < anno1.length; i++) + if (anno1[i].getTypeName().equals(typeName)) + return true; + + if (anno2 != null) + for (int i = 0; i < anno2.length; i++) + if (anno2[i].getTypeName().equals(typeName)) + return true; + + return false; + } + + public Object getAnnotation(Class clz) throws ClassNotFoundException { + ClassFile cf = getClassFile2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.visibleTag); + return getAnnotationType(clz, getClassPool(), ainfo, ainfo2); + } + + static Object getAnnotationType(Class clz, ClassPool cp, + AnnotationsAttribute a1, AnnotationsAttribute a2) + throws ClassNotFoundException + { + Annotation[] anno1, anno2; + + if (a1 == null) + anno1 = null; + else + anno1 = a1.getAnnotations(); + + if (a2 == null) + anno2 = null; + else + anno2 = a2.getAnnotations(); + + String typeName = clz.getName(); + if (anno1 != null) + for (int i = 0; i < anno1.length; i++) + if (anno1[i].getTypeName().equals(typeName)) + return toAnnoType(anno1[i], cp); + + if (anno2 != null) + for (int i = 0; i < anno2.length; i++) + if (anno2[i].getTypeName().equals(typeName)) + return toAnnoType(anno2[i], cp); + + return null; + } + public Object[] getAnnotations() throws ClassNotFoundException { return getAnnotations(false); } diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java index 6903daf2..af573d2c 100644 --- a/src/main/javassist/CtField.java +++ b/src/main/javassist/CtField.java @@ -240,6 +240,44 @@ public class CtField extends CtMember { fieldInfo.setAccessFlags(AccessFlag.of(mod)); } + /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return true if the annotation is found, otherwise false. + * @since 3.11 + */ + public boolean hasAnnotation(Class clz) { + FieldInfo fi = getFieldInfo2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + fi.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + fi.getAttribute(AnnotationsAttribute.visibleTag); + return CtClassType.hasAnnotationType(clz, getDeclaringClass().getClassPool(), + ainfo, ainfo2); + } + + /** + * Returns the annotation if the class has the specified annotation class. + * For example, if an annotation @Author is associated + * with this field, an Author object is returned. + * The member values can be obtained by calling methods on + * the Author object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise null. + * @since 3.11 + */ + public Object getAnnotation(Class clz) throws ClassNotFoundException { + FieldInfo fi = getFieldInfo2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + fi.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + fi.getAttribute(AnnotationsAttribute.visibleTag); + return CtClassType.getAnnotationType(clz, getDeclaringClass().getClassPool(), + ainfo, ainfo2); + } + /** * Returns the annotations associated with this field. * diff --git a/src/main/javassist/CtMember.java b/src/main/javassist/CtMember.java index 9c1406a2..09566427 100644 --- a/src/main/javassist/CtMember.java +++ b/src/main/javassist/CtMember.java @@ -29,6 +29,9 @@ public abstract class CtMember { */ static class Cache extends CtMember { protected void extendToString(StringBuffer buffer) {} + public boolean hasAnnotation(Class clz) { return false; } + public Object getAnnotation(Class clz) + throws ClassNotFoundException { return null; } public Object[] getAnnotations() throws ClassNotFoundException { return null; } public byte[] getAttribute(String name) { return null; } @@ -201,6 +204,28 @@ public abstract class CtMember { */ public abstract void setModifiers(int mod); + /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return true if the annotation is found, otherwise false. + * @since 3.11 + */ + public abstract boolean hasAnnotation(Class clz); + + /** + * Returns the annotation if the class has the specified annotation class. + * For example, if an annotation @Author is associated + * with this member, an Author object is returned. + * The member values can be obtained by calling methods on + * the Author object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise null. + * @since 3.11 + */ + public abstract Object getAnnotation(Class clz) throws ClassNotFoundException; + /** * Returns the annotations associated with this member. * For example, if an annotation @Author is associated -- 2.39.5