Skip to content

Commit cf2429b

Browse files
committed
Remove support for deprecated Java SecurityManager (-> JDK 17 build compatibility)
Includes hard JDK 9+ API dependency in CGLIB ReflectUtils (Lookup.defineClass) and removal of OutputStream spy proxy usage (avoiding invalid Mockito proxy on JDK 17) Closes gh-26901
1 parent 0640da7 commit cf2429b

File tree

47 files changed

+147
-2078
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+147
-2078
lines changed

spring-beans/spring-beans.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ sourceSets {
2323
}
2424

2525
compileGroovy {
26-
options.compilerArgs += "-Werror"
26+
// Groovy generates Java code with "new Boolean" usage which is deprecated on JDK 17
27+
// options.compilerArgs += "-Werror"
2728
}
2829

2930
// This module also builds Kotlin code and the compileKotlin task naturally depends on

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

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,6 @@
1818

1919
import java.beans.PropertyDescriptor;
2020
import java.lang.reflect.Method;
21-
import java.security.AccessControlContext;
22-
import java.security.AccessController;
23-
import java.security.PrivilegedAction;
24-
import java.security.PrivilegedActionException;
25-
import java.security.PrivilegedExceptionAction;
2621

2722
import org.springframework.core.ResolvableType;
2823
import org.springframework.core.convert.Property;
@@ -69,12 +64,6 @@ public class BeanWrapperImpl extends AbstractNestablePropertyAccessor implements
6964
@Nullable
7065
private CachedIntrospectionResults cachedIntrospectionResults;
7166

72-
/**
73-
* The security context used for invoking the property methods.
74-
*/
75-
@Nullable
76-
private AccessControlContext acc;
77-
7867

7968
/**
8069
* Create a new empty BeanWrapperImpl. Wrapped instance needs to be set afterwards.
@@ -131,7 +120,6 @@ public BeanWrapperImpl(Object object, String nestedPath, Object rootObject) {
131120
*/
132121
private BeanWrapperImpl(Object object, String nestedPath, BeanWrapperImpl parent) {
133122
super(object, nestedPath, parent);
134-
setSecurityContext(parent.acc);
135123
}
136124

137125

@@ -176,23 +164,6 @@ private CachedIntrospectionResults getCachedIntrospectionResults() {
176164
return this.cachedIntrospectionResults;
177165
}
178166

179-
/**
180-
* Set the security context used during the invocation of the wrapped instance methods.
181-
* Can be null.
182-
*/
183-
public void setSecurityContext(@Nullable AccessControlContext acc) {
184-
this.acc = acc;
185-
}
186-
187-
/**
188-
* Return the security context used during the invocation of the wrapped instance methods.
189-
* Can be null.
190-
*/
191-
@Nullable
192-
public AccessControlContext getSecurityContext() {
193-
return this.acc;
194-
}
195-
196167

197168
/**
198169
* Convert the given value for the specified property to the latter's type.
@@ -290,47 +261,17 @@ public TypeDescriptor nested(int level) {
290261
@Nullable
291262
public Object getValue() throws Exception {
292263
Method readMethod = this.pd.getReadMethod();
293-
if (System.getSecurityManager() != null) {
294-
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
295-
ReflectionUtils.makeAccessible(readMethod);
296-
return null;
297-
});
298-
try {
299-
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
300-
() -> readMethod.invoke(getWrappedInstance(), (Object[]) null), acc);
301-
}
302-
catch (PrivilegedActionException pae) {
303-
throw pae.getException();
304-
}
305-
}
306-
else {
307-
ReflectionUtils.makeAccessible(readMethod);
308-
return readMethod.invoke(getWrappedInstance(), (Object[]) null);
309-
}
264+
ReflectionUtils.makeAccessible(readMethod);
265+
return readMethod.invoke(getWrappedInstance(), (Object[]) null);
310266
}
311267

312268
@Override
313269
public void setValue(@Nullable Object value) throws Exception {
314270
Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
315271
((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() :
316272
this.pd.getWriteMethod());
317-
if (System.getSecurityManager() != null) {
318-
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
319-
ReflectionUtils.makeAccessible(writeMethod);
320-
return null;
321-
});
322-
try {
323-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
324-
() -> writeMethod.invoke(getWrappedInstance(), value), acc);
325-
}
326-
catch (PrivilegedActionException ex) {
327-
throw ex.getException();
328-
}
329-
}
330-
else {
331-
ReflectionUtils.makeAccessible(writeMethod);
332-
writeMethod.invoke(getWrappedInstance(), value);
333-
}
273+
ReflectionUtils.makeAccessible(writeMethod);
274+
writeMethod.invoke(getWrappedInstance(), value);
334275
}
335276
}
336277

spring-beans/src/main/java/org/springframework/beans/factory/config/ConfigurableBeanFactory.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.beans.factory.config;
1818

1919
import java.beans.PropertyEditor;
20-
import java.security.AccessControlContext;
2120

2221
import org.springframework.beans.PropertyEditorRegistrar;
2322
import org.springframework.beans.PropertyEditorRegistry;
@@ -291,13 +290,6 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
291290
*/
292291
ApplicationStartup getApplicationStartup();
293292

294-
/**
295-
* Provides a security access control context relevant to this factory.
296-
* @return the applicable AccessControlContext (never {@code null})
297-
* @since 3.0
298-
*/
299-
AccessControlContext getAccessControlContext();
300-
301293
/**
302294
* Copy all relevant configuration from the given other factory.
303295
* <p>Should include all standard configuration settings as well as

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

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
import java.lang.reflect.InvocationTargetException;
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
24-
import java.security.AccessController;
25-
import java.security.PrivilegedAction;
26-
import java.security.PrivilegedActionException;
27-
import java.security.PrivilegedExceptionAction;
2824
import java.util.ArrayList;
2925
import java.util.Arrays;
3026
import java.util.Collection;
@@ -387,15 +383,7 @@ public Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyC
387383
return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance();
388384
}
389385
else {
390-
Object bean;
391-
if (System.getSecurityManager() != null) {
392-
bean = AccessController.doPrivileged(
393-
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(bd, null, this),
394-
getAccessControlContext());
395-
}
396-
else {
397-
bean = getInstantiationStrategy().instantiate(bd, null, this);
398-
}
386+
Object bean = getInstantiationStrategy().instantiate(bd, null, this);
399387
populateBean(beanClass.getName(), bd, new BeanWrapperImpl(bean));
400388
return bean;
401389
}
@@ -463,8 +451,7 @@ public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, St
463451

464452
@Override
465453
public void destroyBean(Object existingBean) {
466-
new DisposableBeanAdapter(
467-
existingBean, getBeanPostProcessorCache().destructionAware, getAccessControlContext()).destroy();
454+
new DisposableBeanAdapter(existingBean, getBeanPostProcessorCache().destructionAware).destroy();
468455
}
469456

470457

@@ -1316,15 +1303,7 @@ protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable
13161303
*/
13171304
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
13181305
try {
1319-
Object beanInstance;
1320-
if (System.getSecurityManager() != null) {
1321-
beanInstance = AccessController.doPrivileged(
1322-
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
1323-
getAccessControlContext());
1324-
}
1325-
else {
1326-
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
1327-
}
1306+
Object beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
13281307
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
13291308
initBeanWrapper(bw);
13301309
return bw;
@@ -1655,10 +1634,6 @@ protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrap
16551634
return;
16561635
}
16571636

1658-
if (System.getSecurityManager() != null && bw instanceof BeanWrapperImpl) {
1659-
((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
1660-
}
1661-
16621637
MutablePropertyValues mpvs = null;
16631638
List<PropertyValue> original;
16641639

@@ -1781,15 +1756,7 @@ private Object convertForProperty(
17811756
* @see #applyBeanPostProcessorsAfterInitialization
17821757
*/
17831758
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
1784-
if (System.getSecurityManager() != null) {
1785-
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
1786-
invokeAwareMethods(beanName, bean);
1787-
return null;
1788-
}, getAccessControlContext());
1789-
}
1790-
else {
1791-
invokeAwareMethods(beanName, bean);
1792-
}
1759+
invokeAwareMethods(beanName, bean);
17931760

17941761
Object wrappedBean = bean;
17951762
if (mbd == null || !mbd.isSynthetic()) {
@@ -1848,20 +1815,7 @@ protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBea
18481815
if (logger.isTraceEnabled()) {
18491816
logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
18501817
}
1851-
if (System.getSecurityManager() != null) {
1852-
try {
1853-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
1854-
((InitializingBean) bean).afterPropertiesSet();
1855-
return null;
1856-
}, getAccessControlContext());
1857-
}
1858-
catch (PrivilegedActionException pae) {
1859-
throw pae.getException();
1860-
}
1861-
}
1862-
else {
1863-
((InitializingBean) bean).afterPropertiesSet();
1864-
}
1818+
((InitializingBean) bean).afterPropertiesSet();
18651819
}
18661820

18671821
if (mbd != null && bean.getClass() != NullBean.class) {
@@ -1910,28 +1864,12 @@ protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefi
19101864
}
19111865
Method methodToInvoke = ClassUtils.getInterfaceMethodIfPossible(initMethod);
19121866

1913-
if (System.getSecurityManager() != null) {
1914-
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
1915-
ReflectionUtils.makeAccessible(methodToInvoke);
1916-
return null;
1917-
});
1918-
try {
1919-
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
1920-
() -> methodToInvoke.invoke(bean), getAccessControlContext());
1921-
}
1922-
catch (PrivilegedActionException pae) {
1923-
InvocationTargetException ex = (InvocationTargetException) pae.getException();
1924-
throw ex.getTargetException();
1925-
}
1867+
try {
1868+
ReflectionUtils.makeAccessible(methodToInvoke);
1869+
methodToInvoke.invoke(bean);
19261870
}
1927-
else {
1928-
try {
1929-
ReflectionUtils.makeAccessible(methodToInvoke);
1930-
methodToInvoke.invoke(bean);
1931-
}
1932-
catch (InvocationTargetException ex) {
1933-
throw ex.getTargetException();
1934-
}
1871+
catch (InvocationTargetException ex) {
1872+
throw ex.getTargetException();
19351873
}
19361874
}
19371875

0 commit comments

Comments
 (0)