Skip to content

Commit 4277682

Browse files
committed
Catch InaccessibleObjectException next to IllegalAccessException
Closes gh-35190
1 parent ba6166a commit 4277682

File tree

10 files changed

+21
-13
lines changed

10 files changed

+21
-13
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.springframework.aop.framework.AopConfigException;
@@ -66,7 +67,7 @@ public final Object getAspectInstance() {
6667
throw new AopConfigException(
6768
"Unable to instantiate aspect class: " + this.aspectClass.getName(), ex);
6869
}
69-
catch (IllegalAccessException ex) {
70+
catch (IllegalAccessException | InaccessibleObjectException ex) {
7071
throw new AopConfigException(
7172
"Could not access aspect constructor: " + this.aspectClass.getName(), ex);
7273
}

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;
@@ -367,7 +368,7 @@ public static Object invokeJoinpointUsingReflection(@Nullable Object target, Met
367368
throw new AopInvocationException("AOP configuration seems to be invalid: tried calling method [" +
368369
method + "] on target [" + target + "]", ex);
369370
}
370-
catch (IllegalAccessException ex) {
371+
catch (IllegalAccessException | InaccessibleObjectException ex) {
371372
throw new AopInvocationException("Could not access method [" + method + "]", ex);
372373
}
373374
}

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

@@ -144,7 +145,7 @@ public Object getValue() throws Exception {
144145
ReflectionUtils.makeAccessible(this.field);
145146
return this.field.get(getWrappedInstance());
146147
}
147-
catch (IllegalAccessException ex) {
148+
catch (IllegalAccessException | InaccessibleObjectException ex) {
148149
throw new InvalidPropertyException(getWrappedClass(),
149150
this.field.getName(), "Field is not accessible", ex);
150151
}
@@ -156,7 +157,7 @@ public void setValue(@Nullable Object value) throws Exception {
156157
ReflectionUtils.makeAccessible(this.field);
157158
this.field.set(getWrappedInstance(), value);
158159
}
159-
catch (IllegalAccessException ex) {
160+
catch (IllegalAccessException | InaccessibleObjectException ex) {
160161
throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
161162
"Field is not accessible", ex);
162163
}

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;
@@ -184,7 +185,7 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
184185
"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
185186
"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
186187
}
187-
catch (IllegalAccessException ex) {
188+
catch (IllegalAccessException | InaccessibleObjectException ex) {
188189
throw new BeanInstantiationException(factoryMethod,
189190
"Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
190191
}

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;
@@ -374,8 +375,8 @@ protected Object doInvoke(@Nullable Object... args) {
374375
return null;
375376
}
376377

377-
ReflectionUtils.makeAccessible(this.method);
378378
try {
379+
ReflectionUtils.makeAccessible(this.method);
379380
if (KotlinDetector.isSuspendingFunction(this.method)) {
380381
return CoroutinesUtils.invokeSuspendingFunction(this.method, bean, args);
381382
}
@@ -385,7 +386,7 @@ protected Object doInvoke(@Nullable Object... args) {
385386
assertTargetBean(this.method, bean, args);
386387
throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
387388
}
388-
catch (IllegalAccessException ex) {
389+
catch (IllegalAccessException | InaccessibleObjectException ex) {
389390
throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
390391
}
391392
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;
@@ -341,7 +342,7 @@ protected Object executeScript(ScriptSource scriptSource, Class<?> scriptClass)
341342
throw new ScriptCompilationException(
342343
scriptSource, "Unable to instantiate Groovy script class: " + scriptClass.getName(), ex);
343344
}
344-
catch (IllegalAccessException ex) {
345+
catch (IllegalAccessException | InaccessibleObjectException ex) {
345346
throw new ScriptCompilationException(
346347
scriptSource, "Could not access Groovy script constructor: " + scriptClass.getName(), ex);
347348
}

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;
@@ -172,7 +173,7 @@ public Object getScriptedObject(ScriptSource scriptSource, @Nullable Class<?>...
172173
throw new ScriptCompilationException(
173174
scriptSource, "Unable to instantiate script class: " + scriptClass.getName(), ex);
174175
}
175-
catch (IllegalAccessException ex) {
176+
catch (IllegalAccessException | InaccessibleObjectException ex) {
176177
throw new ScriptCompilationException(
177178
scriptSource, "Could not access script constructor: " + scriptClass.getName(), ex);
178179
}

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-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.test.context.bean.override.convention;
1818

1919
import java.lang.reflect.Field;
20-
import java.lang.reflect.InvocationTargetException;
2120
import java.lang.reflect.Method;
2221
import java.util.Objects;
2322

@@ -58,7 +57,7 @@ protected Object createOverrideInstance(String beanName, @Nullable BeanDefinitio
5857
ReflectionUtils.makeAccessible(this.factoryMethod);
5958
return this.factoryMethod.invoke(null);
6059
}
61-
catch (IllegalAccessException | InvocationTargetException ex) {
60+
catch (Throwable ex) {
6261
throw new IllegalStateException(
6362
"Failed to invoke @TestBean factory method: " + this.factoryMethod, ex);
6463
}

0 commit comments

Comments
 (0)