* @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);
}
/**
* @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.
*/
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,
}
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;
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
{
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;
* @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);
}