Consistent treatment of new Spring system properties

See gh-30606
See gh-30571
This commit is contained in:
Juergen Hoeller 2023-06-14 18:27:06 +02:00
parent 517a073f33
commit e344f3f869
2 changed files with 18 additions and 12 deletions

View File

@ -70,7 +70,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
/**
* Property name for a common context checkpoint: {@value}.
* @since 6.1
* @see #CHECKPOINT_ON_REFRESH
* @see #CHECKPOINT_ON_REFRESH_VALUE
* @see org.crac.Core#checkpointRestore()
*/
public static final String CHECKPOINT_PROPERTY_NAME = "spring.context.checkpoint";
@ -81,11 +81,11 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
* @see #CHECKPOINT_PROPERTY_NAME
* @see org.crac.Core#checkpointRestore()
*/
public static final String CHECKPOINT_ON_REFRESH = "onRefresh";
public static final String CHECKPOINT_ON_REFRESH_VALUE = "onRefresh";
private final static boolean checkpointRestoreOnRefresh = CHECKPOINT_ON_REFRESH.equalsIgnoreCase(
SpringProperties.getProperty(CHECKPOINT_PROPERTY_NAME));
private final static boolean checkpointOnRefresh =
CHECKPOINT_ON_REFRESH_VALUE.equalsIgnoreCase(SpringProperties.getProperty(CHECKPOINT_PROPERTY_NAME));
private final Log logger = LogFactory.getLog(getClass());
@ -169,7 +169,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
@Override
public void onRefresh() {
if (checkpointRestoreOnRefresh) {
if (checkpointOnRefresh) {
new CracDelegate().checkpointRestore();
}

View File

@ -36,7 +36,8 @@ import org.graalvm.nativeimage.hosted.Feature;
*/
class PreComputeFieldFeature implements Feature {
private static final boolean verbose = "verbose".equals(System.getProperty("spring.native.precompute.log"));
private static final boolean verbose =
"verbose".equalsIgnoreCase(System.getProperty("spring.native.precompute.log"));
private static final Pattern[] patterns = {
Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#inNativeImage")),
@ -48,14 +49,15 @@ class PreComputeFieldFeature implements Feature {
Pattern.compile(Pattern.quote("org.apache.commons.logging.LogAdapter") + "#.*Present")
};
private final ThrowawayClassLoader throwawayClassLoader = new ThrowawayClassLoader(PreComputeFieldFeature.class.getClassLoader());
private final ThrowawayClassLoader throwawayClassLoader = new ThrowawayClassLoader(getClass().getClassLoader());
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
access.registerSubtypeReachabilityHandler(this::iterateFields, Object.class);
}
/* This method is invoked for every type that is reachable. */
// This method is invoked for every type that is reachable.
private void iterateFields(DuringAnalysisAccess access, Class<?> subtype) {
try {
for (Field field : subtype.getDeclaredFields()) {
@ -71,12 +73,14 @@ class PreComputeFieldFeature implements Feature {
Object fieldValue = provideFieldValue(field);
access.registerFieldValueTransformer(field, (receiver, originalValue) -> fieldValue);
if (verbose) {
System.out.println("Field " + fieldIdentifier + " set to " + fieldValue + " at build time");
System.out.println(
"Field " + fieldIdentifier + " set to " + fieldValue + " at build time");
}
}
catch (Throwable ex) {
if (verbose) {
System.out.println("Field " + fieldIdentifier + " will be evaluated at runtime due to this error during build time evaluation: " + ex.getMessage());
System.out.println("Field " + fieldIdentifier + " will be evaluated at runtime " +
"due to this error during build time evaluation: " + ex);
}
}
}
@ -88,8 +92,10 @@ class PreComputeFieldFeature implements Feature {
}
}
/* This method is invoked when the field value is written to the image heap or the field is constant folded. */
private Object provideFieldValue(Field field) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
// This method is invoked when the field value is written to the image heap or the field is constant folded.
private Object provideFieldValue(Field field)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> throwawayClass = this.throwawayClassLoader.loadClass(field.getDeclaringClass().getName());
Field throwawayField = throwawayClass.getDeclaredField(field.getName());
throwawayField.setAccessible(true);