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