diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2009-06-26 15:29:48 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2009-06-26 15:29:48 +0000 |
commit | a690c8bf872c9e5fe4e14b6095ddb81de01a34c4 (patch) | |
tree | c48a75507a2a9f9ef16b7983821f8d6a6a6f3e70 /src/main/javassist | |
parent | 3fcf471e97d82d5f7ad17d554bdd582c8a1a6986 (diff) | |
download | javassist-a690c8bf872c9e5fe4e14b6095ddb81de01a34c4.tar.gz javassist-a690c8bf872c9e5fe4e14b6095ddb81de01a34c4.zip |
fixed JASSIST-84
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@484 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist')
-rw-r--r-- | src/main/javassist/CtBehavior.java | 41 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 26 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 77 | ||||
-rw-r--r-- | src/main/javassist/CtField.java | 38 | ||||
-rw-r--r-- | src/main/javassist/CtMember.java | 25 |
5 files changed, 207 insertions, 0 deletions
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 @@ -146,6 +146,47 @@ public abstract class CtBehavior extends CtMember { } /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return <code>true</code> if the annotation is found, + * otherwise <code>false</code>. + * @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 <code>@Author</code> is associated + * with this method/constructor, an <code>Author</code> object is returned. + * The member values can be obtained by calling methods on + * the <code>Author</code> object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise <code>null</code>. + * @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. * * @return an array of annotation-type objects. 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 @@ -475,6 +475,32 @@ public abstract class CtClass { } /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return <code>true</code> if the annotation is found, otherwise <code>false</code>. + * @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 <code>@Author</code> is associated + * with this class, an <code>Author</code> object is returned. + * The member values can be obtained by calling methods on + * the <code>Author</code> object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise <code>null</code>. + * @since 3.11 + */ + public Object getAnnotation(Class clz) throws ClassNotFoundException { + return null; + } + + /** * Returns the annotations associated with this class. * For example, if an annotation <code>@Author</code> is associated * with this class, the returned array contains an <code>Author</code> 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 @@ -241,6 +241,44 @@ public class CtField extends CtMember { } /** + * Returns true if the class has the specified annotation class. + * + * @param clz the annotation class. + * @return <code>true</code> if the annotation is found, otherwise <code>false</code>. + * @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 <code>@Author</code> is associated + * with this field, an <code>Author</code> object is returned. + * The member values can be obtained by calling methods on + * the <code>Author</code> object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise <code>null</code>. + * @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. * * @return an array of annotation-type objects. 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; } @@ -202,6 +205,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 <code>true</code> if the annotation is found, otherwise <code>false</code>. + * @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 <code>@Author</code> is associated + * with this member, an <code>Author</code> object is returned. + * The member values can be obtained by calling methods on + * the <code>Author</code> object. + * + * @param clz the annotation class. + * @return the annotation if found, otherwise <code>null</code>. + * @since 3.11 + */ + public abstract Object getAnnotation(Class clz) throws ClassNotFoundException; + + /** * Returns the annotations associated with this member. * For example, if an annotation <code>@Author</code> is associated * with this member, the returned array contains an <code>Author</code> |