List of usage examples for com.google.gson.internal Primitives wrap
@SuppressWarnings("unchecked") public static <T> Class<T> wrap(Class<T> type)
From source file:ca.oson.json.domain.PrimitiveTypeAdapter.java
License:Apache License
@SuppressWarnings("unchecked") public <T> T adaptType(Object from, Class<T> to) { Class<?> aClass = Primitives.wrap(to); if (Primitives.isWrapperType(aClass)) { if (aClass == Character.class) { String value = from.toString(); if (value.length() == 1) { return (T) (Character) from.toString().charAt(0); }/* ww w . j a v a2 s. c o m*/ throw new JsonParseException("The value: " + value + " contains more than a character."); } try { Constructor<?> constructor = aClass.getConstructor(String.class); return (T) constructor.newInstance(from.toString()); } catch (NoSuchMethodException e) { throw new JsonParseException(e); } catch (IllegalAccessException e) { throw new JsonParseException(e); } catch (InvocationTargetException e) { throw new JsonParseException(e); } catch (InstantiationException e) { throw new JsonParseException(e); } } else if (Enum.class.isAssignableFrom(to)) { // Case where the type being adapted to is an Enum // We will try to convert from.toString() to the enum try { Method valuesMethod = to.getMethod("valueOf", String.class); return (T) valuesMethod.invoke(null, from.toString()); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } else { throw new JsonParseException("Can not adapt type " + from.getClass() + " to " + to); } }
From source file:co.cask.cdap.internal.app.runtime.procedure.DefaultProcedureRequest.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w .j ava 2 s . co m public <T> T getArgument(String key, Class<T> type) { Preconditions.checkNotNull(type, "Type cannnot be null."); Preconditions.checkArgument(!Void.class.equals(type) && !Void.TYPE.equals(type), "Void type not supported."); String value = getArgument(key); if (String.class.equals(type)) { return (T) value; } Class<T> resolvedType = type; if (Primitives.isPrimitive(resolvedType)) { resolvedType = Primitives.wrap(type); } if (Primitives.isWrapperType(resolvedType)) { // All wrapper has the valueOf(String) method try { return (T) resolvedType.getMethod("valueOf", String.class).invoke(null, value); } catch (Exception e) { // Should not happen throw Throwables.propagate(e); } } if (URL.class.equals(type)) { try { return (T) new URL(value); } catch (MalformedURLException e) { throw Throwables.propagate(e); } } if (URI.class.equals(type)) { return (T) URI.create(value); } // TODO: Maybe support gson decode the type?? throw new ClassCastException("Fail to convert from String to " + type); }
From source file:com.emeralddb.util.EDBMessageHelper.java
License:Apache License
public static <T> T fromBson(BSONObject bObj, Class<T> classOfT) { bObj.removeField("_id"); Gson gson = new Gson(); Object object = gson.fromJson(bObj.toString(), (Type) classOfT); return Primitives.wrap(classOfT).cast(object); }