test/tools/javac/varargs/7042566/T7042566.java

changeset 1006
a2d422d480cb
child 1482
954541f13717
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/varargs/7042566/T7042566.java	Wed May 11 13:10:57 2011 +0200
     1.3 @@ -0,0 +1,350 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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 7042566
    1.30 + * @summary Unambiguous varargs method calls flagged as ambiguous
    1.31 + */
    1.32 +
    1.33 +import com.sun.source.util.JavacTask;
    1.34 +import com.sun.tools.classfile.Instruction;
    1.35 +import com.sun.tools.classfile.Attribute;
    1.36 +import com.sun.tools.classfile.ClassFile;
    1.37 +import com.sun.tools.classfile.Code_attribute;
    1.38 +import com.sun.tools.classfile.ConstantPool.*;
    1.39 +import com.sun.tools.classfile.Method;
    1.40 +import com.sun.tools.javac.api.JavacTool;
    1.41 +import com.sun.tools.javac.util.List;
    1.42 +
    1.43 +import java.io.File;
    1.44 +import java.net.URI;
    1.45 +import java.util.Arrays;
    1.46 +import java.util.Locale;
    1.47 +import javax.tools.Diagnostic;
    1.48 +import javax.tools.JavaCompiler;
    1.49 +import javax.tools.JavaFileObject;
    1.50 +import javax.tools.SimpleJavaFileObject;
    1.51 +import javax.tools.StandardJavaFileManager;
    1.52 +import javax.tools.ToolProvider;
    1.53 +
    1.54 +public class T7042566 {
    1.55 +
    1.56 +    VarargsMethod m1;
    1.57 +    VarargsMethod m2;
    1.58 +    TypeConfiguration actuals;
    1.59 +
    1.60 +    T7042566(TypeConfiguration m1_conf, TypeConfiguration m2_conf, TypeConfiguration actuals) {
    1.61 +        this.m1 = new VarargsMethod(m1_conf);
    1.62 +        this.m2 = new VarargsMethod(m2_conf);
    1.63 +        this.actuals = actuals;
    1.64 +    }
    1.65 +
    1.66 +    void compileAndCheck() throws Exception {
    1.67 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    1.68 +        JavaSource source = new JavaSource();
    1.69 +        ErrorChecker ec = new ErrorChecker();
    1.70 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, ec,
    1.71 +                null, null, Arrays.asList(source));
    1.72 +        ct.call();
    1.73 +        check(source, ec);
    1.74 +    }
    1.75 +
    1.76 +    void check(JavaSource source, ErrorChecker ec) {
    1.77 +        checkCount++;
    1.78 +        boolean resolutionError = false;
    1.79 +        VarargsMethod selectedMethod = null;
    1.80 +
    1.81 +        boolean m1_applicable = m1.isApplicable(actuals);
    1.82 +        boolean m2_applicable = m2.isApplicable(actuals);
    1.83 +
    1.84 +        if (!m1_applicable && !m2_applicable) {
    1.85 +            resolutionError = true;
    1.86 +        } else if (m1_applicable && m2_applicable) {
    1.87 +            //most specific
    1.88 +            boolean m1_moreSpecific = m1.isMoreSpecificThan(m2);
    1.89 +            boolean m2_moreSpecific = m2.isMoreSpecificThan(m1);
    1.90 +
    1.91 +            resolutionError = m1_moreSpecific == m2_moreSpecific;
    1.92 +            selectedMethod = m1_moreSpecific ? m1 : m2;
    1.93 +        } else {
    1.94 +            selectedMethod = m1_applicable ?
    1.95 +                m1 : m2;
    1.96 +        }
    1.97 +
    1.98 +        if (ec.errorFound != resolutionError) {
    1.99 +            throw new Error("invalid diagnostics for source:\n" +
   1.100 +                    source.getCharContent(true) +
   1.101 +                    "\nExpected resolution error: " + resolutionError +
   1.102 +                    "\nFound error: " + ec.errorFound +
   1.103 +                    "\nCompiler diagnostics:\n" + ec.printDiags());
   1.104 +        } else if (!resolutionError) {
   1.105 +            verifyBytecode(selectedMethod, source);
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    void verifyBytecode(VarargsMethod selected, JavaSource source) {
   1.110 +        bytecodeCheckCount++;
   1.111 +        File compiledTest = new File("Test.class");
   1.112 +        try {
   1.113 +            ClassFile cf = ClassFile.read(compiledTest);
   1.114 +            Method testMethod = null;
   1.115 +            for (Method m : cf.methods) {
   1.116 +                if (m.getName(cf.constant_pool).equals("test")) {
   1.117 +                    testMethod = m;
   1.118 +                    break;
   1.119 +                }
   1.120 +            }
   1.121 +            if (testMethod == null) {
   1.122 +                throw new Error("Test method not found");
   1.123 +            }
   1.124 +            Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
   1.125 +            if (testMethod == null) {
   1.126 +                throw new Error("Code attribute for test() method not found");
   1.127 +            }
   1.128 +
   1.129 +            for (Instruction i : ea.getInstructions()) {
   1.130 +                if (i.getMnemonic().equals("invokevirtual")) {
   1.131 +                    int cp_entry = i.getUnsignedShort(1);
   1.132 +                    CONSTANT_Methodref_info methRef =
   1.133 +                            (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   1.134 +                    String type = methRef.getNameAndTypeInfo().getType();
   1.135 +                    String sig = selected.parameterTypes.bytecodeSigStr;
   1.136 +                    if (!type.contains(sig)) {
   1.137 +                        throw new Error("Unexpected type method call: " + type + "" +
   1.138 +                                        "\nfound: " + sig +
   1.139 +                                        "\n" + source.getCharContent(true));
   1.140 +                    }
   1.141 +                    break;
   1.142 +                }
   1.143 +            }
   1.144 +        } catch (Exception e) {
   1.145 +            e.printStackTrace();
   1.146 +            throw new Error("error reading " + compiledTest +": " + e);
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    class JavaSource extends SimpleJavaFileObject {
   1.151 +
   1.152 +        static final String source_template = "class Test {\n" +
   1.153 +                "   #V1\n" +
   1.154 +                "   #V2\n" +
   1.155 +                "   void test() { m(#E); }\n" +
   1.156 +                "}";
   1.157 +
   1.158 +        String source;
   1.159 +
   1.160 +        public JavaSource() {
   1.161 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.162 +            source = source_template.replaceAll("#V1", m1.toString()).
   1.163 +                    replaceAll("#V2", m2.toString()).
   1.164 +                    replaceAll("#E", actuals.expressionListStr);
   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 +    /** global decls ***/
   1.174 +
   1.175 +    // Create a single file manager and reuse it for each compile to save time.
   1.176 +    static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
   1.177 +
   1.178 +    //statistics
   1.179 +    static int checkCount = 0;
   1.180 +    static int bytecodeCheckCount = 0;
   1.181 +
   1.182 +    public static void main(String... args) throws Exception {
   1.183 +        for (TypeConfiguration tconf1 : TypeConfiguration.values()) {
   1.184 +            for (TypeConfiguration tconf2 : TypeConfiguration.values()) {
   1.185 +                for (TypeConfiguration tconf3 : TypeConfiguration.values()) {
   1.186 +                    new T7042566(tconf1, tconf2, tconf3).compileAndCheck();
   1.187 +                }
   1.188 +            }
   1.189 +        }
   1.190 +
   1.191 +        System.out.println("Total checks made: " + checkCount);
   1.192 +        System.out.println("Bytecode checks made: " + bytecodeCheckCount);
   1.193 +    }
   1.194 +
   1.195 +    enum TypeKind {
   1.196 +        OBJECT("Object", "(Object)null", "Ljava/lang/Object;"),
   1.197 +        STRING("String", "(String)null", "Ljava/lang/String;");
   1.198 +
   1.199 +        String typeString;
   1.200 +        String valueString;
   1.201 +        String bytecodeString;
   1.202 +
   1.203 +        TypeKind(String typeString, String valueString, String bytecodeString) {
   1.204 +            this.typeString = typeString;
   1.205 +            this.valueString = valueString;
   1.206 +            this.bytecodeString = bytecodeString;
   1.207 +        }
   1.208 +
   1.209 +        boolean isSubtypeOf(TypeKind that) {
   1.210 +            return that == OBJECT ||
   1.211 +                    (that == STRING && this == STRING);
   1.212 +        }
   1.213 +    }
   1.214 +
   1.215 +    enum TypeConfiguration {
   1.216 +        A(TypeKind.OBJECT),
   1.217 +        B(TypeKind.STRING),
   1.218 +        AA(TypeKind.OBJECT, TypeKind.OBJECT),
   1.219 +        AB(TypeKind.OBJECT, TypeKind.STRING),
   1.220 +        BA(TypeKind.STRING, TypeKind.OBJECT),
   1.221 +        BB(TypeKind.STRING, TypeKind.STRING),
   1.222 +        AAA(TypeKind.OBJECT, TypeKind.OBJECT, TypeKind.OBJECT),
   1.223 +        AAB(TypeKind.OBJECT, TypeKind.OBJECT, TypeKind.STRING),
   1.224 +        ABA(TypeKind.OBJECT, TypeKind.STRING, TypeKind.OBJECT),
   1.225 +        ABB(TypeKind.OBJECT, TypeKind.STRING, TypeKind.STRING),
   1.226 +        BAA(TypeKind.STRING, TypeKind.OBJECT, TypeKind.OBJECT),
   1.227 +        BAB(TypeKind.STRING, TypeKind.OBJECT, TypeKind.STRING),
   1.228 +        BBA(TypeKind.STRING, TypeKind.STRING, TypeKind.OBJECT),
   1.229 +        BBB(TypeKind.STRING, TypeKind.STRING, TypeKind.STRING);
   1.230 +
   1.231 +        List<TypeKind> typeKindList;
   1.232 +        String expressionListStr;
   1.233 +        String parameterListStr;
   1.234 +        String bytecodeSigStr;
   1.235 +
   1.236 +        private TypeConfiguration(TypeKind... typeKindList) {
   1.237 +            this.typeKindList = List.from(typeKindList);
   1.238 +            expressionListStr = asExpressionList();
   1.239 +            parameterListStr = asParameterList();
   1.240 +            bytecodeSigStr = asBytecodeString();
   1.241 +        }
   1.242 +
   1.243 +        private String asExpressionList() {
   1.244 +            StringBuilder buf = new StringBuilder();
   1.245 +            String sep = "";
   1.246 +            for (TypeKind tk : typeKindList) {
   1.247 +                buf.append(sep);
   1.248 +                buf.append(tk.valueString);
   1.249 +                sep = ",";
   1.250 +            }
   1.251 +            return buf.toString();
   1.252 +        }
   1.253 +
   1.254 +        private String asParameterList() {
   1.255 +            StringBuilder buf = new StringBuilder();
   1.256 +            String sep = "";
   1.257 +            int count = 0;
   1.258 +            for (TypeKind arg : typeKindList) {
   1.259 +                buf.append(sep);
   1.260 +                buf.append(arg.typeString);
   1.261 +                if (count == (typeKindList.size() - 1)) {
   1.262 +                    buf.append("...");
   1.263 +                }
   1.264 +                buf.append(" ");
   1.265 +                buf.append("arg" + count++);
   1.266 +                sep = ",";
   1.267 +            }
   1.268 +            return buf.toString();
   1.269 +        }
   1.270 +
   1.271 +        private String asBytecodeString() {
   1.272 +            StringBuilder buf = new StringBuilder();
   1.273 +            int count = 0;
   1.274 +            for (TypeKind arg : typeKindList) {
   1.275 +                if (count == (typeKindList.size() - 1)) {
   1.276 +                    buf.append("[");
   1.277 +                }
   1.278 +                buf.append(arg.bytecodeString);
   1.279 +                count++;
   1.280 +            }
   1.281 +            return buf.toString();
   1.282 +        }
   1.283 +    }
   1.284 +
   1.285 +    static class VarargsMethod {
   1.286 +        TypeConfiguration parameterTypes;
   1.287 +
   1.288 +        public VarargsMethod(TypeConfiguration parameterTypes) {
   1.289 +            this.parameterTypes = parameterTypes;
   1.290 +        }
   1.291 +
   1.292 +        @Override
   1.293 +        public String toString() {
   1.294 +            return "void m( " + parameterTypes.parameterListStr + ") {}";
   1.295 +        }
   1.296 +
   1.297 +        boolean isApplicable(TypeConfiguration that) {
   1.298 +            List<TypeKind> actuals = that.typeKindList;
   1.299 +            List<TypeKind> formals = parameterTypes.typeKindList;
   1.300 +            if ((actuals.size() - formals.size()) < -1)
   1.301 +                return false; //not enough args
   1.302 +            for (TypeKind actual : actuals) {
   1.303 +                if (!actual.isSubtypeOf(formals.head))
   1.304 +                    return false; //type mismatch
   1.305 +                formals = formals.tail.isEmpty() ?
   1.306 +                    formals :
   1.307 +                    formals.tail;
   1.308 +            }
   1.309 +            return true;
   1.310 +        }
   1.311 +
   1.312 +        boolean isMoreSpecificThan(VarargsMethod that) {
   1.313 +            List<TypeKind> actuals = parameterTypes.typeKindList;
   1.314 +            List<TypeKind> formals = that.parameterTypes.typeKindList;
   1.315 +            int checks = 0;
   1.316 +            int expectedCheck = Math.max(actuals.size(), formals.size());
   1.317 +            while (checks < expectedCheck) {
   1.318 +                if (!actuals.head.isSubtypeOf(formals.head))
   1.319 +                    return false; //type mismatch
   1.320 +                formals = formals.tail.isEmpty() ?
   1.321 +                    formals :
   1.322 +                    formals.tail;
   1.323 +                actuals = actuals.tail.isEmpty() ?
   1.324 +                    actuals :
   1.325 +                    actuals.tail;
   1.326 +                checks++;
   1.327 +            }
   1.328 +            return true;
   1.329 +        }
   1.330 +    }
   1.331 +
   1.332 +    static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.333 +
   1.334 +        boolean errorFound;
   1.335 +        List<String> errDiags = List.nil();
   1.336 +
   1.337 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.338 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.339 +                errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
   1.340 +                errorFound = true;
   1.341 +            }
   1.342 +        }
   1.343 +
   1.344 +        String printDiags() {
   1.345 +            StringBuilder buf = new StringBuilder();
   1.346 +            for (String s : errDiags) {
   1.347 +                buf.append(s);
   1.348 +                buf.append("\n");
   1.349 +            }
   1.350 +            return buf.toString();
   1.351 +        }
   1.352 +    }
   1.353 +}

mercurial