test/tools/javac/tree/NoPrivateTypesExported.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 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  */


  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.lang.model.util.ElementScanner8;
  55 import javax.lang.model.util.SimpleAnnotationValueVisitor8;
  56 import javax.tools.Diagnostic.Kind;
  57 
  58 public class NoPrivateTypesExported extends JavacTestingAbstractProcessor {
  59 
  60     private static final String[] javaxLangModelPackages = new String[] {
  61         "javax.lang.model",
  62         "javax.lang.model.element",
  63         "javax.lang.model.type",
  64         "javax.lang.model.util",
  65     };
  66 
  67     private static final Set<String> javaxLangModelAcceptable;
  68 
  69     private static final String[] javaxToolsProcessingPackages = new String[] {
  70         "javax.annotation.processing",
  71         "javax.tools",
  72     };
  73 
  74     private static final Set<String> javaxToolsProcessingAcceptable;
  75 


 114                            RoundEnvironment roundEnv) {
 115         if (roundEnv.processingOver()) {
 116             verifyPackage(javaxLangModelPackages, javaxLangModelAcceptable);
 117             verifyPackage(javaxToolsProcessingPackages, javaxToolsProcessingAcceptable);
 118             verifyPackage(comSunSourcePackages, comSunSourceAcceptable);
 119         }
 120         return true;
 121     }
 122 
 123     private void verifyPackage(String[] packagesToTest, Set<String> acceptable) {
 124         for (String packageToTest : packagesToTest) {
 125             PackageElement packageElement = processingEnv.getElementUtils()
 126                     .getPackageElement(packageToTest);
 127 
 128             verifyReferredTypesAcceptable(packageElement, acceptable);
 129         }
 130     }
 131 
 132     private void verifyReferredTypesAcceptable(Element rootElement,
 133                                                final Set<String> acceptable) {
 134         new ElementScanner8<Void, Void>() {
 135             @Override public Void visitType(TypeElement e, Void p) {
 136                 verifyTypeAcceptable(e.getSuperclass(), acceptable);
 137                 verifyTypesAcceptable(e.getInterfaces(), acceptable);
 138                 scan(e.getTypeParameters(), p);
 139                 scan(e.getEnclosedElements(), p);
 140                 verifyAnnotations(e.getAnnotationMirrors(), acceptable);
 141                 return null;
 142             }
 143             @Override public Void visitTypeParameter(TypeParameterElement e, Void p) {
 144                 verifyTypesAcceptable(e.getBounds(), acceptable);
 145                 scan(e.getEnclosedElements(), p);
 146                 verifyAnnotations(e.getAnnotationMirrors(), acceptable);
 147                 return null;
 148             }
 149             @Override public Void visitPackage(PackageElement e, Void p) {
 150                 scan(e.getEnclosedElements(), p);
 151                 verifyAnnotations(e.getAnnotationMirrors(), acceptable);
 152                 return null;
 153             }
 154             @Override public Void visitVariable(VariableElement e, Void p) {


 172 
 173     private void verifyAnnotations(Iterable<? extends AnnotationMirror> annotations,
 174                                    Set<String> acceptable) {
 175         for (AnnotationMirror mirror : annotations) {
 176             Element annotationElement = mirror.getAnnotationType().asElement();
 177 
 178             if (annotationElement.getAnnotation(Documented.class) == null) {
 179                 note("Ignoring undocumented annotation: " + mirror.getAnnotationType());
 180             }
 181 
 182             verifyTypeAcceptable(mirror.getAnnotationType(), acceptable);
 183 
 184             for (AnnotationValue value : mirror.getElementValues().values()) {
 185                 verifyAnnotationValue(value, acceptable);
 186             }
 187         }
 188     }
 189 
 190     private void verifyAnnotationValue(AnnotationValue value,
 191                                        final Set<String> acceptable) {
 192         value.accept(new SimpleAnnotationValueVisitor8<Void, Void>() {
 193             @Override public Void visitType(TypeMirror t, Void p) {
 194                 verifyTypeAcceptable(t, acceptable);
 195                 return null;
 196             }
 197             @Override
 198             public Void visitEnumConstant(VariableElement c, Void p) {
 199                 verifyReferredTypesAcceptable(c, acceptable);
 200                 return null;
 201             }
 202             @Override public Void visitArray(List<? extends AnnotationValue> vals,
 203                                              Void p) {
 204                 for (AnnotationValue val : vals) {
 205                     val.accept(this, p);
 206                 }
 207                 return null;
 208             }
 209             @Override protected Void defaultAction(Object o, Void p) {
 210                 error("Unexpected AnnotationValue: " + o.toString());
 211                 return super.defaultAction(o, p);
 212             }


   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  */


  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 


 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) {


 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             }