我试图创建一个返回通用对象列表的方法,但我得到了ClassCastException。
HashMap<String, String> dados = new HashMap<>();
List<HashMap<String, String>> result = new ArrayList<>();
dados.put("sigla", "TST");
dados.put("nome", "Teste");
result.add(dados);
List<Posto> dadosProcessados = convertDados(Posto.class, result);
for (Posto posto : dadosProcessados) { //ClassCastException line 38
System.out.println(posto.getSigla() + " => " + posto.getNome());
}
这是完整的方法:
public static <T> List<T> convertDados(Class<T> entity, List<HashMap<String, String>> dados) throws NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
Field[] fields = entity.getDeclaredFields();
Method[] allSetterMethods = entity.getMethods();
Map<Integer, Method> setters = new HashMap<>();
Class<?>[] paramTypes = new Class<?>[fields.length -1];
List<T> result = new ArrayList<>();
int cont = 0;
//Pega todos os setter
for(Method method : allSetterMethods) {
if(method.getName().startsWith("set")) {
paramTypes[cont] = method.getParameterTypes()[0];
setters.put(cont, method);
cont++;
}
}
for (Field field : fields) {
for(Map.Entry<String, String> dado: dados.get(0).entrySet()) {
if(dado.getValue() != null && field.getName().equals(dado.getKey())) {
for(Map.Entry<Integer, Method> set : setters.entrySet()) {
if(set.getValue().getName().substring(3).equalsIgnoreCase(field.getName())) {
Method method = entity.getMethod(set.getValue().getName(), paramTypes[set.getKey()]);
method.invoke(entity.getConstructor().newInstance(), dado.getValue());
result.add((T) entity);
break;
}
}
}
}
}
错误堆栈跟踪
线程中的异常&#34; main&#34; java.lang.ClassCastException:java.lang.Class无法强制转换为beans.Teste.main中的data.bo.Posto(Teste.java:38)
答案 0 :(得分:0)
您正在为结果添加result.add((T) entity);
。实体是您传入的参数,它是一个类,而不是您想要的类型的实例。
也许您打算将entity.getConstructor().newInstance()
的结果添加到列表中?
答案 1 :(得分:0)
更改result.add((T)实体); with T obj = entity.getConstructor()。newInstance();
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
List<HashMap<String, String>> result = new ArrayList<>();
HashMap<String, String> dados = new HashMap<>();
dados.put("sigla", "TST");
dados.put("nome", "Teste");
result.add(dados);
List<Posto> dadosProcessados = convertDados(Posto.class, result);
for (Posto posto : dadosProcessados) {
System.out.println(posto.getSigla() + " => " + posto.getNome());
}
}
public static <T> List<T> convertDados(Class<T> entity, List<HashMap<String, String>> dados) throws NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
Field[] fields = entity.getDeclaredFields();
Method[] allSetterMethods = entity.getMethods();
Map<Integer, Method> setters = new HashMap<>();
Class<?>[] paramTypes = new Class<?>[fields.length];
List<T> result = new ArrayList<>();
int cont = 0;
for (Method method : allSetterMethods) {
if (method.getName().startsWith("set")) {
paramTypes[cont] = method.getParameterTypes()[0];
setters.put(cont, method);
cont++;
}
}
for (Field field : fields) {
for (Map.Entry<String, String> dado : dados.get(0).entrySet()) {
if (dado.getValue() != null && field.getName().equalsIgnoreCase(dado.getKey())) {
for (Map.Entry<Integer, Method> set : setters.entrySet()) {
if (set.getValue().getName().substring(3).equalsIgnoreCase(field.getName())) {
Method method = entity.getMethod(set.getValue().getName(), paramTypes[set.getKey()]);
T obj = entity.getConstructor().newInstance();
method.invoke(obj, dado.getValue());
result.add(obj);
break;
}
}
}
}
}
return result;
}
}