Beispiel #1
0
 /** Adds the subtypes listed in the provided annotation to the supplied list. */
 protected static void addSubtypes(EditorTypes annotation, Collection<Class<?>> types) {
   for (Class<?> sclazz : annotation.value()) {
     // handle the case where the annotation includes the annotated class
     if (sclazz.getAnnotation(EditorTypes.class) == annotation) {
       types.add(sclazz);
     } else {
       addSubtypes(sclazz, types);
     }
   }
 }
Beispiel #2
0
  /**
   * Returns an array containing the available subtypes of the specified type, first looking to
   * subtypes listed in the annotation, then attempting to find a method using reflection.
   */
  protected Class<?>[] getSubtypes(Class<?> type) {
    ArrayList<Class<?>> types = new ArrayList<Class<?>>();

    // start with the null class, if allowed
    boolean nullable = getAnnotation().nullable();
    if (nullable) {
      types.add(null);
    }

    // look for a subtype annotation and add its types
    EditorTypes ownAnnotation = getAnnotation(EditorTypes.class);
    EditorTypes annotation =
        (ownAnnotation == null) ? type.getAnnotation(EditorTypes.class) : ownAnnotation;
    if (annotation != null) {
      addSubtypes(annotation, types);
    }

    // get the config key and add the config types
    String key = (annotation == null) ? type.getName() : annotation.key();
    if (StringUtil.isBlank(key)) {
      if (annotation == ownAnnotation) {
        Member member = getMember();
        key = member.getDeclaringClass().getName() + "." + member.getName();
      } else {
        key = type.getName();
      }
    }
    Class<?>[] ctypes = _configTypes.get(key);
    if (ctypes != null) {
      Collections.addAll(types, ctypes);
    }

    // if we don't have at least one non-null class, add the type itself
    if (types.size() == (nullable ? 1 : 0)) {
      types.add(type);
    }

    // convert to array, return
    return types.toArray(new Class<?>[types.size()]);
  }
Beispiel #3
0
 /** Returns the label for subtypes of the specified type. */
 protected String getTypeLabel(Class<?> type) {
   EditorTypes annotation = getEditorTypes(type);
   return (annotation == null) ? "type" : annotation.label();
 }