Exemplo n.º 1
0
 /**
  * Computes the display string for any value, for a specific type.
  *
  * @param desc the field descriptor - custom formatters are extracted from this descriptor.
  * @param t the value to print
  * @return the formatted string
  */
 public static <T> String print(TypeDescriptor desc, T t) {
   if (t == null) {
     return "";
   }
   if (desc != null && conversion.canConvert(desc, TypeDescriptor.valueOf(String.class))) {
     return (String) conversion.convert(t, desc, TypeDescriptor.valueOf(String.class));
   } else if (conversion.canConvert(t.getClass(), String.class)) {
     return conversion.convert(t, String.class);
   } else {
     return t.toString();
   }
 }
Exemplo n.º 2
0
 /**
  * Parses this string as instance of a specific field in the given class
  *
  * @param field the related field (custom formatters are extracted from this field annotation)
  * @param text the text to parse
  * @param clazz class representing the required type
  * @return the parsed value
  */
 @SuppressWarnings("unchecked")
 public static <T> T parse(Field field, String text, Class<T> clazz) {
   return (T) conversion.convert(text, new TypeDescriptor(field), TypeDescriptor.valueOf(clazz));
 }