Merge branch '6.0.x'

This commit is contained in:
Sébastien Deleuze
2023-05-26 19:15:03 +02:00
16 changed files with 107 additions and 166 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,11 +19,14 @@ package org.springframework.aot;
import org.springframework.core.NativeDetector;
import org.springframework.core.SpringProperties;
import static org.springframework.core.NativeDetector.Context;
/**
* Utility for determining if AOT-processed optimizations must be used rather
* than the regular runtime. Strictly for internal use within the framework.
*
* @author Stephane Nicoll
* @author Sebastien Deleuze
* @since 6.0
*/
public abstract class AotDetector {
@@ -36,6 +39,8 @@ public abstract class AotDetector {
*/
public static final String AOT_ENABLED = "spring.aot.enabled";
private static final boolean inNativeImage = NativeDetector.inNativeImage(Context.RUNTIME, Context.BUILD_TIME);
/**
* Determine whether AOT optimizations must be considered at runtime. This
* is mandatory in a native image but can be triggered on the JVM using
@@ -43,7 +48,7 @@ public abstract class AotDetector {
* @return whether AOT optimizations must be considered
*/
public static boolean useGeneratedArtifacts() {
return (NativeDetector.inNativeImage() || SpringProperties.getFlag(AOT_ENABLED));
return (inNativeImage || SpringProperties.getFlag(AOT_ENABLED));
}
}

View File

@@ -33,8 +33,9 @@ import org.graalvm.nativeimage.hosted.Feature;
class PreComputeFieldFeature implements Feature {
private static Pattern[] patterns = {
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#imageCode")),
Pattern.compile(Pattern.quote("org.springframework.cglib.core.AbstractClassGenerator#imageCode")),
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#inNativeImage")),
Pattern.compile(Pattern.quote("org.springframework.cglib.core.AbstractClassGenerator#inNativeImage")),
Pattern.compile(Pattern.quote("org.springframework.aot.AotDetector#inNativeImage")),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*Present"),
Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*PRESENT"),
Pattern.compile(Pattern.quote("reactor.") + ".*#.*Available"),

View File

@@ -43,8 +43,12 @@ abstract public class AbstractClassGenerator<T> implements ClassGenerator {
private static final boolean DEFAULT_USE_CACHE =
Boolean.parseBoolean(System.getProperty("cglib.useCache", "true"));
// See https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java
private static final boolean imageCode = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
private static final boolean inNativeImage;
static {
String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
inNativeImage = "buildtime".equals(imageCode) || "runtime".equals(imageCode);
}
private GeneratorStrategy strategy = DefaultGeneratorStrategy.INSTANCE;
@@ -354,7 +358,7 @@ abstract public class AbstractClassGenerator<T> implements ClassGenerator {
}
}
// SPRING PATCH BEGIN
if (imageCode) {
if (inNativeImage) {
throw new UnsupportedOperationException("CGLIB runtime enhancement not supported on native image. " +
"Make sure to include a pre-generated class on the classpath instead: " + getClassName());
}

View File

@@ -16,6 +16,8 @@
package org.springframework.core;
import org.springframework.lang.Nullable;
/**
* A common delegate for detecting a GraalVM native image environment.
*
@@ -25,12 +27,61 @@ package org.springframework.core;
public abstract class NativeDetector {
// See https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java
private static final boolean imageCode = (System.getProperty("org.graalvm.nativeimage.imagecode") != null);
@Nullable
private static final String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
private static final boolean inNativeImage = (imageCode != null);
/**
* Returns {@code true} if invoked in the context of image building or during image runtime, else {@code false}.
* Returns {@code true} if running in a native image context (for example {@code buildtime}, {@code runtime} or
* {@code agent}) expressed by setting {@code org.graalvm.nativeimage.imagecode} system property to any value, else {@code false}.
*/
public static boolean inNativeImage() {
return imageCode;
return inNativeImage;
}
/**
* Returns {@code true} if running in any of the specified native image context(s), else {@code false}.
* @param contexts the native image context(s)
* @since 6.0.10
*/
public static boolean inNativeImage(Context... contexts) {
for (Context context: contexts) {
if (context.key.equals(imageCode)) {
return true;
}
}
return false;
}
/**
* Native image context as defined in
* <a href="https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java">ImageInfo.java</a>.
*
* @since 6.0.10
*/
public enum Context {
/**
* The code is executing in the context of image building.
*/
BUILD_TIME("buildtime"),
/**
* The code is executing at image runtime.
*/
RUNTIME("runtime");
private final String key;
Context(final String key) {
this.key = key;
}
@Override
public String toString() {
return this.key;
}
}
}