Description
Ben Rowlands opened SPR-5644 and commented
We use a map to hold options that can be passed into certain operations to influence their behavior. The map is keyed by some enum. Since we don't know all possible options we can't have a single enum. So we instead follow a pattern where each "subsystem" provides its own enum class. As such the key type in our map is Enum<?>. We preferred this approach over static final strings as we'd hoped to offer some degree of type safety when loading an XML configuration since only strings representing valid enums and this options can be set. For example:
Map<Enum<?>, String> options = ...
options.put( FooOptions.FEATURE_X, "1");
options.put( BarOptions.FEATURE_Y, "2");
...
We hoped Spring would load the keys as enums and thus valid the options when defined in XML config. For example:
// config
<property name="options">
<map>
<entry key="example.FooOptions.FEATURE_X" value="1"/>
<entry key="example.BarOptions.FEATURE_Y" value="2"/>
</map>
</property>
// code
void setOptions( Map<Enum<?>, String> options) {..}
However out the box Spring only binds enums when it knows the target enum type. This certainly makes sense, however we think - especially for the map-enum-key case - adding support for this would be a useful extension to the enum binding support already available. In a sense, its a shortcut for a static-field-lookup when the target type is Enum<?>.
I've attached a test-case demonstrating this in more detail. It also shows a custom PropertyEditor workaround, however as mentioned we think support for this in the core framework would be generally useful.
Attachments:
- EnumBindingTest.java (5.37 kB)
Referenced from: commits 1480202
1 votes, 2 watchers