title: Retrieving the data type for an object using reflection
body: <p>I have an object initialized like :</p>
  
  <pre><code>Object obj  = new Object(){
    final String type = "java.lang.Integer";
    final Object value = 6;
  };
  </code></pre>
  
  <p>I want to recreate this object as :</p>
  
  <pre><code> Integer i = 6;
  </code></pre>
  
  <p>Is there any way I can get the <code>type</code> field of <code>obj</code> object and create a new instance using reflection and feed the value in it?</p>
  
  <p>EDIT : Upon extending this question, I find that if I have the object stored in file and retrieve it from file using Jackson using this : </p>
  
  <pre><code>Reader reader = new Reader();
  MyClass[] instances = reader.readValue(fileName);
  </code></pre>
  
  <p>And <code>MyClass</code> is defined as :</p>
  
  <pre><code>class MyClass{
  
    List&lt;Object&gt; fields;
    .
    .
    .
  }
  </code></pre>
  
  <p>Now I am iterating the <code>fields</code> and converting them into proper objects using the code :</p>
  
  <pre><code>public static Class&lt;?&gt; getTypeForObject(Object field) {
  
      Field returnType = null;
      try {
          returnType = field.getClass().getDeclaredField("type");
      } catch (NoSuchFieldException e) {
          e.printStackTrace();
      } catch (SecurityException e) {
          e.printStackTrace();
      } catch (IllegalArgumentException e) {
          e.printStackTrace();
      }
      return returnType.getType();
  }
  
  public static Object getValueForObject(Object field) {
  
      Object obj = null;
      try {
          obj = field.getClass().getDeclaredField("value").get(field);
      } catch (IllegalArgumentException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (NoSuchFieldException e) {
          e.printStackTrace();
      } catch (SecurityException e) {
          e.printStackTrace();
      }
      return obj;
  }
  </code></pre>
  
  <p>But when I watch the expression <code>field.getClass()</code>, it gives me <code>LinkedHashMap</code> as its class. I am confused why and if it that Object is internally treated as <code>Map</code> what options am I left with if I want to do it with reflection without using an concrete data structures so that everything is generalized.</p>