1 /* 2 * Copyright (c) 2013, 2014, 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 * @test 26 * @bug 8026180 27 * @summary Ensuring javax.lang.model.**, javax.tools.**, javax.annotation.processing.** 28 * and com.sun.source.** don't export inappropriate types. 29 * @library /tools/javac/lib 30 * @build JavacTestingAbstractProcessor NoPrivateTypesExported 31 * @compile -processor NoPrivateTypesExported NoPrivateTypesExported.java 32 */ 33 import java.lang.annotation.Documented; 34 import java.util.Arrays; 35 import java.util.HashSet; 36 import java.util.List; 37 import java.util.Set; 38 import javax.annotation.processing.RoundEnvironment; 39 import javax.lang.model.element.AnnotationMirror; 40 import javax.lang.model.element.AnnotationValue; 41 import javax.lang.model.element.Element; 42 import javax.lang.model.element.ElementKind; 43 import javax.lang.model.element.ExecutableElement; 44 import javax.lang.model.element.PackageElement; 45 import javax.lang.model.element.TypeElement; 46 import javax.lang.model.element.TypeParameterElement; 47 import javax.lang.model.element.VariableElement; 48 import javax.lang.model.type.ArrayType; 49 import javax.lang.model.type.DeclaredType; 50 import javax.lang.model.type.IntersectionType; 51 import javax.lang.model.type.TypeMirror; 52 import javax.lang.model.type.TypeVariable; 53 import javax.lang.model.type.WildcardType; 54 import javax.tools.Diagnostic.Kind; 55 56 public class NoPrivateTypesExported extends JavacTestingAbstractProcessor { 57 58 private static final String[] javaxLangModelPackages = new String[] { 59 "javax.lang.model", 60 "javax.lang.model.element", 61 "javax.lang.model.type", 62 "javax.lang.model.util", 63 }; 64 65 private static final Set<String> javaxLangModelAcceptable; 66 67 private static final String[] javaxToolsProcessingPackages = new String[] { 68 "javax.annotation.processing", 69 "javax.tools", 70 }; 71 72 private static final Set<String> javaxToolsProcessingAcceptable; 73 74 private static final String[] comSunSourcePackages = new String[] { 75 "com.sun.source.doctree", 76 "com.sun.source.tree", 77 "com.sun.source.util" 78 }; 79 80 private static final Set<String> comSunSourceAcceptable; 81 82 static { 83 javaxLangModelAcceptable = new HashSet<>(Arrays.asList( 84 "java.io.", 85 "java.lang.", 86 "java.net.", 87 "java.nio.", 88 "java.util.", 89 "javax.lang.model.", 90 "javax.annotation.processing.SupportedSourceVersion", 91 "jdk.Exported" 92 )); 93 Set<String> javaxToolsProcessingAcceptableTemp = new HashSet<>(); 94 javaxToolsProcessingAcceptableTemp.addAll(javaxLangModelAcceptable); 95 javaxToolsProcessingAcceptableTemp.addAll(Arrays.asList( 96 "javax.annotation.processing.", 97 "javax.tools." 98 )); 99 javaxToolsProcessingAcceptable = javaxToolsProcessingAcceptableTemp; 100 Set<String> comSunSourceAcceptableTemp = new HashSet<>(); 101 comSunSourceAcceptableTemp.addAll(javaxToolsProcessingAcceptable); 102 comSunSourceAcceptableTemp.addAll(Arrays.asList( 103 "com.sun.source.doctree.", 104 "com.sun.source.tree.", 105 "com.sun.source.util." 106 )); 107 comSunSourceAcceptable = comSunSourceAcceptableTemp; 108 } 109 110 @Override 111 public boolean process(Set<? extends TypeElement> annotations, 112 RoundEnvironment roundEnv) { 113 if (roundEnv.processingOver()) { 114 verifyPackage(javaxLangModelPackages, javaxLangModelAcceptable); 115 verifyPackage(javaxToolsProcessingPackages, javaxToolsProcessingAcceptable); 116 verifyPackage(comSunSourcePackages, comSunSourceAcceptable); 117 } 118 return true; 119 } 120 121 private void verifyPackage(String[] packagesToTest, Set<String> acceptable) { 122 for (String packageToTest : packagesToTest) { 123 PackageElement packageElement = processingEnv.getElementUtils() 124 .getPackageElement(packageToTest); 125 126 verifyReferredTypesAcceptable(packageElement, acceptable); 127 } 128 } 129 130 private void verifyReferredTypesAcceptable(Element rootElement, 131 final Set<String> acceptable) { 132 new ElementScanner<Void, Void>() { 133 @Override public Void visitType(TypeElement e, Void p) { 134 verifyTypeAcceptable(e.getSuperclass(), acceptable); 135 verifyTypesAcceptable(e.getInterfaces(), acceptable); 136 scan(e.getTypeParameters(), p); 137 scan(e.getEnclosedElements(), p); 138 verifyAnnotations(e.getAnnotationMirrors(), acceptable); 139 return null; 140 } 141 @Override public Void visitTypeParameter(TypeParameterElement e, Void p) { 142 verifyTypesAcceptable(e.getBounds(), acceptable); 143 scan(e.getEnclosedElements(), p); 144 verifyAnnotations(e.getAnnotationMirrors(), acceptable); 145 return null; 146 } 147 @Override public Void visitPackage(PackageElement e, Void p) { 148 scan(e.getEnclosedElements(), p); 149 verifyAnnotations(e.getAnnotationMirrors(), acceptable); 150 return null; 151 } 152 @Override public Void visitVariable(VariableElement e, Void p) { 153 verifyTypeAcceptable(e.asType(), acceptable); 154 scan(e.getEnclosedElements(), p); 155 verifyAnnotations(e.getAnnotationMirrors(), acceptable); 156 return null; 157 } 158 @Override 159 public Void visitExecutable(ExecutableElement e, Void p) { 160 scan(e.getTypeParameters(), p); 161 verifyTypeAcceptable(e.getReturnType(), acceptable); 162 scan(e.getParameters(), p); 163 verifyTypesAcceptable(e.getThrownTypes(), acceptable); 164 scan(e.getEnclosedElements(), p); 165 verifyAnnotations(e.getAnnotationMirrors(), acceptable); 166 return null; 167 } 168 }.scan(rootElement, null); 169 } 170 171 private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations, 172 Set<String> acceptable) { 173 for (AnnotationMirror mirror : annotations) { 174 Element annotationElement = mirror.getAnnotationType().asElement(); 175 176 if (annotationElement.getAnnotation(Documented.class) == null) { 177 note("Ignoring undocumented annotation: " + mirror.getAnnotationType()); 178 } 179 180 verifyTypeAcceptable(mirror.getAnnotationType(), acceptable); 181 182 for (AnnotationValue value : mirror.getElementValues().values()) { 183 verifyAnnotationValue(value, acceptable); 184 } 185 } 186 } 187 188 private void verifyAnnotationValue(AnnotationValue value, 189 final Set<String> acceptable) { 190 value.accept(new SimpleAnnotationValueVisitor<Void, Void>() { 191 @Override public Void visitType(TypeMirror t, Void p) { 192 verifyTypeAcceptable(t, acceptable); 193 return null; 194 } 195 @Override 196 public Void visitEnumConstant(VariableElement c, Void p) { 197 verifyReferredTypesAcceptable(c, acceptable); 198 return null; 199 } 200 @Override public Void visitArray(List<? extends AnnotationValue> vals, 201 Void p) { 202 for (AnnotationValue val : vals) { 203 val.accept(this, p); 204 } 205 return null; 206 } 207 @Override protected Void defaultAction(Object o, Void p) { 208 error("Unexpected AnnotationValue: " + o.toString()); 209 return super.defaultAction(o, p); 210 } 211 }, null); 212 } 213 214 private void verifyTypesAcceptable(Iterable<? extends TypeMirror> types, 215 Set<String> acceptable) { 216 if (types == null) return ; 217 218 for (TypeMirror type : types) { 219 verifyTypeAcceptable(type, acceptable); 220 } 221 } 222 223 private void verifyTypeAcceptable(TypeMirror type, Set<String> acceptable) { 224 if (type == null) return ; 225 226 verifyAnnotations(type.getAnnotationMirrors(), acceptable); 227 228 switch (type.getKind()) { 229 case BOOLEAN: case BYTE: case CHAR: case DOUBLE: case FLOAT: 230 case INT: case LONG: case SHORT: case VOID: case NONE: case NULL: 231 return ; 232 case DECLARED: 233 DeclaredType dt = (DeclaredType) type; 234 TypeElement outermostTypeElement = outermostTypeElement(dt.asElement()); 235 String outermostType = outermostTypeElement.getQualifiedName().toString(); 236 boolean isAcceptable = false; 237 for (String acceptablePackage : acceptable) { 238 if (outermostType.startsWith(acceptablePackage)) { 239 isAcceptable = true; 240 break; 241 } 242 } 243 if (!isAcceptable) { 244 error("Type not acceptable for this API: " + dt.toString()); 245 } 246 247 for (TypeMirror bound : dt.getTypeArguments()) { 248 verifyTypeAcceptable(bound, acceptable); 249 } 250 break; 251 case ARRAY: 252 verifyTypeAcceptable(((ArrayType) type).getComponentType(), acceptable); 253 break; 254 case INTERSECTION: 255 for (TypeMirror element : ((IntersectionType) type).getBounds()) { 256 verifyTypeAcceptable(element, acceptable); 257 } 258 break; 259 case TYPEVAR: 260 verifyTypeAcceptable(((TypeVariable) type).getLowerBound(), acceptable); 261 verifyTypeAcceptable(((TypeVariable) type).getUpperBound(), acceptable); 262 break; 263 case WILDCARD: 264 verifyTypeAcceptable(((WildcardType) type).getExtendsBound(), acceptable); 265 verifyTypeAcceptable(((WildcardType) type).getSuperBound(), acceptable); 266 break; 267 default: 268 error("Type not acceptable for this API: " + type.toString()); 269 break; 270 271 } 272 } 273 274 private TypeElement outermostTypeElement(Element el) { 275 while (el.getEnclosingElement().getKind() != ElementKind.PACKAGE) { 276 el = el.getEnclosingElement(); 277 } 278 279 return (TypeElement) el; 280 } 281 282 private void error(String text) { 283 processingEnv.getMessager().printMessage(Kind.ERROR, text); 284 } 285 286 private void note(String text) { 287 processingEnv.getMessager().printMessage(Kind.NOTE, text); 288 } 289 }