1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * NOTE: this class is an almost a replica of the example used in the 26 * package-info.java. 27 */ 28import java.io.IOException; 29import java.util.Arrays; 30import java.util.HashSet; 31import java.util.List; 32import java.util.Locale; 33import java.util.Set; 34 35import javax.lang.model.SourceVersion; 36import javax.lang.model.element.Element; 37import javax.lang.model.element.TypeElement; 38import javax.lang.model.util.ElementFilter; 39import javax.tools.Diagnostic.Kind; 40 41import com.sun.source.doctree.DocCommentTree; 42import com.sun.source.util.DocTrees; 43 44import jdk.javadoc.doclet.*; 45 46/** 47 * An Example class implementing the Doclet. 48 */ 49public class Example implements Doclet { 50 51 Reporter reporter; 52 53 @Override 54 public void init(Locale locale, Reporter reporter) { 55 reporter.print(Kind.NOTE, "Doclet using locale: " + locale); 56 this.reporter = reporter; 57 } 58 59 /** 60 * Prints an element. 61 * 62 * @param trees the utility class 63 * @param e the element to be printed 64 */ 65 public void printElement(DocTrees trees, Element e) { 66 DocCommentTree docCommentTree = trees.getDocCommentTree(e); 67 if (docCommentTree != null) { 68 System.out.println("Element (" + e.getKind() + ": " 69 + e + ") has the following comments:"); 70 System.out.println("Entire body: " + docCommentTree.getFullBody()); 71 System.out.println("Block tags: " + docCommentTree.getBlockTags()); 72 } 73 } 74 75 @Override 76 public boolean run(DocletEnvironment docEnv) { 77 reporter.print(Kind.NOTE, "overviewfile: " + overviewfile); 78 // get the DocTrees utility class to access DocComments 79 DocTrees docTrees = docEnv.getDocTrees(); 80 81 // location of an element in the same directory as overview.html 82 try { 83 Element e = ElementFilter.typesIn(docEnv.getSpecifiedElements()).iterator().next(); 84 DocCommentTree docCommentTree 85 = docTrees.getDocCommentTree(e, overviewfile); 86 if (docCommentTree != null) { 87 System.out.println("Overview html: " + docCommentTree.getFullBody()); 88 } 89 } catch (IOException missing) { 90 reporter.print(Kind.ERROR, "No overview.html found."); 91 } 92 93 for (TypeElement t : ElementFilter.typesIn(docEnv.getIncludedElements())) { 94 System.out.println(t.getKind() + ":" + t); 95 for (Element e : t.getEnclosedElements()) { 96 printElement(docTrees, e); 97 } 98 } 99 return true; 100 } 101 102 @Override 103 public String getName() { 104 return "Example"; 105 } 106 107 private String overviewfile; 108 109 @Override 110 public Set<? extends Option> getSupportedOptions() { 111 Option[] options = { 112 new Option() { 113 private final List<String> someOption = Arrays.asList( 114 "-overviewfile", 115 "-overview-file", 116 "--over-view-file" 117 ); 118 119 @Override 120 public int getArgumentCount() { 121 return 1; 122 } 123 124 @Override 125 public String getDescription() { 126 return "an option with aliases"; 127 } 128 129 @Override 130 public Option.Kind getKind() { 131 return Option.Kind.STANDARD; 132 } 133 134 @Override 135 public List<String> getNames() { 136 return someOption; 137 } 138 139 @Override 140 public String getParameters() { 141 return "file"; 142 } 143 144 @Override 145 public boolean process(String opt, List<String> arguments) { 146 overviewfile = arguments.get(0); 147 return true; 148 } 149 } 150 }; 151 return new HashSet<>(Arrays.asList(options)); 152 } 153 154 @Override 155 public SourceVersion getSupportedSourceVersion() { 156 // support the latest release 157 return SourceVersion.latest(); 158 } 159} 160