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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (c) 2012, 2016, 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 8003280 8006694 8129962
* @summary Add lambda tests
* Automatic test for checking correctness of structural most specific test routine
* temporarily workaround combo tests are causing time out in several platforms
* @library /tools/javac/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.code
* jdk.compiler/com.sun.tools.javac.comp
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.tree
* jdk.compiler/com.sun.tools.javac.util
* @build combo.ComboTestHelper
* @run main StructuralMostSpecificTest
*/
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.List;
import combo.ComboInstance;
import combo.ComboParameter;
import combo.ComboTask.Result;
import combo.ComboTestHelper;
public class StructuralMostSpecificTest extends ComboInstance<StructuralMostSpecificTest> {
enum RetTypeKind implements ComboParameter {
SHORT("short"),
INT("int"),
OBJECT("Object"),
INTEGER("Integer"),
VOID("void"),
J_L_VOID("Void");
String retTypeStr;
RetTypeKind(String retTypeStr) {
this.retTypeStr = retTypeStr;
}
boolean moreSpecificThan(RetTypeKind rk) {
return moreSpecificThan[this.ordinal()][rk.ordinal()];
}
static boolean[][] moreSpecificThan = {
// SHORT | INT | OBJECT | INTEGER | VOID | J_L_VOID
/* SHORT */ { true , true , true , false , false , false },
/* INT */ { false , true , true , true , false , false },
/* OBJECT */ { false , false , true , false , false , false },
/* INTEGER */ { false , false , true , true , false , false },
/* VOID */ { false , false , false , false , true , true },
/* J_L_VOID */{ false , false , true , false , false , true } };
public String expand(String optParameter) {
return retTypeStr;
}
}
enum ArgTypeKind implements ComboParameter {
SHORT("short"),
INT("int"),
BOOLEAN("boolean"),
OBJECT("Object"),
INTEGER("Integer"),
DOUBLE("Double");
String argTypeStr;
ArgTypeKind(String typeStr) {
this.argTypeStr = typeStr;
}
public String expand(String optParameter) {
return argTypeStr;
}
}
enum ExceptionKind implements ComboParameter {
NONE(""),
EXCEPTION("throws Exception"),
SQL_EXCEPTION("throws java.sql.SQLException"),
IO_EXCEPTION("throws java.io.IOException");
String exceptionStr;
ExceptionKind(String exceptionStr) {
this.exceptionStr = exceptionStr;
}
public String expand(String optParameter) {
return exceptionStr;
}
}
enum LambdaReturnKind implements ComboParameter {
VOID("return;"),
SHORT("return (short)0;"),
INT("return 0;"),
INTEGER("return (Integer)null;"),
NULL("return null;");
String retStr;
LambdaReturnKind(String retStr) {
this.retStr = retStr;
}
boolean compatibleWith(RetTypeKind rk) {
return compatibleWith[rk.ordinal()][ordinal()];
}
static boolean[][] compatibleWith = {
// VOID | SHORT | INT | INTEGER | NULL
/* SHORT */ { false , true , false , false , false },
/* INT */ { false , true , true , true , false },
/* OBJECT */ { false , true , true , true , true },
/* INTEGER */ { false , false , true , true , true },
/* VOID */ { true , false , false , false , false },
/* J_L_VOID */{ false , false , false , false , true } };
boolean needsConversion(RetTypeKind rk) {
return needsConversion[rk.ordinal()][ordinal()];
}
static boolean[][] needsConversion = {
// VOID | SHORT | INT | INTEGER | NULL
/* SHORT */ { false , false , false , false , false },
/* INT */ { false , false , false , true , false },
/* OBJECT */ { false , true , true , false , false },
/* INTEGER */ { false , false , true , false , false },
/* VOID */ { false , false , false , false , false },
/* J_L_VOID */{ true , false , false , false , false } };
public String expand(String optParameter) {
return retStr;
}
}
static final String sourceTemplate =
"interface SAM1 {\n" +
" #{RET[0]} m(#{ARG[0]} a1) #{EX[0]};\n" +
"}\n" +
"interface SAM2 {\n" +
" #{RET[1]} m(#{ARG[1]} a1) #{EX[1]};\n" +
"}\n" +
"class Test {\n" +
" void m(SAM1 s) { }\n" +
" void m(SAM2 s) { }\n" +
" { m((#{ARG[0]} x)->{ #{EXPR} }); }\n" +
"}\n";
public static void main(String... args) throws Exception {
new ComboTestHelper<StructuralMostSpecificTest>()
.withFilter(StructuralMostSpecificTest::hasSameArguments)
.withFilter(StructuralMostSpecificTest::hasCompatibleReturns)
.withFilter(StructuralMostSpecificTest::hasSameOverloadPhase)
.withDimension("EXPR", (x, expr) -> x.lambdaReturnKind = expr, LambdaReturnKind.values())
.withArrayDimension("RET", (x, ret, idx) -> x.returnType[idx] = ret, 2, RetTypeKind.values())
.withArrayDimension("EX", 2, ExceptionKind.values())
.withArrayDimension("ARG", (x, arg, idx) -> x.argumentKind[idx] = arg, 2, ArgTypeKind.values())
.run(StructuralMostSpecificTest::new);
}
LambdaReturnKind lambdaReturnKind;
RetTypeKind[] returnType = new RetTypeKind[2];
ArgTypeKind[] argumentKind = new ArgTypeKind[2];
boolean hasSameArguments() {
return argumentKind[0] == argumentKind[1];
}
boolean hasCompatibleReturns() {
return lambdaReturnKind.compatibleWith(returnType[0]) &&
lambdaReturnKind.compatibleWith(returnType[1]);
}
boolean hasSameOverloadPhase() {
return lambdaReturnKind.needsConversion(returnType[0]) == lambdaReturnKind.needsConversion(returnType[1]);
}
@Override
public void doWork() throws Throwable {
check(newCompilationTask()
.withSourceFromTemplate(sourceTemplate)
.withOption("--debug:verboseResolution=all,-predef,-internal,-object-init")
.analyze());
}
void check(Result<Iterable<? extends Element>> result) {
boolean m1MoreSpecific = returnType[0].moreSpecificThan(returnType[1]);
boolean m2MoreSpecific = returnType[1].moreSpecificThan(returnType[0]);
boolean ambiguous = (m1MoreSpecific == m2MoreSpecific);
if (ambiguous != ambiguityFound(result)) {
fail("invalid diagnostics for combo:\n" +
result.compilationInfo() + "\n" +
"\nAmbiguity found: " + ambiguityFound(result) +
"\nm1 more specific: " + m1MoreSpecific +
"\nm2 more specific: " + m2MoreSpecific +
"\nexpected ambiguity: " + ambiguous);
}
if (!ambiguous) {
String sigToCheck = m1MoreSpecific ? "m(SAM1)" : "m(SAM2)";
if (!sigToCheck.equals(mostSpecificSignature(result))) {
fail("invalid most specific method selected:\n" +
result.compilationInfo() + "\n" +
"\nMost specific found: " + mostSpecificSignature(result) +
"\nm1 more specific: " + m1MoreSpecific +
"\nm2 more specific: " + m2MoreSpecific);
}
}
}
boolean ambiguityFound(Result<Iterable<? extends Element>> result) {
return result.containsKey("compiler.err.ref.ambiguous");
}
String mostSpecificSignature(Result<Iterable<? extends Element>> result) {
List<Diagnostic<? extends JavaFileObject>> rsDiag =
result.diagnosticsForKey("compiler.note.verbose.resolve.multi");
if (rsDiag.nonEmpty()) {
ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
(ClientCodeWrapper.DiagnosticSourceUnwrapper)rsDiag.head;
JCDiagnostic.MultilineDiagnostic mdiag =
(JCDiagnostic.MultilineDiagnostic)dsu.d;
int mostSpecificIndex = (Integer)mdiag.getArgs()[2];
return mdiag.getSubdiagnostics().get(mostSpecificIndex).getArgs()[1].toString();
} else {
return null;
}
}
}
|