test/tools/javac/lambda/mostSpecific/StructuralMostSpecificTest.java

changeset 1415
01c9d4161882
child 1482
954541f13717
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/mostSpecific/StructuralMostSpecificTest.java	Sat Nov 17 19:01:03 2012 +0000
     1.3 @@ -0,0 +1,303 @@
     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 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *  Automatic test for checking correctness of structural most specific test routine
    1.32 + * @run main/timeout=360 StructuralMostSpecificTest
    1.33 + */
    1.34 +
    1.35 +import com.sun.source.util.JavacTask;
    1.36 +import com.sun.tools.javac.api.ClientCodeWrapper;
    1.37 +import com.sun.tools.javac.util.JCDiagnostic;
    1.38 +import java.net.URI;
    1.39 +import java.util.Arrays;
    1.40 +import javax.tools.Diagnostic;
    1.41 +import javax.tools.JavaCompiler;
    1.42 +import javax.tools.JavaFileObject;
    1.43 +import javax.tools.SimpleJavaFileObject;
    1.44 +import javax.tools.StandardJavaFileManager;
    1.45 +import javax.tools.ToolProvider;
    1.46 +
    1.47 +public class StructuralMostSpecificTest {
    1.48 +
    1.49 +    static int checkCount = 0;
    1.50 +
    1.51 +    enum RetTypeKind {
    1.52 +        SHORT("short"),
    1.53 +        INT("int"),
    1.54 +        OBJECT("Object"),
    1.55 +        INTEGER("Integer"),
    1.56 +        VOID("void"),
    1.57 +        J_L_VOID("Void");
    1.58 +
    1.59 +        String retTypeStr;
    1.60 +
    1.61 +        RetTypeKind(String retTypeStr) {
    1.62 +            this.retTypeStr = retTypeStr;
    1.63 +        }
    1.64 +
    1.65 +        boolean moreSpecificThan(RetTypeKind rk) {
    1.66 +            return moreSpecificThan[this.ordinal()][rk.ordinal()];
    1.67 +        }
    1.68 +
    1.69 +        static boolean[][] moreSpecificThan = {
    1.70 +                //              SHORT |  INT  | OBJECT | INTEGER | VOID  | J_L_VOID
    1.71 +                /* SHORT */   { true  , true  , true   , false   , false , false },
    1.72 +                /* INT */     { false , true  , true   , true    , false , false },
    1.73 +                /* OBJECT */  { false , false , true   , false   , false , false },
    1.74 +                /* INTEGER */ { false , false , true   , true    , false , false },
    1.75 +                /* VOID */    { false , false , false  , false   , true  , true  },
    1.76 +                /* J_L_VOID */{ false , false , true   , false   , false , true  } };
    1.77 +    }
    1.78 +
    1.79 +    enum ArgTypeKind {
    1.80 +        SHORT("short"),
    1.81 +        INT("int"),
    1.82 +        BOOLEAN("boolean"),
    1.83 +        OBJECT("Object"),
    1.84 +        INTEGER("Integer"),
    1.85 +        DOUBLE("Double");
    1.86 +
    1.87 +        String argTypeStr;
    1.88 +
    1.89 +        ArgTypeKind(String typeStr) {
    1.90 +            this.argTypeStr = typeStr;
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    enum ExceptionKind {
    1.95 +        NONE(""),
    1.96 +        EXCEPTION("throws Exception"),
    1.97 +        SQL_EXCEPTION("throws java.sql.SQLException"),
    1.98 +        IO_EXCEPTION("throws java.io.IOException");
    1.99 +
   1.100 +        String exceptionStr;
   1.101 +
   1.102 +        ExceptionKind(String exceptionStr) {
   1.103 +            this.exceptionStr = exceptionStr;
   1.104 +        }
   1.105 +    }
   1.106 +
   1.107 +    enum LambdaReturnKind {
   1.108 +        VOID("return;"),
   1.109 +        SHORT("return (short)0;"),
   1.110 +        INT("return 0;"),
   1.111 +        INTEGER("return (Integer)null"),
   1.112 +        NULL("return null;");
   1.113 +
   1.114 +        String retStr;
   1.115 +
   1.116 +        LambdaReturnKind(String retStr) {
   1.117 +            this.retStr = retStr;
   1.118 +        }
   1.119 +
   1.120 +        boolean compatibleWith(RetTypeKind rk) {
   1.121 +            return compatibleWith[rk.ordinal()][ordinal()];
   1.122 +        }
   1.123 +
   1.124 +        static boolean[][] compatibleWith = {
   1.125 +                //              VOID  | SHORT | INT     | INTEGER | NULL
   1.126 +                /* SHORT */   { false , true  , false   , false   , false },
   1.127 +                /* INT */     { false , true  , true    , true    , false },
   1.128 +                /* OBJECT */  { false , true  , true    , true    , true  },
   1.129 +                /* INTEGER */ { false , false , true    , true    , true  },
   1.130 +                /* VOID */    { true  , false , false   , false   , false },
   1.131 +                /* J_L_VOID */{ false , false , false   , false   , true  } };
   1.132 +
   1.133 +        boolean needsConversion(RetTypeKind rk) {
   1.134 +            return needsConversion[rk.ordinal()][ordinal()];
   1.135 +        }
   1.136 +
   1.137 +        static boolean[][] needsConversion = {
   1.138 +                //              VOID  | SHORT | INT     | INTEGER | NULL
   1.139 +                /* SHORT */   { false , false , false   , false   , false },
   1.140 +                /* INT */     { false , false , false   , true    , false },
   1.141 +                /* OBJECT */  { false , true  , true    , false   , false },
   1.142 +                /* INTEGER */ { false , false , true    , false   , false },
   1.143 +                /* VOID */    { false , false , false   , false   , false },
   1.144 +                /* J_L_VOID */{ true  , false , false   , false   , false } };
   1.145 +    }
   1.146 +
   1.147 +    public static void main(String... args) throws Exception {
   1.148 +
   1.149 +        //create default shared JavaCompiler - reused across multiple compilations
   1.150 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.151 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.152 +
   1.153 +        for (LambdaReturnKind lrk : LambdaReturnKind.values()) {
   1.154 +            for (RetTypeKind rk1 : RetTypeKind.values()) {
   1.155 +                for (RetTypeKind rk2 : RetTypeKind.values()) {
   1.156 +                    for (ExceptionKind ek1 : ExceptionKind.values()) {
   1.157 +                        for (ExceptionKind ek2 : ExceptionKind.values()) {
   1.158 +                            for (ArgTypeKind ak11 : ArgTypeKind.values()) {
   1.159 +                                for (ArgTypeKind ak12 : ArgTypeKind.values()) {
   1.160 +                                    new StructuralMostSpecificTest(lrk, rk1, rk2, ek1, ek2, ak11, ak12).run(comp, fm);
   1.161 +                                }
   1.162 +                            }
   1.163 +                        }
   1.164 +                    }
   1.165 +                }
   1.166 +            }
   1.167 +        }
   1.168 +        System.out.println("Total check executed: " + checkCount);
   1.169 +    }
   1.170 +
   1.171 +    LambdaReturnKind lrk;
   1.172 +    RetTypeKind rt1, rt2;
   1.173 +    ArgTypeKind ak1, ak2;
   1.174 +    ExceptionKind ek1, ek2;
   1.175 +    JavaSource source;
   1.176 +    DiagnosticChecker diagChecker;
   1.177 +
   1.178 +    StructuralMostSpecificTest(LambdaReturnKind lrk, RetTypeKind rt1, RetTypeKind rt2,
   1.179 +            ExceptionKind ek1, ExceptionKind ek2, ArgTypeKind ak1, ArgTypeKind ak2) {
   1.180 +        this.lrk = lrk;
   1.181 +        this.rt1 = rt1;
   1.182 +        this.rt2 = rt2;
   1.183 +        this.ek1 = ek1;
   1.184 +        this.ek2 = ek2;
   1.185 +        this.ak1 = ak1;
   1.186 +        this.ak2 = ak2;
   1.187 +        this.source = new JavaSource();
   1.188 +        this.diagChecker = new DiagnosticChecker();
   1.189 +    }
   1.190 +
   1.191 +    class JavaSource extends SimpleJavaFileObject {
   1.192 +
   1.193 +        String template = "interface SAM1 {\n" +
   1.194 +                          "   #R1 m(#A1 a1) #E1;\n" +
   1.195 +                          "}\n" +
   1.196 +                          "interface SAM2 {\n" +
   1.197 +                          "   #R2 m(#A2 a1) #E2;\n" +
   1.198 +                          "}\n" +
   1.199 +                          "class Test {\n" +
   1.200 +                          "   void m(SAM1 s) { }\n" +
   1.201 +                          "   void m(SAM2 s) { }\n" +
   1.202 +                          "   { m(x->{ #LR }); }\n" +
   1.203 +                          "}\n";
   1.204 +
   1.205 +        String source;
   1.206 +
   1.207 +        public JavaSource() {
   1.208 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.209 +            source = template.replaceAll("#LR", lrk.retStr)
   1.210 +                    .replaceAll("#R1", rt1.retTypeStr)
   1.211 +                    .replaceAll("#R2", rt2.retTypeStr)
   1.212 +                    .replaceAll("#A1", ak1.argTypeStr)
   1.213 +                    .replaceAll("#A2", ak2.argTypeStr)
   1.214 +                    .replaceAll("#E1", ek1.exceptionStr)
   1.215 +                    .replaceAll("#E2", ek2.exceptionStr);
   1.216 +        }
   1.217 +
   1.218 +        @Override
   1.219 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.220 +            return source;
   1.221 +        }
   1.222 +    }
   1.223 +
   1.224 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.225 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.226 +                Arrays.asList("-XDverboseResolution=all,-predef,-internal,-object-init"),
   1.227 +                null, Arrays.asList(source));
   1.228 +        try {
   1.229 +            ct.analyze();
   1.230 +        } catch (Throwable ex) {
   1.231 +            throw new AssertionError("Error thron when analyzing the following source:\n" + source.getCharContent(true));
   1.232 +        }
   1.233 +        check();
   1.234 +    }
   1.235 +
   1.236 +    void check() {
   1.237 +        checkCount++;
   1.238 +
   1.239 +        if (!lrk.compatibleWith(rt1) || !lrk.compatibleWith(rt2))
   1.240 +            return;
   1.241 +
   1.242 +        if (lrk.needsConversion(rt1) != lrk.needsConversion(rt2))
   1.243 +            return;
   1.244 +
   1.245 +        boolean m1MoreSpecific = moreSpecific(rt1, rt2, ek1, ek2, ak1, ak2);
   1.246 +        boolean m2MoreSpecific = moreSpecific(rt2, rt1, ek2, ek1, ak2, ak1);
   1.247 +
   1.248 +        boolean ambiguous = (m1MoreSpecific == m2MoreSpecific);
   1.249 +
   1.250 +        if (ambiguous != diagChecker.ambiguityFound) {
   1.251 +            throw new Error("invalid diagnostics for source:\n" +
   1.252 +                source.getCharContent(true) +
   1.253 +                "\nAmbiguity found: " + diagChecker.ambiguityFound +
   1.254 +                "\nm1 more specific: " + m1MoreSpecific +
   1.255 +                "\nm2 more specific: " + m2MoreSpecific +
   1.256 +                "\nexpected ambiguity: " + ambiguous);
   1.257 +        }
   1.258 +
   1.259 +        if (!ambiguous) {
   1.260 +            String sigToCheck = m1MoreSpecific ? "m(SAM1)" : "m(SAM2)";
   1.261 +            if (!sigToCheck.equals(diagChecker.mostSpecificSig)) {
   1.262 +                throw new Error("invalid most specific method selected:\n" +
   1.263 +                source.getCharContent(true) +
   1.264 +                "\nMost specific found: " + diagChecker.mostSpecificSig +
   1.265 +                "\nm1 more specific: " + m1MoreSpecific +
   1.266 +                "\nm2 more specific: " + m2MoreSpecific);
   1.267 +            }
   1.268 +        }
   1.269 +    }
   1.270 +
   1.271 +    boolean moreSpecific(RetTypeKind rk1, RetTypeKind rk2, ExceptionKind ek1, ExceptionKind ek2,
   1.272 +            ArgTypeKind ak1, ArgTypeKind ak2) {
   1.273 +        if (!rk1.moreSpecificThan(rk2))
   1.274 +            return false;
   1.275 +
   1.276 +        if (ak1 != ak2)
   1.277 +            return false;
   1.278 +
   1.279 +        return true;
   1.280 +    }
   1.281 +
   1.282 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.283 +
   1.284 +        boolean ambiguityFound;
   1.285 +        String mostSpecificSig;
   1.286 +
   1.287 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.288 +            try {
   1.289 +                if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
   1.290 +                        diagnostic.getCode().equals("compiler.err.ref.ambiguous")) {
   1.291 +                    ambiguityFound = true;
   1.292 +                } else if (diagnostic.getKind() == Diagnostic.Kind.NOTE &&
   1.293 +                        diagnostic.getCode().equals("compiler.note.verbose.resolve.multi")) {
   1.294 +                    ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
   1.295 +                            (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
   1.296 +                    JCDiagnostic.MultilineDiagnostic mdiag = (JCDiagnostic.MultilineDiagnostic)dsu.d;
   1.297 +                    int mostSpecificIndex = (Integer)mdiag.getArgs()[2];
   1.298 +                    mostSpecificSig = ((JCDiagnostic)mdiag.getSubdiagnostics().get(mostSpecificIndex)).getArgs()[1].toString();
   1.299 +                }
   1.300 +            } catch (RuntimeException t) {
   1.301 +                t.printStackTrace();
   1.302 +                throw t;
   1.303 +            }
   1.304 +        }
   1.305 +    }
   1.306 +}

mercurial