aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7017664 aoqi@0: * @summary Basher for CompoundScopes aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.tools.javac.code.Symbol; aoqi@0: import com.sun.tools.javac.code.Types; aoqi@0: import com.sun.tools.javac.file.JavacFileManager; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: aoqi@0: import com.sun.tools.javac.code.Symbol.*; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.List; aoqi@0: import javax.lang.model.element.Element; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: import static javax.tools.JavaFileObject.Kind; aoqi@0: aoqi@0: public class ImplementationCacheTest { aoqi@0: aoqi@0: static class SourceFile extends SimpleJavaFileObject { aoqi@0: aoqi@0: final String source = "interface I { void m(); }\n" + aoqi@0: "class A implements I { public void m() {} }\n" + aoqi@0: "class B extends A { }\n"; aoqi@0: aoqi@0: public SourceFile() { aoqi@0: super(URI.create("test.java"), Kind.SOURCE); aoqi@0: } aoqi@0: aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) throws IOException { aoqi@0: List files = Arrays.asList(new SourceFile()); aoqi@0: JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); aoqi@0: JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files); aoqi@0: Context ctx = new Context(); aoqi@0: JavacFileManager.preRegister(ctx); aoqi@0: checkImplementationCache(ct.analyze(), Types.instance(ctx)); aoqi@0: } aoqi@0: aoqi@0: static void checkImplementationCache(Iterable elements, Types types) { aoqi@0: if (types == null) { aoqi@0: throw new AssertionError("problems initializing Types"); aoqi@0: } aoqi@0: aoqi@0: Symbol a = null; aoqi@0: Symbol b = null; aoqi@0: Symbol i = null; aoqi@0: aoqi@0: for (Element e : elements) { aoqi@0: if (e.getSimpleName().contentEquals("A")) { aoqi@0: a = (Symbol)e; aoqi@0: } else if (e.getSimpleName().contentEquals("B")) { aoqi@0: b = (Symbol)e; aoqi@0: } else if (e.getSimpleName().contentEquals("I")) { aoqi@0: i = (Symbol)e; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (a == null || b == null || i == null) { aoqi@0: throw new AssertionError("missing class"); aoqi@0: } aoqi@0: aoqi@0: MethodSymbol I_m = null; aoqi@0: aoqi@0: for (Symbol sym : i.members().getElements()) { aoqi@0: if (sym.name.contentEquals("m")) { aoqi@0: I_m = (MethodSymbol)sym; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (I_m == null) { aoqi@0: throw new AssertionError("missing method m() in scope of interface I"); aoqi@0: } aoqi@0: aoqi@0: Symbol impl = I_m.implementation((TypeSymbol)b, types, true); aoqi@0: aoqi@0: if (impl == null || impl.owner != a) { aoqi@0: throw new AssertionError("wrong implementation for m() in B"); aoqi@0: } aoqi@0: aoqi@0: b.members().enter(I_m.clone(b)); aoqi@0: aoqi@0: Symbol newImpl = I_m.implementation((TypeSymbol)b, types, true); aoqi@0: aoqi@0: if (newImpl == impl) { aoqi@0: throw new AssertionError("stale implementation for m() in B"); aoqi@0: } aoqi@0: aoqi@0: if (newImpl == null || newImpl.owner != b) { aoqi@0: throw new AssertionError("wrong implementation for m() in B"); aoqi@0: } aoqi@0: } aoqi@0: }