1 /* 2 * Copyright (c) 2019, 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 package pkg; 25 26 import java.util.List; 27 import java.util.Locale; 28 import java.util.Set; 29 import javax.lang.model.SourceVersion; 30 import javax.lang.model.element.Element; 31 import javax.lang.model.util.ElementScanner14; 32 import javax.tools.Diagnostic; 33 34 import jdk.javadoc.doclet.Doclet; 35 import jdk.javadoc.doclet.DocletEnvironment; 36 import jdk.javadoc.doclet.Reporter; 37 38 public class MyDoclet implements Doclet { 39 private static final boolean OK = true; 40 private boolean verbose; 41 private Reporter reporter; 42 43 Set<Option> options = Set.of( 44 new Option("--alpha -a", false, "an example no-arg option") { 45 @Override 46 public boolean process(String option, List<String> arguments) { 47 System.out.println("received option " + option + " " + arguments); 48 return OK; 49 } 50 }, 51 new Option("--beta -b", true, "an example 1-arg option") { 52 @Override 53 public boolean process(String option, List<String> arguments) { 54 System.out.println("received option " + option + " " + arguments); 55 return OK; 56 } 57 }, 58 new Option("--verbose", false, "report progress") { 59 @Override 60 public boolean process(String option, List<String> arguments) { 61 verbose = true; 62 return OK; 63 } 64 } 65 ); 66 67 @Override 68 public void init(Locale locale, Reporter reporter) { 69 this.reporter = reporter; 70 } 71 72 @Override 73 public String getName() { 74 return "MyDoclet"; 75 } 76 77 @Override 78 public Set<? extends Option> getSupportedOptions() { 79 return options; 80 } 81 82 @Override 83 public SourceVersion getSupportedSourceVersion() { 84 return SourceVersion.latest(); 85 } 86 87 @Override 88 public boolean run(DocletEnvironment environment) { 89 MyScanner myScanner = new MyScanner(); 90 for (Element e : environment.getSpecifiedElements()) { 91 myScanner.scan(e, 0); 92 } 93 94 return OK; 95 } 96 97 class MyScanner extends ElementScanner14<Void, Integer> { 98 @Override 99 public Void scan(Element e, Integer depth) { 100 String msg = e.getKind() + " " + e; 101 reporter.print(Diagnostic.Kind.NOTE, e, msg); 102 return super.scan(e, depth + 1); 103 } 104 } 105 106 107 abstract class Option implements Doclet.Option { 108 final List<String> names; 109 final boolean hasArg; 110 final String description; 111 112 Option(String names, boolean hasArg, String description) { 113 this.names = List.of(names.split("\\s+")); 114 this.hasArg = hasArg; 115 this.description = description; 116 } 117 118 @Override 119 public int getArgumentCount() { 120 return hasArg ? 1 : 0; 121 } 122 123 @Override 124 public String getDescription() { 125 return description; 126 } 127 128 @Override 129 public Kind getKind() { 130 return Kind.STANDARD; 131 } 132 133 @Override 134 public List<String> getNames() { 135 return names; 136 } 137 138 @Override 139 public String getParameters() { 140 return hasArg ? "<arg>" : null; 141 } 142 } 143 }