test/tools/javac/generics/diamond/7030687/ParserTest.java

Thu, 06 Jun 2013 15:33:40 +0100

author
mcimadamore
date
Thu, 06 Jun 2013 15:33:40 +0100
changeset 1811
349160289ba2
parent 0
959103a6100f
permissions
-rw-r--r--

8008627: Compiler mishandles three-way return-type-substitutability
Summary: Compiler should not enforce an order in how ambiguous methods should be resolved
Reviewed-by: jjg, vromero

     1 /*
     2  * Copyright (c) 2011, 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 7030687
    27  * @summary Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
    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 ParserTest {
    42     enum TypeArgumentKind {
    43         NONE(""),
    44         EXPLICIT("<String>"),
    45         DIAMOND("<>");
    47         String typeargStr;
    49         private TypeArgumentKind(String typeargStr) {
    50             this.typeargStr = typeargStr;
    51         }
    52     }
    54     enum TypeQualifierArity {
    55         ONE(1, "A1#TA1"),
    56         TWO(2, "A1#TA1.A2#TA2"),
    57         THREE(3, "A1#TA1.A2#TA2.A3#TA3"),
    58         FOUR(4, "A1#TA1.A2#TA2.A3#TA3.A4#TA4");
    60         int n;
    61         String qualifierStr;
    63         private TypeQualifierArity(int n, String qualifierStr) {
    64             this.n = n;
    65             this.qualifierStr = qualifierStr;
    66         }
    68         String getType(TypeArgumentKind... typeArgumentKinds) {
    69             String res = qualifierStr;
    70             for (int i = 1 ; i <= typeArgumentKinds.length ; i++) {
    71                 res = res.replace("#TA" + i, typeArgumentKinds[i-1].typeargStr);
    72             }
    73             return res;
    74         }
    75     }
    77     public static void main(String... args) throws Exception {
    79         //create default shared JavaCompiler - reused across multiple compilations
    80         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    81         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    83         for (TypeQualifierArity arity : TypeQualifierArity.values()) {
    84             for (TypeArgumentKind tak1 : TypeArgumentKind.values()) {
    85                 if (arity == TypeQualifierArity.ONE) {
    86                     new ParserTest(arity, tak1).run(comp, fm);
    87                     continue;
    88                 }
    89                 for (TypeArgumentKind tak2 : TypeArgumentKind.values()) {
    90                     if (arity == TypeQualifierArity.TWO) {
    91                         new ParserTest(arity, tak1, tak2).run(comp, fm);
    92                         continue;
    93                     }
    94                     for (TypeArgumentKind tak3 : TypeArgumentKind.values()) {
    95                         if (arity == TypeQualifierArity.THREE) {
    96                             new ParserTest(arity, tak1, tak2, tak3).run(comp, fm);
    97                             continue;
    98                         }
    99                         for (TypeArgumentKind tak4 : TypeArgumentKind.values()) {
   100                             new ParserTest(arity, tak1, tak2, tak3, tak4).run(comp, fm);
   101                         }
   102                     }
   103                 }
   104             }
   105         }
   106     }
   108     TypeQualifierArity qualifierArity;
   109     TypeArgumentKind[] typeArgumentKinds;
   110     JavaSource source;
   111     DiagnosticChecker diagChecker;
   113     ParserTest(TypeQualifierArity qualifierArity, TypeArgumentKind... typeArgumentKinds) {
   114         this.qualifierArity = qualifierArity;
   115         this.typeArgumentKinds = typeArgumentKinds;
   116         this.source = new JavaSource();
   117         this.diagChecker = new DiagnosticChecker();
   118     }
   120     class JavaSource extends SimpleJavaFileObject {
   122         String template = "class Test {\n" +
   123                               "R res = new #T();\n" +
   124                           "}\n";
   126         String source;
   128         public JavaSource() {
   129             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   130             source = template.replace("#T", qualifierArity.getType(typeArgumentKinds));
   131         }
   133         @Override
   134         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   135             return source;
   136         }
   137     }
   139     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   140         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   141                 null, null, Arrays.asList(source));
   142         ct.parse();
   143         check();
   144     }
   146     void check() {
   148         boolean errorExpected = false;
   150         for (int i = 0 ; i < qualifierArity.n - 1 ; i++) {
   151             if (typeArgumentKinds[i] == TypeArgumentKind.DIAMOND) {
   152                 errorExpected = true;
   153                 break;
   154             }
   155         }
   157         if (errorExpected != diagChecker.errorFound) {
   158             throw new Error("invalid diagnostics for source:\n" +
   159                 source.getCharContent(true) +
   160                 "\nFound error: " + diagChecker.errorFound +
   161                 "\nExpected error: " + errorExpected);
   162         }
   163     }
   165     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   167         boolean errorFound;
   169         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   170             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   171                 errorFound = true;
   172             }
   173         }
   174     }
   175 }

mercurial