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

changeset 787
b1c98bfd4709
child 892
3e30c95da3c6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/varargs/6199075/T6199075.java	Fri Dec 10 15:24:17 2010 +0000
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 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 6199075
    1.30 + *
    1.31 + * @summary Unambiguous varargs method calls flagged as ambiguous
    1.32 + * @author mcimadamore
    1.33 + *
    1.34 + */
    1.35 +
    1.36 +import com.sun.source.util.JavacTask;
    1.37 +import com.sun.tools.classfile.Instruction;
    1.38 +import com.sun.tools.classfile.Attribute;
    1.39 +import com.sun.tools.classfile.ClassFile;
    1.40 +import com.sun.tools.classfile.Code_attribute;
    1.41 +import com.sun.tools.classfile.ConstantPool.*;
    1.42 +import com.sun.tools.classfile.Method;
    1.43 +import com.sun.tools.javac.util.List;
    1.44 +
    1.45 +import java.io.File;
    1.46 +import java.net.URI;
    1.47 +import java.util.Arrays;
    1.48 +import java.util.Locale;
    1.49 +import javax.tools.Diagnostic;
    1.50 +import javax.tools.JavaCompiler;
    1.51 +import javax.tools.JavaFileObject;
    1.52 +import javax.tools.SimpleJavaFileObject;
    1.53 +import javax.tools.ToolProvider;
    1.54 +
    1.55 +public class T6199075 {
    1.56 +
    1.57 +    int checkCount = 0;
    1.58 +    int bytecodeCheckCount = 0;
    1.59 +
    1.60 +    enum TypeKind {
    1.61 +        BYTE("byte", "(byte)1", "[B", 0),
    1.62 +        CHAR("char", "'c'", "[C", 1),
    1.63 +        SHORT("short", "(short)1", "[S", 2),
    1.64 +        INT("int", "1", "[I", 3),
    1.65 +        LONG("long", "1L", "[J", 4),
    1.66 +        FLOAT("float", "1.0F", "[F", 5),
    1.67 +        DOUBLE("double", "1.0D", "[D", 6),
    1.68 +        BOOLEAN("boolean", "true", "[Z", -1);
    1.69 +
    1.70 +        String typeString;
    1.71 +        String valueString;
    1.72 +        String bytecodeString;
    1.73 +        private int subtypeTag;
    1.74 +
    1.75 +        TypeKind(String typeString, String valueString, String bytecodeString, int subtypeTag) {
    1.76 +            this.typeString = typeString;
    1.77 +            this.valueString = valueString;
    1.78 +            this.bytecodeString = bytecodeString;
    1.79 +            this.subtypeTag = subtypeTag;
    1.80 +        }
    1.81 +
    1.82 +        boolean isSubtypeOf(TypeKind that) {
    1.83 +            switch (this) {
    1.84 +                case BOOLEAN:
    1.85 +                    return that == BOOLEAN;
    1.86 +                case BYTE:
    1.87 +                case CHAR:
    1.88 +                    return this.subtypeTag == that.subtypeTag ||
    1.89 +                            this.subtypeTag + 2 <= that.subtypeTag;
    1.90 +                default:
    1.91 +                    return this.subtypeTag <= that.subtypeTag;
    1.92 +            }
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    enum ArgumentsArity {
    1.97 +        ZERO(0),
    1.98 +        ONE(1),
    1.99 +        TWO(2),
   1.100 +        THREE(3);
   1.101 +
   1.102 +        int arity;
   1.103 +
   1.104 +        ArgumentsArity(int arity) {
   1.105 +            this.arity = arity;
   1.106 +        }
   1.107 +
   1.108 +        String asExpressionList(TypeKind type) {
   1.109 +            StringBuilder buf = new StringBuilder();
   1.110 +            String sep = "";
   1.111 +            for (int i = 0; i < arity; i++) {
   1.112 +                buf.append(sep);
   1.113 +                buf.append(type.valueString);
   1.114 +                sep = ",";
   1.115 +            }
   1.116 +            return buf.toString();
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    static class VarargsMethod {
   1.121 +        TypeKind varargsElement;
   1.122 +
   1.123 +        VarargsMethod(TypeKind varargsElement) {
   1.124 +            this.varargsElement = varargsElement;
   1.125 +        }
   1.126 +
   1.127 +        @Override
   1.128 +        public String toString() {
   1.129 +            return "void m("+ varargsElement.typeString+ "... args) {}";
   1.130 +        }
   1.131 +
   1.132 +        boolean isApplicable(TypeKind actual, ArgumentsArity argsArity) {
   1.133 +            return argsArity == ArgumentsArity.ZERO ||
   1.134 +                    actual.isSubtypeOf(varargsElement);
   1.135 +        }
   1.136 +
   1.137 +        boolean isMoreSpecificThan(VarargsMethod that) {
   1.138 +            return varargsElement.isSubtypeOf(that.varargsElement);
   1.139 +        }
   1.140 +    }
   1.141 +
   1.142 +    public static void main(String... args) throws Exception {
   1.143 +        new T6199075().test();
   1.144 +    }
   1.145 +
   1.146 +    void test() throws Exception {
   1.147 +        for (TypeKind formal1 : TypeKind.values()) {
   1.148 +            VarargsMethod m1 = new VarargsMethod(formal1);
   1.149 +            for (TypeKind formal2 : TypeKind.values()) {
   1.150 +                VarargsMethod m2 = new VarargsMethod(formal2);
   1.151 +                for (TypeKind actual : TypeKind.values()) {
   1.152 +                    for (ArgumentsArity argsArity : ArgumentsArity.values()) {
   1.153 +                        compileAndCheck(m1, m2, actual, argsArity);
   1.154 +                    }
   1.155 +                }
   1.156 +            }
   1.157 +        }
   1.158 +
   1.159 +        System.out.println("Total checks made: " + checkCount);
   1.160 +        System.out.println("Bytecode checks made: " + bytecodeCheckCount);
   1.161 +    }
   1.162 +
   1.163 +    void compileAndCheck(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) throws Exception {
   1.164 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.165 +        JavaSource source = new JavaSource(m1, m2, actual, argsArity);
   1.166 +        ErrorChecker ec = new ErrorChecker();
   1.167 +        JavacTask ct = (JavacTask)tool.getTask(null, null, ec,
   1.168 +                null, null, Arrays.asList(source));
   1.169 +        ct.generate();
   1.170 +        check(source, ec, m1, m2, actual, argsArity);
   1.171 +    }
   1.172 +
   1.173 +    void check(JavaSource source, ErrorChecker ec, VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) {
   1.174 +        checkCount++;
   1.175 +        boolean resolutionError = false;
   1.176 +        VarargsMethod selectedMethod = null;
   1.177 +
   1.178 +        boolean m1_applicable = m1.isApplicable(actual, argsArity);
   1.179 +        boolean m2_applicable = m2.isApplicable(actual, argsArity);
   1.180 +
   1.181 +        if (!m1_applicable && !m2_applicable) {
   1.182 +            resolutionError = true;
   1.183 +        } else if (m1_applicable && m2_applicable) {
   1.184 +            //most specific
   1.185 +            boolean m1_moreSpecific = m1.isMoreSpecificThan(m2);
   1.186 +            boolean m2_moreSpecific = m2.isMoreSpecificThan(m1);
   1.187 +            resolutionError = m1_moreSpecific == m2_moreSpecific;
   1.188 +            selectedMethod = m1_moreSpecific ? m1 : m2;
   1.189 +        } else {
   1.190 +            selectedMethod = m1_applicable ?
   1.191 +                m1 : m2;
   1.192 +        }
   1.193 +
   1.194 +        if (ec.errorFound != resolutionError) {
   1.195 +            throw new Error("invalid diagnostics for source:\n" +
   1.196 +                    source.getCharContent(true) +
   1.197 +                    "\nExpected resolution error: " + resolutionError +
   1.198 +                    "\nFound error: " + ec.errorFound +
   1.199 +                    "\nCompiler diagnostics:\n" + ec.printDiags());
   1.200 +        } else if (!resolutionError) {
   1.201 +            verifyBytecode(selectedMethod);
   1.202 +        }
   1.203 +    }
   1.204 +
   1.205 +    void verifyBytecode(VarargsMethod selected) {
   1.206 +        bytecodeCheckCount++;
   1.207 +        File compiledTest = new File("Test.class");
   1.208 +        try {
   1.209 +            ClassFile cf = ClassFile.read(compiledTest);
   1.210 +            Method testMethod = null;
   1.211 +            for (Method m : cf.methods) {
   1.212 +                if (m.getName(cf.constant_pool).equals("test")) {
   1.213 +                    testMethod = m;
   1.214 +                    break;
   1.215 +                }
   1.216 +            }
   1.217 +            if (testMethod == null) {
   1.218 +                throw new Error("Test method not found");
   1.219 +            }
   1.220 +            Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code);
   1.221 +            if (testMethod == null) {
   1.222 +                throw new Error("Code attribute for test() method not found");
   1.223 +            }
   1.224 +
   1.225 +            for (Instruction i : ea.getInstructions()) {
   1.226 +                if (i.getMnemonic().equals("invokevirtual")) {
   1.227 +                    int cp_entry = i.getUnsignedShort(1);
   1.228 +                    CONSTANT_Methodref_info methRef =
   1.229 +                            (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   1.230 +                    String type = methRef.getNameAndTypeInfo().getType();
   1.231 +                    if (!type.contains(selected.varargsElement.bytecodeString)) {
   1.232 +                        throw new Error("Unexpected type method call: " + type);
   1.233 +                    }
   1.234 +                    break;
   1.235 +                }
   1.236 +            }
   1.237 +        } catch (Exception e) {
   1.238 +            e.printStackTrace();
   1.239 +            throw new Error("error reading " + compiledTest +": " + e);
   1.240 +        }
   1.241 +    }
   1.242 +
   1.243 +    static class JavaSource extends SimpleJavaFileObject {
   1.244 +
   1.245 +        static final String source_template = "class Test {\n" +
   1.246 +                "   #V1\n" +
   1.247 +                "   #V2\n" +
   1.248 +                "   void test() { m(#E); }\n" +
   1.249 +                "}";
   1.250 +
   1.251 +        String source;
   1.252 +
   1.253 +        public JavaSource(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) {
   1.254 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.255 +            source = source_template.replaceAll("#V1", m1.toString()).
   1.256 +                    replaceAll("#V2", m2.toString()).
   1.257 +                    replaceAll("#E", argsArity.asExpressionList(actual));
   1.258 +        }
   1.259 +
   1.260 +        @Override
   1.261 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.262 +            return source;
   1.263 +        }
   1.264 +    }
   1.265 +
   1.266 +    static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.267 +
   1.268 +        boolean errorFound;
   1.269 +        List<String> errDiags = List.nil();
   1.270 +
   1.271 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.272 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.273 +                errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
   1.274 +                errorFound = true;
   1.275 +            }
   1.276 +        }
   1.277 +
   1.278 +        String printDiags() {
   1.279 +            StringBuilder buf = new StringBuilder();
   1.280 +            for (String s : errDiags) {
   1.281 +                buf.append(s);
   1.282 +                buf.append("\n");
   1.283 +            }
   1.284 +            return buf.toString();
   1.285 +        }
   1.286 +    }
   1.287 +}

mercurial