|
| 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 com.sun.source.util.DocTrees; |
| 21 | +import jdk.javadoc.doclet.DocletEnvironment; |
| 22 | +import jdk.javadoc.internal.tool.DocEnvImpl; |
| 23 | +import jdk.javadoc.internal.tool.ToolEnvironment; |
| 24 | + |
| 25 | +import org.apache.hadoop.classification.InterfaceAudience; |
| 26 | +import org.apache.hadoop.classification.InterfaceStability; |
| 27 | + |
| 28 | +import javax.lang.model.SourceVersion; |
| 29 | +import javax.lang.model.element.AnnotationMirror; |
| 30 | +import javax.lang.model.element.Element; |
| 31 | +import javax.lang.model.element.ElementKind; |
| 32 | +import javax.lang.model.element.TypeElement; |
| 33 | +import javax.lang.model.util.Elements; |
| 34 | +import javax.lang.model.util.Types; |
| 35 | +import javax.tools.JavaFileManager; |
| 36 | +import javax.tools.JavaFileObject; |
| 37 | +import java.util.LinkedHashSet; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +/** |
| 42 | + * This class extends Java internal DocEnvImpl to avoid cast error while |
| 43 | + * migrating to JDK17. It filters elements based on Hadoop's InterfaceAudience |
| 44 | + * and InterfaceStability annotations. |
| 45 | + * This class depends on JDK internal implementation, so we might need to |
| 46 | + * update the source code when upgrading to upper JDK versions. |
| 47 | + */ |
| 48 | +public class HadoopDocEnvImpl extends DocEnvImpl { |
| 49 | + |
| 50 | + private final DocletEnvironment delegate; |
| 51 | + private final String stability; |
| 52 | + private final boolean treatUnannotatedClassesAsPrivate; |
| 53 | + |
| 54 | + public HadoopDocEnvImpl(DocletEnvironment original, String stability, |
| 55 | + boolean treatUnannotatedClassesAsPrivate) { |
| 56 | + super(extractToolEnvironment(original), null); |
| 57 | + this.delegate = original; |
| 58 | + this.stability = stability; |
| 59 | + this.treatUnannotatedClassesAsPrivate = treatUnannotatedClassesAsPrivate; |
| 60 | + } |
| 61 | + |
| 62 | + // Use original ToolEnvironment to avoid NullPointerException |
| 63 | + private static ToolEnvironment extractToolEnvironment(DocletEnvironment original) { |
| 64 | + if (original instanceof DocEnvImpl impl) { |
| 65 | + return impl.toolEnv; |
| 66 | + } |
| 67 | + throw new IllegalArgumentException( |
| 68 | + "Expected DocEnvImpl but got: " + original.getClass().getName()); |
| 69 | + } |
| 70 | + |
| 71 | + private boolean exclude(Element el) { |
| 72 | + boolean sawPublic = false; |
| 73 | + |
| 74 | + for (AnnotationMirror am : el.getAnnotationMirrors()) { |
| 75 | + final String qname = am.getAnnotationType().toString(); |
| 76 | + |
| 77 | + if (qname.equals(InterfaceAudience.Private.class.getCanonicalName()) || qname.equals( |
| 78 | + InterfaceAudience.LimitedPrivate.class.getCanonicalName())) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + if (stability.equals(StabilityOptions.EVOLVING_OPTION)) { |
| 83 | + if (qname.equals(InterfaceStability.Unstable.class.getCanonicalName())) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + if (stability.equals(StabilityOptions.STABLE_OPTION)) { |
| 88 | + if (qname.equals(InterfaceStability.Unstable.class.getCanonicalName()) || qname.equals( |
| 89 | + InterfaceStability.Evolving.class.getCanonicalName())) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (qname.equals(InterfaceAudience.Public.class.getCanonicalName())) { |
| 95 | + sawPublic = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (sawPublic) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (treatUnannotatedClassesAsPrivate) { |
| 104 | + ElementKind k = el.getKind(); |
| 105 | + if (k == ElementKind.CLASS || k == ElementKind.INTERFACE |
| 106 | + || k == ElementKind.ANNOTATION_TYPE) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Set<? extends Element> getSpecifiedElements() { |
| 116 | + Set<? extends Element> base = delegate.getSpecifiedElements(); |
| 117 | + return base.stream().filter(e -> !exclude(e)) |
| 118 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Set<? extends Element> getIncludedElements() { |
| 123 | + Set<? extends Element> base = delegate.getIncludedElements(); |
| 124 | + return base.stream().filter(e -> !exclude(e)) |
| 125 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public DocTrees getDocTrees() { |
| 130 | + return delegate.getDocTrees(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Elements getElementUtils() { |
| 135 | + return delegate.getElementUtils(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Types getTypeUtils() { |
| 140 | + return delegate.getTypeUtils(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean isIncluded(Element e) { |
| 145 | + boolean base = delegate.isIncluded(e); |
| 146 | + return base && !exclude(e); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean isSelected(Element e) { |
| 151 | + return delegate.isSelected(e); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public JavaFileManager getJavaFileManager() { |
| 156 | + return delegate.getJavaFileManager(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public SourceVersion getSourceVersion() { |
| 161 | + return delegate.getSourceVersion(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public DocletEnvironment.ModuleMode getModuleMode() { |
| 166 | + return delegate.getModuleMode(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public JavaFileObject.Kind getFileKind(TypeElement type) { |
| 171 | + return delegate.getFileKind(type); |
| 172 | + } |
| 173 | +} |
0 commit comments