Ejemplo n.º 1
0
  /**
   * Note: Does test for plural form of fieldName, e.g. when the field is a collection
   *
   * @param objClass
   * @param fieldName
   * @return
   */
  public static boolean isFieldACollection(Class objClass, String fieldName) {

    boolean isCollection = false;

    Class fieldClass;

    Field field = ReflectionHelper.findField(objClass, fieldName);

    if (field != null) {

      fieldClass = field.getType();

      if (isCollection(fieldClass)) {

        isCollection = true;
      }
    }

    return isCollection;
  }
Ejemplo n.º 2
0
  /**
   * Given an object, its class and the name of a field on that object, try to deduce the field's
   * Class. Note: Does test for plural form of fieldName, e.g. when the field is a collection
   *
   * @param obj
   * @param fieldName
   * @return
   */
  public static Class determineClass(Object obj, Class objClass, String fieldName)
      throws ToolkitException {

    Class fieldClass;

    Field field = ReflectionHelper.findField(objClass, fieldName);

    if (field != null) {

      fieldClass = field.getType();

      if (Collection.class.isAssignableFrom(fieldClass)) {

        // This is why we require an indexed getter for our collections so we can determine the
        // return type
        Method method = findMethod(objClass, "get" + fieldName, int.class);

        if (method != null) {

          fieldClass = method.getReturnType();

        } else {

          throw new ToolkitException(
              "Can not determine class of Collection field '"
                  + fieldName
                  + "' in "
                  + obj.getClass().getName()
                  + "; perhaps there is no indexed getter?");
        }
      }

    } else {

      throw new ToolkitException(
          "No such field '" + fieldName + "' in " + obj.getClass().getName());
    }

    return fieldClass;
  }