Advertisement
Guest User

Untitled

a guest
May 24th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1.     @NonNull
  2.     private static Object stringToObject(@NonNull String string, @NonNull Class<?> clazz) {
  3.         Class<?> wrapper = Array.get(Array.newInstance(clazz, 1), 0).getClass();
  4.         try {
  5.             return wrapper.getConstructor(String.class).newInstance(string);
  6.         } catch (NoSuchMethodException e) {
  7.             try {
  8.                 return wrapper.getConstructor(char.class).newInstance(string.charAt(0));
  9.             } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | InstantiationException e1) {
  10.                 // cannot be
  11.             }
  12.         } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
  13.             // cannot be
  14.         }
  15.         // cannot be
  16.         return null;
  17.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement