title: Using reflection to get a specific attribute from a extended instance
body: <p>I would like to make a generic method to get a List from the parameter object. </p>
  
  <p>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 <code>instanceof</code> solution because the number of classes that extends LimitedValue can be big.</p>
  
  <p>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:</p>
  
  <pre><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 &lt; 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 ?
          }
      }
  </code></pre>
  
  <p>This is my full code:</p>
  
  <pre><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 &lt; 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;
      }
  }
  </code></pre>
  
  <p>If it help, these are my other classes:</p>
  
  <p>LimitedValue.class </p>
  
  <pre><code>public class LimitedValue { }
  </code></pre>
  
  <p>StoreItem.class</p>
  
  <pre><code>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...
  }
  </code></pre>
  
  <p>SupermarketItem.class</p>
  
  <pre><code>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...
  }
  </code></pre>
  
  <p>Store.class</p>
  
  <pre><code>public class Store extends LimitedValue {
      private List&lt;StoreItem&gt; storeItems;
  
      public Store(List&lt;StoreItem&gt; storeItems) {
          this.storeItems = storeItems;
      }
      //getters and setters
  }
  </code></pre>
  
  <p>Supermarket.class</p>
  
  <pre><code>public class Supermarket extends LimitedValue {
      private List&lt;SupermarketItem&gt; supermarketItems;
  
      public Supermarket(List&lt;SupermarketItem&gt; supermarketItems) {
          this.supermarketItems = supermarketItems;
      }
      //getters and setters
  }
  </code></pre>