@@ -110,6 +110,11 @@ public abstract class ClassUtils {
110110 */
111111 private static final Set <Class <?>> javaLanguageInterfaces ;
112112
113+ /**
114+ * Cache for equivalent methods on an interface implemented by the declaring class.
115+ */
116+ private static final Map <Method , Method > interfaceMethodCache = new ConcurrentReferenceHashMap <>(256 );
117+
113118
114119 static {
115120 primitiveWrapperTypeMap .put (Boolean .class , boolean .class );
@@ -1291,22 +1296,25 @@ public static Method getMostSpecificMethod(Method method, @Nullable Class<?> tar
12911296 * @see #getMostSpecificMethod
12921297 */
12931298 public static Method getInterfaceMethodIfPossible (Method method ) {
1294- if (Modifier .isPublic (method .getModifiers ()) && !method .getDeclaringClass ().isInterface ()) {
1295- Class <?> current = method .getDeclaringClass ();
1299+ if (!Modifier .isPublic (method .getModifiers ()) || method .getDeclaringClass ().isInterface ()) {
1300+ return method ;
1301+ }
1302+ return interfaceMethodCache .computeIfAbsent (method , key -> {
1303+ Class <?> current = key .getDeclaringClass ();
12961304 while (current != null && current != Object .class ) {
12971305 Class <?>[] ifcs = current .getInterfaces ();
12981306 for (Class <?> ifc : ifcs ) {
12991307 try {
1300- return ifc .getMethod (method .getName (), method .getParameterTypes ());
1308+ return ifc .getMethod (key .getName (), key .getParameterTypes ());
13011309 }
13021310 catch (NoSuchMethodException ex ) {
13031311 // ignore
13041312 }
13051313 }
13061314 current = current .getSuperclass ();
13071315 }
1308- }
1309- return method ;
1316+ return key ;
1317+ }) ;
13101318 }
13111319
13121320 /**
0 commit comments