title: Java Reflection get the Instance from a Field
body: <p>is there any way to get the Instance from a Field?<br>
  Here's a sample code:  </p>
  
  <pre><code>public class Apple {
      // ... a bunch of stuffs..
  }
  
  public class Person {
      @MyAnnotation(value=123)
      private Apple apple;
  }
  
  public class AppleList {
      public add(Apple apple) {
          //...
      }
  }
  
  public class Main {
      public static void main(String args[]) {
          Person person = new Person();
          Field field = person.getClass().getDeclaredField("apple");
  
          // Do some random stuffs with the annotation ...
  
          AppleList appleList = new AppleList();
  
          // Now I want to add the "apple" instance into appleList, which I think
          // that is inside of field.
  
          appleList.add( .. . // how do I add it here? is it possible?
  
          // I can't do .. .add( field );
          // nor .add( (Apple) field );
      }
  }
  </code></pre>
  
  <p>I need to use Reflection, because I'm using it with annotations. This is just a "sample", the method <code>AppleList.add(Apple apple)</code> is actually called by getting the method from the class, and then invoking it.</p>
  
  <p>and doing so, like: <code>method.invoke( appleList, field );</code></p>
  
  <p>causes: <code>java.lang.IllegalArgumentException: argument type mismatch</code></p>
  
  <p>*<strong><em>EDIT</em>*</strong>
  This might be helpful for someone who's looking for the same thing.</p>
  
  <p>if the class Person, had 2 or more Apple variables:</p>
  
  <pre><code>public class Person {
      private Apple appleOne;
      private Apple appleTwo;
      private Apple appleThree;
  }
  </code></pre>
  
  <p>when I get the Field, like:</p>
  
  <pre><code>Person person = new Person();
  // populate person
  Field field = person.getClass().getDeclaredField("appleTwo");
  // and now I'm getting the instance...
  Apple apple = (Apple) field.get( person );
  // this will actually get me the instance "appleTwo"
  // because of the field itself...
  </code></pre>
  
  <p>at the beginning, by looking at the line alone: <code>(Apple) field.get( person );</code><br>
  made me think that it would go and get an Instance which matches Apple class.<br>
  that's why I wondered: "which Apple will it return?"</p>