2

i want use reflection on generic type

i have this class

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

and in my controller

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

but i get

java.lang.NoSuchMethodException: java.lang.Class.idGeoRegion()

java search on generic type e not on type that I use in constructor

I expect the search to be made about the type I spend in controller. In GeoRegion class the method exists.

this is 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 class

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;
    }
}
2
  • Can you post the code for GeoRegion class ? Commented Dec 9, 2016 at 22:16
  • @javaguy posted Commented Dec 11, 2016 at 20:24

2 Answers 2

2

You have an extra getClass() in this line:

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

In fact, you are calling getClass() on the Class<E> object. And as the class of Class<E> is not E but java.lang.Class you get the NoSuchMethodException you posted.

Also the instance which you are invoking your method on (single in your case), should be of type E and not of type Class<E>.

Overall you would end up with something like:

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){
        ...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

not work beacuse SysMyAbDao is abstract and generic class. edit question with code
@ciro: Then you should fix the type of your dao-parameter accordingly. I assumed that the first generic type is the one that is returned by the findAll()-method in the changed code above.
in which way please?
0

You are trying to invoke your method on single, which is a Class object.

I don't see any instances of GeoRegion in this code. But in order for this to work, you need to use this method on one of them:

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

1 Comment

i don't know At prior if Class i GeoRegion or other clasd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.