Converting from raw class type to generic wildcard type

I'm doing this (fetch data for a static registry):
private static <T extends BreweryKeyed> Map<BreweryKey, T> getFields(Class<T> tClass) {
try {
ImmutableMap.Builder<BreweryKey, T> outputBuilder = ImmutableMap.builder();
for (Field field : tClass.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
continue;
}
Object staticField = field.get(null);
if (tClass.isInstance(staticField)) {
T tInstance = tClass.cast(staticField);
outputBuilder.put(tInstance.key(), tInstance);
}
}
return outputBuilder.build();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static <T extends BreweryKeyed> Map<BreweryKey, T> getFields(Class<T> tClass) {
try {
ImmutableMap.Builder<BreweryKey, T> outputBuilder = ImmutableMap.builder();
for (Field field : tClass.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
continue;
}
Object staticField = field.get(null);
if (tClass.isInstance(staticField)) {
T tInstance = tClass.cast(staticField);
outputBuilder.put(tInstance.key(), tInstance);
}
}
return outputBuilder.build();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
The problem is that I don't know how to set a non raw type to tClass, for example this is not possible (compile issues):
public static final Map<BreweryKey, StructureMeta<?>> STRUCTURE_META = (Map<BreweryKey, StructureMeta<?>>)getFields(StructureMeta.class);
public static final Map<BreweryKey, StructureMeta<?>> STRUCTURE_META = (Map<BreweryKey, StructureMeta<?>>)getFields(StructureMeta.class);
Any ideas?
3 Replies
JavaBot
JavaBot6d ago
This post has been reserved for your question.
Hey @Thorinwasher! 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 marked as dormant 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.
Thorinwasher
ThorinwasherOP6d ago
Okay, doing this with a cast works, why is it necessary to do initially though? (I added a wrapper class around the map)
private static <T extends BreweryKeyed> Registry<? extends T> fromFields(Class<T> tClass) {
try {
List<T> tList = new ArrayList<>();
for (Field field : tClass.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
continue;
}
Object staticField = field.get(null);
if (tClass.isInstance(staticField)) {
tList.add(tClass.cast(staticField));
}
}
return new Registry<>(tList);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static <T extends BreweryKeyed> Registry<? extends T> fromFields(Class<T> tClass) {
try {
List<T> tList = new ArrayList<>();
for (Field field : tClass.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
continue;
}
Object staticField = field.get(null);
if (tClass.isInstance(staticField)) {
tList.add(tClass.cast(staticField));
}
}
return new Registry<>(tList);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Like do the <? extends T> instead of just <T>
JavaBot
JavaBot5d 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?