コード例 #1
0
ファイル: Utils.java プロジェクト: juzipeek/MyReadingCode
 /** Try to create a Java object using a one-string-param constructor. */
 public static Object newStringConstructor(String type, String param) throws Exception {
   Constructor c = Utils.getClass(type).getConstructor(String.class);
   try {
     return c.newInstance(param);
   } catch (InvocationTargetException e) {
     Throwable t = e.getTargetException();
     if (t instanceof Exception) {
       throw (Exception) t;
     } else {
       throw e;
     }
   }
 }
コード例 #2
0
ファイル: Utils.java プロジェクト: juzipeek/MyReadingCode
 /**
  * This method attempts to create an object of the given "type" using the "value" parameter. e.g.
  * calling createObjectFromString("java.lang.Integer", "10") will return an Integer object
  * initialized to 10.
  */
 public static Object createObjectFromString(String type, String value) throws Exception {
   Object result;
   if (primitiveToWrapper.containsKey(type)) {
     if (type.equals(Character.TYPE.getName())) {
       result = new Character(value.charAt(0));
     } else {
       result = newStringConstructor(((Class<?>) primitiveToWrapper.get(type)).getName(), value);
     }
   } else if (type.equals(Character.class.getName())) {
     result = new Character(value.charAt(0));
   } else if (Number.class.isAssignableFrom(Utils.getClass(type))) {
     result = createNumberFromStringValue(value);
   } else if (value == null || value.toString().equals("null")) {
     // hack for null value
     result = null;
   } else {
     // try to create a Java object using
     // the one-string-param constructor
     result = newStringConstructor(type, value);
   }
   return result;
 }