test/tools/javac/defaultMethods/super/TestDefaultSuperCall.java

changeset 1393
d7d932236fee
child 1415
01c9d4161882
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/defaultMethods/super/TestDefaultSuperCall.java	Sun Nov 04 10:59:42 2012 +0000
     1.3 @@ -0,0 +1,406 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 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 + * @summary Automatic test for checking correctness of default super/this resolution
    1.30 + */
    1.31 +
    1.32 +import com.sun.source.util.JavacTask;
    1.33 +import java.net.URI;
    1.34 +import java.util.Arrays;
    1.35 +import java.util.ArrayList;
    1.36 +import java.util.List;
    1.37 +import java.util.Locale;
    1.38 +import java.util.Map;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.JavaCompiler;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.SimpleJavaFileObject;
    1.43 +import javax.tools.StandardJavaFileManager;
    1.44 +import javax.tools.ToolProvider;
    1.45 +
    1.46 +public class TestDefaultSuperCall {
    1.47 +
    1.48 +    static int checkCount = 0;
    1.49 +
    1.50 +    enum InterfaceKind {
    1.51 +        DEFAULT("interface A extends B { default void m() { } }"),
    1.52 +        ABSTRACT("interface A extends B { void m(); }"),
    1.53 +        NONE("interface A extends B { }");
    1.54 +
    1.55 +        String interfaceStr;
    1.56 +
    1.57 +        InterfaceKind(String interfaceStr) {
    1.58 +            this.interfaceStr = interfaceStr;
    1.59 +        }
    1.60 +
    1.61 +        boolean methodDefined() {
    1.62 +            return this == DEFAULT;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    enum PruneKind {
    1.67 +        NO_PRUNE("interface C { }"),
    1.68 +        PRUNE("interface C extends A { }");
    1.69 +
    1.70 +        boolean methodDefined(InterfaceKind ik) {
    1.71 +            return this == PRUNE &&
    1.72 +                    ik.methodDefined();
    1.73 +        }
    1.74 +
    1.75 +        String interfaceStr;
    1.76 +
    1.77 +        PruneKind(String interfaceStr) {
    1.78 +            this.interfaceStr = interfaceStr;
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +    enum QualifierKind {
    1.83 +        DIRECT_1("C"),
    1.84 +        DIRECT_2("A"),
    1.85 +        INDIRECT("B"),
    1.86 +        UNRELATED("E"),
    1.87 +        ENCLOSING_1(null),
    1.88 +        ENCLOSING_2(null);
    1.89 +
    1.90 +        String qualifierStr;
    1.91 +
    1.92 +        QualifierKind(String qualifierStr) {
    1.93 +            this.qualifierStr = qualifierStr;
    1.94 +        }
    1.95 +
    1.96 +        String getQualifier(Shape sh) {
    1.97 +            switch (this) {
    1.98 +                case ENCLOSING_1: return sh.enclosingAt(0);
    1.99 +                case ENCLOSING_2: return sh.enclosingAt(1);
   1.100 +                default:
   1.101 +                    return qualifierStr;
   1.102 +            }
   1.103 +        }
   1.104 +
   1.105 +        boolean isEnclosing() {
   1.106 +            return this == ENCLOSING_1 ||
   1.107 +                    this == ENCLOSING_2;
   1.108 +        }
   1.109 +
   1.110 +        boolean allowSuperCall(InterfaceKind ik, PruneKind pk) {
   1.111 +            switch (this) {
   1.112 +                case DIRECT_1:
   1.113 +                    return pk.methodDefined(ik);
   1.114 +                case DIRECT_2:
   1.115 +                    return ik.methodDefined() && pk == PruneKind.NO_PRUNE;
   1.116 +                default:
   1.117 +                    return false;
   1.118 +            }
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    enum ExprKind {
   1.123 +        THIS("this"),
   1.124 +        SUPER("super");
   1.125 +
   1.126 +        String exprStr;
   1.127 +
   1.128 +        ExprKind(String exprStr) {
   1.129 +            this.exprStr = exprStr;
   1.130 +        }
   1.131 +    }
   1.132 +
   1.133 +    enum ElementKind {
   1.134 +        INTERFACE("interface #N { #B }", true),
   1.135 +        INTERFACE_EXTENDS("interface #N extends A, C { #B }", true),
   1.136 +        CLASS("class #N { #B }", false),
   1.137 +        CLASS_EXTENDS("abstract class #N implements A, C { #B }", false),
   1.138 +        STATIC_CLASS("static class #N { #B }", true),
   1.139 +        STATIC_CLASS_EXTENDS("abstract static class #N implements A, C { #B }", true),
   1.140 +        ANON_CLASS("new Object() { #B };", false),
   1.141 +        METHOD("void test() { #B }", false),
   1.142 +        STATIC_METHOD("static void test() { #B }", true),
   1.143 +        DEFAULT_METHOD("default void test() { #B }", false);
   1.144 +
   1.145 +        String templateDecl;
   1.146 +        boolean isStatic;
   1.147 +
   1.148 +        ElementKind(String templateDecl, boolean isStatic) {
   1.149 +            this.templateDecl = templateDecl;
   1.150 +            this.isStatic = isStatic;
   1.151 +        }
   1.152 +
   1.153 +        boolean isClassDecl() {
   1.154 +            switch(this) {
   1.155 +                case METHOD:
   1.156 +                case STATIC_METHOD:
   1.157 +                case DEFAULT_METHOD:
   1.158 +                    return false;
   1.159 +                default:
   1.160 +                    return true;
   1.161 +            }
   1.162 +        }
   1.163 +
   1.164 +        boolean isAllowedEnclosing(ElementKind ek, boolean isTop) {
   1.165 +            switch (this) {
   1.166 +                case CLASS:
   1.167 +                case CLASS_EXTENDS:
   1.168 +                    //class is implicitly static inside interface, so skip this combo
   1.169 +                    return ek.isClassDecl() &&
   1.170 +                            ek != INTERFACE && ek != INTERFACE_EXTENDS;
   1.171 +                case ANON_CLASS:
   1.172 +                    return !ek.isClassDecl();
   1.173 +                case METHOD:
   1.174 +                    return ek == CLASS || ek == CLASS_EXTENDS ||
   1.175 +                            ek == STATIC_CLASS || ek == STATIC_CLASS_EXTENDS ||
   1.176 +                            ek == ANON_CLASS;
   1.177 +                case INTERFACE:
   1.178 +                case INTERFACE_EXTENDS:
   1.179 +                case STATIC_CLASS:
   1.180 +                case STATIC_CLASS_EXTENDS:
   1.181 +                case STATIC_METHOD:
   1.182 +                    return (isTop && (ek == CLASS || ek == CLASS_EXTENDS)) ||
   1.183 +                            ek == STATIC_CLASS || ek == STATIC_CLASS_EXTENDS;
   1.184 +                case DEFAULT_METHOD:
   1.185 +                    return ek == INTERFACE || ek == INTERFACE_EXTENDS;
   1.186 +                default:
   1.187 +                    throw new AssertionError("Bad enclosing element kind" + this);
   1.188 +            }
   1.189 +        }
   1.190 +
   1.191 +        boolean isAllowedTop() {
   1.192 +            switch (this) {
   1.193 +                case CLASS:
   1.194 +                case CLASS_EXTENDS:
   1.195 +                case INTERFACE:
   1.196 +                case INTERFACE_EXTENDS:
   1.197 +                    return true;
   1.198 +                default:
   1.199 +                    return false;
   1.200 +            }
   1.201 +        }
   1.202 +
   1.203 +        boolean hasSuper() {
   1.204 +            return this == INTERFACE_EXTENDS ||
   1.205 +                    this == STATIC_CLASS_EXTENDS ||
   1.206 +                    this == CLASS_EXTENDS;
   1.207 +        }
   1.208 +    }
   1.209 +
   1.210 +    static class Shape {
   1.211 +
   1.212 +        String shapeStr;
   1.213 +        List<ElementKind> enclosingElements;
   1.214 +        List<String> enclosingNames;
   1.215 +        List<String> elementsWithMethod;
   1.216 +
   1.217 +        Shape(ElementKind... elements) {
   1.218 +            System.err.println("elements = " + Arrays.toString(elements));
   1.219 +            enclosingElements = new ArrayList<>();
   1.220 +            enclosingNames = new ArrayList<>();
   1.221 +            elementsWithMethod = new ArrayList<>();
   1.222 +            int count = 0;
   1.223 +            String prevName = null;
   1.224 +            for (ElementKind ek : elements) {
   1.225 +                String name = "name"+count++;
   1.226 +                if (ek.isStatic) {
   1.227 +                    enclosingElements = new ArrayList<>();
   1.228 +                    enclosingNames = new ArrayList<>();
   1.229 +                }
   1.230 +                if (ek.isClassDecl()) {
   1.231 +                    enclosingElements.add(ek);
   1.232 +                    enclosingNames.add(name);
   1.233 +                } else {
   1.234 +                    elementsWithMethod.add(prevName);
   1.235 +                }
   1.236 +                String element = ek.templateDecl.replaceAll("#N", name);
   1.237 +                shapeStr = shapeStr == null ? element : shapeStr.replaceAll("#B", element);
   1.238 +                prevName = name;
   1.239 +            }
   1.240 +        }
   1.241 +
   1.242 +        String getShape(QualifierKind qk, ExprKind ek) {
   1.243 +            String methName = ek == ExprKind.THIS ? "test" : "m";
   1.244 +            String call = qk.getQualifier(this) + "." + ek.exprStr + "." + methName + "();";
   1.245 +            return shapeStr.replaceAll("#B", call);
   1.246 +        }
   1.247 +
   1.248 +        String enclosingAt(int index) {
   1.249 +            return index < enclosingNames.size() ? enclosingNames.get(index) : "BAD";
   1.250 +        }
   1.251 +    }
   1.252 +
   1.253 +    public static void main(String... args) throws Exception {
   1.254 +
   1.255 +        //create default shared JavaCompiler - reused across multiple compilations
   1.256 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.257 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.258 +
   1.259 +        for (InterfaceKind ik : InterfaceKind.values()) {
   1.260 +            for (PruneKind pk : PruneKind.values()) {
   1.261 +                for (ElementKind ek1 : ElementKind.values()) {
   1.262 +                    if (!ek1.isAllowedTop()) continue;
   1.263 +                    for (ElementKind ek2 : ElementKind.values()) {
   1.264 +                        if (!ek2.isAllowedEnclosing(ek1, true)) continue;
   1.265 +                        for (ElementKind ek3 : ElementKind.values()) {
   1.266 +                            if (!ek3.isAllowedEnclosing(ek2, false)) continue;
   1.267 +                            for (ElementKind ek4 : ElementKind.values()) {
   1.268 +                                if (!ek4.isAllowedEnclosing(ek3, false)) continue;
   1.269 +                                for (ElementKind ek5 : ElementKind.values()) {
   1.270 +                                    if (!ek5.isAllowedEnclosing(ek4, false) || ek5.isClassDecl()) continue;
   1.271 +                                    for (QualifierKind qk : QualifierKind.values()) {
   1.272 +                                        for (ExprKind ek : ExprKind.values()) {
   1.273 +                                            new TestDefaultSuperCall(ik, pk, new Shape(ek1, ek2, ek3, ek4, ek5), qk, ek).run(comp, fm);
   1.274 +                                        }
   1.275 +                                    }
   1.276 +                                }
   1.277 +                            }
   1.278 +                        }
   1.279 +                    }
   1.280 +                }
   1.281 +            }
   1.282 +        }
   1.283 +        System.out.println("Total check executed: " + checkCount);
   1.284 +    }
   1.285 +
   1.286 +    InterfaceKind ik;
   1.287 +    PruneKind pk;
   1.288 +    Shape sh;
   1.289 +    QualifierKind qk;
   1.290 +    ExprKind ek;
   1.291 +    JavaSource source;
   1.292 +    DiagnosticChecker diagChecker;
   1.293 +
   1.294 +    TestDefaultSuperCall(InterfaceKind ik, PruneKind pk, Shape sh, QualifierKind qk, ExprKind ek) {
   1.295 +        this.ik = ik;
   1.296 +        this.pk = pk;
   1.297 +        this.sh = sh;
   1.298 +        this.qk = qk;
   1.299 +        this.ek = ek;
   1.300 +        this.source = new JavaSource();
   1.301 +        this.diagChecker = new DiagnosticChecker();
   1.302 +    }
   1.303 +
   1.304 +    class JavaSource extends SimpleJavaFileObject {
   1.305 +
   1.306 +        String template = "interface E {}\n" +
   1.307 +                          "interface B { }\n" +
   1.308 +                          "#I\n" +
   1.309 +                          "#P\n" +
   1.310 +                          "#C";
   1.311 +
   1.312 +        String source;
   1.313 +
   1.314 +        public JavaSource() {
   1.315 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.316 +            source = template.replaceAll("#I", ik.interfaceStr)
   1.317 +                    .replaceAll("#P", pk.interfaceStr)
   1.318 +                    .replaceAll("#C", sh.getShape(qk, ek));
   1.319 +        }
   1.320 +
   1.321 +        @Override
   1.322 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.323 +            return source;
   1.324 +        }
   1.325 +    }
   1.326 +
   1.327 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.328 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.329 +                Arrays.asList("-XDallowDefaultMethods"), null, Arrays.asList(source));
   1.330 +        try {
   1.331 +            ct.analyze();
   1.332 +        } catch (Throwable ex) {
   1.333 +            throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
   1.334 +        }
   1.335 +        check();
   1.336 +    }
   1.337 +
   1.338 +    void check() {
   1.339 +        boolean errorExpected = false;
   1.340 +
   1.341 +        boolean badEnclosing = false;
   1.342 +        boolean badThis = false;
   1.343 +        boolean badSuper = false;
   1.344 +
   1.345 +        if (qk == QualifierKind.ENCLOSING_1 &&
   1.346 +                sh.enclosingNames.size() < 1) {
   1.347 +            errorExpected |= true;
   1.348 +            badEnclosing = true;
   1.349 +        }
   1.350 +
   1.351 +        if (qk == QualifierKind.ENCLOSING_2 &&
   1.352 +                sh.enclosingNames.size() < 2) {
   1.353 +            errorExpected |= true;
   1.354 +            badEnclosing = true;
   1.355 +        }
   1.356 +
   1.357 +        if (ek == ExprKind.THIS) {
   1.358 +            boolean found = false;
   1.359 +            for (int i = 0; i < sh.enclosingElements.size(); i++) {
   1.360 +                if (sh.enclosingElements.get(i) == ElementKind.ANON_CLASS) continue;
   1.361 +                if (sh.enclosingNames.get(i).equals(qk.getQualifier(sh))) {
   1.362 +                    found = sh.elementsWithMethod.contains(sh.enclosingNames.get(i));
   1.363 +                    break;
   1.364 +                }
   1.365 +            }
   1.366 +            errorExpected |= !found;
   1.367 +            if (!found) {
   1.368 +                badThis = true;
   1.369 +            }
   1.370 +        }
   1.371 +
   1.372 +        if (ek == ExprKind.SUPER) {
   1.373 +
   1.374 +            int lastIdx = sh.enclosingElements.size() - 1;
   1.375 +            boolean found = lastIdx == -1 ? false :
   1.376 +                    sh.enclosingElements.get(lastIdx).hasSuper() && qk.allowSuperCall(ik, pk);
   1.377 +
   1.378 +            errorExpected |= !found;
   1.379 +            if (!found) {
   1.380 +                badSuper = true;
   1.381 +            }
   1.382 +        }
   1.383 +
   1.384 +        checkCount++;
   1.385 +        if (diagChecker.errorFound != errorExpected) {
   1.386 +            throw new AssertionError("Problem when compiling source:\n" + source.getCharContent(true) +
   1.387 +                    "\nenclosingElems: " + sh.enclosingElements +
   1.388 +                    "\nenclosingNames: " + sh.enclosingNames +
   1.389 +                    "\nelementsWithMethod: " + sh.elementsWithMethod +
   1.390 +                    "\nbad encl: " + badEnclosing +
   1.391 +                    "\nbad this: " + badThis +
   1.392 +                    "\nbad super: " + badSuper +
   1.393 +                    "\nqual kind: " + qk +
   1.394 +                    "\nfound error: " + diagChecker.errorFound);
   1.395 +        }
   1.396 +    }
   1.397 +
   1.398 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.399 +
   1.400 +        boolean errorFound;
   1.401 +
   1.402 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.403 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.404 +                System.err.println(diagnostic.getMessage(Locale.getDefault()));
   1.405 +                errorFound = true;
   1.406 +            }
   1.407 +        }
   1.408 +    }
   1.409 +}

mercurial