samedi 8 août 2015

Using reflection to get a specific attribute from a extended instance

I would like to make a generic method to get a List from the parameter object.

The problem is because I have a declared object with a instance of the other class that extends the declared class. I don't want to use the instanceof solution because the number of classes that extends LimitedValue can be big.

I thought to use reflection for a solution, but I don't know how to use that with an instance of object, in this part of the code:

    Class cls = Class.forName(limitedValue.getClass().getName());
    Object obj = cls.newInstance();
    //This is wrong, I don't want a new instance.

    Method[] methods = cls.getDeclaredMethods();
    for(int x= 0; x < methods.length; x++) {
        Method method = methods[x];
        if ("java.util.List".equals(method.getReturnType().getName())) {
            //How to get the value of this method from limitedValue instance ?
        }
    }

This is my full code:

public class CalculatorLimitedValue {

    public static void main(String[] args) throws Exception {
        StoreItem storeItem = new StoreItem(1L, "Name of StoreItem", 50L);
        List listOfStoreItems = new ArrayList();
        listOfStoreItems.add(storeItem);
        LimitedValue limitedValue0 = new Store(listOfStoreItems);

        List firstList = calculator(limitedValue0);
        //do something with the list

        SupermarketItem supermarketItem = new SupermarketItem(1L, "Name of SupermarketItem", 21L);
        List listOfSupermarketItems = new ArrayList();
        listOfSupermarketItems.add(supermarketItem);
        LimitedValue limitedValue1 = new Supermarket(listOfSupermarketItems);

        List secondList = calculator(limitedValue1);
        //do something with the list
    }

    /** This is the method that I'd like to make generic to return a List */
    private static List calculator(LimitedValue limitedValue) throws Exception{

        Class cls = Class.forName(limitedValue.getClass().getName());
        Object obj = cls.newInstance();
        //This is wrong, I don't want a new instance.

        Method[] methods = cls.getDeclaredMethods();
        for(int x= 0; x < methods.length; x++) {
            Method method = methods[x];
            if ("java.util.List".equals(method.getReturnType().getName())) {
                //How to get the value of this method from limitedValue instance ?
            }
        }

        /* I don't want to use this one way, because my classes that extends LimitedValue 
        can be big. I would like to made a generic way to get de list of classes. */
        if (limitedValue instanceof Store) {
            System.out.println("This is a store");
            return ((Store) limitedValue).getStoreItems();
        } else if (limitedValue instanceof Supermarket) {
            System.out.println("This is a supermarket");
            return ((Supermarket) limitedValue).getSupermarketItems();
        }
        return null;
    }
}

If it help, these are my other classes:

LimitedValue.class

public class LimitedValue { }

StoreItem.class

public class StoreItem {
    private Long id;
    private String nameOfStoreItem;
    private Long valueOfStoreItem;

    public StoreItem(Long id, String nameOfStoreItem, Long valueOfStoreItem){ 
        this.id = id;
        this.nameOfStoreItem = nameOfStoreItem;
        this.valueOfStoreItem = valueOfStoreItem;
    }
//getters and setters...
}

SupermarketItem.class

public class SupermarketItem {
    private Long id;
    private String nameOfSupermarketItem;
    private Long valueOfSupermarketItem;

    public SupermarketItem() {
    }

    public SupermarketItem(Long id, String nameOfSupermarketItem, Long valueOfSupermarketItem) {
        this.id = id;
        this.nameOfSupermarketItem = nameOfSupermarketItem;
        this.valueOfSupermarketItem = valueOfSupermarketItem;
    }
//getters and setters...
}

Store.class

public class Store extends LimitedValue {
    private List<StoreItem> storeItems;

    public Store(List<StoreItem> storeItems) {
        this.storeItems = storeItems;
    }
    //getters and setters
}

Supermarket.class

public class Supermarket extends LimitedValue {
    private List<SupermarketItem> supermarketItems;

    public Supermarket(List<SupermarketItem> supermarketItems) {
        this.supermarketItems = supermarketItems;
    }
    //getters and setters
}





Aucun commentaire:

Enregistrer un commentaire