|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.classification.tools; |
| 19 | + |
| 20 | +import jdk.javadoc.doclet.Doclet; |
| 21 | +import jdk.javadoc.doclet.DocletEnvironment; |
| 22 | +import jdk.javadoc.doclet.Reporter; |
| 23 | +import javax.lang.model.SourceVersion; |
| 24 | +import javax.lang.model.element.Element; |
| 25 | + |
| 26 | +import jdk.javadoc.doclet.StandardDoclet; |
| 27 | + |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +/** |
| 32 | + * <a href="https://docs.oracle.com/en/java/javase/17/docs/api/jdk.javadoc/jdk/javadoc/doclet/Doclet.html">Doclet</a> |
| 33 | + * for excluding elements that are annotated with |
| 34 | + * {@link org.apache.hadoop.classification.InterfaceAudience.Private} or |
| 35 | + * {@link org.apache.hadoop.classification.InterfaceAudience.LimitedPrivate}. |
| 36 | + * It delegates to the Standard Doclet, and takes the same options. |
| 37 | + */ |
| 38 | +public class ExcludePrivateAnnotationsStandardDoclet implements Doclet { |
| 39 | + |
| 40 | + private final StandardDoclet delegate = new StandardDoclet(); |
| 41 | + private Reporter reporter; |
| 42 | + private Locale locale; |
| 43 | + |
| 44 | + public static SourceVersion languageVersion() { |
| 45 | + return SourceVersion.RELEASE_17; |
| 46 | + } |
| 47 | + |
| 48 | + public static boolean start(DocletEnvironment root) { |
| 49 | + System.out.println(ExcludePrivateAnnotationsStandardDoclet.class.getSimpleName()); |
| 50 | + if (root.getSpecifiedElements().isEmpty()) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + return new StandardDoclet().run(root); |
| 54 | + } |
| 55 | + |
| 56 | + public static int optionLength(String option) { |
| 57 | + Integer length = StabilityOptions.optionLength(option); |
| 58 | + if (length != null) { |
| 59 | + return length; |
| 60 | + } |
| 61 | + for (Doclet.Option o : new StandardDoclet().getSupportedOptions()) { |
| 62 | + for (String name : o.getNames()) { |
| 63 | + if (name.equals(option)) { |
| 64 | + return o.getArgumentCount() + 1; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + public static boolean validOptions(String[][] options, Reporter reporter) { |
| 72 | + StabilityOptions.validOptions(options, reporter); |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void init(Locale locale, Reporter reporter) { |
| 78 | + this.locale = locale; |
| 79 | + this.reporter = reporter; |
| 80 | + delegate.init(locale, reporter); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getName() { |
| 85 | + return getClass().getSimpleName(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Set<Option> getSupportedOptions() { |
| 90 | + Set<Option> s = new java.util.HashSet<>(delegate.getSupportedOptions()); |
| 91 | + s.add(new Option() { |
| 92 | + @Override |
| 93 | + public int getArgumentCount() { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getDescription() { |
| 99 | + return ""; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Kind getKind() { |
| 104 | + return Kind.OTHER; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public java.util.List<String> getNames() { |
| 109 | + return java.util.Collections.singletonList("-unstable"); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getParameters() { |
| 114 | + return ""; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean process(String opt, java.util.List<String> args) { |
| 119 | + StabilityOptions.setLevel(StabilityOptions.Level.UNSTABLE); |
| 120 | + return true; |
| 121 | + } |
| 122 | + }); |
| 123 | + s.add(new Option() { |
| 124 | + @Override |
| 125 | + public int getArgumentCount() { |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public String getDescription() { |
| 131 | + return ""; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Kind getKind() { |
| 136 | + return Kind.OTHER; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public java.util.List<String> getNames() { |
| 141 | + return java.util.Collections.singletonList("-evolving"); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String getParameters() { |
| 146 | + return ""; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean process(String opt, java.util.List<String> args) { |
| 151 | + StabilityOptions.setLevel(StabilityOptions.Level.EVOLVING); |
| 152 | + return true; |
| 153 | + } |
| 154 | + }); |
| 155 | + return s; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public SourceVersion getSupportedSourceVersion() { |
| 160 | + return SourceVersion.RELEASE_17; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public boolean run(DocletEnvironment environment) { |
| 165 | + StabilityOptions.applyToRootProcessor(); |
| 166 | + DocletEnvironment ignored = RootDocProcessor.process(environment); |
| 167 | + |
| 168 | + if (environment.getIncludedElements().isEmpty()) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + return delegate.run(environment); |
| 172 | + } |
| 173 | +} |
0 commit comments