From 0273ae2abc0455feb3137e2ca7202e996d28085a Mon Sep 17 00:00:00 2001 From: kkhan Date: Mon, 17 Jul 2006 16:48:29 +0000 Subject: [PATCH] add getAvailableAnnotations() methods to CtClass, CtBehaviour and CtField. These work the same as getAnnotations() but instead of throwing a ClassNotFoundException, annotations not on the classpath are not returned. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@294 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/CtBehavior.java | 61 ++++++++++++++--- src/main/javassist/CtClass.java | 15 ++++ src/main/javassist/CtClassType.java | 102 +++++++++++++++++++++------- src/main/javassist/CtField.java | 25 ++++++- 4 files changed, 169 insertions(+), 34 deletions(-) diff --git a/src/main/javassist/CtBehavior.java b/src/main/javassist/CtBehavior.java index 4f2de78a..54ecddd4 100644 --- a/src/main/javassist/CtBehavior.java +++ b/src/main/javassist/CtBehavior.java @@ -143,13 +143,33 @@ public abstract class CtBehavior extends CtMember { * @since 3.1 */ public Object[] getAnnotations() throws ClassNotFoundException { - MethodInfo mi = getMethodInfo2(); - AnnotationsAttribute ainfo = (AnnotationsAttribute) - mi.getAttribute(AnnotationsAttribute.invisibleTag); - AnnotationsAttribute ainfo2 = (AnnotationsAttribute) - mi.getAttribute(AnnotationsAttribute.visibleTag); - return CtClassType.toAnnotationType(getDeclaringClass().getClassPool(), - ainfo, ainfo2); + return getAnnotations(false); + } + + /** + * Returns the annotations associated with this method or constructor. + * If any annotations are not on the classpath, they are not returned + * + * @return an array of annotation-type objects. + * @see CtMember#getAnnotations() + * @since 3.3 + */ + public Object[] getAvailableAnnotations(){ + try{ + return getAnnotations(true); + }catch (ClassNotFoundException e){ + throw new RuntimeException("Unexpected exception", e); + } + } + + private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { + MethodInfo mi = getMethodInfo2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + mi.getAttribute(AnnotationsAttribute.visibleTag); + return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(), + ainfo, ainfo2); } /** @@ -163,15 +183,38 @@ public abstract class CtBehavior extends CtMember { * @since 3.1 */ public Object[][] getParameterAnnotations() throws ClassNotFoundException { + return getParameterAnnotations(false); + } + + /** + * Returns the parameter annotations associated with this method or constructor. + * If any annotations are not on the classpath, they are not returned + * + * @return an array of annotation-type objects. The length of the returned array is + * equal to the number of the formal parameters. If each parameter has no + * annotation, the elements of the returned array are empty arrays. + * + * @see CtMember#getAnnotations() + * @since 3.3 + */ + public Object[][] getAvailableParameterAnnotations(){ + try { + return getParameterAnnotations(true); + }catch(ClassNotFoundException e) { + throw new RuntimeException("Unexpected exception", e); + } + } + + Object[][] getParameterAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { MethodInfo mi = getMethodInfo2(); ParameterAnnotationsAttribute ainfo = (ParameterAnnotationsAttribute) mi.getAttribute(ParameterAnnotationsAttribute.invisibleTag); ParameterAnnotationsAttribute ainfo2 = (ParameterAnnotationsAttribute) mi.getAttribute(ParameterAnnotationsAttribute.visibleTag); - return CtClassType.toAnnotationType(getDeclaringClass().getClassPool(), + return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(), ainfo, ainfo2, mi); } - + /** * Obtains parameter types of this method/constructor. */ diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 287058f2..a8261b37 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -480,6 +480,21 @@ public abstract class CtClass { return new Object[0]; } + /** + * Returns the annotations associated with this class. + * For example, if an annotation @Author is associated + * with this class, the returned array contains an Author + * object. The member values can be obtained by calling methods on + * the Author object. If any annotations are not on the + * classpath, they are not returned + * + * @return an array of annotation-type objects. + * @since 3.3 + */ + public Object[] getAvailableAnnotations(){ + return new Object[0]; + } + /** * Returns an array of nested classes declared in the class. * Nested classes are inner classes, anonymous classes, local classes, diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 1a29269c..bc059026 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -413,15 +413,30 @@ class CtClassType extends CtClass { } public Object[] getAnnotations() throws ClassNotFoundException { - ClassFile cf = getClassFile2(); - AnnotationsAttribute ainfo = (AnnotationsAttribute) - cf.getAttribute(AnnotationsAttribute.invisibleTag); - AnnotationsAttribute ainfo2 = (AnnotationsAttribute) - cf.getAttribute(AnnotationsAttribute.visibleTag); - return toAnnotationType(getClassPool(), ainfo, ainfo2); + return getAnnotations(false); + } + + public Object[] getAvailableAnnotations(){ + try + { + return getAnnotations(true); + } + catch (ClassNotFoundException e) + { + throw new RuntimeException("Unexpected exception ", e); + } + } + + private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { + ClassFile cf = getClassFile2(); + AnnotationsAttribute ainfo = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.invisibleTag); + AnnotationsAttribute ainfo2 = (AnnotationsAttribute) + cf.getAttribute(AnnotationsAttribute.visibleTag); + return toAnnotationType(ignoreNotFound, getClassPool(), ainfo, ainfo2); } - static Object[] toAnnotationType(ClassPool cp, AnnotationsAttribute a1, + static Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp, AnnotationsAttribute a1, AnnotationsAttribute a2) throws ClassNotFoundException { Annotation[] anno1, anno2; int size1, size2; @@ -444,17 +459,37 @@ class CtClassType extends CtClass { size2 = anno2.length; } - Object[] result = new Object[size1 + size2]; - for (int i = 0; i < size1; i++) - result[i] = toAnnoType(anno1[i], cp); - - for (int j = 0; j < size2; j++) - result[j + size1] = toAnnoType(anno2[j], cp); - - return result; - } - - static Object[][] toAnnotationType(ClassPool cp, ParameterAnnotationsAttribute a1, + if (!ignoreNotFound){ + Object[] result = new Object[size1 + size2]; + for (int i = 0; i < size1; i++) + result[i] = toAnnoType(anno1[i], cp); + + for (int j = 0; j < size2; j++) + result[j + size1] = toAnnoType(anno2[j], cp); + + return result; + } + else{ + ArrayList annotations = new ArrayList(); + for (int i = 0 ; i < size1 ; i++){ + try{ + annotations.add(toAnnoType(anno1[i], cp)); + }catch(ClassNotFoundException e){ + } + } + for (int j = 0; j < size2; j++) + { + try{ + annotations.add(toAnnoType(anno2[j], cp)); + }catch(ClassNotFoundException e){ + } + } + + return annotations.toArray(); + } + } + + static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1, ParameterAnnotationsAttribute a2, MethodInfo minfo) throws ClassNotFoundException { @@ -489,12 +524,31 @@ class CtClassType extends CtClass { size2 = anno2.length; } - result[i] = new Object[size1 + size2]; - for (int j = 0; j < size1; ++j) - result[i][j] = toAnnoType(anno1[j], cp); - - for (int j = 0; j < size2; ++j) - result[i][j + size1] = toAnnoType(anno2[j], cp); + if (!ignoreNotFound){ + result[i] = new Object[size1 + size2]; + for (int j = 0; j < size1; ++j) + result[i][j] = toAnnoType(anno1[j], cp); + + for (int j = 0; j < size2; ++j) + result[i][j + size1] = toAnnoType(anno2[j], cp); + } + else{ + ArrayList annotations = new ArrayList(); + for (int j = 0 ; j < size1 ; j++){ + try{ + annotations.add(toAnnoType(anno1[j], cp)); + }catch(ClassNotFoundException e){ + } + } + for (int j = 0; j < size2; j++){ + try{ + annotations.add(toAnnoType(anno2[j], cp)); + }catch(ClassNotFoundException e){ + } + } + + result[i] = annotations.toArray(); + } } return result; diff --git a/src/main/javassist/CtField.java b/src/main/javassist/CtField.java index 86dd9b5d..ea535290 100644 --- a/src/main/javassist/CtField.java +++ b/src/main/javassist/CtField.java @@ -241,12 +241,35 @@ public class CtField extends CtMember { * @since 3.1 */ public Object[] getAnnotations() throws ClassNotFoundException { + return getAnnotations(false); + } + + /** + * Returns the annotations associated with this field. + * If any annotations are not on the classpath, they are not returned + * + * @return an array of annotation-type objects. + * @see CtMember#getAnnotations() + * @since 3.3 + */ + public Object[] getAvailableAnnotations(){ + try + { + return getAnnotations(true); + } + catch (ClassNotFoundException e) + { + throw new RuntimeException("Unexpected exception", e); + } + } + + private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { FieldInfo fi = getFieldInfo2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) fi.getAttribute(AnnotationsAttribute.visibleTag); - return CtClassType.toAnnotationType(getDeclaringClass().getClassPool(), + return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(), ainfo, ainfo2); } -- 2.39.5