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