2016-12-09 9 views
0

私は、このクラスにのjava getMethod()メソッド

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; 
    } 
} 

、私のコントローラで

SelectOption<GeoProvince> selectOption = new SelectOption(geoRegionDao,GeoRegion.class,"idGeoRegion","name"); 

を持っていますが、私は

java.lang.NoSuchMethodException: java.lang.Class.idGeoRegion() 
を取得ジェネリック型

での反射を利用したいです

Java検索on一般tyコンストラクタで使用しているタイプではありません

私がコントローラで費やしたタイプについての検索が行われると思います。 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; 
    } 
    ..... 

GeoRegionクラス

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; 
    } 
} 
+0

全体的に、あなたのようなもので終わるのでしょうか? – developer

+0

@javaguy投稿 – ciro

答えて

0

であるあなたはClass目的である、singleにあなたの方法を呼び出そうとしています。

このコードにはGeoRegionのインスタンスはありません。しかし、これが動作するためには、あなたがそれらのいずれかで、このメソッドを使用する必要があります。

E instance = getSomeObjectFromSomewhere(); 
optionList.put((String)idMethod.invoke(instance),(String)labelMethod.invoke(instance)); 
+0

私は知らないクラスi GeoRegionまたは他のクラスの場合は、先に – ciro

+0

うん、あなたは正しい。それに応じて答えを更新。 –

2

あなたは、この行で余分なgetClass()を持っています。実際には

Method labelMethod = this.entity.getClass().getMethod(labelName); 

を、あなたは上のgetClass()を呼び出していますClass<E>オブジェクトです。 Class<E>のクラスはEではありませんが、java.lang.Classとなっていますので、投稿したのはNoSuchMethodExceptionです。

また、あなたのメソッド(あなたの場合はsingle)でメソッドを呼び出すインスタンスは、タイプで、タイプはClass<E>でなければなりません。あなたは `GeoRegion`クラスのコードを投稿することができ

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){ 
     ... 
    } 
} 
+0

は動作しません。SysMyAbDaoは抽象クラスであり汎用クラスです。 – ciro

+0

@ciro:あなたの 'dao'パラメータのタイプをそれに応じて修正する必要があります。最初のジェネリック型は、上記の変更されたコードの 'findAll()'メソッドによって返されるものと仮定しました。 –

+0

どのようにしてください? – ciro

関連する問題