TestLazyImportScope.java revision 2739:9d2192f36e53
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
30import com.sun.source.util.JavacTask;
31import java.net.URI;
32import java.util.Arrays;
33import javax.tools.Diagnostic;
34import javax.tools.JavaCompiler;
35import javax.tools.JavaFileObject;
36import javax.tools.SimpleJavaFileObject;
37import javax.tools.StandardJavaFileManager;
38import javax.tools.ToolProvider;
39
40public class TestLazyImportScope {
41
42    static int checkCount = 0;
43
44    enum ImportOrder {
45        NORMAL("import a.C.D;\n" +
46               "#I"),
47        REVERSE("#I\n" +
48               "import a.C.D;");
49
50        String importLayout;
51
52        ImportOrder(String importLayout) {
53            this.importLayout = importLayout;
54        }
55
56        String getImportString(ImportKind ik) {
57            return importLayout.replaceAll("#I", ik.importStr);
58        }
59    }
60
61    enum ImportKind {
62        NAMED("import a.A.B.E;"),
63        ON_DEMAND("import a.A.B.*;"),
64        STATIC_NAMED_TYPE("import static a.A.B.E;"),
65        STATIC_NAMED_MEMBER("import static a.A.B.bm;"),
66        STATIC_ON_DEMAND("import static a.A.B.*;");
67
68        String importStr;
69
70        private ImportKind(String importStr) {
71            this.importStr = importStr;
72        }
73    }
74
75    enum TypeRefKind {
76        NONE(""),
77        E("E e = null;"),
78        F("F f = null;"),
79        BOTH("E e = null; F f = null;");
80
81        String typeRefStr;
82
83        private TypeRefKind(String typeRefStr) {
84            this.typeRefStr = typeRefStr;
85        }
86
87        boolean isImported(ImportKind ik) {
88            switch (ik) {
89                case NAMED:
90                case STATIC_NAMED_TYPE: return this == NONE || this == E;
91                case ON_DEMAND:
92                case STATIC_ON_DEMAND: return true;
93                default: return this == NONE;
94            }
95        }
96    }
97
98    enum MemberRefKind {
99        NONE(""),
100        FIELD("Object o = bf;"),
101        METHOD("bm();"),
102        BOTH("Object o = bf; bm();");
103
104        String memberRefStr;
105
106        private MemberRefKind(String memberRefStr) {
107            this.memberRefStr = memberRefStr;
108        }
109
110        boolean isImported(ImportKind ik) {
111            switch (ik) {
112                case STATIC_NAMED_MEMBER: return this == NONE || this == METHOD;
113                case STATIC_ON_DEMAND: return true;
114                default: return this == NONE;
115            }
116        }
117    }
118
119    public static void main(String... args) throws Exception {
120
121        //create default shared JavaCompiler - reused across multiple compilations
122        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
123        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
124
125        for (ImportOrder ord : ImportOrder.values()) {
126            for (ImportKind ik : ImportKind.values()) {
127                for (TypeRefKind tk : TypeRefKind.values()) {
128                    for (MemberRefKind mk : MemberRefKind.values()) {
129                        new TestLazyImportScope(ord, ik, tk, mk).run(comp, fm);
130                    }
131                }
132            }
133        }
134        System.out.println("Total check executed: " + checkCount);
135    }
136
137    ImportOrder ord;
138    ImportKind ik;
139    TypeRefKind tk;
140    MemberRefKind mk;
141    JavaSource source;
142    DiagnosticChecker diagChecker;
143
144    TestLazyImportScope(ImportOrder ord, ImportKind ik, TypeRefKind tk, MemberRefKind mk) {
145        this.ord = ord;
146        this.ik = ik;
147        this.tk = tk;
148        this.mk = mk;
149        this.source = new JavaSource();
150        this.diagChecker = new DiagnosticChecker();
151    }
152
153    class JavaSource extends SimpleJavaFileObject {
154
155        String bodyTemplate = "package a;\n" +
156                              "#I\n" +
157                              "class A {\n" +
158                              "   static class B extends D {\n" +
159                              "      static class E { }\n" +
160                              "      static class F { }\n" +
161                              "      static Object bf;\n" +
162                              "      static void bm() { }\n" +
163                              "   }\n" +
164                              "}\n" +
165                              "class C {\n" +
166                              "   static class D { }\n" +
167                              "}\n" +
168                              "class Test {\n" +
169                              "   void test() {\n" +
170                              "      #T\n" +
171                              "      #M\n" +
172                              "   }\n" +
173                              "}";
174
175        String source;
176
177        public JavaSource() {
178            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
179            source = bodyTemplate.replaceAll("#I", ord.getImportString(ik))
180                    .replaceAll("#T", tk.typeRefStr)
181                    .replaceAll("#M", mk.memberRefStr);
182        }
183
184        @Override
185        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
186            return source;
187        }
188    }
189
190    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
191        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
192                null, null, Arrays.asList(source));
193        try {
194            ct.analyze();
195        } catch (Throwable ex) {
196            throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true));
197        }
198        check();
199    }
200
201    void check() {
202        checkCount++;
203
204        boolean errorExpected = !tk.isImported(ik) || !mk.isImported(ik);
205
206        if (errorExpected != diagChecker.errorFound) {
207            throw new Error("invalid diagnostics for source:\n" +
208                source.getCharContent(true) +
209                "\nFound error: " + diagChecker.errorFound +
210                "\nExpected error: " + errorExpected);
211        }
212    }
213
214    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
215
216        boolean errorFound;
217
218        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
219            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
220                errorFound = true;
221            }
222        }
223    }
224}
225