test/tools/javac/cast/intersection/IntersectionTypeParserTest.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/cast/intersection/IntersectionTypeParserTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, 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 8002099
    1.30 + * @summary Add support for intersection types in cast expression
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import java.net.URI;
    1.35 +import java.util.Arrays;
    1.36 +import javax.tools.Diagnostic;
    1.37 +import javax.tools.JavaCompiler;
    1.38 +import javax.tools.JavaFileObject;
    1.39 +import javax.tools.SimpleJavaFileObject;
    1.40 +import javax.tools.StandardJavaFileManager;
    1.41 +import javax.tools.ToolProvider;
    1.42 +
    1.43 +public class IntersectionTypeParserTest {
    1.44 +
    1.45 +    static int checkCount = 0;
    1.46 +
    1.47 +    enum TypeKind {
    1.48 +        SIMPLE("A"),
    1.49 +        GENERIC("A<X>"),
    1.50 +        WILDCARD("A<? super X, ? extends Y>");
    1.51 +
    1.52 +        String typeStr;
    1.53 +
    1.54 +        TypeKind(String typeStr) {
    1.55 +            this.typeStr = typeStr;
    1.56 +        }
    1.57 +    }
    1.58 +
    1.59 +    enum ArrayKind {
    1.60 +        NONE(""),
    1.61 +        SINGLE("[]"),
    1.62 +        DOUBLE("[][]");
    1.63 +
    1.64 +        String arrStr;
    1.65 +
    1.66 +        ArrayKind(String arrStr) {
    1.67 +            this.arrStr = arrStr;
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    static class Type {
    1.72 +        TypeKind tk;
    1.73 +        ArrayKind ak;
    1.74 +
    1.75 +        Type(TypeKind tk, ArrayKind ak) {
    1.76 +            this.tk = tk;
    1.77 +            this.ak = ak;
    1.78 +        }
    1.79 +
    1.80 +        String asString() {
    1.81 +            return tk.typeStr + ak.arrStr;
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    enum CastKind {
    1.86 +        ONE("(#T0)", 1),
    1.87 +        TWO("(#T0 & T1)", 2),
    1.88 +        THREE("(#T0 & #T1 & #T2)", 3);
    1.89 +
    1.90 +        String castTemplate;
    1.91 +        int nBounds;
    1.92 +
    1.93 +        CastKind(String castTemplate, int nBounds) {
    1.94 +            this.castTemplate = castTemplate;
    1.95 +            this.nBounds = nBounds;
    1.96 +        }
    1.97 +
    1.98 +        String asString(Type... types) {
    1.99 +            String res = castTemplate;
   1.100 +            for (int i = 0; i < nBounds ; i++) {
   1.101 +                res = res.replaceAll(String.format("#T%d", i), types[i].asString());
   1.102 +            }
   1.103 +            return res;
   1.104 +        }
   1.105 +    }
   1.106 +
   1.107 +    public static void main(String... args) throws Exception {
   1.108 +        //create default shared JavaCompiler - reused across multiple compilations
   1.109 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.110 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.111 +
   1.112 +        for (CastKind ck : CastKind.values()) {
   1.113 +            for (TypeKind t1 : TypeKind.values()) {
   1.114 +                for (ArrayKind ak1 : ArrayKind.values()) {
   1.115 +                    Type typ1 = new Type(t1, ak1);
   1.116 +                    if (ck.nBounds == 1) {
   1.117 +                        new IntersectionTypeParserTest(ck, typ1).run(comp, fm);
   1.118 +                        continue;
   1.119 +                    }
   1.120 +                    for (TypeKind t2 : TypeKind.values()) {
   1.121 +                        for (ArrayKind ak2 : ArrayKind.values()) {
   1.122 +                            Type typ2 = new Type(t2, ak2);
   1.123 +                            if (ck.nBounds == 2) {
   1.124 +                                new IntersectionTypeParserTest(ck, typ1, typ2).run(comp, fm);
   1.125 +                                continue;
   1.126 +                            }
   1.127 +                            for (TypeKind t3 : TypeKind.values()) {
   1.128 +                                for (ArrayKind ak3 : ArrayKind.values()) {
   1.129 +                                    Type typ3 = new Type(t3, ak3);
   1.130 +                                    new IntersectionTypeParserTest(ck, typ1, typ2, typ3).run(comp, fm);
   1.131 +                                }
   1.132 +                            }
   1.133 +                        }
   1.134 +                    }
   1.135 +                }
   1.136 +            }
   1.137 +        }
   1.138 +        System.out.println("Total check executed: " + checkCount);
   1.139 +    }
   1.140 +
   1.141 +    CastKind ck;
   1.142 +    Type[] types;
   1.143 +    JavaSource source;
   1.144 +    DiagnosticChecker diagChecker;
   1.145 +
   1.146 +    IntersectionTypeParserTest(CastKind ck, Type... types) {
   1.147 +        this.ck = ck;
   1.148 +        this.types = types;
   1.149 +        this.source = new JavaSource();
   1.150 +        this.diagChecker = new DiagnosticChecker();
   1.151 +    }
   1.152 +
   1.153 +    class JavaSource extends SimpleJavaFileObject {
   1.154 +
   1.155 +        String bodyTemplate = "class Test {\n" +
   1.156 +                              "   void test() {\n" +
   1.157 +                              "      Object o = #Cnull;\n" +
   1.158 +                              "   } }";
   1.159 +
   1.160 +        String source = "";
   1.161 +
   1.162 +        public JavaSource() {
   1.163 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.164 +            source += bodyTemplate.replaceAll("#C", ck.asString(types));
   1.165 +        }
   1.166 +
   1.167 +        @Override
   1.168 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.169 +            return source;
   1.170 +        }
   1.171 +    }
   1.172 +
   1.173 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.174 +        checkCount++;
   1.175 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.176 +                null, null, Arrays.asList(source));
   1.177 +        ct.parse();
   1.178 +        if (diagChecker.errorFound) {
   1.179 +            throw new Error("Unexpected parser error for source:\n" +
   1.180 +                source.getCharContent(true));
   1.181 +        }
   1.182 +    }
   1.183 +
   1.184 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.185 +
   1.186 +        boolean errorFound;
   1.187 +
   1.188 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.189 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.190 +                errorFound = true;
   1.191 +            }
   1.192 +        }
   1.193 +    }
   1.194 +}

mercurial