[jboss-cvs] javassist/src/main/javassist ...

Kabir Khan kkhan at jboss.com
Mon Jul 17 12:48:30 EDT 2006


  User: kkhan   
  Date: 06/07/17 12:48:30

  Modified:    src/main/javassist     CtBehavior.java CtField.java
                        CtClassType.java CtClass.java
  Log:
  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.
  
  Revision  Changes    Path
  1.29      +52 -9     javassist/src/main/javassist/CtBehavior.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtBehavior.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtBehavior.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -b -r1.28 -r1.29
  --- CtBehavior.java	29 May 2006 09:41:55 -0000	1.28
  +++ CtBehavior.java	17 Jul 2006 16:48:29 -0000	1.29
  @@ -143,12 +143,32 @@
        * @since 3.1
        */
       public Object[] getAnnotations() throws ClassNotFoundException {
  +       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(getDeclaringClass().getClassPool(),
  +       return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(),
                                               ainfo, ainfo2);
       }
   
  @@ -163,12 +183,35 @@
        * @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);
       }
   
  
  
  
  1.17      +24 -1     javassist/src/main/javassist/CtField.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtField.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtField.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -b -r1.16 -r1.17
  --- CtField.java	11 Jan 2006 06:45:54 -0000	1.16
  +++ CtField.java	17 Jul 2006 16:48:29 -0000	1.17
  @@ -241,12 +241,35 @@
        * @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);
       }
   
  
  
  
  1.51      +76 -22    javassist/src/main/javassist/CtClassType.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtClassType.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtClassType.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -b -r1.50 -r1.51
  --- CtClassType.java	8 Jun 2006 23:54:44 -0000	1.50
  +++ CtClassType.java	17 Jul 2006 16:48:29 -0000	1.51
  @@ -413,15 +413,30 @@
       }
   
       public Object[] getAnnotations() throws ClassNotFoundException {
  +       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(getClassPool(), ainfo, ainfo2);
  +       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,6 +459,7 @@
               size2 = anno2.length;
           }
   
  +        if (!ignoreNotFound){
           Object[] result = new Object[size1 + size2];
           for (int i = 0; i < size1; i++)
               result[i] = toAnnoType(anno1[i], cp);
  @@ -453,8 +469,27 @@
   
           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(ClassPool cp, ParameterAnnotationsAttribute a1,
  +    static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp, ParameterAnnotationsAttribute a1,
                                          ParameterAnnotationsAttribute a2, MethodInfo minfo)
           throws ClassNotFoundException
       {
  @@ -489,6 +524,7 @@
                   size2 = anno2.length;
               }
   
  +            if (!ignoreNotFound){
               result[i] = new Object[size1 + size2];
               for (int j = 0; j < size1; ++j)
                   result[i][j] = toAnnoType(anno1[j], cp);
  @@ -496,6 +532,24 @@
               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;
       }
  
  
  
  1.73      +15 -0     javassist/src/main/javassist/CtClass.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtClass.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtClass.java,v
  retrieving revision 1.72
  retrieving revision 1.73
  diff -u -b -r1.72 -r1.73
  --- CtClass.java	20 Jun 2006 17:18:06 -0000	1.72
  +++ CtClass.java	17 Jul 2006 16:48:29 -0000	1.73
  @@ -481,6 +481,21 @@
       }
   
       /**
  +     * 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,
        * and static nested classes.
  
  
  



More information about the jboss-cvs-commits mailing list