title: Setting values to object dynamically (dynamic property/type)
body: <p>I'm trying to create JSON to Object mapper. Its main idea is that "user" defines a dictionary where keys are JSON attributes and values are Objects property names. So how does it work (so far):</p>
  
  <ol>
  <li>Get value from JSON (var jsonValue)</li>
  <li>Get property type from getter (var methodType)</li>
  <li>Create setter method and insert value from json</li>
  </ol>
  
  <p>Only problem is that I can't cast jsonValue into object dynamically. I have to check what's the object type (methodType) and then cast it differently for String, Long, Integer and so on. Can I somehow cast it dynamically? </p>
  
  <pre><code>    private Cookbook createCookbook(JsonObject jsonCookbook) {
      //Cookbook to return
      Cookbook cookbook = new Cookbook();
  
      Enumeration&lt;String&gt; e = mappingDictionary.keys();
      while (e.hasMoreElements()) {
          //get JSON value
          String mappingKey = e.nextElement();
          JsonElement json = jsonCookbook.get(mappingKey);
          String jsonValue = json.getAsString();
  
          //set JSON value to property
          String mappingValue = mappingDictionary.get(mappingKey);
  
          //reflection
          try {
              //get type of the getter
              String getMethodName = "get" + mappingValue; 
              Method getMethod = cookbook.getClass().getMethod(getMethodName, null);
              Class&lt;?&gt; methodType = getMethod.getReturnType(); 
  
              //set methods
              String setMethodName = "set" + mappingValue;
              Method setMethod = cookbook.getClass().getMethod(setMethodName, methodType);
  
              //set value to property
              /* DONT WANT TO DO IT LIKE THIS, THIS IS MY PROBLEM */
              if (methodType.equals(String.class))
                  setMethod.invoke(cookbook, jsonValue);
              if (methodType.equals(Long.class))
                  setMethod.invoke(cookbook, Long.valueOf(jsonValue));
  
          } catch (NoSuchMethodException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IllegalArgumentException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IllegalAccessException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (InvocationTargetException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }           
      }
  
      return cookbook;
      }
  </code></pre>