Path: blob/master/test/langtools/jdk/javadoc/tool/example/Example.java
40974 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* NOTE: this class is an almost a replica of the example used in the25* package-info.java.26*/27import java.io.IOException;28import java.util.Arrays;29import java.util.HashSet;30import java.util.List;31import java.util.Locale;32import java.util.Set;3334import javax.lang.model.SourceVersion;35import javax.lang.model.element.Element;36import javax.lang.model.element.TypeElement;37import javax.lang.model.util.ElementFilter;38import javax.tools.Diagnostic.Kind;3940import com.sun.source.doctree.DocCommentTree;41import com.sun.source.util.DocTrees;4243import jdk.javadoc.doclet.*;4445/**46* An Example class implementing the Doclet.47*/48public class Example implements Doclet {4950Reporter reporter;5152@Override53public void init(Locale locale, Reporter reporter) {54reporter.print(Kind.NOTE, "Doclet using locale: " + locale);55this.reporter = reporter;56}5758/**59* Prints an element.60*61* @param trees the utility class62* @param e the element to be printed63*/64public void printElement(DocTrees trees, Element e) {65DocCommentTree docCommentTree = trees.getDocCommentTree(e);66if (docCommentTree != null) {67System.out.println("Element (" + e.getKind() + ": "68+ e + ") has the following comments:");69System.out.println("Entire body: " + docCommentTree.getFullBody());70System.out.println("Block tags: " + docCommentTree.getBlockTags());71}72}7374@Override75public boolean run(DocletEnvironment docEnv) {76reporter.print(Kind.NOTE, "overviewfile: " + overviewfile);77// get the DocTrees utility class to access DocComments78DocTrees docTrees = docEnv.getDocTrees();7980// location of an element in the same directory as overview.html81try {82Element e = ElementFilter.typesIn(docEnv.getSpecifiedElements()).iterator().next();83DocCommentTree docCommentTree84= docTrees.getDocCommentTree(e, overviewfile);85if (docCommentTree != null) {86System.out.println("Overview html: " + docCommentTree.getFullBody());87}88} catch (IOException missing) {89reporter.print(Kind.ERROR, "No overview.html found.");90}9192for (TypeElement t : ElementFilter.typesIn(docEnv.getIncludedElements())) {93System.out.println(t.getKind() + ":" + t);94for (Element e : t.getEnclosedElements()) {95printElement(docTrees, e);96}97}98return true;99}100101@Override102public String getName() {103return "Example";104}105106private String overviewfile;107108@Override109public Set<? extends Option> getSupportedOptions() {110Option[] options = {111new Option() {112private final List<String> someOption = Arrays.asList(113"-overviewfile",114"-overview-file",115"--over-view-file"116);117118@Override119public int getArgumentCount() {120return 1;121}122123@Override124public String getDescription() {125return "an option with aliases";126}127128@Override129public Option.Kind getKind() {130return Option.Kind.STANDARD;131}132133@Override134public List<String> getNames() {135return someOption;136}137138@Override139public String getParameters() {140return "file";141}142143@Override144public boolean process(String opt, List<String> arguments) {145overviewfile = arguments.get(0);146return true;147}148}149};150return new HashSet<>(Arrays.asList(options));151}152153@Override154public SourceVersion getSupportedSourceVersion() {155// support the latest release156return SourceVersion.latest();157}158}159160161