I am new to Java Reflection and generics and I am trying to understand them. Here I came accros a problem. I can make Lists and maps of or and other stuff.. But can I make a map of Classes ?
I have for example this class
public class ClassA{}
public class ClassB extends ClassA{}
public class ClassC extends ClassA{}
and so on.. Now I want a map like this Map(); In this map I can put anyhing that extends from A. Thats good. But what if I want to store something like this:
ClassX extends ClassA...
//some stuff
Map<String, ?????> customMapForTest = new HashMap<>();
customMapForTest.add("customKey", ClassX.class);
So I will have something like a map of templates of certain classes I want to use dependant on my settings.
Is something like this possible? How is it called if I want to store this kind of .class files in map ?
To get more into the situation I have my own application //Don't worry, this is not a public project or something.. just for experimenting. I have a file with some characters like this : X7WE2C3AD4A2 Just some random stuff. Now I have a file reader that reads each character and based on it it searches the map I created, takes the class of that file and creates new instance of that class and adds some values based on type.
example:
customMapForTesting.get("X").getClass().getDeclaredConstructor(int.class,String.class).newInstance(numberVariable, stringVariable);
I store that new instances to a different map and use it in some other stuff. Problem is I need all possible objects that are extending my template and I don't want to store instances inside the templateMap...
Aucun commentaire:
Enregistrer un commentaire