public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { @SuppressWarnings("unchecked") Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; } final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); for (T constant : rawType.getEnumConstants()) { lowercaseToConstant.put(toLowercase(constant), constant); } return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(toLowercase(value)); } } public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } else { return lowercaseToConstant.get(reader.nextString()); } } }; }
protected T blankInstance() { try { return backedType.newInstance(); } catch (Exception e) { throw new RuntimeException( "Cannot instantiate " + backedType + ". It must have a default constructor", e); } }
private static <T> T mapToClazz(Map<String, String> map, Class<T> clazz, String formatStr) throws InstantiationException, IllegalAccessException, InvocationTargetException { Map<String, String> mapdata = getCaseInsensitveMap(map); T paypalClassicResponseModel; paypalClassicResponseModel = clazz.newInstance(); for (Field field : FieldUtils.getAllFields(clazz)) { if (String.class.isAssignableFrom(field.getType())) { BeanUtils.setProperty( paypalClassicResponseModel, field.getName(), mapdata.get(getFormatedKeyName(formatStr, field.getName(), null))); } if (List.class.isAssignableFrom(field.getType())) { PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class); ParameterizedType ty = ((ParameterizedType) field.getGenericType()); if (ty.getActualTypeArguments()[0] instanceof Class) { Class subClazz = (Class<?>) ty.getActualTypeArguments()[0]; if (String.class.isAssignableFrom(subClazz)) { if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) { List<String> values = fetchPartialKey( mapdata, getFormatedKeyName(paypalCollection.format(), field.getName(), null)); BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values); } else { List<String> values = fetchPartialKey(mapdata, field.getName().toUpperCase()); BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values); } } if (PaypalClassicModel.class.isAssignableFrom(subClazz)) { if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) { BeanUtils.setProperty( paypalClassicResponseModel, field.getName(), mapToClazzList(mapdata, subClazz, paypalCollection.format())); } } } } if (PaypalClassicModel.class.isAssignableFrom(field.getType())) { BeanUtils.setProperty( paypalClassicResponseModel, field.getName(), mapToClazz(mapdata, field.getType())); } } return paypalClassicResponseModel; }
/** * Retrieve a field. * * @param key field name * @return the field (even if the field does not exist you get a field) */ public Field field(String key) { // Value String fieldValue = null; if (data.containsKey(key)) { fieldValue = data.get(key); } else { if (value.isPresent()) { BeanWrapper beanWrapper = new BeanWrapperImpl(value.get()); beanWrapper.setAutoGrowNestedPaths(true); String objectKey = key; if (rootName != null && key.startsWith(rootName + ".")) { objectKey = key.substring(rootName.length() + 1); } if (beanWrapper.isReadableProperty(objectKey)) { Object oValue = beanWrapper.getPropertyValue(objectKey); if (oValue != null) { final String objectKeyFinal = objectKey; fieldValue = withRequestLocale( () -> formatters.print( beanWrapper.getPropertyTypeDescriptor(objectKeyFinal), oValue)); } } } } // Error List<ValidationError> fieldErrors = errors.get(key); if (fieldErrors == null) { fieldErrors = new ArrayList<>(); } // Format Tuple<String, List<Object>> format = null; BeanWrapper beanWrapper = new BeanWrapperImpl(blankInstance()); beanWrapper.setAutoGrowNestedPaths(true); try { for (Annotation a : beanWrapper.getPropertyTypeDescriptor(key).getAnnotations()) { Class<?> annotationType = a.annotationType(); if (annotationType.isAnnotationPresent(play.data.Form.Display.class)) { play.data.Form.Display d = annotationType.getAnnotation(play.data.Form.Display.class); if (d.name().startsWith("format.")) { List<Object> attributes = new ArrayList<>(); for (String attr : d.attributes()) { Object attrValue = null; try { attrValue = a.getClass().getDeclaredMethod(attr).invoke(a); } catch (Exception e) { // do nothing } attributes.add(attrValue); } format = Tuple(d.name(), attributes); } } } } catch (NullPointerException e) { // do nothing } // Constraints List<Tuple<String, List<Object>>> constraints = new ArrayList<>(); Class<?> classType = backedType; String leafKey = key; if (rootName != null && leafKey.startsWith(rootName + ".")) { leafKey = leafKey.substring(rootName.length() + 1); } int p = leafKey.lastIndexOf('.'); if (p > 0) { classType = beanWrapper.getPropertyType(leafKey.substring(0, p)); leafKey = leafKey.substring(p + 1); } if (classType != null) { BeanDescriptor beanDescriptor = play.data.validation.Validation.getValidator().getConstraintsForClass(classType); if (beanDescriptor != null) { PropertyDescriptor property = beanDescriptor.getConstraintsForProperty(leafKey); if (property != null) { constraints = Constraints.displayableConstraint(property.getConstraintDescriptors()); } } } return new Field(this, key, constraints, format, fieldErrors, fieldValue); }