Reputation: 2721
Problem:
I am trying to find the java type of a map's key and value, but without having to iterate over the map and then using instanceof
, and want to know it even if the map is empty.
The context:
CKEditorConfig
which can be downloaded from here (http://ckeditor.com/download) is implemented using a map. But you cannot set this map using injection because setter method is not provided, and there's no constructor provided to set it using constructor injection. However, there are five addConfigValue method (see below), which I can use to add those values.
public void addConfigValue(final String key, final Number value);
public void addConfigValue(final String key, final String value);
public void addConfigValue(final String key, final Boolean value);
public void addConfigValue(final String key, final Map<String, ? extends Object> value);
public void addConfigValue(final String key, final List<? extends Object> value);
I will be using ckeditor in a couple of places in my j2ee/java web app, and so I figured I should create a factory class that returns the settings needed to initialize the editor by some name. But I wanted to externalize these config and set them in the spring context file. so since I cannot create the config object I have to use regular maps, and then build the config object using the map. but I need to validate those maps coming from spring before building the object.
This is my method for building the config object:
@SuppressWarnings("unchecked")
public CKEditorConfig buildConfigFromMap(Map<String, Object> configMap) {
CKEditorConfig config = new CKEditorConfig();
for (String key : configMap.keySet()) {
Object val = configMap.get(key);
if (val instanceof Number)
config.addConfigValue(key, (Number) val);
else if (val instanceof String)
config.addConfigValue(key, (String) val);
else if (val instanceof Boolean)
config.addConfigValue(key, (Boolean) val);
else if (val instanceof List) {
config.addConfigValue(key, (List<Object>) val);
} else if (val instanceof Map) {
// TODO
}
throw new IllegalArgumentException("invalid map value types");
}
return config;
}
And as you can see the if the object is a map, I need to be able to validate if its key is a String. I know I can do something like this:
} else if (val instanceof Map) {
for (Object mapKey : ((Map<Object, Object>) val).keySet()) {
if (!(mapKey instanceof String))
throw new IllegalArgumentException("invalid map value types");
}
config.addConfigValue(key, (Map<String, Object>) val);
}
but the whole exercise makes me think I am not doing the right thing? any suggestions? I am open to using a totally different approach. I appreciate your input.
Upvotes: 0
Views: 1696
Reputation: 9545
It's impossible, Java generics are erased at runtime. In fact they only exist in the compiler (and the source code).
You cannot inspect the actual types of keys and values and deduce the generics used for the Map from that.
You can only infer generics in source code for Maps under your control.
It seems to me you want to force Spring to only supply Map<String, ?>. Here is a workaround :
public class StringKeyMapEntry { public String key; public Object value; }
inject List<StringKeyMapEntry> or StringKeyMapEntry[] (not sure if Spring can do arrays) and convert it to Map<String, ?>
Upvotes: 5
Reputation: 47290
You have a number of settings that are referenced by a string ... I would use one method and change the signature to this.
public void configure(final Map<String, ? extends Object> map);
And then do what you want with each setting, you could call each setting directly or loop through it.
Upvotes: 0