]> source.dussan.org Git - javassist.git/commitdiff
add getAvailableAnnotations() methods to CtClass, CtBehaviour and CtField. These...
authorkkhan <kkhan@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Mon, 17 Jul 2006 16:48:29 +0000 (16:48 +0000)
committerkkhan <kkhan@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Mon, 17 Jul 2006 16:48:29 +0000 (16:48 +0000)
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@294 30ef5769-5b8d-40dd-aea6-55b5d6557bb3

src/main/javassist/CtBehavior.java
src/main/javassist/CtClass.java
src/main/javassist/CtClassType.java
src/main/javassist/CtField.java

index 4f2de78aedae08e3c2e06f9d4a06de96f58b06e1..54ecddd41fa427cd6cab4a675acc99c82c097a30 100644 (file)
@@ -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.
      */
index 287058f2d72d0c0b11c522c69835d1f0e428615c..a8261b37c09bfe7a481c9b53b95d044345ddcabb 100644 (file)
@@ -480,6 +480,21 @@ public abstract class CtClass {
         return new Object[0];
     }
 
+    /**
+     * 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>
+     * object.  The member values can be obtained by calling methods on
+     * the <code>Author</code> 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,
index 1a29269cf75d52e3f708b37fd8d6c876a44c93b7..bc059026e16b2fc12165762485dd14aa47a9cb6a 100644 (file)
@@ -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;
index 86dd9b5d5a6a3c81fd9e478d62cba582ca9dc3fb..ea5352902dd5b2663032bf7268d5dbc46f135f0b 100644 (file)
@@ -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);
     }