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

Mon, 21 Jan 2013 20:14:39 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:14:39 +0000
changeset 1511
c7c41a044e7c
parent 1436
f6f1fd261f57
child 2227
998b10c43157
permissions
-rw-r--r--

8006566: Remove transient lambda-related guards from JavacParser
Summary: Remove transitional internal flag for allowing intersection types in cast
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8002099
    27  * @summary Add support for intersection types in cast expression
    28  */
    30 import com.sun.source.util.JavacTask;
    31 import java.net.URI;
    32 import java.util.Arrays;
    33 import javax.tools.Diagnostic;
    34 import javax.tools.JavaCompiler;
    35 import javax.tools.JavaFileObject;
    36 import javax.tools.SimpleJavaFileObject;
    37 import javax.tools.StandardJavaFileManager;
    38 import javax.tools.ToolProvider;
    40 public class IntersectionTypeParserTest {
    42     static int checkCount = 0;
    44     enum TypeKind {
    45         SIMPLE("A"),
    46         GENERIC("A<X>"),
    47         WILDCARD("A<? super X, ? extends Y>");
    49         String typeStr;
    51         TypeKind(String typeStr) {
    52             this.typeStr = typeStr;
    53         }
    54     }
    56     enum ArrayKind {
    57         NONE(""),
    58         SINGLE("[]"),
    59         DOUBLE("[][]");
    61         String arrStr;
    63         ArrayKind(String arrStr) {
    64             this.arrStr = arrStr;
    65         }
    66     }
    68     static class Type {
    69         TypeKind tk;
    70         ArrayKind ak;
    72         Type(TypeKind tk, ArrayKind ak) {
    73             this.tk = tk;
    74             this.ak = ak;
    75         }
    77         String asString() {
    78             return tk.typeStr + ak.arrStr;
    79         }
    80     }
    82     enum CastKind {
    83         ONE("(#T0)", 1),
    84         TWO("(#T0 & T1)", 2),
    85         THREE("(#T0 & #T1 & #T2)", 3);
    87         String castTemplate;
    88         int nBounds;
    90         CastKind(String castTemplate, int nBounds) {
    91             this.castTemplate = castTemplate;
    92             this.nBounds = nBounds;
    93         }
    95         String asString(Type... types) {
    96             String res = castTemplate;
    97             for (int i = 0; i < nBounds ; i++) {
    98                 res = res.replaceAll(String.format("#T%d", i), types[i].asString());
    99             }
   100             return res;
   101         }
   102     }
   104     public static void main(String... args) throws Exception {
   105         //create default shared JavaCompiler - reused across multiple compilations
   106         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   107         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   109         for (CastKind ck : CastKind.values()) {
   110             for (TypeKind t1 : TypeKind.values()) {
   111                 for (ArrayKind ak1 : ArrayKind.values()) {
   112                     Type typ1 = new Type(t1, ak1);
   113                     if (ck.nBounds == 1) {
   114                         new IntersectionTypeParserTest(ck, typ1).run(comp, fm);
   115                         continue;
   116                     }
   117                     for (TypeKind t2 : TypeKind.values()) {
   118                         for (ArrayKind ak2 : ArrayKind.values()) {
   119                             Type typ2 = new Type(t2, ak2);
   120                             if (ck.nBounds == 2) {
   121                                 new IntersectionTypeParserTest(ck, typ1, typ2).run(comp, fm);
   122                                 continue;
   123                             }
   124                             for (TypeKind t3 : TypeKind.values()) {
   125                                 for (ArrayKind ak3 : ArrayKind.values()) {
   126                                     Type typ3 = new Type(t3, ak3);
   127                                     new IntersectionTypeParserTest(ck, typ1, typ2, typ3).run(comp, fm);
   128                                 }
   129                             }
   130                         }
   131                     }
   132                 }
   133             }
   134         }
   135         System.out.println("Total check executed: " + checkCount);
   136     }
   138     CastKind ck;
   139     Type[] types;
   140     JavaSource source;
   141     DiagnosticChecker diagChecker;
   143     IntersectionTypeParserTest(CastKind ck, Type... types) {
   144         this.ck = ck;
   145         this.types = types;
   146         this.source = new JavaSource();
   147         this.diagChecker = new DiagnosticChecker();
   148     }
   150     class JavaSource extends SimpleJavaFileObject {
   152         String bodyTemplate = "class Test {\n" +
   153                               "   void test() {\n" +
   154                               "      Object o = #Cnull;\n" +
   155                               "   } }";
   157         String source = "";
   159         public JavaSource() {
   160             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   161             source += bodyTemplate.replaceAll("#C", ck.asString(types));
   162         }
   164         @Override
   165         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   166             return source;
   167         }
   168     }
   170     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   171         checkCount++;
   172         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   173                 null, null, Arrays.asList(source));
   174         ct.parse();
   175         if (diagChecker.errorFound) {
   176             throw new Error("Unexpected parser error for source:\n" +
   177                 source.getCharContent(true));
   178         }
   179     }
   181     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   183         boolean errorFound;
   185         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   186             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   187                 errorFound = true;
   188             }
   189         }
   190     }
   191 }

mercurial