泛型 - 从Class <t> </t>获取值

时间:2014-07-23 18:49:05

标签: java generics jpa reflection

我有一个将我的jpa实体转换为TO的类,反之亦然。

当我在方法convertEntityListInTOList中进行转换时,返回的ListList<Class<T>>,我需要List<T>

是否可以迭代此集合(List<Class<T>>)并获得“TO”值?

转换器

    public class MyConverter<E, T> implements Serializable {

        private Class<E> myEntity;
        private Class<T> myTO;

        public MyConverter(Class<E> myEntity, Class<T> myTO) {
            this.myEntity = myEntity;
            this.myTO = myTO;
        }

        public List<T> convertEntityListInTOList(List<E> entityList) {
            List<T> listTO = new ArrayList<T>();
            for(E obj : entityList) {
                myTO = convertEntityInTO(obj);
                listTO.add(myTO);
            }               
            return listTO;
        }

        public List<E> convertTOListInEntityList(List<T> listTOs) {
            List<E> entityList = new ArrayList<E>();
            for(T to : listTOs) {
                myEntity = convertTOInEntity(to);
                entityList.add(myEntity);
            }           
            return entityList;
        }

        public T convertEntityInTO(Object myEntity) {
            T myTO = createInstanceTO();
            if(myEntity != null) {
                try {
                    BeanUtils.copyProperties(myTO, myEntity);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return myTO;
        }

        public E convertTOInEntity(T myTO) {
            E myEntity = createInstanceEntity();
            if(myTO != null) {
                try {
                    BeanUtils.copyProperties(myEntity, myTO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }    
            return myEntity;    
        }

/**
     * 
     * @return
     */
    public T createInstanceTO() {
        try {
            return getMyTO().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @return
     */
    public E createInstanceEntity() {
        try {
            return getMyEntity().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}

转换器制作人

@Produces
public MyConverter create(InjectionPoint ip) {
    ParameterizedType type = (ParameterizedType) ip.getType();
    Class myEntity = (Class) type.getActualTypeArguments()[0];
    Class myTO = (Class) type.getActualTypeArguments()[1];
    return new MyConverter(myEntity, myTO);
}

1 个答案:

答案 0 :(得分:1)

您的代码存在很多问题。您正在混合对象(例如,类型为MyEntity的对象)和表示这些对象类的对象(例如Class<MyEntity>)。

所以,当你写

Class myEntity = (Class) type.getActualTypeArguments()[0];
    Class myTO = (Class) type.getActualTypeArguments()[1];
    return new MyConverter(myEntity, myTO);

您正在尝试创建一个将Class转换为另一个Class的转换器,这是没有意义的。这就是convertEntityListInTOList的返回类型是类列表的原因。

创建包含每个实体的一个实例的转换器也不会产生敏感。您应该每次创建一个新实例,或者将作为参数给出的实例填充到convert方法中。

相关问题