I want to store properties in a Map
by their class, i.e. the map should contain only one object of any runtime type. There already is a current implementation (it is an existing project), and the mechanism is implemented with a Map<String, Object>
.
Current implementation:
import com.foo.IConfiguration;
public class ClientCode {
private Options options = new Options();
public void putMethod() {
options.subOptions(new IConfiguration() {
public void configure() {}
});
}
public Object getMethod() {
return options.getSubOption(IConfiguration.class);
}
}
public class Options {
private Map<String, Object> map = new HashMap<>();
public void subOptions(Object subOptionsValue) {
if (subOptionsValue != null) {
map.put(subOptionsValue.getClass().getName(), subOptionsValue);
}
}
public Object getSubOption(Class<?> subOptionsClass) {
return subOptionsClass == null ? null : map.get(subOptionsClass.getName());
}
}
However, this does not work as I want to for anonymous classes. In this case, getName()
and related methods will put Options$1
or something similar, while I need it to put com.foo.IConfiguration
so that I can use it reliably in my getMethod().
I have looked through the API for java.lang.Class
, but I can't find anything that will point me back to the definition of com.foo.IConfiguration
. Well, I can, but it's not foolproof: clazz.getInterfaces()
gives an array of interfaces, which is good here, but I can also create a new Object() {}
or a local class, or a regular public class.
So is there a decent way to retrieve unambiguously the FQDN of the interface or superclass of an anonymous class ?
Aucun commentaire:
Enregistrer un commentaire