获取准确泛型的代码

/** 泛型构造类 */

public class TypeReference implements ParameterizedType {

private Type rawType;

private Type [] actualTypeArguments;

public TypeRefrence(Type rawType, Type [] actualTypeArguments){

  this.rawType = rawType;

  this.actualTypeArguments = actualTypeArguments;

}

public Type[] getActualTypeArguments() {

  return actualTypeArguments;

}

public Type getRawType() {

   return rawType;

}

public Type getOwnerType() {

   return null;

}

}

 

 

 

if (!type.isPrimitive()) {

  //set generic type if field not primitive type

  java.lang.reflect.Type rawType = (java.lang.reflect.Type) value.getClass();

  java.lang.reflect.Type genericType = field.getGenericType();

  try {

    ParameterizedType parameterizedType = (ParameterizedType) genericType;

    com.xxx.xxx.xxxxx.core.TypeRefrence typeRefrence = new TypeRefrence(rawType, parameterizedType.getActualTypeArguments());

    //构造泛型类型

    switchBean.setGeneric(typeRefrence);

  } catch (ClassCastException e) {

    switchBean.setGeneric(genericType);

  }

}

posted @ 2017-05-03 17:21  李高峰  阅读(478)  评论(0编辑  收藏  举报