Avoid need for reflection hints for MBeanExporter in native image

Prior to this commit, MBeanExporter used
org.springframework.core.Constants which used reflection to find
constant fields in the MBeanExporter class. Consequently, one had to
register reflection hints in order to use MBeanExporter in a GraalVM
native image.

This commit addresses this by replacing the use of the `Constants`
class with a simple java.util.Map which maps constant names to constant
values for the autodetect constants defined in MBeanExporter.

See gh-30851
Closes gh-30846
This commit is contained in:
Sam Brannen
2023-07-10 19:00:40 +02:00
parent 676daa990b
commit 679b668bbb
2 changed files with 78 additions and 30 deletions

View File

@@ -53,7 +53,6 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Constants;
import org.springframework.jmx.export.assembler.AutodetectCapableMBeanInfoAssembler;
import org.springframework.jmx.export.assembler.MBeanInfoAssembler;
import org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler;
@@ -135,13 +134,19 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
/** Constant for the JMX {@code mr_type} "ObjectReference". */
private static final String MR_TYPE_OBJECT_REFERENCE = "ObjectReference";
/** Prefix for the autodetect constants defined in this class. */
private static final String CONSTANT_PREFIX_AUTODETECT = "AUTODETECT_";
/**
* Map of constant names to constant values for the autodetect constants defined
* in this class.
* @since 6.0.11
*/
private static final Map<String, Integer> constants = Map.of(
"AUTODETECT_NONE", AUTODETECT_NONE,
"AUTODETECT_MBEAN", AUTODETECT_MBEAN,
"AUTODETECT_ASSEMBLER", AUTODETECT_ASSEMBLER,
"AUTODETECT_ALL", AUTODETECT_ALL
);
/** Constants instance for this class. */
private static final Constants constants = new Constants(MBeanExporter.class);
/** The beans to be exposed as JMX managed resources, with JMX names as keys. */
@Nullable
private Map<String, Object> beans;
@@ -235,10 +240,10 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* @see #AUTODETECT_NONE
*/
public void setAutodetectModeName(String constantName) {
if (!constantName.startsWith(CONSTANT_PREFIX_AUTODETECT)) {
throw new IllegalArgumentException("Only autodetect constants allowed");
}
this.autodetectMode = (Integer) constants.asNumber(constantName);
Assert.hasText(constantName, "'constantName' must not be null or blank");
Integer mode = constants.get(constantName);
Assert.notNull(mode, "Only autodetect constants allowed");
this.autodetectMode = mode;
}
/**
@@ -253,9 +258,8 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo
* @see #AUTODETECT_NONE
*/
public void setAutodetectMode(int autodetectMode) {
if (!constants.getValues(CONSTANT_PREFIX_AUTODETECT).contains(autodetectMode)) {
throw new IllegalArgumentException("Only values of autodetect constants allowed");
}
Assert.isTrue(constants.containsValue(autodetectMode),
"Only values of autodetect constants allowed");
this.autodetectMode = autodetectMode;
}