Is there a way to extract the T Type from a Java HashMap? We have different TranslationModels that all need to be prepared before they can be used. So therefore I created this generic Class which is supposed to handle all the different TranslationModels. The generic Type T holds the different TranslationModels.
package ch.lepa.app.zep.util;
import java.util.HashMap;
import java.util.List;
import ch.lepa.app.zep.bean.PersistenceHelperBean;
import ch.lepa.app.zep.model.LanguageModel;
public class LanguageSetup<T> {
private HashMap<String, T> processedTranslations;
private Class<T> type;
public LanguageSetup(HashMap<String, T> unprocessedTranslations) throws Exception {
// Extract the generic T class from the HashMap to set the Type
type = //Extracted T class;
processedTranslations = setupTranslationModel(unprocessedTranslations);
}
public HashMap<String, T> setupTranslationModel(HashMap<String, T> translation) throws Exception {
PersistenceHelperBean phb = new PersistenceHelperBean();
List<LanguageModel> languages = phb.getLanguages();
for (LanguageModel language : languages) {
if (!translation.containsKey(language.getCode())) {
translation.put(language.getCode(), type.newInstance());
}
}
return translation;
}
public HashMap<String, T> getProcessedTranslations() {
return processedTranslations;
}
public void setProcessedTranslations(HashMap<String, T> processedTranslations) {
this.processedTranslations = processedTranslations;
}
}
>`.