Exemplo n.º 1
0
 public static Object bind(Object o, String name, Map<String, String[]> params) {
   for (PlayPlugin plugin : Play.plugins) {
     Object result = plugin.bind(name, o, params);
     if (result != null) {
       return result;
     }
   }
   try {
     return new BeanWrapper(o.getClass()).bind(name, null, params, "", o, null);
   } catch (Exception e) {
     Validation.addError(name, "validation.invalid");
     return null;
   }
 }
Exemplo n.º 2
0
  public static Object bind(
      String name,
      Class<?> clazz,
      Type type,
      Annotation[] annotations,
      Map<String, String[]> params,
      Object o,
      Method method,
      int parameterIndex) {
    Logger.trace("bind: name [" + name + "] annotation [" + Utils.join(annotations, " ") + "] ");

    Object result = null;
    // Let a chance to plugins to bind this object
    for (PlayPlugin plugin : Play.plugins) {
      result = plugin.bind(name, clazz, type, annotations, params);
      if (result != null) {
        return result;
      }
    }
    String[] profiles = null;
    if (annotations != null) {
      for (Annotation annotation : annotations) {
        if (annotation.annotationType().equals(As.class)) {
          As as = ((As) annotation);
          profiles = as.value();
        }
        if (annotation.annotationType().equals(NoBinding.class)) {
          NoBinding bind = ((NoBinding) annotation);
          profiles = bind.value();
        }
      }
    }
    result = bindInternal(name, clazz, type, annotations, params, "", profiles);

    if (result == MISSING) {
      // Try the scala default
      if (o != null && parameterIndex > 0) {
        try {
          Method defaultMethod =
              method
                  .getDeclaringClass()
                  .getDeclaredMethod(method.getName() + "$default$" + parameterIndex);
          return defaultMethod.invoke(o);
        } catch (NoSuchMethodException e) {
          //
        } catch (Exception e) {
          throw new UnexpectedException(e);
        }
      }
      if (clazz.equals(boolean.class)) {
        return false;
      }
      if (clazz.equals(int.class)) {
        return 0;
      }
      if (clazz.equals(long.class)) {
        return 0;
      }
      if (clazz.equals(double.class)) {
        return 0;
      }
      if (clazz.equals(short.class)) {
        return 0;
      }
      if (clazz.equals(byte.class)) {
        return 0;
      }
      if (clazz.equals(char.class)) {
        return ' ';
      }
      return null;
    }
    return result;
  }