Java 从ParameteredTypeImpl获取类的列表类型

Java 从ParameteredTypeImpl获取类的列表类型,java,generics,reflection,Java,Generics,Reflection,我有一个映射字段,我正在尝试实例化它,看起来像这样 Map<Long, List<MyObj>> 是参数化TypeImpl对象。如果我在debug中查看该对象的值,它看起来像java.util.List(MyObj路径) 如何“知道”对象是一个列表,以便我可以进行转换 更新 这是一个对象工厂,它将自动生成的域对象从WebService转换为DTO域对象。因此,下面的代码是通用的,因此它应该能够处理任何类型的参数。实例化应该如下所示: Map<Long, List&

我有一个映射字段,我正在尝试实例化它,看起来像这样

Map<Long, List<MyObj>>
是参数化TypeImpl对象。如果我在debug中查看该对象的值,它看起来像java.util.List(MyObj路径)

如何“知道”对象是一个列表,以便我可以进行转换

更新


这是一个对象工厂,它将自动生成的域对象从WebService转换为DTO域对象。因此,下面的代码是通用的,因此它应该能够处理任何类型的参数。

实例化应该如下所示:

Map<Long, List<MyObj>> newMap;

...

if(mapType.isInterface()) {
    newMap = new HashMap<Long, List<MyObj>>();
} else {
    try {
        newMap = (Map<Long, List<MyObj>>) mapType.newInstance();
    } catch(Exception e) {
        newMap = new HashMap<Long, List<MyObj>>();
    }
}
Map-newMap;
...
if(mapType.isInterface()){
newMap=newhashmap();
}否则{
试一试{
newMap=(Map)mapType.newInstance();
}捕获(例外e){
newMap=newhashmap();
}
}

我最终得到了以下代码

ParameterizedType targetMapParameterizedType = (ParameterizedType) targetMethodMap.get(targetMethodName).getGenericParameterTypes()[0];
Class<?> mapType = targetMethodMap.get(targetMethodName).getParameterTypes()[0];
if(mapType.isInterface()) {
    newMap = new HashMap<Object, Object>();
} else {
    try {
        newMap = (Map<Object, Object>) mapType.newInstance();
    } catch(Exception e) {
        newMap = new HashMap<Object, Object>();
    }
}
Class<?> targetKeyType = null;
Class<?> targetValueType = null;
try {
    targetKeyType = (Class<?>)targetMapParameterizedType.getActualTypeArguments()[0];
} catch (ClassCastException cce) {
    cce.printStackTrace();
}

if(targetMapParameterizedType.getActualTypeArguments()[1] instanceof ParameterizedType) {

ParameterizedType paramTypeImpl = (ParameterizedType) targetMapParameterizedType.getActualTypeArguments()[1];
Class<?> containerParam = (Class<?>) paramTypeImpl.getRawType();

if(containerParam.isInstance(new ArrayList<>()) && containerParam.isInterface()) {

    containerParam = new ArrayList<>().getClass();
    }
    targetValueType = containerParam;
} else {
    targetValueType = (Class<?>)targetMapParameterizedType.getActualTypeArguments()[1];
}
ParameteredType targetMapParameteredType=(ParameteredType)targetMethodMap.get(targetMethodName.getGenericParameterTypes()[0];
类mapType=targetMethodMap.get(targetMethodName).getParameterTypes()[0];
if(mapType.isInterface()){
newMap=newhashmap();
}否则{
试一试{
newMap=(Map)mapType.newInstance();
}捕获(例外e){
newMap=newhashmap();
}
}
类targetKeyType=null;
类targetValueType=null;
试一试{
targetKeyType=(类)targetMapParameterizedType.getActualTypeArguments()[0];
}捕获(ClassCastException cce){
cce.printStackTrace();
}
if(targetMapParameteredType.getActualTypeArguments()[1]参数化类型实例){
ParameteredType paramTypeImpl=(ParameteredType)TargetMapParameteredType.getActualTypeArguments()[1];
类containerParam=(类)paramTypeImpl.getRawType();
if(containerParam.isInstance(新ArrayList())和&containerParam.isInterface()){
containerParam=new ArrayList().getClass();
}
targetValueType=容器参数;
}否则{
targetValueType=(类)targetMapParameterizedType.getActualTypeArguments()[1];
}

我必须得到ParameteredTypeImple对象的原始类型。然后检查它是否与列表和接口相关,并将其用作类对象。

我不能这样做,因为这需要是泛型的。@lowlyone确定后,您应该将newMap的声明更改为
Map newMap
Map<Long, List<MyObj>> newMap;

...

if(mapType.isInterface()) {
    newMap = new HashMap<Long, List<MyObj>>();
} else {
    try {
        newMap = (Map<Long, List<MyObj>>) mapType.newInstance();
    } catch(Exception e) {
        newMap = new HashMap<Long, List<MyObj>>();
    }
}
ParameterizedType targetMapParameterizedType = (ParameterizedType) targetMethodMap.get(targetMethodName).getGenericParameterTypes()[0];
Class<?> mapType = targetMethodMap.get(targetMethodName).getParameterTypes()[0];
if(mapType.isInterface()) {
    newMap = new HashMap<Object, Object>();
} else {
    try {
        newMap = (Map<Object, Object>) mapType.newInstance();
    } catch(Exception e) {
        newMap = new HashMap<Object, Object>();
    }
}
Class<?> targetKeyType = null;
Class<?> targetValueType = null;
try {
    targetKeyType = (Class<?>)targetMapParameterizedType.getActualTypeArguments()[0];
} catch (ClassCastException cce) {
    cce.printStackTrace();
}

if(targetMapParameterizedType.getActualTypeArguments()[1] instanceof ParameterizedType) {

ParameterizedType paramTypeImpl = (ParameterizedType) targetMapParameterizedType.getActualTypeArguments()[1];
Class<?> containerParam = (Class<?>) paramTypeImpl.getRawType();

if(containerParam.isInstance(new ArrayList<>()) && containerParam.isInterface()) {

    containerParam = new ArrayList<>().getClass();
    }
    targetValueType = containerParam;
} else {
    targetValueType = (Class<?>)targetMapParameterizedType.getActualTypeArguments()[1];
}