langtools/test/tools/javac/importscope/TestDuplicateImport.java
changeset 27857 7e913a535736
child 30730 d3ce7619db2c
equal deleted inserted replaced
27856:d4711a6931e2 27857:7e913a535736
       
     1 /*
       
     2  * Copyright (c) 2011-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 7101822
       
    27  * @summary static import fails to resolve interfaces on nested enums via import statements
       
    28  */
       
    29 
       
    30 import com.sun.source.util.JavacTask;
       
    31 import java.net.URI;
       
    32 import java.util.Arrays;
       
    33 import javax.tools.Diagnostic;
       
    34 import javax.tools.JavaCompiler;
       
    35 import javax.tools.JavaFileObject;
       
    36 import javax.tools.SimpleJavaFileObject;
       
    37 import javax.tools.StandardJavaFileManager;
       
    38 import javax.tools.ToolProvider;
       
    39 
       
    40 
       
    41 public class TestDuplicateImport {
       
    42 
       
    43     static int checkCount = 0;
       
    44 
       
    45     enum ImportKind {
       
    46         NORMAL("import a.#Q.#N;"),
       
    47         STATIC("import static a.#Q.#N;");
       
    48 
       
    49         String importStr;
       
    50 
       
    51         ImportKind(String importStr) {
       
    52             this.importStr = importStr;
       
    53         }
       
    54 
       
    55         String getImportStatement(QualifierKind qk, NameKind nk) {
       
    56             return importStr.replaceAll("#Q", qk.qualifierStr)
       
    57                     .replaceAll("#N", nk.nameStr);
       
    58         }
       
    59 
       
    60         boolean isStatic() {
       
    61             return this == STATIC;
       
    62         }
       
    63     }
       
    64 
       
    65     enum QualifierKind {
       
    66         A("A"),
       
    67         B("B"),
       
    68         C("C");
       
    69 
       
    70         String qualifierStr;
       
    71 
       
    72         QualifierKind(String qualifierStr) {
       
    73             this.qualifierStr = qualifierStr;
       
    74         }
       
    75 
       
    76         public boolean compatible(QualifierKind ik) {
       
    77             return this == ik || (this != A && ik != A);
       
    78         }
       
    79     }
       
    80 
       
    81     enum NameKind {
       
    82         D("D"),
       
    83         E("E"),
       
    84         M("m"),
       
    85         F("f"),
       
    86         STAR("*"),
       
    87         NON_EXISTENT("NonExistent");
       
    88 
       
    89         String nameStr;
       
    90 
       
    91         NameKind(String nameStr) {
       
    92             this.nameStr = nameStr;
       
    93         }
       
    94 
       
    95         boolean exists() {
       
    96             return this != NON_EXISTENT;
       
    97         }
       
    98 
       
    99         boolean isMember() {
       
   100             return this == M || this == F;
       
   101         }
       
   102 
       
   103         boolean isType() {
       
   104             return this == D || this == E;
       
   105         }
       
   106     }
       
   107 
       
   108     public static void main(String... args) throws Exception {
       
   109 
       
   110         //create default shared JavaCompiler - reused across multiple compilations
       
   111         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
       
   112         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
       
   113 
       
   114         for (ImportKind ik1 : ImportKind.values()) {
       
   115             for (ImportKind ik2 : ImportKind.values()) {
       
   116                 for (QualifierKind qk1 : QualifierKind.values()) {
       
   117                     for (QualifierKind qk2 : QualifierKind.values()) {
       
   118                         for (NameKind nk1 : NameKind.values()) {
       
   119                             for (NameKind nk2 : NameKind.values()) {
       
   120                                 new TestDuplicateImport(ik1, ik2, qk1, qk2, nk1, nk2).run(comp, fm);
       
   121                             }
       
   122                         }
       
   123                     }
       
   124                 }
       
   125             }
       
   126         }
       
   127         System.out.println("Total check executed: " + checkCount);
       
   128     }
       
   129 
       
   130     ImportKind ik1;
       
   131     ImportKind ik2;
       
   132     QualifierKind qk1;
       
   133     QualifierKind qk2;
       
   134     NameKind nk1;
       
   135     NameKind nk2;
       
   136     JavaSource source;
       
   137     DiagnosticChecker diagChecker;
       
   138 
       
   139     TestDuplicateImport(ImportKind ik1, ImportKind ik2, QualifierKind qk1, QualifierKind qk2, NameKind nk1, NameKind nk2) {
       
   140         this.ik1 = ik1;
       
   141         this.ik2 = ik2;
       
   142         this.qk1 = qk1;
       
   143         this.qk2 = qk2;
       
   144         this.nk1 = nk1;
       
   145         this.nk2 = nk2;
       
   146         this.source = new JavaSource();
       
   147         this.diagChecker = new DiagnosticChecker();
       
   148     }
       
   149     class JavaSource extends SimpleJavaFileObject {
       
   150 
       
   151         String bodyTemplate = "package a;\n" +
       
   152                               "#I1\n" +
       
   153                               "#I2\n" +
       
   154                               "class A {\n" +
       
   155                               "   static class D { }\n" +
       
   156                               "   static class E { }\n" +
       
   157                               "   static Object f;\n" +
       
   158                               "   static void m() { }\n" +
       
   159                               "}\n" +
       
   160                               "class B {\n" +
       
   161                               "   static class D { }\n" +
       
   162                               "   static class E { }\n" +
       
   163                               "   static Object f;\n" +
       
   164                               "   static void m() { }\n" +
       
   165                               "}\n" +
       
   166                               "class C extends B {\n" +
       
   167                               "}\n";
       
   168 
       
   169         String source;
       
   170 
       
   171         public JavaSource() {
       
   172             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
   173             source = bodyTemplate.replaceAll("#I1", ik1.getImportStatement(qk1, nk1))
       
   174                     .replaceAll("#I2", ik2.getImportStatement(qk2, nk2));
       
   175         }
       
   176 
       
   177         @Override
       
   178         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
   179             return source;
       
   180         }
       
   181     }
       
   182 
       
   183     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
       
   184         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
       
   185                 null, null, Arrays.asList(source));
       
   186         try {
       
   187             ct.analyze();
       
   188         } catch (Throwable ex) {
       
   189             throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true));
       
   190         }
       
   191         check();
       
   192     }
       
   193 
       
   194     void check() {
       
   195         checkCount++;
       
   196 
       
   197         boolean errorExpected = false;
       
   198 
       
   199         //error if the import refers to a non-existent symbol
       
   200         if (!nk1.exists() || !nk2.exists()) {
       
   201             errorExpected = true;
       
   202         }
       
   203 
       
   204         //error if a non-static import refers to a non-type symbol
       
   205         if ((nk1.isMember() && !ik1.isStatic()) ||
       
   206                 (nk2.isMember() && !ik2.isStatic())) {
       
   207             errorExpected = true;
       
   208         }
       
   209 
       
   210         //error if two single non-static (or one static and one non-static)
       
   211         //imports import same names from different places
       
   212         if (nk1 == nk2 && nk1 != NameKind.STAR && !qk1.compatible(qk2) &&
       
   213                 (!ik1.isStatic() || !ik2.isStatic())) {
       
   214             errorExpected = true;
       
   215         }
       
   216 
       
   217         if ((qk1 == QualifierKind.C && !ik1.isStatic() && nk1 != NameKind.STAR) ||
       
   218             (qk2 == QualifierKind.C && !ik2.isStatic() && nk2 != NameKind.STAR)) {
       
   219             errorExpected = true;
       
   220         }
       
   221 
       
   222         if (errorExpected != diagChecker.errorFound) {
       
   223             throw new Error("invalid diagnostics for source:\n" +
       
   224                 source.getCharContent(true) +
       
   225                 "\nFound error: " + diagChecker.errorFound +
       
   226                 "\nExpected error: " + errorExpected);
       
   227         }
       
   228     }
       
   229 
       
   230     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
       
   231 
       
   232         boolean errorFound;
       
   233 
       
   234         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   235             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
       
   236                 errorFound = true;
       
   237             }
       
   238         }
       
   239     }
       
   240 }