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

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/scope/7017664/ImplementationCacheTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7017664
    1.30 + * @summary Basher for CompoundScopes
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import com.sun.tools.javac.code.Symbol;
    1.35 +import com.sun.tools.javac.code.Types;
    1.36 +import com.sun.tools.javac.file.JavacFileManager;
    1.37 +import com.sun.tools.javac.util.Context;
    1.38 +
    1.39 +import com.sun.tools.javac.code.Symbol.*;
    1.40 +
    1.41 +import java.io.IOException;
    1.42 +import java.net.URI;
    1.43 +import java.util.Arrays;
    1.44 +import java.util.List;
    1.45 +import javax.lang.model.element.Element;
    1.46 +import javax.tools.JavaCompiler;
    1.47 +import javax.tools.JavaFileObject;
    1.48 +import javax.tools.SimpleJavaFileObject;
    1.49 +import javax.tools.ToolProvider;
    1.50 +
    1.51 +import static javax.tools.JavaFileObject.Kind;
    1.52 +
    1.53 +public class ImplementationCacheTest {
    1.54 +
    1.55 +    static class SourceFile extends SimpleJavaFileObject {
    1.56 +
    1.57 +        final String source = "interface I { void m(); }\n" +
    1.58 +                              "class A implements I { public void m() {} }\n" +
    1.59 +                              "class B extends A { }\n";
    1.60 +
    1.61 +        public SourceFile() {
    1.62 +            super(URI.create("test.java"), Kind.SOURCE);
    1.63 +        }
    1.64 +
    1.65 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    1.66 +            return source;
    1.67 +        }
    1.68 +    }
    1.69 +
    1.70 +    public static void main(String[] args) throws IOException {
    1.71 +        List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
    1.72 +        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    1.73 +        JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    1.74 +        Context ctx = new Context();
    1.75 +        JavacFileManager.preRegister(ctx);
    1.76 +        checkImplementationCache(ct.analyze(), Types.instance(ctx));
    1.77 +    }
    1.78 +
    1.79 +    static void checkImplementationCache(Iterable<? extends Element> elements, Types types) {
    1.80 +        if (types == null) {
    1.81 +            throw new AssertionError("problems initializing Types");
    1.82 +        }
    1.83 +
    1.84 +        Symbol a = null;
    1.85 +        Symbol b = null;
    1.86 +        Symbol i = null;
    1.87 +
    1.88 +        for (Element e : elements) {
    1.89 +            if (e.getSimpleName().contentEquals("A")) {
    1.90 +                a = (Symbol)e;
    1.91 +            } else if (e.getSimpleName().contentEquals("B")) {
    1.92 +                b = (Symbol)e;
    1.93 +            } else if (e.getSimpleName().contentEquals("I")) {
    1.94 +                i = (Symbol)e;
    1.95 +            }
    1.96 +        }
    1.97 +
    1.98 +        if (a == null || b == null || i == null) {
    1.99 +            throw new AssertionError("missing class");
   1.100 +        }
   1.101 +
   1.102 +        MethodSymbol I_m = null;
   1.103 +
   1.104 +        for (Symbol sym : i.members().getElements()) {
   1.105 +            if (sym.name.contentEquals("m")) {
   1.106 +                I_m = (MethodSymbol)sym;
   1.107 +            }
   1.108 +        }
   1.109 +
   1.110 +        if (I_m == null) {
   1.111 +            throw new AssertionError("missing method m() in scope of interface I");
   1.112 +        }
   1.113 +
   1.114 +        Symbol impl = I_m.implementation((TypeSymbol)b, types, true);
   1.115 +
   1.116 +        if (impl == null || impl.owner != a) {
   1.117 +            throw new AssertionError("wrong implementation for m() in B");
   1.118 +        }
   1.119 +
   1.120 +        b.members().enter(I_m.clone(b));
   1.121 +
   1.122 +        Symbol newImpl = I_m.implementation((TypeSymbol)b, types, true);
   1.123 +
   1.124 +        if (newImpl == impl) {
   1.125 +            throw new AssertionError("stale implementation for m() in B");
   1.126 +        }
   1.127 +
   1.128 +        if (newImpl == null || newImpl.owner != b) {
   1.129 +            throw new AssertionError("wrong implementation for m() in B");
   1.130 +        }
   1.131 +    }
   1.132 +}

mercurial