public
Created

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
// If internalDirectBind was not able to bind it, it returns a special variable instance: DIRECTBIND_MISSING
// Needs this because sometimes we need to know if no value was returned..
private static Object internalDirectBind(String name, Annotation[] annotations, String value, Class<?> clazz, Type type) throws Exception {
 
 
try {
boolean nullOrEmpty = value == null || value.trim().length() == 0;
 
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(As.class)) {
Class<? extends TypeBinder<?>> toInstanciate = ((As) annotation).binder();
if (!(toInstanciate.equals(As.DEFAULT.class))) {
// Instantiate the binder
TypeBinder<?> myInstance = toInstanciate.newInstance();
return myInstance.bind(name, annotations, value, clazz, type);
}
}
}
}
 
// application custom types have higher priority. If unable to bind proceed with the next one
for (Class<TypeBinder<?>> c : Play.classloader.getAssignableClasses(TypeBinder.class)) {
if (c.isAnnotationPresent(Global.class)) {
Class<?> forType = (Class) ((ParameterizedType) c.getGenericInterfaces()[0]).getActualTypeArguments()[0];
if (forType.isAssignableFrom(clazz)) {
Object result = c.newInstance().bind(name, annotations, value, clazz, type);
if (result != null) {
return result;
}
}
}
}
 
// custom types
for (Class<?> c : supportedTypes.keySet()) {
if (Logger.isTraceEnabled()) {
Logger.trace("directBind: value [" + value + "] c [" + c + "] Class [" + clazz + "]");
}
 
if (c.isAssignableFrom(clazz)) {
if (Logger.isTraceEnabled()) {
Logger.trace("directBind: isAssignableFrom is true");
}
return supportedTypes.get(c).bind(name, annotations, value, clazz, type);
}
}
 
// raw String
if (clazz.equals(String.class)) {
return value;
}
 
// Handles the case where the model property is a sole character
if (clazz.equals(Character.class)) {
return value.charAt(0);
}
 
// Enums
if (Enum.class.isAssignableFrom(clazz)) {
return nullOrEmpty ? null : Enum.valueOf((Class<Enum>) clazz, value);
}
 
// int or Integer binding
if (clazz.getName().equals("int") || clazz.equals(Integer.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? 0 : null;
}
 
return Integer.parseInt(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
}
 
// long or Long binding
if (clazz.getName().equals("long") || clazz.equals(Long.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? 0l : null;
}
 
return Long.parseLong(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
}
 
// byte or Byte binding
if (clazz.getName().equals("byte") || clazz.equals(Byte.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? (byte) 0 : null;
}
 
return Byte.parseByte(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
}
 
// short or Short binding
if (clazz.getName().equals("short") || clazz.equals(Short.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? (short) 0 : null;
}
 
return Short.parseShort(value.contains(".") ? value.substring(0, value.indexOf(".")) : value);
}
 
// float or Float binding
if (clazz.getName().equals("float") || clazz.equals(Float.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? 0f : null;
}
 
return Float.parseFloat(value);
}
 
// double or Double binding
if (clazz.getName().equals("double") || clazz.equals(Double.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? 0d : null;
}
 
return Double.parseDouble(value);
}
 
// BigDecimal binding
if (clazz.equals(BigDecimal.class)) {
return nullOrEmpty ? null : new BigDecimal(value);
}
 
// boolean or Boolean binding
if (clazz.getName().equals("boolean") || clazz.equals(Boolean.class)) {
if (nullOrEmpty) {
return clazz.isPrimitive() ? false : null;
}
 
if (value.equals("1") || value.toLowerCase().equals("on") || value.toLowerCase().equals("yes")) {
return true;
}
 
return Boolean.parseBoolean(value);
}
} catch (Exception e){
Logger.warn("Unable to bind value: " + value + " Exception: " + e.getMessage());
}
 
return DIRECTBINDING_NO_RESULT;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.