Use a Class for a generic type?

is there a way to for example do
String clazz = "java.lang.String";
List<Class.forName(clazz)> list = new List<>();
String clazz = "java.lang.String";
List<Class.forName(clazz)> list = new List<>();
In case this is xy problem: I have a class called DataObject for storing some extra data on things, and it has a generic type for what type of data it's storing. There's another class called DataMap that takes its own generic type and extends DataObject with a HashMap<DataObject<T>, DataObject<?>>. I'm then serializing it to json with gson as my method of storing it in a file, but I need a custom deserializer for it, and I'm getting errors for it not being able to convert HashMap<DataObject<?>, DataObject<?>> to HashMap<DataObject<T>, DataObject<T>>. Storing the class name in the json is my current idea for a workaround.
11 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
tjoener
tjoener7mo ago
Yeah, using something like this is usually a bad idea What are you trying to store?
blockgoblin31
blockgoblin31OP7mo ago
Not exactly sure yet, probably mostly text to then get stuff from registries, plus some numbers
tjoener
tjoener7mo ago
yeah, but like, what structure? This has a graph feeling to it? What's with the DataObject? What's it's goal etc
blockgoblin31
blockgoblin31OP7mo ago
its just a container for data of whatever type, supposed to be a system to json. Specifically I want to be able to store a couple different random options(for example a random monster and random loot) so basically to save rng dependant options in a file
tjoener
tjoener7mo ago
ok yeah, your only option in this case is to store the type in the json, or in the application, and do a hard cast from Map<?,?> to the one you need Although I'm not even sure that would work In cases like this it's usually just easier to read a specific type from a file than to come up with some magic generic incantation
blockgoblin31
blockgoblin31OP7mo ago
ok, I'll scrap this whole system and try to come up with something else, maybe just accept that keys have to be Strings and use json instead of making my own system
tjoener
tjoener7mo ago
yeah, that seems like a good idea I mean, you can get a Map<String, Monster> back if you want That's very easily doable With TypeReference I believe in Gson
blockgoblin31
blockgoblin31OP7mo ago
ok ty
tjoener
tjoener7mo ago
TypeToken Try the simplest solution first, and if the bit of repetition still bothers you, then look for a fancier way to do it 🙂
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?