Skip to content

Commit 806eb5c

Browse files
sdeleuzekenny5he
authored andcommitted
Disable and remove unsupported features from native images
This commit removes load time weaving, CGLIB and Objenesis support from native images. GraalDetector has been removed for now because of oracle/graal#2594. It should be reintroduced when this bug will be fixed with NativeImageDetector class name. Closes gh-25179
1 parent a48e4be commit 806eb5c

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*
3939
* @author Rod Johnson
4040
* @author Juergen Hoeller
41+
* @author Sebastien Deleuze
4142
* @since 12.03.2004
4243
* @see AdvisedSupport#setOptimize
4344
* @see AdvisedSupport#setProxyTargetClass
@@ -46,9 +47,18 @@
4647
@SuppressWarnings("serial")
4748
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
4849

50+
/**
51+
* Whether this environment lives within a native image.
52+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
53+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
54+
*/
55+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
56+
57+
4958
@Override
5059
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
51-
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
60+
if (!IN_NATIVE_IMAGE &&
61+
(config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config))) {
5262
Class<?> targetClass = config.getTargetClass();
5363
if (targetClass == null) {
5464
throw new AopConfigException("TargetSource cannot determine target class: " +

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,16 @@
123123
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
124124
implements AutowireCapableBeanFactory {
125125

126+
/**
127+
* Whether this environment lives within a native image.
128+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
129+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
130+
*/
131+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
132+
133+
126134
/** Strategy for creating bean instances. */
127-
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
135+
private InstantiationStrategy instantiationStrategy;
128136

129137
/** Resolver strategy for method parameter names. */
130138
@Nullable
@@ -176,6 +184,12 @@ public AbstractAutowireCapableBeanFactory() {
176184
ignoreDependencyInterface(BeanNameAware.class);
177185
ignoreDependencyInterface(BeanFactoryAware.class);
178186
ignoreDependencyInterface(BeanClassLoaderAware.class);
187+
if (IN_NATIVE_IMAGE) {
188+
this.instantiationStrategy = new SimpleInstantiationStrategy();
189+
}
190+
else {
191+
this.instantiationStrategy = new CglibSubclassingInstantiationStrategy();
192+
}
179193
}
180194

181195
/**

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
101101
private static final String IMPORT_REGISTRY_BEAN_NAME =
102102
ConfigurationClassPostProcessor.class.getName() + ".importRegistry";
103103

104+
/**
105+
* Whether this environment lives within a native image.
106+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
107+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
108+
*/
109+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
110+
104111

105112
private final Log logger = LogFactory.getLog(getClass());
106113

@@ -412,6 +419,9 @@ else if (logger.isInfoEnabled() && beanFactory.containsSingleton(beanName)) {
412419
// nothing to enhance -> return immediately
413420
return;
414421
}
422+
if (IN_NATIVE_IMAGE) {
423+
throw new BeanDefinitionStoreException("@Configuration classes need to be marked as proxyBeanMethods=false. Found: " + configBeanDefs.keySet());
424+
}
415425

416426
ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();
417427
for (Map.Entry<String, AbstractBeanDefinition> entry : configBeanDefs.entrySet()) {

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
161161
*/
162162
private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore");
163163

164+
/**
165+
* Whether this environment lives within a native image.
166+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
167+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
168+
*/
169+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
170+
164171

165172
static {
166173
// Eagerly load the ContextClosedEvent class to avoid weird classloader issues
@@ -681,7 +688,7 @@ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
681688
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
682689

683690
// Detect a LoadTimeWeaver and prepare for weaving, if found.
684-
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
691+
if (!IN_NATIVE_IMAGE && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
685692
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
686693
// Set a temporary ClassLoader for type matching.
687694
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));

spring-core/src/main/java/org/springframework/core/DefaultParameterNameDiscoverer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@
3939
*/
4040
public class DefaultParameterNameDiscoverer extends PrioritizedParameterNameDiscoverer {
4141

42+
/**
43+
* Whether this environment lives within a native image.
44+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
45+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
46+
*/
47+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
48+
4249
public DefaultParameterNameDiscoverer() {
43-
if (KotlinDetector.isKotlinReflectPresent() && !GraalDetector.inImageCode()) {
50+
if (KotlinDetector.isKotlinReflectPresent() && !IN_NATIVE_IMAGE) {
4451
addDiscoverer(new KotlinReflectionParameterNameDiscoverer());
4552
}
4653
addDiscoverer(new StandardReflectionParameterNameDiscoverer());

spring-core/src/main/java/org/springframework/core/GraalDetector.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

spring-core/src/main/java/org/springframework/core/SerializableTypeWrapper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ final class SerializableTypeWrapper {
5959
private static final Class<?>[] SUPPORTED_SERIALIZABLE_TYPES = {
6060
GenericArrayType.class, ParameterizedType.class, TypeVariable.class, WildcardType.class};
6161

62+
/**
63+
* Whether this environment lives within a native image.
64+
* Exposed as a private static field rather than in a {@code NativeImageDetector.inNativeImage()} static method due to https://github.com/oracle/graal/issues/2594.
65+
* @see <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>
66+
*/
67+
private static final boolean IN_NATIVE_IMAGE = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
68+
6269
static final ConcurrentReferenceHashMap<Type, Type> cache = new ConcurrentReferenceHashMap<>(256);
6370

6471

@@ -109,9 +116,9 @@ static Type forTypeProvider(TypeProvider provider) {
109116
// No serializable type wrapping necessary (e.g. for java.lang.Class)
110117
return providedType;
111118
}
112-
if (GraalDetector.inImageCode() || !Serializable.class.isAssignableFrom(Class.class)) {
119+
if (IN_NATIVE_IMAGE || !Serializable.class.isAssignableFrom(Class.class)) {
113120
// Let's skip any wrapping attempts if types are generally not serializable in
114-
// the current runtime environment (even java.lang.Class itself, e.g. on Graal)
121+
// the current runtime environment (even java.lang.Class itself, e.g. on GraalVM native images)
115122
return providedType;
116123
}
117124

0 commit comments

Comments
 (0)