Skip to content

Commit 64de254

Browse files
committed
Merge branch '6.2.x'
2 parents b59dca9 + 4277682 commit 64de254

File tree

13 files changed

+69
-44
lines changed

13 files changed

+69
-44
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/SimpleAspectInstanceFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.aop.aspectj;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.lang.reflect.InvocationTargetException;
2021

2122
import org.jspecify.annotations.Nullable;
@@ -67,7 +68,7 @@ public final Object getAspectInstance() {
6768
throw new AopConfigException(
6869
"Unable to instantiate aspect class: " + this.aspectClass.getName(), ex);
6970
}
70-
catch (IllegalAccessException ex) {
71+
catch (IllegalAccessException | InaccessibleObjectException ex) {
7172
throw new AopConfigException(
7273
"Could not access aspect constructor: " + this.aspectClass.getName(), ex);
7374
}

spring-aop/src/main/java/org/springframework/aop/support/AopUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.aop.support;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
@@ -366,7 +367,7 @@ public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvi
366367
throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" +
367368
method + "] on target [" + target + "]", ex);
368369
}
369-
catch (IllegalAccessException ex) {
370+
catch (IllegalAccessException | InaccessibleObjectException ex) {
370371
throw new AopInvocationException("Could not access method [" + method + "]", ex);
371372
}
372373
}

spring-beans/src/main/java/org/springframework/beans/DirectFieldAccessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.beans;
1818

1919
import java.lang.reflect.Field;
20+
import java.lang.reflect.InaccessibleObjectException;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223

@@ -142,7 +143,7 @@ public TypeDescriptor getCollectionType(int nestingLevel) {
142143
ReflectionUtils.makeAccessible(this.field);
143144
return this.field.get(getWrappedInstance());
144145
}
145-
catch (IllegalAccessException ex) {
146+
catch (IllegalAccessException | InaccessibleObjectException ex) {
146147
throw new InvalidPropertyException(getWrappedClass(),
147148
this.field.getName(), "Field is not accessible", ex);
148149
}
@@ -154,7 +155,7 @@ public void setValue(@Nullable Object value) throws Exception {
154155
ReflectionUtils.makeAccessible(this.field);
155156
this.field.set(getWrappedInstance(), value);
156157
}
157-
catch (IllegalAccessException ex) {
158+
catch (IllegalAccessException | InaccessibleObjectException ex) {
158159
throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
159160
"Field is not accessible", ex);
160161
}

spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import java.lang.reflect.Constructor;
20+
import java.lang.reflect.InaccessibleObjectException;
2021
import java.lang.reflect.InvocationTargetException;
2122
import java.lang.reflect.Method;
2223
import java.util.function.Supplier;
@@ -167,7 +168,7 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
167168
"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
168169
"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
169170
}
170-
catch (IllegalAccessException ex) {
171+
catch (IllegalAccessException | InaccessibleObjectException ex) {
171172
throw new BeanInstantiationException(factoryMethod,
172173
"Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
173174
}

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.event;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Proxy;
@@ -366,8 +367,8 @@ protected void handleAsyncError(Throwable t) {
366367
return null;
367368
}
368369

369-
ReflectionUtils.makeAccessible(this.method);
370370
try {
371+
ReflectionUtils.makeAccessible(this.method);
371372
if (KotlinDetector.isSuspendingFunction(this.method)) {
372373
return CoroutinesUtils.invokeSuspendingFunction(this.method, bean, args);
373374
}
@@ -377,7 +378,7 @@ protected void handleAsyncError(Throwable t) {
377378
assertTargetBean(this.method, bean, args);
378379
throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
379380
}
380-
catch (IllegalAccessException ex) {
381+
catch (IllegalAccessException | InaccessibleObjectException ex) {
381382
throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
382383
}
383384
catch (InvocationTargetException ex) {

spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationReactiveSupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.scheduling.annotation;
1818

19+
import java.lang.reflect.InaccessibleObjectException;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.util.List;
@@ -173,7 +174,7 @@ static Publisher<?> getPublisherFor(Method method, Object bean) {
173174
"Cannot obtain a Publisher-convertible value from the @Scheduled reactive method",
174175
ex.getTargetException());
175176
}
176-
catch (IllegalAccessException ex) {
177+
catch (IllegalAccessException | InaccessibleObjectException ex) {
177178
throw new IllegalArgumentException(
178179
"Cannot obtain a Publisher-convertible value from the @Scheduled reactive method", ex);
179180
}

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.scripting.groovy;
1818

1919
import java.io.IOException;
20+
import java.lang.reflect.InaccessibleObjectException;
2021
import java.lang.reflect.InvocationTargetException;
2122

2223
import groovy.lang.GroovyClassLoader;
@@ -331,7 +332,7 @@ public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) {
331332
throw new ScriptCompilationException(
332333
scriptSource, "Unable to instantiate Groovy script class: " + scriptClass.getName(), ex);
333334
}
334-
catch (IllegalAccessException ex) {
335+
catch (IllegalAccessException | InaccessibleObjectException ex) {
335336
throw new ScriptCompilationException(
336337
scriptSource, "Could not access Groovy script constructor: " + scriptClass.getName(), ex);
337338
}

spring-context/src/main/java/org/springframework/scripting/support/StandardScriptFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.scripting.support;
1818

1919
import java.io.IOException;
20+
import java.lang.reflect.InaccessibleObjectException;
2021
import java.lang.reflect.InvocationTargetException;
2122

2223
import javax.script.Invocable;
@@ -167,7 +168,7 @@ public boolean requiresConfigInterface() {
167168
throw new ScriptCompilationException(
168169
scriptSource, "Unable to instantiate script class: " + scriptClass.getName(), ex);
169170
}
170-
catch (IllegalAccessException ex) {
171+
catch (IllegalAccessException | InaccessibleObjectException ex) {
171172
throw new ScriptCompilationException(
172173
scriptSource, "Could not access script constructor: " + scriptClass.getName(), ex);
173174
}

spring-core/src/main/java/org/springframework/util/AutoPopulatingList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.util;
1818

1919
import java.io.Serializable;
20+
import java.lang.reflect.InaccessibleObjectException;
2021
import java.lang.reflect.InvocationTargetException;
2122
import java.lang.reflect.Modifier;
2223
import java.util.ArrayList;
@@ -305,7 +306,7 @@ public E createElement(int index) {
305306
throw new ElementInstantiationException(
306307
"Unable to instantiate element class: " + this.elementClass.getName(), ex);
307308
}
308-
catch (IllegalAccessException ex) {
309+
catch (IllegalAccessException | InaccessibleObjectException ex) {
309310
throw new ElementInstantiationException(
310311
"Could not access element constructor: " + this.elementClass.getName(), ex);
311312
}

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,10 +1466,8 @@ private static Method getInterfaceMethodIfPossible(Method method, @Nullable Clas
14661466
}
14671467

14681468
/**
1469-
* Get the first publicly accessible method in the supplied method's type hierarchy that
1469+
* Get the highest publicly accessible method in the supplied method's type hierarchy that
14701470
* has a method signature equivalent to the supplied method, if possible.
1471-
* <p>If the supplied method is {@code public} and declared in a {@code public} type,
1472-
* the supplied method will be returned.
14731471
* <p>Otherwise, this method recursively searches the class hierarchy and implemented
14741472
* interfaces for an equivalent method that is {@code public} and declared in a
14751473
* {@code public} type.
@@ -1492,19 +1490,23 @@ private static Method getInterfaceMethodIfPossible(Method method, @Nullable Clas
14921490
* @see #getMostSpecificMethod(Method, Class)
14931491
*/
14941492
public static Method getPubliclyAccessibleMethodIfPossible(Method method, @Nullable Class<?> targetClass) {
1495-
Class<?> declaringClass = method.getDeclaringClass();
1496-
// If the method is not public, we can abort the search immediately; or if the method's
1497-
// declaring class is public, the method is already publicly accessible.
1498-
if (!Modifier.isPublic(method.getModifiers()) || Modifier.isPublic(declaringClass.getModifiers())) {
1493+
// If the method is not public, we can abort the search immediately.
1494+
if (!Modifier.isPublic(method.getModifiers())) {
14991495
return method;
15001496
}
15011497

15021498
Method interfaceMethod = getInterfaceMethodIfPossible(method, targetClass, true);
15031499
// If we found a method in a public interface, return the interface method.
1504-
if (!interfaceMethod.equals(method)) {
1500+
if (interfaceMethod != method) {
15051501
return interfaceMethod;
15061502
}
15071503

1504+
Class<?> declaringClass = method.getDeclaringClass();
1505+
// Bypass cache for java.lang.Object unless it is actually an overridable method declared there.
1506+
if (declaringClass.getSuperclass() == Object.class && !ReflectionUtils.isObjectMethod(method)) {
1507+
return method;
1508+
}
1509+
15081510
Method result = publiclyAccessibleMethodCache.computeIfAbsent(method,
15091511
key -> findPubliclyAccessibleMethodIfPossible(key.getName(), key.getParameterTypes(), declaringClass));
15101512
return (result != null ? result : method);
@@ -1513,19 +1515,19 @@ public static Method getPubliclyAccessibleMethodIfPossible(Method method, @Nulla
15131515
private static @Nullable Method findPubliclyAccessibleMethodIfPossible(
15141516
String methodName, Class<?>[] parameterTypes, Class<?> declaringClass) {
15151517

1518+
Method result = null;
15161519
Class<?> current = declaringClass.getSuperclass();
15171520
while (current != null) {
1518-
if (Modifier.isPublic(current.getModifiers())) {
1519-
try {
1520-
return current.getDeclaredMethod(methodName, parameterTypes);
1521-
}
1522-
catch (NoSuchMethodException ex) {
1523-
// ignore
1524-
}
1521+
Method method = getMethodOrNull(current, methodName, parameterTypes);
1522+
if (method == null) {
1523+
break;
15251524
}
1526-
current = current.getSuperclass();
1525+
if (Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
1526+
result = method;
1527+
}
1528+
current = method.getDeclaringClass().getSuperclass();
15271529
}
1528-
return null;
1530+
return result;
15291531
}
15301532

15311533
/**

0 commit comments

Comments
 (0)