Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.