All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pageseeder.xmldoclet.options.AnnotatedOption Maven / Gradle / Ivy

The newest version!
package org.pageseeder.xmldoclet.options;

import jdk.javadoc.doclet.Reporter;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Option to filter classes with the specified annotation.
 *
 * @author Christophe Lauret
 * @version 1.0
 */
public final class AnnotatedOption extends XMLDocletOptionBase {

  private final List annotations = new ArrayList<>();

  public AnnotatedOption(Reporter reporter) {
    super(reporter);
  }

  @Override
  public int getArgumentCount() {
    return 1;
  }

  @Override
  public String getDescription() {
    return "filter classes with the specified annotation";
  }

  @Override
  public Kind getKind() {
    return Kind.STANDARD;
  }

  @Override
  public List getNames() {
    return Collections.singletonList("-annotated");
  }

  @Override
  public String getParameters() {
    return "";
  }

  @Override
  public boolean process(String option, List arguments) {
    String annotation = arguments.get(0);
    note("Filtering classes matching annotation: "+annotation);
    this.annotations.add(annotation);
    return true;
  }

  public List getAnnotations() {
    return this.annotations;
  }

  public boolean hasFilter() {
    return this.annotations.size() > 0;
  }

  /**
   * Filters the included set of classes by checking whether the specified class matches
   * one of the annotations specified in this option.
   *
   * @param element     the class documentation.
   * @return true if the class should be included; false otherwise.
   */
  public boolean matches(TypeElement element) {
    if (this.annotations.isEmpty()) return true;
    List annotations = element.getAnnotationMirrors();
    for (AnnotationMirror i : annotations) {
      // TODO Previous version supported matching the simple name also
      String name = i.getAnnotationType().asElement().toString();
      if (this.annotations.contains(name)) return true;
    }
    return false;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy