test/tools/javac/varargs/6199075/T6199075.java

Fri, 10 Dec 2010 15:24:17 +0000

author
mcimadamore
date
Fri, 10 Dec 2010 15:24:17 +0000
changeset 787
b1c98bfd4709
child 892
3e30c95da3c6
permissions
-rw-r--r--

6199075: Unambiguous varargs method calls flagged as ambiguous
Summary: javac does not implement overload resolution w.r.t. varargs methods as described in the JLS
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2010, 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 6199075
    27  *
    28  * @summary Unambiguous varargs method calls flagged as ambiguous
    29  * @author mcimadamore
    30  *
    31  */
    33 import com.sun.source.util.JavacTask;
    34 import com.sun.tools.classfile.Instruction;
    35 import com.sun.tools.classfile.Attribute;
    36 import com.sun.tools.classfile.ClassFile;
    37 import com.sun.tools.classfile.Code_attribute;
    38 import com.sun.tools.classfile.ConstantPool.*;
    39 import com.sun.tools.classfile.Method;
    40 import com.sun.tools.javac.util.List;
    42 import java.io.File;
    43 import java.net.URI;
    44 import java.util.Arrays;
    45 import java.util.Locale;
    46 import javax.tools.Diagnostic;
    47 import javax.tools.JavaCompiler;
    48 import javax.tools.JavaFileObject;
    49 import javax.tools.SimpleJavaFileObject;
    50 import javax.tools.ToolProvider;
    52 public class T6199075 {
    54     int checkCount = 0;
    55     int bytecodeCheckCount = 0;
    57     enum TypeKind {
    58         BYTE("byte", "(byte)1", "[B", 0),
    59         CHAR("char", "'c'", "[C", 1),
    60         SHORT("short", "(short)1", "[S", 2),
    61         INT("int", "1", "[I", 3),
    62         LONG("long", "1L", "[J", 4),
    63         FLOAT("float", "1.0F", "[F", 5),
    64         DOUBLE("double", "1.0D", "[D", 6),
    65         BOOLEAN("boolean", "true", "[Z", -1);
    67         String typeString;
    68         String valueString;
    69         String bytecodeString;
    70         private int subtypeTag;
    72         TypeKind(String typeString, String valueString, String bytecodeString, int subtypeTag) {
    73             this.typeString = typeString;
    74             this.valueString = valueString;
    75             this.bytecodeString = bytecodeString;
    76             this.subtypeTag = subtypeTag;
    77         }
    79         boolean isSubtypeOf(TypeKind that) {
    80             switch (this) {
    81                 case BOOLEAN:
    82                     return that == BOOLEAN;
    83                 case BYTE:
    84                 case CHAR:
    85                     return this.subtypeTag == that.subtypeTag ||
    86                             this.subtypeTag + 2 <= that.subtypeTag;
    87                 default:
    88                     return this.subtypeTag <= that.subtypeTag;
    89             }
    90         }
    91     }
    93     enum ArgumentsArity {
    94         ZERO(0),
    95         ONE(1),
    96         TWO(2),
    97         THREE(3);
    99         int arity;
   101         ArgumentsArity(int arity) {
   102             this.arity = arity;
   103         }
   105         String asExpressionList(TypeKind type) {
   106             StringBuilder buf = new StringBuilder();
   107             String sep = "";
   108             for (int i = 0; i < arity; i++) {
   109                 buf.append(sep);
   110                 buf.append(type.valueString);
   111                 sep = ",";
   112             }
   113             return buf.toString();
   114         }
   115     }
   117     static class VarargsMethod {
   118         TypeKind varargsElement;
   120         VarargsMethod(TypeKind varargsElement) {
   121             this.varargsElement = varargsElement;
   122         }
   124         @Override
   125         public String toString() {
   126             return "void m("+ varargsElement.typeString+ "... args) {}";
   127         }
   129         boolean isApplicable(TypeKind actual, ArgumentsArity argsArity) {
   130             return argsArity == ArgumentsArity.ZERO ||
   131                     actual.isSubtypeOf(varargsElement);
   132         }
   134         boolean isMoreSpecificThan(VarargsMethod that) {
   135             return varargsElement.isSubtypeOf(that.varargsElement);
   136         }
   137     }
   139     public static void main(String... args) throws Exception {
   140         new T6199075().test();
   141     }
   143     void test() throws Exception {
   144         for (TypeKind formal1 : TypeKind.values()) {
   145             VarargsMethod m1 = new VarargsMethod(formal1);
   146             for (TypeKind formal2 : TypeKind.values()) {
   147                 VarargsMethod m2 = new VarargsMethod(formal2);
   148                 for (TypeKind actual : TypeKind.values()) {
   149                     for (ArgumentsArity argsArity : ArgumentsArity.values()) {
   150                         compileAndCheck(m1, m2, actual, argsArity);
   151                     }
   152                 }
   153             }
   154         }
   156         System.out.println("Total checks made: " + checkCount);
   157         System.out.println("Bytecode checks made: " + bytecodeCheckCount);
   158     }
   160     void compileAndCheck(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) throws Exception {
   161         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   162         JavaSource source = new JavaSource(m1, m2, actual, argsArity);
   163         ErrorChecker ec = new ErrorChecker();
   164         JavacTask ct = (JavacTask)tool.getTask(null, null, ec,
   165                 null, null, Arrays.asList(source));
   166         ct.generate();
   167         check(source, ec, m1, m2, actual, argsArity);
   168     }
   170     void check(JavaSource source, ErrorChecker ec, VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) {
   171         checkCount++;
   172         boolean resolutionError = false;
   173         VarargsMethod selectedMethod = null;
   175         boolean m1_applicable = m1.isApplicable(actual, argsArity);
   176         boolean m2_applicable = m2.isApplicable(actual, argsArity);
   178         if (!m1_applicable && !m2_applicable) {
   179             resolutionError = true;
   180         } else if (m1_applicable && m2_applicable) {
   181             //most specific
   182             boolean m1_moreSpecific = m1.isMoreSpecificThan(m2);
   183             boolean m2_moreSpecific = m2.isMoreSpecificThan(m1);
   184             resolutionError = m1_moreSpecific == m2_moreSpecific;
   185             selectedMethod = m1_moreSpecific ? m1 : m2;
   186         } else {
   187             selectedMethod = m1_applicable ?
   188                 m1 : m2;
   189         }
   191         if (ec.errorFound != resolutionError) {
   192             throw new Error("invalid diagnostics for source:\n" +
   193                     source.getCharContent(true) +
   194                     "\nExpected resolution error: " + resolutionError +
   195                     "\nFound error: " + ec.errorFound +
   196                     "\nCompiler diagnostics:\n" + ec.printDiags());
   197         } else if (!resolutionError) {
   198             verifyBytecode(selectedMethod);
   199         }
   200     }
   202     void verifyBytecode(VarargsMethod selected) {
   203         bytecodeCheckCount++;
   204         File compiledTest = new File("Test.class");
   205         try {
   206             ClassFile cf = ClassFile.read(compiledTest);
   207             Method testMethod = null;
   208             for (Method m : cf.methods) {
   209                 if (m.getName(cf.constant_pool).equals("test")) {
   210                     testMethod = m;
   211                     break;
   212                 }
   213             }
   214             if (testMethod == null) {
   215                 throw new Error("Test method not found");
   216             }
   217             Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
   218             if (testMethod == null) {
   219                 throw new Error("Code attribute for test() method not found");
   220             }
   222             for (Instruction i : ea.getInstructions()) {
   223                 if (i.getMnemonic().equals("invokevirtual")) {
   224                     int cp_entry = i.getUnsignedShort(1);
   225                     CONSTANT_Methodref_info methRef =
   226                             (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   227                     String type = methRef.getNameAndTypeInfo().getType();
   228                     if (!type.contains(selected.varargsElement.bytecodeString)) {
   229                         throw new Error("Unexpected type method call: " + type);
   230                     }
   231                     break;
   232                 }
   233             }
   234         } catch (Exception e) {
   235             e.printStackTrace();
   236             throw new Error("error reading " + compiledTest +": " + e);
   237         }
   238     }
   240     static class JavaSource extends SimpleJavaFileObject {
   242         static final String source_template = "class Test {\n" +
   243                 "   #V1\n" +
   244                 "   #V2\n" +
   245                 "   void test() { m(#E); }\n" +
   246                 "}";
   248         String source;
   250         public JavaSource(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) {
   251             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   252             source = source_template.replaceAll("#V1", m1.toString()).
   253                     replaceAll("#V2", m2.toString()).
   254                     replaceAll("#E", argsArity.asExpressionList(actual));
   255         }
   257         @Override
   258         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   259             return source;
   260         }
   261     }
   263     static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   265         boolean errorFound;
   266         List<String> errDiags = List.nil();
   268         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   269             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   270                 errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
   271                 errorFound = true;
   272             }
   273         }
   275         String printDiags() {
   276             StringBuilder buf = new StringBuilder();
   277             for (String s : errDiags) {
   278                 buf.append(s);
   279                 buf.append("\n");
   280             }
   281             return buf.toString();
   282         }
   283     }
   284 }

mercurial