예제 #1
0
  public static <A extends Annotation> void getAnnotations(
      final Class<?> fromClass,
      final Class<A> annotationType,
      final Map<String, A> annotations,
      final boolean climbTree) {
    final Map<String, PropertyHelper> properties = PropertyHelper.getFromClass(fromClass);

    for (final Entry<String, PropertyHelper> entry : properties.entrySet()) {
      final PropertyHelper ph = entry.getValue();
      final A annotation = ph.getAnnotation(annotationType);

      if (annotation != null) {
        annotations.put(entry.getKey(), annotation);
      }
    }

    if (climbTree && !fromClass.getSuperclass().equals(Object.class)) {
      getAnnotations(fromClass.getSuperclass(), annotationType, annotations, true);
    }
  }
예제 #2
0
  /**
   * Get properties of the given type. Results are cached, so call as often as you want.
   *
   * @param klass The class to examine for properties.
   * @return A map of the property name, to an instance of the helper.
   */
  public static Map<String, PropertyHelper> getFromClass(final Class<?> klass) {

    if (GLOBAL_PROPERTY_MAP.containsKey(klass)) {
      return GLOBAL_PROPERTY_MAP.get(klass);
    }

    final Map<String, PropertyHelper> propertyList = new LinkedHashMap<String, PropertyHelper>();

    addHelpersFromFields(klass, propertyList, true);

    // but if there is a getter or setter in the super class for the private
    // field, it will be
    // detected in these
    // loops
    for (final Method method : klass.getMethods()) {
      final String methodName = method.getName();
      if (methodName.startsWith("get") && method.getParameterTypes().length == 0) {
        final String propertyName = getPropertyNameFromMethodName(methodName);

        PropertyHelper prop = propertyList.get(propertyName);

        // make an attempt to look for the field
        final Field field = getField(klass, propertyName);

        if (prop == null) {
          prop = new PropertyHelper(field, method, null);
          propertyList.put(propertyName, prop);
        } else {
          prop.getter = method;
          prop.field = field;
        }
      }

      if (methodName.startsWith("set") && method.getParameterTypes().length == 1) {
        final String propertyName = getPropertyNameFromMethodName(methodName);

        PropertyHelper prop = propertyList.get(propertyName);

        // make an attempt to look for the field
        final Field field = getField(klass, propertyName);

        if (prop == null) {
          prop = new PropertyHelper(field, null, method);
          propertyList.put(propertyName, prop);
        } else {
          prop.setter = method;
          prop.field = field;
        }
      }
    }

    synchronized (GLOBAL_PROPERTY_MAP) {
      GLOBAL_PROPERTY_MAP.put(klass, propertyList);
    }

    return propertyList;
  }