I have some requirements for the heterogeneous generic implementation for different types of Java objects. I can see warnings related to unchecked conversion. Any thought on the below implementation to avoid any runtime exception. Please find the below sample code.
class GenericData<T> {
private final Class<T> type;
private final static Map<Class<?>, GenericData> map = new HashMap<>(); //Warning :: GenericData is a raw type. References to generic type GenericData<T> should be parameterized
public final static <T> GenericData<T> of(Class<T> type) {
return map.putIfAbsent(type, new GenericData<T>(type)); // Warning :: Type safety: The expression of type GenericData needs unchecked conversion to conform to GenericData<T>
}
private GenericData(Class<T> type) {
this.type = type;
}
public final T getDataAfterDefaultValueApplied(T Data, T Body) {
Map<String, Object> defaultMap = getDataMap(Data);
Map<String, Object> dataMap = getDataMap(Body);
defaultMap.putAll(dataMap);
final ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(defaultMap, type);
}
private Map<String, Object> getDataMap(T data) {
// Demo implementation, ignore this one
return (Map<String, Object>) data;
}
}