Avoid throwing checked exceptions from AnnotationUtils.hashCode()

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1077895 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2011-03-04 12:11:41 +00:00
parent ed69b85620
commit 4046e160ff
1 changed files with 18 additions and 13 deletions

View File

@ -135,9 +135,9 @@ public class AnnotationUtils {
} }
} }
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException ex) {
return false; return false;
} catch (InvocationTargetException e) { } catch (InvocationTargetException ex) {
return false; return false;
} }
return true; return true;
@ -155,17 +155,22 @@ public class AnnotationUtils {
* @throws IllegalAccessException if thrown during annotation access * @throws IllegalAccessException if thrown during annotation access
* @throws InvocationTargetException if thrown during annotation access * @throws InvocationTargetException if thrown during annotation access
*/ */
public static int hashCode(Annotation a) public static int hashCode(Annotation a) {
throws IllegalAccessException, InvocationTargetException {
int result = 0; int result = 0;
Class<? extends Annotation> type = a.annotationType(); Class<? extends Annotation> type = a.annotationType();
for (Method m : type.getDeclaredMethods()) { for (Method m : type.getDeclaredMethods()) {
Object value = m.invoke(a); try {
if (value == null) { Object value = m.invoke(a);
throw new IllegalStateException( if (value == null) {
String.format("Annotation method %s returned null", m)); throw new IllegalStateException(
String.format("Annotation method %s returned null", m));
}
result += hashMember(m.getName(), value);
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
} }
result += hashMember(m.getName(), value);
} }
return result; return result;
} }
@ -185,10 +190,10 @@ public class AnnotationUtils {
} }
try { try {
builder.append(m.getName(), m.invoke(a)); builder.append(m.getName(), m.invoke(a));
} catch (RuntimeException e) { } catch (RuntimeException ex) {
throw e; throw ex;
} catch (Exception e) { } catch (Exception ex) {
throw new RuntimeException(e); throw new RuntimeException(ex);
} }
} }
return builder.build(); return builder.build();