Thu, 21 Feb 2013 15:27:05 +0000
8008444: Inherited generic functional descriptors are merged incorrectly
Summary: Missing call to Types.createMethodWithThrownTypes
Reviewed-by: jjg
mcimadamore@787 | 1 | /* |
ohair@962 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@787 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@787 | 4 | * |
mcimadamore@787 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@787 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@787 | 7 | * published by the Free Software Foundation. |
mcimadamore@787 | 8 | * |
mcimadamore@787 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@787 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@787 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@787 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@787 | 13 | * accompanied this code). |
mcimadamore@787 | 14 | * |
mcimadamore@787 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@787 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@787 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@787 | 18 | * |
mcimadamore@787 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@787 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@787 | 21 | * questions. |
mcimadamore@787 | 22 | */ |
mcimadamore@787 | 23 | |
mcimadamore@787 | 24 | /* |
mcimadamore@787 | 25 | * @test |
mcimadamore@787 | 26 | * @bug 6199075 |
mcimadamore@787 | 27 | * |
mcimadamore@787 | 28 | * @summary Unambiguous varargs method calls flagged as ambiguous |
mcimadamore@787 | 29 | * @author mcimadamore |
mcimadamore@787 | 30 | * |
mcimadamore@787 | 31 | */ |
mcimadamore@787 | 32 | |
mcimadamore@787 | 33 | import com.sun.source.util.JavacTask; |
mcimadamore@787 | 34 | import com.sun.tools.classfile.Instruction; |
mcimadamore@787 | 35 | import com.sun.tools.classfile.Attribute; |
mcimadamore@787 | 36 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@787 | 37 | import com.sun.tools.classfile.Code_attribute; |
mcimadamore@787 | 38 | import com.sun.tools.classfile.ConstantPool.*; |
mcimadamore@787 | 39 | import com.sun.tools.classfile.Method; |
jjh@892 | 40 | import com.sun.tools.javac.api.JavacTool; |
mcimadamore@787 | 41 | import com.sun.tools.javac.util.List; |
mcimadamore@787 | 42 | |
mcimadamore@787 | 43 | import java.io.File; |
mcimadamore@787 | 44 | import java.net.URI; |
mcimadamore@787 | 45 | import java.util.Arrays; |
mcimadamore@787 | 46 | import java.util.Locale; |
mcimadamore@787 | 47 | import javax.tools.Diagnostic; |
mcimadamore@787 | 48 | import javax.tools.JavaCompiler; |
mcimadamore@787 | 49 | import javax.tools.JavaFileObject; |
mcimadamore@787 | 50 | import javax.tools.SimpleJavaFileObject; |
jjh@892 | 51 | import javax.tools.StandardJavaFileManager; |
mcimadamore@787 | 52 | import javax.tools.ToolProvider; |
mcimadamore@787 | 53 | |
mcimadamore@787 | 54 | public class T6199075 { |
mcimadamore@787 | 55 | |
mcimadamore@787 | 56 | int checkCount = 0; |
mcimadamore@787 | 57 | int bytecodeCheckCount = 0; |
mcimadamore@787 | 58 | |
mcimadamore@787 | 59 | enum TypeKind { |
mcimadamore@787 | 60 | BYTE("byte", "(byte)1", "[B", 0), |
mcimadamore@787 | 61 | CHAR("char", "'c'", "[C", 1), |
mcimadamore@787 | 62 | SHORT("short", "(short)1", "[S", 2), |
mcimadamore@787 | 63 | INT("int", "1", "[I", 3), |
mcimadamore@787 | 64 | LONG("long", "1L", "[J", 4), |
mcimadamore@787 | 65 | FLOAT("float", "1.0F", "[F", 5), |
mcimadamore@787 | 66 | DOUBLE("double", "1.0D", "[D", 6), |
mcimadamore@787 | 67 | BOOLEAN("boolean", "true", "[Z", -1); |
mcimadamore@787 | 68 | |
mcimadamore@787 | 69 | String typeString; |
mcimadamore@787 | 70 | String valueString; |
mcimadamore@787 | 71 | String bytecodeString; |
mcimadamore@787 | 72 | private int subtypeTag; |
mcimadamore@787 | 73 | |
mcimadamore@787 | 74 | TypeKind(String typeString, String valueString, String bytecodeString, int subtypeTag) { |
mcimadamore@787 | 75 | this.typeString = typeString; |
mcimadamore@787 | 76 | this.valueString = valueString; |
mcimadamore@787 | 77 | this.bytecodeString = bytecodeString; |
mcimadamore@787 | 78 | this.subtypeTag = subtypeTag; |
mcimadamore@787 | 79 | } |
mcimadamore@787 | 80 | |
mcimadamore@787 | 81 | boolean isSubtypeOf(TypeKind that) { |
mcimadamore@787 | 82 | switch (this) { |
mcimadamore@787 | 83 | case BOOLEAN: |
mcimadamore@787 | 84 | return that == BOOLEAN; |
mcimadamore@787 | 85 | case BYTE: |
mcimadamore@787 | 86 | case CHAR: |
mcimadamore@787 | 87 | return this.subtypeTag == that.subtypeTag || |
mcimadamore@787 | 88 | this.subtypeTag + 2 <= that.subtypeTag; |
mcimadamore@787 | 89 | default: |
mcimadamore@787 | 90 | return this.subtypeTag <= that.subtypeTag; |
mcimadamore@787 | 91 | } |
mcimadamore@787 | 92 | } |
mcimadamore@787 | 93 | } |
mcimadamore@787 | 94 | |
mcimadamore@787 | 95 | enum ArgumentsArity { |
mcimadamore@787 | 96 | ZERO(0), |
mcimadamore@787 | 97 | ONE(1), |
mcimadamore@787 | 98 | TWO(2), |
mcimadamore@787 | 99 | THREE(3); |
mcimadamore@787 | 100 | |
mcimadamore@787 | 101 | int arity; |
mcimadamore@787 | 102 | |
mcimadamore@787 | 103 | ArgumentsArity(int arity) { |
mcimadamore@787 | 104 | this.arity = arity; |
mcimadamore@787 | 105 | } |
mcimadamore@787 | 106 | |
mcimadamore@787 | 107 | String asExpressionList(TypeKind type) { |
mcimadamore@787 | 108 | StringBuilder buf = new StringBuilder(); |
mcimadamore@787 | 109 | String sep = ""; |
mcimadamore@787 | 110 | for (int i = 0; i < arity; i++) { |
mcimadamore@787 | 111 | buf.append(sep); |
mcimadamore@787 | 112 | buf.append(type.valueString); |
mcimadamore@787 | 113 | sep = ","; |
mcimadamore@787 | 114 | } |
mcimadamore@787 | 115 | return buf.toString(); |
mcimadamore@787 | 116 | } |
mcimadamore@787 | 117 | } |
mcimadamore@787 | 118 | |
mcimadamore@787 | 119 | static class VarargsMethod { |
mcimadamore@787 | 120 | TypeKind varargsElement; |
mcimadamore@787 | 121 | |
mcimadamore@787 | 122 | VarargsMethod(TypeKind varargsElement) { |
mcimadamore@787 | 123 | this.varargsElement = varargsElement; |
mcimadamore@787 | 124 | } |
mcimadamore@787 | 125 | |
mcimadamore@787 | 126 | @Override |
mcimadamore@787 | 127 | public String toString() { |
mcimadamore@787 | 128 | return "void m("+ varargsElement.typeString+ "... args) {}"; |
mcimadamore@787 | 129 | } |
mcimadamore@787 | 130 | |
mcimadamore@787 | 131 | boolean isApplicable(TypeKind actual, ArgumentsArity argsArity) { |
mcimadamore@787 | 132 | return argsArity == ArgumentsArity.ZERO || |
mcimadamore@787 | 133 | actual.isSubtypeOf(varargsElement); |
mcimadamore@787 | 134 | } |
mcimadamore@787 | 135 | |
mcimadamore@787 | 136 | boolean isMoreSpecificThan(VarargsMethod that) { |
mcimadamore@787 | 137 | return varargsElement.isSubtypeOf(that.varargsElement); |
mcimadamore@787 | 138 | } |
mcimadamore@787 | 139 | } |
mcimadamore@787 | 140 | |
mcimadamore@787 | 141 | public static void main(String... args) throws Exception { |
mcimadamore@787 | 142 | new T6199075().test(); |
mcimadamore@787 | 143 | } |
mcimadamore@787 | 144 | |
mcimadamore@787 | 145 | void test() throws Exception { |
mcimadamore@787 | 146 | for (TypeKind formal1 : TypeKind.values()) { |
mcimadamore@787 | 147 | VarargsMethod m1 = new VarargsMethod(formal1); |
mcimadamore@787 | 148 | for (TypeKind formal2 : TypeKind.values()) { |
mcimadamore@787 | 149 | VarargsMethod m2 = new VarargsMethod(formal2); |
mcimadamore@787 | 150 | for (TypeKind actual : TypeKind.values()) { |
mcimadamore@787 | 151 | for (ArgumentsArity argsArity : ArgumentsArity.values()) { |
mcimadamore@787 | 152 | compileAndCheck(m1, m2, actual, argsArity); |
mcimadamore@787 | 153 | } |
mcimadamore@787 | 154 | } |
mcimadamore@787 | 155 | } |
mcimadamore@787 | 156 | } |
mcimadamore@787 | 157 | |
mcimadamore@787 | 158 | System.out.println("Total checks made: " + checkCount); |
mcimadamore@787 | 159 | System.out.println("Bytecode checks made: " + bytecodeCheckCount); |
mcimadamore@787 | 160 | } |
mcimadamore@787 | 161 | |
jjh@892 | 162 | // Create a single file manager and reuse it for each compile to save time. |
jjh@892 | 163 | StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
jjh@892 | 164 | |
mcimadamore@787 | 165 | void compileAndCheck(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) throws Exception { |
mcimadamore@787 | 166 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@787 | 167 | JavaSource source = new JavaSource(m1, m2, actual, argsArity); |
mcimadamore@787 | 168 | ErrorChecker ec = new ErrorChecker(); |
jjh@892 | 169 | JavacTask ct = (JavacTask)tool.getTask(null, fm, ec, |
mcimadamore@787 | 170 | null, null, Arrays.asList(source)); |
mcimadamore@787 | 171 | ct.generate(); |
mcimadamore@787 | 172 | check(source, ec, m1, m2, actual, argsArity); |
mcimadamore@787 | 173 | } |
mcimadamore@787 | 174 | |
mcimadamore@787 | 175 | void check(JavaSource source, ErrorChecker ec, VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) { |
mcimadamore@787 | 176 | checkCount++; |
mcimadamore@787 | 177 | boolean resolutionError = false; |
mcimadamore@787 | 178 | VarargsMethod selectedMethod = null; |
mcimadamore@787 | 179 | |
mcimadamore@787 | 180 | boolean m1_applicable = m1.isApplicable(actual, argsArity); |
mcimadamore@787 | 181 | boolean m2_applicable = m2.isApplicable(actual, argsArity); |
mcimadamore@787 | 182 | |
mcimadamore@787 | 183 | if (!m1_applicable && !m2_applicable) { |
mcimadamore@787 | 184 | resolutionError = true; |
mcimadamore@787 | 185 | } else if (m1_applicable && m2_applicable) { |
mcimadamore@787 | 186 | //most specific |
mcimadamore@787 | 187 | boolean m1_moreSpecific = m1.isMoreSpecificThan(m2); |
mcimadamore@787 | 188 | boolean m2_moreSpecific = m2.isMoreSpecificThan(m1); |
mcimadamore@787 | 189 | resolutionError = m1_moreSpecific == m2_moreSpecific; |
mcimadamore@787 | 190 | selectedMethod = m1_moreSpecific ? m1 : m2; |
mcimadamore@787 | 191 | } else { |
mcimadamore@787 | 192 | selectedMethod = m1_applicable ? |
mcimadamore@787 | 193 | m1 : m2; |
mcimadamore@787 | 194 | } |
mcimadamore@787 | 195 | |
mcimadamore@787 | 196 | if (ec.errorFound != resolutionError) { |
mcimadamore@787 | 197 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@787 | 198 | source.getCharContent(true) + |
mcimadamore@787 | 199 | "\nExpected resolution error: " + resolutionError + |
mcimadamore@787 | 200 | "\nFound error: " + ec.errorFound + |
mcimadamore@787 | 201 | "\nCompiler diagnostics:\n" + ec.printDiags()); |
mcimadamore@787 | 202 | } else if (!resolutionError) { |
mcimadamore@787 | 203 | verifyBytecode(selectedMethod); |
mcimadamore@787 | 204 | } |
mcimadamore@787 | 205 | } |
mcimadamore@787 | 206 | |
mcimadamore@787 | 207 | void verifyBytecode(VarargsMethod selected) { |
mcimadamore@787 | 208 | bytecodeCheckCount++; |
mcimadamore@787 | 209 | File compiledTest = new File("Test.class"); |
mcimadamore@787 | 210 | try { |
mcimadamore@787 | 211 | ClassFile cf = ClassFile.read(compiledTest); |
mcimadamore@787 | 212 | Method testMethod = null; |
mcimadamore@787 | 213 | for (Method m : cf.methods) { |
mcimadamore@787 | 214 | if (m.getName(cf.constant_pool).equals("test")) { |
mcimadamore@787 | 215 | testMethod = m; |
mcimadamore@787 | 216 | break; |
mcimadamore@787 | 217 | } |
mcimadamore@787 | 218 | } |
mcimadamore@787 | 219 | if (testMethod == null) { |
mcimadamore@787 | 220 | throw new Error("Test method not found"); |
mcimadamore@787 | 221 | } |
mcimadamore@787 | 222 | Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); |
mcimadamore@787 | 223 | if (testMethod == null) { |
mcimadamore@787 | 224 | throw new Error("Code attribute for test() method not found"); |
mcimadamore@787 | 225 | } |
mcimadamore@787 | 226 | |
mcimadamore@787 | 227 | for (Instruction i : ea.getInstructions()) { |
mcimadamore@787 | 228 | if (i.getMnemonic().equals("invokevirtual")) { |
mcimadamore@787 | 229 | int cp_entry = i.getUnsignedShort(1); |
mcimadamore@787 | 230 | CONSTANT_Methodref_info methRef = |
mcimadamore@787 | 231 | (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry); |
mcimadamore@787 | 232 | String type = methRef.getNameAndTypeInfo().getType(); |
mcimadamore@787 | 233 | if (!type.contains(selected.varargsElement.bytecodeString)) { |
mcimadamore@787 | 234 | throw new Error("Unexpected type method call: " + type); |
mcimadamore@787 | 235 | } |
mcimadamore@787 | 236 | break; |
mcimadamore@787 | 237 | } |
mcimadamore@787 | 238 | } |
mcimadamore@787 | 239 | } catch (Exception e) { |
mcimadamore@787 | 240 | e.printStackTrace(); |
mcimadamore@787 | 241 | throw new Error("error reading " + compiledTest +": " + e); |
mcimadamore@787 | 242 | } |
mcimadamore@787 | 243 | } |
mcimadamore@787 | 244 | |
mcimadamore@787 | 245 | static class JavaSource extends SimpleJavaFileObject { |
mcimadamore@787 | 246 | |
mcimadamore@787 | 247 | static final String source_template = "class Test {\n" + |
mcimadamore@787 | 248 | " #V1\n" + |
mcimadamore@787 | 249 | " #V2\n" + |
mcimadamore@787 | 250 | " void test() { m(#E); }\n" + |
mcimadamore@787 | 251 | "}"; |
mcimadamore@787 | 252 | |
mcimadamore@787 | 253 | String source; |
mcimadamore@787 | 254 | |
mcimadamore@787 | 255 | public JavaSource(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) { |
mcimadamore@787 | 256 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@787 | 257 | source = source_template.replaceAll("#V1", m1.toString()). |
mcimadamore@787 | 258 | replaceAll("#V2", m2.toString()). |
mcimadamore@787 | 259 | replaceAll("#E", argsArity.asExpressionList(actual)); |
mcimadamore@787 | 260 | } |
mcimadamore@787 | 261 | |
mcimadamore@787 | 262 | @Override |
mcimadamore@787 | 263 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@787 | 264 | return source; |
mcimadamore@787 | 265 | } |
mcimadamore@787 | 266 | } |
mcimadamore@787 | 267 | |
mcimadamore@787 | 268 | static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@787 | 269 | |
mcimadamore@787 | 270 | boolean errorFound; |
mcimadamore@787 | 271 | List<String> errDiags = List.nil(); |
mcimadamore@787 | 272 | |
mcimadamore@787 | 273 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@787 | 274 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@787 | 275 | errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault())); |
mcimadamore@787 | 276 | errorFound = true; |
mcimadamore@787 | 277 | } |
mcimadamore@787 | 278 | } |
mcimadamore@787 | 279 | |
mcimadamore@787 | 280 | String printDiags() { |
mcimadamore@787 | 281 | StringBuilder buf = new StringBuilder(); |
mcimadamore@787 | 282 | for (String s : errDiags) { |
mcimadamore@787 | 283 | buf.append(s); |
mcimadamore@787 | 284 | buf.append("\n"); |
mcimadamore@787 | 285 | } |
mcimadamore@787 | 286 | return buf.toString(); |
mcimadamore@787 | 287 | } |
mcimadamore@787 | 288 | } |
mcimadamore@787 | 289 | } |