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

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 0
959103a6100f
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7017664
aoqi@0 27 * @summary Basher for CompoundScopes
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import com.sun.source.util.JavacTask;
aoqi@0 31 import com.sun.tools.javac.code.Symbol;
aoqi@0 32 import com.sun.tools.javac.code.Types;
aoqi@0 33 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 34 import com.sun.tools.javac.util.Context;
aoqi@0 35
aoqi@0 36 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 37
aoqi@0 38 import java.io.IOException;
aoqi@0 39 import java.net.URI;
aoqi@0 40 import java.util.Arrays;
aoqi@0 41 import java.util.List;
aoqi@0 42 import javax.lang.model.element.Element;
aoqi@0 43 import javax.tools.JavaCompiler;
aoqi@0 44 import javax.tools.JavaFileObject;
aoqi@0 45 import javax.tools.SimpleJavaFileObject;
aoqi@0 46 import javax.tools.ToolProvider;
aoqi@0 47
aoqi@0 48 import static javax.tools.JavaFileObject.Kind;
aoqi@0 49
aoqi@0 50 public class ImplementationCacheTest {
aoqi@0 51
aoqi@0 52 static class SourceFile extends SimpleJavaFileObject {
aoqi@0 53
aoqi@0 54 final String source = "interface I { void m(); }\n" +
aoqi@0 55 "class A implements I { public void m() {} }\n" +
aoqi@0 56 "class B extends A { }\n";
aoqi@0 57
aoqi@0 58 public SourceFile() {
aoqi@0 59 super(URI.create("test.java"), Kind.SOURCE);
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 63 return source;
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 public static void main(String[] args) throws IOException {
aoqi@0 68 List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
aoqi@0 69 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
aoqi@0 70 JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
aoqi@0 71 Context ctx = new Context();
aoqi@0 72 JavacFileManager.preRegister(ctx);
aoqi@0 73 checkImplementationCache(ct.analyze(), Types.instance(ctx));
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 static void checkImplementationCache(Iterable<? extends Element> elements, Types types) {
aoqi@0 77 if (types == null) {
aoqi@0 78 throw new AssertionError("problems initializing Types");
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 Symbol a = null;
aoqi@0 82 Symbol b = null;
aoqi@0 83 Symbol i = null;
aoqi@0 84
aoqi@0 85 for (Element e : elements) {
aoqi@0 86 if (e.getSimpleName().contentEquals("A")) {
aoqi@0 87 a = (Symbol)e;
aoqi@0 88 } else if (e.getSimpleName().contentEquals("B")) {
aoqi@0 89 b = (Symbol)e;
aoqi@0 90 } else if (e.getSimpleName().contentEquals("I")) {
aoqi@0 91 i = (Symbol)e;
aoqi@0 92 }
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 if (a == null || b == null || i == null) {
aoqi@0 96 throw new AssertionError("missing class");
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 MethodSymbol I_m = null;
aoqi@0 100
aoqi@0 101 for (Symbol sym : i.members().getElements()) {
aoqi@0 102 if (sym.name.contentEquals("m")) {
aoqi@0 103 I_m = (MethodSymbol)sym;
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 if (I_m == null) {
aoqi@0 108 throw new AssertionError("missing method m() in scope of interface I");
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 Symbol impl = I_m.implementation((TypeSymbol)b, types, true);
aoqi@0 112
aoqi@0 113 if (impl == null || impl.owner != a) {
aoqi@0 114 throw new AssertionError("wrong implementation for m() in B");
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 b.members().enter(I_m.clone(b));
aoqi@0 118
aoqi@0 119 Symbol newImpl = I_m.implementation((TypeSymbol)b, types, true);
aoqi@0 120
aoqi@0 121 if (newImpl == impl) {
aoqi@0 122 throw new AssertionError("stale implementation for m() in B");
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 if (newImpl == null || newImpl.owner != b) {
aoqi@0 126 throw new AssertionError("wrong implementation for m() in B");
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129 }

mercurial