泛型类型上的java getMethod()
我想在泛型类型上使用反射 我有这门课泛型类型上的java getMethod(),java,generics,reflection,Java,Generics,Reflection,我想在泛型类型上使用反射 我有这门课 package it.ciro.service; import it.ciro.dao.SysMyAbDao; import org.apache.log4j.Logger; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList;
package it.ciro.service;
import it.ciro.dao.SysMyAbDao;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ciro on 09/12/2016.
*/
public class SelectOption<E extends Serializable> {
private SysMyAbDao dao;
private Class<E> entity;
private ArrayList<Class<E>> entityAll;
private Map<String,String> optionList = new HashMap<String,String>();
protected Logger logger;
public SelectOption(SysMyAbDao dao,Class<E> entity,String idName, String labelName ){
logger = Logger.getLogger(this.getClass());
this.dao = dao;
this.entity = entity;
entityAll = dao.findAll();
try{
Method idMethod = this.entity.getMethod(idName);
Method labelMethod = this.entity.getClass().getMethod(labelName);
for (Class<E> single : entityAll) {
optionList.put((String)idMethod.invoke(single),(String)labelMethod.invoke(single));
}
}catch (NoSuchMethodException ex){
ex.printStackTrace();
logger.error(ex.getMessage());
} catch (InvocationTargetException e) {
logger.error(e.getMessage());
} catch (IllegalAccessException e) {
logger.error(e.getMessage());
}
}
public Map<String, String> getOptionList() {
return optionList;
}
}
java搜索泛型类型e,而不是我在构造函数中使用的类型
我希望搜索我在controller中使用的类型。在GeoRegion类中存在该方法
我是SysMyAbDao
public abstract class SysMyAbDao<T, E, Id extends Serializable> {
protected String message;
protected Boolean status;
protected T t ;
protected Logger logger;
protected Long totalRow;
private Class<T> type;
public SysMyAbDao(Class<T> type){
this.type = type;
}
.....
公共抽象类SysMyAbDao{
受保护的字符串消息;
受保护的布尔状态;
保护T;
受保护的记录器;
受保护的长行;
私人阶级类型;
公共SysMyAbDao(类类型){
this.type=type;
}
.....
乔治亚班
public class GeoRegion implements java.io.Serializable {
private int idRegion;
private String name;
private String code;
private Set<GeoProvince> geoProvinces = new HashSet<GeoProvince>(0);
private Set<GeoCity> geoCities = new HashSet<GeoCity>(0);
public GeoRegion() {
}
public GeoRegion(int idRegion) {
this.idRegion = idRegion;
}
public GeoRegion(int idRegion, String name, String code, Set<GeoProvince> geoProvinces, Set<GeoCity> geoCities) {
this.idRegion = idRegion;
this.name = name;
this.code = code;
this.geoProvinces = geoProvinces;
this.geoCities = geoCities;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_region", unique=true, nullable=false)
public int getIdRegion() {
return this.idRegion;
}
public void setIdRegion(int idRegion) {
this.idRegion = idRegion;
}
@Column(name="name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="code", unique=true)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="geoRegion")
public Set<GeoProvince> getGeoProvinces() {
return this.geoProvinces;
}
public void setGeoProvinces(Set<GeoProvince> geoProvinces) {
this.geoProvinces = geoProvinces;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="geoRegion")
public Set<GeoCity> getGeoCities() {
return this.geoCities;
}
public void setGeoCities(Set<GeoCity> geoCities) {
this.geoCities = geoCities;
}
}
public类GeoRegion实现java.io.Serializable{
私人互联网区;
私有字符串名称;
私有字符串码;
专用集GeoProvisions=新哈希集(0);
私有集地理城市=新哈希集(0);
公共地理区(){
}
公共地理区域(国际地理区域){
this.idRegion=idRegion;
}
公共地理区域(int idRegion、字符串名称、字符串代码、设置地理省、设置地理城市){
this.idRegion=idRegion;
this.name=名称;
this.code=代码;
this.geoprovices=geoprovices;
这个。地理城市=地理城市;
}
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“id\u region”,unique=true,nullable=false)
public int getIdRegion(){
返回此.idRegion;
}
公共无效设置idRegion(int idRegion){
this.idRegion=idRegion;
}
@列(name=“name”)
公共字符串getName(){
返回此.name;
}
公共void集合名(字符串名){
this.name=名称;
}
@列(name=“code”,unique=true)
公共字符串getCode(){
返回此.code;
}
公共无效设置码(字符串码){
this.code=代码;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“geoRegion”)
公共集GetGeoProvisions(){
返回此项。地理省;
}
公共作废设置地理省(设置地理省){
this.geoprovices=geoprovices;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“geoRegion”)
公共集getGeoCities(){
返回此项。地理城市;
}
公共空间集合地理城市(集合地理城市){
这个。地理城市=地理城市;
}
}
您正试图在单个
对象上调用您的方法,该对象是类
对象
我在这段代码中没有看到任何GeoRegion
的实例。但要使其正常工作,您需要对其中一个实例使用此方法:
E instance = getSomeObjectFromSomewhere();
optionList.put((String)idMethod.invoke(instance),(String)labelMethod.invoke(instance));
这一行中有一个额外的
getClass()
:
Method labelMethod = this.entity.getClass().getMethod(labelName);
事实上,您正在Class
对象上调用getClass()
。由于Class
的类不是E
,而是java.lang.Class
您将得到发布的NoSuchMethodException
另外,您在其上调用方法的实例(single
,在您的例子中)应该是E
类型,而不是Class
类型
总的来说,你会得到如下结果:
public SelectOption(SysMyAbDao<E, ?, ? extends Serializable> dao,
Class<E> entityClass,
String idName,
String labelName) {
this.dao = dao;
this.entityClass = entityClass;
this.entityAll = dao.findAll(); // make sure your SysMyAbDao<E, ?, ?>#findAll() returns a Collection<E>
try{
Method idMethod = this.entityClass.getMethod(idName);
Method labelMethod = this.entityClass.getMethod(labelName);
for (E instance : entityAll) {
optionList.put((String)idMethod.invoke(instance),(String)labelMethod.invoke(instance));
}
} catch (NoSuchMethodException ex){
...
}
}
public SelectOption(SysMyAbDao-dao,
类实体类,
字符串idName,
字符串labelName){
this.dao=dao;
this.entityClass=entityClass;
this.entityAll=dao.findAll();//确保SysMyAbDao#findAll()返回一个集合
试一试{
方法idMethod=this.entityClass.getMethod(idName);
方法labelMethod=this.entityClass.getMethod(labelName);
例如(例如:entityAll){
optionList.put((String)idMethod.invoke(instance),(String)labelMethod.invoke(instance));
}
}catch(NoSuchMethodException-ex){
...
}
}
i类地理区域或其他类别之前我不知道您是否可以发布GeoRegion
Class的代码?@javaguy postednot work,因为SysMyAbDao是抽象和泛型类。请使用编辑问题code@ciro:那么您应该相应地修复dao
-参数的类型。我假设第一个泛型类型是这是由上面更改的代码中的findAll()
-方法返回的。请使用哪种方式?
Method labelMethod = this.entity.getClass().getMethod(labelName);
public SelectOption(SysMyAbDao<E, ?, ? extends Serializable> dao,
Class<E> entityClass,
String idName,
String labelName) {
this.dao = dao;
this.entityClass = entityClass;
this.entityAll = dao.findAll(); // make sure your SysMyAbDao<E, ?, ?>#findAll() returns a Collection<E>
try{
Method idMethod = this.entityClass.getMethod(idName);
Method labelMethod = this.entityClass.getMethod(labelName);
for (E instance : entityAll) {
optionList.put((String)idMethod.invoke(instance),(String)labelMethod.invoke(instance));
}
} catch (NoSuchMethodException ex){
...
}
}