From bc492b9251dd41d0e568d72454de8de74260f083 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 14 Dec 2015 15:13:29 +0100 Subject: [PATCH] Defensive error reporting when StandardAnnotationMetadata introspects declared methods Issue: SPR-13791 (cherry picked from commit a36c0a5) --- .../annotation/AnnotatedElementUtils.java | 2 +- .../core/type/StandardAnnotationMetadata.java | 34 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index b7a528609d..c5932b9459 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -163,7 +163,7 @@ public class AnnotatedElementUtils { new HashSet(), 0); } catch (Throwable ex) { - throw new IllegalStateException("Failed to introspect annotations: " + element, ex); + throw new IllegalStateException("Failed to introspect annotations on " + element, ex); } } diff --git a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java index cc2a86ce09..157d7860d9 100644 --- a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,25 +126,35 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @Override public boolean hasAnnotatedMethods(String annotationType) { - Method[] methods = getIntrospectedClass().getDeclaredMethods(); - for (Method method : methods) { - if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) { - return true; + try { + Method[] methods = getIntrospectedClass().getDeclaredMethods(); + for (Method method : methods) { + if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) { + return true; + } } + return false; + } + catch (Throwable ex) { + throw new IllegalStateException("Failed to introspect annotated methods on " + getIntrospectedClass(), ex); } - return false; } @Override public Set getAnnotatedMethods(String annotationType) { - Method[] methods = getIntrospectedClass().getDeclaredMethods(); - Set annotatedMethods = new LinkedHashSet(); - for (Method method : methods) { - if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) { - annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap)); + try { + Method[] methods = getIntrospectedClass().getDeclaredMethods(); + Set annotatedMethods = new LinkedHashSet(); + for (Method method : methods) { + if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) { + annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap)); + } } + return annotatedMethods; + } + catch (Throwable ex) { + throw new IllegalStateException("Failed to introspect annotated methods on " + getIntrospectedClass(), ex); } - return annotatedMethods; } }