Wed, 11 Apr 2012 10:50:11 +0100
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
Summary: Remove bound checking rules from recursive subtype visitors in Types.java and replace with centralized bound-checking logic
Reviewed-by: jjg, dlsmith
mcimadamore@1087 | 1 | /* |
mcimadamore@1087 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1087 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1087 | 4 | * |
mcimadamore@1087 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1087 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1087 | 7 | * published by the Free Software Foundation. |
mcimadamore@1087 | 8 | * |
mcimadamore@1087 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1087 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1087 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1087 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1087 | 13 | * accompanied this code). |
mcimadamore@1087 | 14 | * |
mcimadamore@1087 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1087 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1087 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1087 | 18 | * |
mcimadamore@1087 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1087 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1087 | 21 | * questions. |
mcimadamore@1087 | 22 | */ |
mcimadamore@1087 | 23 | |
mcimadamore@1087 | 24 | /* |
mcimadamore@1087 | 25 | * @test |
mcimadamore@1087 | 26 | * @bug 7086601 |
mcimadamore@1087 | 27 | * @summary Error message bug: cause for method mismatch is 'null' |
mcimadamore@1087 | 28 | */ |
mcimadamore@1087 | 29 | |
mcimadamore@1087 | 30 | import com.sun.source.util.JavacTask; |
mcimadamore@1087 | 31 | import java.net.URI; |
mcimadamore@1087 | 32 | import java.util.Arrays; |
mcimadamore@1087 | 33 | import java.util.ArrayList; |
mcimadamore@1087 | 34 | import javax.tools.Diagnostic; |
mcimadamore@1087 | 35 | import javax.tools.JavaCompiler; |
mcimadamore@1087 | 36 | import javax.tools.JavaFileObject; |
mcimadamore@1087 | 37 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1087 | 38 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1087 | 39 | import javax.tools.ToolProvider; |
mcimadamore@1087 | 40 | |
mcimadamore@1087 | 41 | |
mcimadamore@1087 | 42 | public class T7086601b { |
mcimadamore@1087 | 43 | |
mcimadamore@1087 | 44 | static int checkCount = 0; |
mcimadamore@1087 | 45 | |
mcimadamore@1087 | 46 | enum TypeKind { |
mcimadamore@1087 | 47 | STRING("String", false), |
mcimadamore@1087 | 48 | INTEGER("Integer", false), |
mcimadamore@1087 | 49 | NUMBER("Number", false), |
mcimadamore@1087 | 50 | SERIALIZABLE("java.io.Serializable", true), |
mcimadamore@1087 | 51 | CLONEABLE("Cloneable", true), |
mcimadamore@1087 | 52 | X("X", false), |
mcimadamore@1087 | 53 | Y("Y", false), |
mcimadamore@1087 | 54 | Z("Z", false); |
mcimadamore@1087 | 55 | |
mcimadamore@1087 | 56 | String typeStr; |
mcimadamore@1087 | 57 | boolean isInterface; |
mcimadamore@1087 | 58 | |
mcimadamore@1087 | 59 | private TypeKind(String typeStr, boolean isInterface) { |
mcimadamore@1087 | 60 | this.typeStr = typeStr; |
mcimadamore@1087 | 61 | this.isInterface = isInterface; |
mcimadamore@1087 | 62 | } |
mcimadamore@1087 | 63 | |
mcimadamore@1087 | 64 | boolean isSubtypeof(TypeKind other) { |
mcimadamore@1087 | 65 | return (this == INTEGER && other == NUMBER || |
mcimadamore@1087 | 66 | this == Z && other == Y || |
mcimadamore@1087 | 67 | this == other); |
mcimadamore@1087 | 68 | } |
mcimadamore@1087 | 69 | } |
mcimadamore@1087 | 70 | |
mcimadamore@1087 | 71 | enum MethodCallKind { |
mcimadamore@1087 | 72 | ARITY_ONE("m(a1);", 1), |
mcimadamore@1087 | 73 | ARITY_TWO("m(a1, a2);", 2), |
mcimadamore@1087 | 74 | ARITY_THREE("m(a1, a2, a3);", 3); |
mcimadamore@1087 | 75 | |
mcimadamore@1087 | 76 | String invokeString; |
mcimadamore@1087 | 77 | int arity; |
mcimadamore@1087 | 78 | |
mcimadamore@1087 | 79 | private MethodCallKind(String invokeString, int arity) { |
mcimadamore@1087 | 80 | this.invokeString = invokeString; |
mcimadamore@1087 | 81 | this.arity = arity; |
mcimadamore@1087 | 82 | } |
mcimadamore@1087 | 83 | } |
mcimadamore@1087 | 84 | |
mcimadamore@1087 | 85 | public static void main(String... args) throws Exception { |
mcimadamore@1087 | 86 | |
mcimadamore@1087 | 87 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1087 | 88 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1087 | 89 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1087 | 90 | |
mcimadamore@1087 | 91 | for (TypeKind a1 : TypeKind.values()) { |
mcimadamore@1087 | 92 | for (TypeKind a2 : TypeKind.values()) { |
mcimadamore@1087 | 93 | for (TypeKind a3 : TypeKind.values()) { |
mcimadamore@1087 | 94 | for (MethodCallKind mck : MethodCallKind.values()) { |
mcimadamore@1087 | 95 | new T7086601b(a1, a2, a3, mck).run(comp, fm); |
mcimadamore@1087 | 96 | } |
mcimadamore@1087 | 97 | } |
mcimadamore@1087 | 98 | } |
mcimadamore@1087 | 99 | } |
mcimadamore@1087 | 100 | System.out.println("Total check executed: " + checkCount); |
mcimadamore@1087 | 101 | } |
mcimadamore@1087 | 102 | |
mcimadamore@1087 | 103 | TypeKind a1; |
mcimadamore@1087 | 104 | TypeKind a2; |
mcimadamore@1087 | 105 | TypeKind a3; |
mcimadamore@1087 | 106 | MethodCallKind mck; |
mcimadamore@1087 | 107 | JavaSource source; |
mcimadamore@1087 | 108 | DiagnosticChecker diagChecker; |
mcimadamore@1087 | 109 | |
mcimadamore@1087 | 110 | T7086601b(TypeKind a1, TypeKind a2, TypeKind a3, MethodCallKind mck) { |
mcimadamore@1087 | 111 | this.a1 = a1; |
mcimadamore@1087 | 112 | this.a2 = a2; |
mcimadamore@1087 | 113 | this.a3 = a3; |
mcimadamore@1087 | 114 | this.mck = mck; |
mcimadamore@1087 | 115 | this.source = new JavaSource(); |
mcimadamore@1087 | 116 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1087 | 117 | } |
mcimadamore@1087 | 118 | |
mcimadamore@1087 | 119 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1087 | 120 | |
mcimadamore@1087 | 121 | final String bodyTemplate = "import java.util.List;\n"+ |
mcimadamore@1087 | 122 | "class Test {\n" + |
mcimadamore@1087 | 123 | " <Z> void m(List<? super Z> l1) { }\n" + |
mcimadamore@1087 | 124 | " <Z> void m(List<? super Z> l1, List<? super Z> l2) { }\n" + |
mcimadamore@1087 | 125 | " <Z> void m(List<? super Z> l1, List<? super Z> l2, List<? super Z> l3) { }\n" + |
mcimadamore@1087 | 126 | " <X,Y,Z extends Y> void test(List<#A1> a1, List<#A2> a2, List<#A3> a3) { #MC } }"; |
mcimadamore@1087 | 127 | |
mcimadamore@1087 | 128 | String source; |
mcimadamore@1087 | 129 | |
mcimadamore@1087 | 130 | public JavaSource() { |
mcimadamore@1087 | 131 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1087 | 132 | source = bodyTemplate.replace("#A1", a1.typeStr) |
mcimadamore@1087 | 133 | .replace("#A2", a2.typeStr).replace("#A3", a3.typeStr) |
mcimadamore@1087 | 134 | .replace("#MC", mck.invokeString); |
mcimadamore@1087 | 135 | } |
mcimadamore@1087 | 136 | |
mcimadamore@1087 | 137 | @Override |
mcimadamore@1087 | 138 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1087 | 139 | return source; |
mcimadamore@1087 | 140 | } |
mcimadamore@1087 | 141 | } |
mcimadamore@1087 | 142 | |
mcimadamore@1087 | 143 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1087 | 144 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1087 | 145 | null, null, Arrays.asList(source)); |
mcimadamore@1087 | 146 | try { |
mcimadamore@1087 | 147 | ct.analyze(); |
mcimadamore@1087 | 148 | } catch (Throwable ex) { |
jjg@1169 | 149 | throw new AssertionError("Error thrown when compiling the following code:\n" + source.getCharContent(true)); |
mcimadamore@1087 | 150 | } |
mcimadamore@1087 | 151 | check(); |
mcimadamore@1087 | 152 | } |
mcimadamore@1087 | 153 | |
mcimadamore@1087 | 154 | void check() { |
mcimadamore@1087 | 155 | checkCount++; |
mcimadamore@1087 | 156 | |
mcimadamore@1087 | 157 | boolean errorExpected = false; |
mcimadamore@1087 | 158 | |
mcimadamore@1087 | 159 | if (mck.arity > 1) { |
mcimadamore@1087 | 160 | TypeKind[] argtypes = { a1, a2, a3 }; |
mcimadamore@1087 | 161 | ArrayList<TypeKind> classes = new ArrayList<>(); |
mcimadamore@1087 | 162 | for (int i = 0 ; i < mck.arity ; i ++ ) { |
mcimadamore@1087 | 163 | if (!argtypes[i].isInterface) { |
mcimadamore@1087 | 164 | classes.add(argtypes[i]); |
mcimadamore@1087 | 165 | } |
mcimadamore@1087 | 166 | } |
mcimadamore@1087 | 167 | boolean glb_exists = true; |
mcimadamore@1087 | 168 | for (TypeKind arg_i : classes) { |
mcimadamore@1087 | 169 | glb_exists = true; |
mcimadamore@1087 | 170 | for (TypeKind arg_j : classes) { |
mcimadamore@1087 | 171 | if (!arg_i.isSubtypeof(arg_j)) { |
mcimadamore@1087 | 172 | glb_exists = false; |
mcimadamore@1087 | 173 | break; |
mcimadamore@1087 | 174 | } |
mcimadamore@1087 | 175 | } |
mcimadamore@1087 | 176 | if (glb_exists) break; |
mcimadamore@1087 | 177 | } |
mcimadamore@1087 | 178 | errorExpected = !glb_exists; |
mcimadamore@1087 | 179 | } |
mcimadamore@1087 | 180 | |
mcimadamore@1087 | 181 | if (errorExpected != diagChecker.errorFound) { |
mcimadamore@1087 | 182 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@1087 | 183 | source.getCharContent(true) + |
mcimadamore@1087 | 184 | "\nFound error: " + diagChecker.errorFound + |
mcimadamore@1087 | 185 | "\nExpected error: " + errorExpected); |
mcimadamore@1087 | 186 | } |
mcimadamore@1087 | 187 | } |
mcimadamore@1087 | 188 | |
mcimadamore@1087 | 189 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1087 | 190 | |
mcimadamore@1087 | 191 | boolean errorFound; |
mcimadamore@1087 | 192 | |
mcimadamore@1087 | 193 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1087 | 194 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1087 | 195 | errorFound = true; |
mcimadamore@1087 | 196 | } |
mcimadamore@1087 | 197 | } |
mcimadamore@1087 | 198 | } |
mcimadamore@1087 | 199 | } |