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