test/tools/javac/scope/7017664/ImplementationCacheTest.java

changeset 877
351027202f60
parent 0
959103a6100f
equal deleted inserted replaced
876:ef6c66215a93 877:351027202f60
1 /*
2 * Copyright (c) 2011, 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 7017664
27 * @summary Basher for CompoundScopes
28 */
29
30 import com.sun.source.util.JavacTask;
31 import com.sun.tools.javac.code.Symbol;
32 import com.sun.tools.javac.code.Types;
33 import com.sun.tools.javac.file.JavacFileManager;
34 import com.sun.tools.javac.util.Context;
35
36 import com.sun.tools.javac.code.Symbol.*;
37
38 import java.io.IOException;
39 import java.net.URI;
40 import java.util.Arrays;
41 import java.util.List;
42 import javax.lang.model.element.Element;
43 import javax.tools.JavaCompiler;
44 import javax.tools.JavaFileObject;
45 import javax.tools.SimpleJavaFileObject;
46 import javax.tools.ToolProvider;
47
48 import static javax.tools.JavaFileObject.Kind;
49
50 public class ImplementationCacheTest {
51
52 static class SourceFile extends SimpleJavaFileObject {
53
54 final String source = "interface I { void m(); }\n" +
55 "class A implements I { public void m() {} }\n" +
56 "class B extends A { }\n";
57
58 public SourceFile() {
59 super(URI.create("test.java"), Kind.SOURCE);
60 }
61
62 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
63 return source;
64 }
65 }
66
67 public static void main(String[] args) throws IOException {
68 List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
69 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
70 JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
71 Context ctx = new Context();
72 JavacFileManager.preRegister(ctx);
73 checkImplementationCache(ct.analyze(), Types.instance(ctx));
74 }
75
76 static void checkImplementationCache(Iterable<? extends Element> elements, Types types) {
77 if (types == null) {
78 throw new AssertionError("problems initializing Types");
79 }
80
81 Symbol a = null;
82 Symbol b = null;
83 Symbol i = null;
84
85 for (Element e : elements) {
86 if (e.getSimpleName().contentEquals("A")) {
87 a = (Symbol)e;
88 } else if (e.getSimpleName().contentEquals("B")) {
89 b = (Symbol)e;
90 } else if (e.getSimpleName().contentEquals("I")) {
91 i = (Symbol)e;
92 }
93 }
94
95 if (a == null || b == null || i == null) {
96 throw new AssertionError("missing class");
97 }
98
99 MethodSymbol I_m = null;
100
101 for (Symbol sym : i.members().getElements()) {
102 if (sym.name.contentEquals("m")) {
103 I_m = (MethodSymbol)sym;
104 }
105 }
106
107 if (I_m == null) {
108 throw new AssertionError("missing method m() in scope of interface I");
109 }
110
111 Symbol impl = I_m.implementation((TypeSymbol)b, types, true);
112
113 if (impl == null || impl.owner != a) {
114 throw new AssertionError("wrong implementation for m() in B");
115 }
116
117 b.members().enter(I_m.clone(b));
118
119 Symbol newImpl = I_m.implementation((TypeSymbol)b, types, true);
120
121 if (newImpl == impl) {
122 throw new AssertionError("stale implementation for m() in B");
123 }
124
125 if (newImpl == null || newImpl.owner != b) {
126 throw new AssertionError("wrong implementation for m() in B");
127 }
128 }
129 }

mercurial