langtools/test/tools/javac/importscope/TestDuplicateImport.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 30730 d3ce7619db2c
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     1
/*
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 30730
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     4
 *
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     8
 *
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    14
 *
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    18
 *
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    21
 * questions.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    22
 */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    23
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    24
/*
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    25
 * @test
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    26
 * @bug 7101822
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    27
 * @summary static import fails to resolve interfaces on nested enums via import statements
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    28
 * @modules jdk.compiler
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    29
 */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    30
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    31
import com.sun.source.util.JavacTask;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    32
import java.net.URI;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    33
import java.util.Arrays;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    34
import javax.tools.Diagnostic;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    35
import javax.tools.JavaCompiler;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    36
import javax.tools.JavaFileObject;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    37
import javax.tools.SimpleJavaFileObject;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    38
import javax.tools.StandardJavaFileManager;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    39
import javax.tools.ToolProvider;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    40
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    41
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    42
public class TestDuplicateImport {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    43
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    44
    static int checkCount = 0;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    45
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    46
    enum ImportKind {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    47
        NORMAL("import a.#Q.#N;"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    48
        STATIC("import static a.#Q.#N;");
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    49
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    50
        String importStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    51
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    52
        ImportKind(String importStr) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    53
            this.importStr = importStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    54
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    55
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    56
        String getImportStatement(QualifierKind qk, NameKind nk) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    57
            return importStr.replaceAll("#Q", qk.qualifierStr)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    58
                    .replaceAll("#N", nk.nameStr);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    59
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    60
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    61
        boolean isStatic() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    62
            return this == STATIC;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    63
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    64
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    65
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    66
    enum QualifierKind {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    67
        A("A"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    68
        B("B"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    69
        C("C");
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    70
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    71
        String qualifierStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    72
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    73
        QualifierKind(String qualifierStr) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    74
            this.qualifierStr = qualifierStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    75
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    76
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    77
        public boolean compatible(QualifierKind ik) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    78
            return this == ik || (this != A && ik != A);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    79
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    80
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    81
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    82
    enum NameKind {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    83
        D("D"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    84
        E("E"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    85
        M("m"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    86
        F("f"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    87
        STAR("*"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    88
        NON_EXISTENT("NonExistent");
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    89
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    90
        String nameStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    91
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    92
        NameKind(String nameStr) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    93
            this.nameStr = nameStr;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    94
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    95
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    96
        boolean exists() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    97
            return this != NON_EXISTENT;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    98
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
    99
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   100
        boolean isMember() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   101
            return this == M || this == F;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   102
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   103
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   104
        boolean isType() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   105
            return this == D || this == E;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   106
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   107
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   108
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   109
    public static void main(String... args) throws Exception {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   110
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   111
        //create default shared JavaCompiler - reused across multiple compilations
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   112
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   113
        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   114
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   115
        for (ImportKind ik1 : ImportKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   116
            for (ImportKind ik2 : ImportKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   117
                for (QualifierKind qk1 : QualifierKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   118
                    for (QualifierKind qk2 : QualifierKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   119
                        for (NameKind nk1 : NameKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   120
                            for (NameKind nk2 : NameKind.values()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   121
                                new TestDuplicateImport(ik1, ik2, qk1, qk2, nk1, nk2).run(comp, fm);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   122
                            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   123
                        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   124
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   125
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   126
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   127
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   128
        System.out.println("Total check executed: " + checkCount);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   129
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   130
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   131
    ImportKind ik1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   132
    ImportKind ik2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   133
    QualifierKind qk1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   134
    QualifierKind qk2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   135
    NameKind nk1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   136
    NameKind nk2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   137
    JavaSource source;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   138
    DiagnosticChecker diagChecker;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   139
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   140
    TestDuplicateImport(ImportKind ik1, ImportKind ik2, QualifierKind qk1, QualifierKind qk2, NameKind nk1, NameKind nk2) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   141
        this.ik1 = ik1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   142
        this.ik2 = ik2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   143
        this.qk1 = qk1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   144
        this.qk2 = qk2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   145
        this.nk1 = nk1;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   146
        this.nk2 = nk2;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   147
        this.source = new JavaSource();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   148
        this.diagChecker = new DiagnosticChecker();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   149
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   150
    class JavaSource extends SimpleJavaFileObject {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   151
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   152
        String bodyTemplate = "package a;\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   153
                              "#I1\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   154
                              "#I2\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   155
                              "class A {\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   156
                              "   static class D { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   157
                              "   static class E { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   158
                              "   static Object f;\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   159
                              "   static void m() { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   160
                              "}\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   161
                              "class B {\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   162
                              "   static class D { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   163
                              "   static class E { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   164
                              "   static Object f;\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   165
                              "   static void m() { }\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   166
                              "}\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   167
                              "class C extends B {\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   168
                              "}\n";
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   169
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   170
        String source;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   171
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   172
        public JavaSource() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   173
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   174
            source = bodyTemplate.replaceAll("#I1", ik1.getImportStatement(qk1, nk1))
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   175
                    .replaceAll("#I2", ik2.getImportStatement(qk2, nk2));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   176
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   177
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   178
        @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   179
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   180
            return source;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   181
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   182
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   183
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   184
    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   185
        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   186
                null, null, Arrays.asList(source));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   187
        try {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   188
            ct.analyze();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   189
        } catch (Throwable ex) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   190
            throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   191
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   192
        check();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   193
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   194
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   195
    void check() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   196
        checkCount++;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   197
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   198
        boolean errorExpected = false;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   199
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   200
        //error if the import refers to a non-existent symbol
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   201
        if (!nk1.exists() || !nk2.exists()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   202
            errorExpected = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   203
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   204
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   205
        //error if a non-static import refers to a non-type symbol
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   206
        if ((nk1.isMember() && !ik1.isStatic()) ||
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   207
                (nk2.isMember() && !ik2.isStatic())) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   208
            errorExpected = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   209
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   210
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   211
        //error if two single non-static (or one static and one non-static)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   212
        //imports import same names from different places
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   213
        if (nk1 == nk2 && nk1 != NameKind.STAR && !qk1.compatible(qk2) &&
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   214
                (!ik1.isStatic() || !ik2.isStatic())) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   215
            errorExpected = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   216
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   217
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   218
        if ((qk1 == QualifierKind.C && !ik1.isStatic() && nk1 != NameKind.STAR) ||
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   219
            (qk2 == QualifierKind.C && !ik2.isStatic() && nk2 != NameKind.STAR)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   220
            errorExpected = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   221
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   222
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   223
        if (errorExpected != diagChecker.errorFound) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   224
            throw new Error("invalid diagnostics for source:\n" +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   225
                source.getCharContent(true) +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   226
                "\nFound error: " + diagChecker.errorFound +
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   227
                "\nExpected error: " + errorExpected);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   228
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   229
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   230
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   231
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   232
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   233
        boolean errorFound;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   234
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   235
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   236
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   237
                errorFound = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   238
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   239
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   240
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents:
diff changeset
   241
}