test/langtools/tools/javac/processing/rounds/ValidTypesAreKept.java
changeset 50867 e84038f37713
equal deleted inserted replaced
50866:cb4b07d61aac 50867:e84038f37713
       
     1 /*
       
     2  * Copyright (c) 2018, 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 8195986
       
    27  * @summary Verify the type of String does not change across annotation processing rounds
       
    28  * @library /tools/lib
       
    29  * @modules
       
    30  *      jdk.compiler/com.sun.tools.javac.api
       
    31  *      jdk.compiler/com.sun.tools.javac.main
       
    32  * @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
       
    33  * @run main ValidTypesAreKept
       
    34  */
       
    35 
       
    36 import java.nio.file.Files;
       
    37 import java.nio.file.Path;
       
    38 import java.nio.file.Paths;
       
    39 import java.util.Set;
       
    40 
       
    41 import javax.annotation.processing.AbstractProcessor;
       
    42 import javax.annotation.processing.RoundEnvironment;
       
    43 import javax.annotation.processing.SupportedAnnotationTypes;
       
    44 import javax.lang.model.SourceVersion;
       
    45 import javax.lang.model.element.TypeElement;
       
    46 import javax.lang.model.type.TypeMirror;
       
    47 
       
    48 import toolbox.JavacTask;
       
    49 import toolbox.Task;
       
    50 import toolbox.TestRunner;
       
    51 import toolbox.ToolBox;
       
    52 
       
    53 public class ValidTypesAreKept extends TestRunner {
       
    54 
       
    55     private ToolBox tb;
       
    56 
       
    57     public ValidTypesAreKept() {
       
    58         super(System.err);
       
    59         tb = new ToolBox();
       
    60     }
       
    61 
       
    62     public static void main(String... args) throws Exception {
       
    63         new ValidTypesAreKept().runTests();
       
    64     }
       
    65 
       
    66     @Test
       
    67     public void testBrokenErrors() throws Exception {
       
    68         Path base = Paths.get(".");
       
    69         Path src = base.resolve("src");
       
    70         Path classes = base.resolve("classes");
       
    71 
       
    72         Files.createDirectories(classes);
       
    73 
       
    74         tb.writeJavaFiles(src,
       
    75                           "package t; @A(B.E) public @interface A { B value(); }",
       
    76                           "package t; public enum B { E; }");
       
    77 
       
    78         new JavacTask(tb)
       
    79                 .outdir(classes)
       
    80                 .files(tb.findJavaFiles(src))
       
    81                 .run()
       
    82                 .writeAll()
       
    83                 .getOutput(Task.OutputKind.DIRECT);
       
    84 
       
    85         Files.delete(classes.resolve("t").resolve("B.class"));
       
    86 
       
    87         Path src2 = base.resolve("src2");
       
    88         Path classes2 = base.resolve("classes2");
       
    89 
       
    90         Files.createDirectories(classes2);
       
    91 
       
    92         Path output2 = base.resolve("output2");
       
    93 
       
    94         Files.createDirectories(output2);
       
    95 
       
    96         tb.writeJavaFiles(src2,
       
    97                           "package t2; @t.A class C { public native void t(String str); }");
       
    98 
       
    99         new JavacTask(tb)
       
   100                 .classpath(classes)
       
   101                 .options("-processor", AP.class.getName(),
       
   102                          "--processor-path", System.getProperty("test.classes"),
       
   103                          "-h", output2.toString())
       
   104                 .outdir(classes2)
       
   105                 .files(tb.findJavaFiles(src2))
       
   106                 .run()
       
   107                 .writeAll()
       
   108                 .getOutput(Task.OutputKind.DIRECT);
       
   109 
       
   110         String content = new String(Files.readAllBytes(output2.resolve("t2_C.h")));
       
   111 
       
   112         if (!content.contains("(JNIEnv *, jobject, jstring)")) {
       
   113             fail("incorrect header file content: " + content);
       
   114         }
       
   115     }
       
   116 
       
   117     @SupportedAnnotationTypes("*")
       
   118     public static final class AP extends AbstractProcessor {
       
   119 
       
   120         private TypeMirror stringType;
       
   121 
       
   122         @Override
       
   123         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       
   124             TypeElement str = processingEnv.getElementUtils().getTypeElement("java.lang.String");
       
   125             if (str == null) {
       
   126                 throw new AssertionError("java.lang.String");
       
   127             }
       
   128             if (stringType == null) {
       
   129                 stringType = str.asType();
       
   130             } else {
       
   131                 if (stringType != str.asType()) {
       
   132                     throw new AssertionError("The type of j.l.String changed.");
       
   133                 }
       
   134             }
       
   135 
       
   136             return false;
       
   137         }
       
   138 
       
   139         @Override
       
   140         public SourceVersion getSupportedSourceVersion() {
       
   141             return SourceVersion.latest();
       
   142         }
       
   143 
       
   144     }
       
   145 
       
   146     private static void assertNonNull(String msg, Object val) {
       
   147         if (val == null) {
       
   148             throw new AssertionError(msg);
       
   149         }
       
   150     }
       
   151 
       
   152     static Path resolveFile(Path base, String... pathElements) {
       
   153         Path file = base;
       
   154 
       
   155         for (String el : pathElements) {
       
   156             file = file.resolve(el);
       
   157         }
       
   158 
       
   159         return file;
       
   160     }
       
   161 
       
   162     private static void fail(String msg) {
       
   163         throw new AssertionError(msg);
       
   164     }
       
   165 
       
   166 }