test/tools/javac/lambda/methodReference/SamConversionComboTest.java

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 1415
01c9d4161882
child 2227
998b10c43157
permissions
-rw-r--r--

8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg

mcimadamore@1415 1 /*
mcimadamore@1415 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /**
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Test SAM conversion of method references in combinations of different contexts,
mcimadamore@1415 29 * lambda body types(statement/expression), boxing/unboxing etc, to verify
mcimadamore@1415 30 * SAM conversion being conducted successfully as expected.
mcimadamore@1415 31 */
mcimadamore@1415 32
mcimadamore@1415 33 import com.sun.source.util.JavacTask;
mcimadamore@1415 34 import java.net.URI;
mcimadamore@1415 35 import java.util.Arrays;
mcimadamore@1415 36 import javax.tools.Diagnostic;
mcimadamore@1415 37 import javax.tools.JavaCompiler;
mcimadamore@1415 38 import javax.tools.JavaFileObject;
mcimadamore@1415 39 import javax.tools.SimpleJavaFileObject;
mcimadamore@1415 40 import javax.tools.ToolProvider;
mcimadamore@1415 41 import javax.tools.StandardJavaFileManager;
mcimadamore@1415 42
mcimadamore@1415 43 public class SamConversionComboTest {
mcimadamore@1415 44
mcimadamore@1415 45 enum FInterface {
mcimadamore@1415 46 A("A", "interface A { Integer m(int i); }"),
mcimadamore@1415 47 B("B", "interface B { int m(Integer i); }"),
mcimadamore@1415 48 C("C", "interface C { int m(int i) throws Exception; }");
mcimadamore@1415 49
mcimadamore@1415 50 String interfaceType;
mcimadamore@1415 51 String interfaceDef;
mcimadamore@1415 52
mcimadamore@1415 53 FInterface(String interfaceType, String interfaceDef) {
mcimadamore@1415 54 this.interfaceType = interfaceType;
mcimadamore@1415 55 this.interfaceDef = interfaceDef;
mcimadamore@1415 56 }
mcimadamore@1415 57 }
mcimadamore@1415 58
mcimadamore@1415 59 enum Context {
mcimadamore@1415 60 ASSIGNMENT("#FType f = #MR;"),
mcimadamore@1415 61 METHOD_CALL("void method1(#FType f) { }\n" +
mcimadamore@1415 62 "void method2() {\n" +
mcimadamore@1415 63 " method1(#MR);\n" +
mcimadamore@1415 64 "}"),
mcimadamore@1415 65 CONSTRUCTOR("X x = new X(#MR);"),
mcimadamore@1415 66 RETURN_OF_METHOD("#FType method1() {\n" +
mcimadamore@1415 67 " return #MR;\n" +
mcimadamore@1415 68 "}"),
mcimadamore@1415 69 ARRAY_INITIALIZER("#FType[] oarray = {#MR};"),
mcimadamore@1415 70 LAMBDA_BODY("#FType f = n -> ((#FType)#MR).m(n);"),
mcimadamore@1415 71 CAST("void test() throws Exception { int n = ((#FType)#MR).m(1); }"),
mcimadamore@1415 72 CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #MR : null;");
mcimadamore@1415 73
mcimadamore@1415 74 String context;
mcimadamore@1415 75
mcimadamore@1415 76 Context(String context) {
mcimadamore@1415 77 this.context = context;
mcimadamore@1415 78 }
mcimadamore@1415 79
mcimadamore@1415 80 String getContext(FInterface f, MethodReference mr) {
mcimadamore@1415 81 return context.replace("#FType", f.interfaceType).replace("#MR", mr.mrValue);
mcimadamore@1415 82 }
mcimadamore@1415 83 }
mcimadamore@1415 84
mcimadamore@1415 85 enum MethodReference {
mcimadamore@1415 86 METHOD1("X::method1"),
mcimadamore@1415 87 METHOD2("new X()::method2"),
mcimadamore@1415 88 METHOD3("X::method3"),
mcimadamore@1415 89 METHOD4("new X()::method4"),
mcimadamore@1415 90 METHOD5("new X()::method5"),
mcimadamore@1415 91 METHOD6("X::method6"),
mcimadamore@1415 92 METHOD7("X::method7"),
mcimadamore@1415 93 METHOD8("X::method8");
mcimadamore@1415 94
mcimadamore@1415 95 String mrValue;
mcimadamore@1415 96
mcimadamore@1415 97 MethodReference(String mr) {
mcimadamore@1415 98 mrValue = mr;
mcimadamore@1415 99 }
mcimadamore@1415 100 }
mcimadamore@1415 101
mcimadamore@1415 102 enum MethodDef {
mcimadamore@1415 103 METHOD1(" static Integer method1(int n) {\n" +
mcimadamore@1415 104 " return n + 1;\n" +
mcimadamore@1415 105 " }\n", 0),
mcimadamore@1415 106 METHOD2(" int method2(Integer n) {\n" +
mcimadamore@1415 107 " return value == 0 ? n + 2 : n + value;\n" +
mcimadamore@1415 108 " }\n", 1),
mcimadamore@1415 109 METHOD3(" static int method3(int n) {\n" +
mcimadamore@1415 110 " return n + 3;\n" +
mcimadamore@1415 111 " }\n", 2),
mcimadamore@1415 112 METHOD4(" Integer method4(Integer n) {\n" +
mcimadamore@1415 113 " return value == 0 ? n + 4 : n + value;\n" +
mcimadamore@1415 114 " }\n", 3),
mcimadamore@1415 115 METHOD5(" Integer method5(Integer n) {\n" +
mcimadamore@1415 116 " return value == 0 ? new Integer(n + 5) : new Integer(n + value);\n" +
mcimadamore@1415 117 " }\n", 4),
mcimadamore@1415 118 METHOD6(" static int method6(Integer n) throws Exception{\n" +
mcimadamore@1415 119 " throw new Exception();\n" +
mcimadamore@1415 120 " }\n", 5),
mcimadamore@1415 121 METHOD7(" static int method7(String s){\n" +
mcimadamore@1415 122 " return s.length();\n" +
mcimadamore@1415 123 " }\n", 6),
mcimadamore@1415 124 METHOD8(" static String method8(Integer n){\n" +
mcimadamore@1415 125 " return n + \"\";\n" +
mcimadamore@1415 126 " }\n", 7);
mcimadamore@1415 127
mcimadamore@1415 128 String methodStr;
mcimadamore@1415 129 int index;
mcimadamore@1415 130
mcimadamore@1415 131 MethodDef(String ms, int i) {
mcimadamore@1415 132 methodStr = ms;
mcimadamore@1415 133 index = i;
mcimadamore@1415 134 }
mcimadamore@1415 135
mcimadamore@1415 136 MethodReference getMethodReference() {
mcimadamore@1415 137 return MethodReference.values()[index];
mcimadamore@1415 138 }
mcimadamore@1415 139 }
mcimadamore@1415 140
mcimadamore@1415 141 SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") {
mcimadamore@1415 142 public String toString() {
mcimadamore@1415 143 String interfaces = "";
mcimadamore@1415 144 for(FInterface fi : FInterface.values())
mcimadamore@1415 145 interfaces += fi.interfaceDef + "\n";
mcimadamore@1415 146 return template.replace("#C", interfaces);
mcimadamore@1415 147 }
mcimadamore@1415 148 };
mcimadamore@1415 149
mcimadamore@1415 150 String clientTemplate = "class Client {\n" +
mcimadamore@1415 151 " #Context\n" +
mcimadamore@1415 152 "}\n\n" +
mcimadamore@1415 153
mcimadamore@1415 154 "class X {\n" +
mcimadamore@1415 155 " int value = 0;\n\n" +
mcimadamore@1415 156
mcimadamore@1415 157 " X() {\n" +
mcimadamore@1415 158 " }\n\n" +
mcimadamore@1415 159
mcimadamore@1415 160 " X(A a) {\n" +
mcimadamore@1415 161 " value = a.m(9);\n" +
mcimadamore@1415 162 " }\n\n" +
mcimadamore@1415 163
mcimadamore@1415 164 " X(B b) {\n" +
mcimadamore@1415 165 " value = b.m(9);\n" +
mcimadamore@1415 166 " }\n\n" +
mcimadamore@1415 167
mcimadamore@1415 168 " X(C c) {\n" +
mcimadamore@1415 169 " try {\n" +
mcimadamore@1415 170 " value = c.m(9);\n" +
mcimadamore@1415 171 " } catch (Exception e){}\n" +
mcimadamore@1415 172 " }\n\n" +
mcimadamore@1415 173
mcimadamore@1415 174 "#MethodDef" +
mcimadamore@1415 175 "}";
mcimadamore@1415 176
mcimadamore@1415 177 SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) {
mcimadamore@1415 178 public String toString() {
mcimadamore@1415 179 return template.replace("#Context", context.getContext(fInterface, methodReference)).replace("#MethodDef", methodDef.methodStr);
mcimadamore@1415 180 }
mcimadamore@1415 181 };
mcimadamore@1415 182
mcimadamore@1415 183 boolean checkSamConversion() {
mcimadamore@1415 184 if(methodDef == MethodDef.METHOD7 || methodDef == MethodDef.METHOD8)//method signature mismatch
mcimadamore@1415 185 return false;
mcimadamore@1415 186 if(context != Context.CONSTRUCTOR && fInterface != FInterface.C && methodDef == MethodDef.METHOD6)
mcimadamore@1415 187 //method that throws exceptions not thrown by the interface method is a mismatch
mcimadamore@1415 188 return false;
mcimadamore@1510 189 if(context == Context.CONSTRUCTOR)
mcimadamore@1415 190 return false;
mcimadamore@1415 191 return true;
mcimadamore@1415 192 }
mcimadamore@1415 193
mcimadamore@1415 194 void test() throws Exception {
mcimadamore@1415 195 System.out.println("\n====================================");
mcimadamore@1415 196 System.out.println(fInterface + ", " + context + ", " + methodReference);
mcimadamore@1415 197 System.out.println(samSourceFile + "\n" + clientSourceFile);
mcimadamore@1415 198
mcimadamore@1415 199 DiagnosticChecker dc = new DiagnosticChecker();
mcimadamore@1415 200 JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
mcimadamore@1415 201 ct.analyze();
mcimadamore@1415 202 if (dc.errorFound == checkSamConversion()) {
mcimadamore@1415 203 throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
mcimadamore@1415 204 }
mcimadamore@1415 205 count++;
mcimadamore@1415 206 }
mcimadamore@1415 207
mcimadamore@1415 208 abstract class SourceFile extends SimpleJavaFileObject {
mcimadamore@1415 209
mcimadamore@1415 210 protected String template;
mcimadamore@1415 211
mcimadamore@1415 212 public SourceFile(String filename, String template) {
mcimadamore@1415 213 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
mcimadamore@1415 214 this.template = template;
mcimadamore@1415 215 }
mcimadamore@1415 216
mcimadamore@1415 217 @Override
mcimadamore@1415 218 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1415 219 return toString();
mcimadamore@1415 220 }
mcimadamore@1415 221
mcimadamore@1415 222 public abstract String toString();
mcimadamore@1415 223 }
mcimadamore@1415 224
mcimadamore@1415 225 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1415 226
mcimadamore@1415 227 boolean errorFound = false;
mcimadamore@1415 228
mcimadamore@1415 229 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1415 230 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1415 231 errorFound = true;
mcimadamore@1415 232 }
mcimadamore@1415 233 }
mcimadamore@1415 234 }
mcimadamore@1415 235
mcimadamore@1415 236 FInterface fInterface;
mcimadamore@1415 237 Context context;
mcimadamore@1415 238 MethodDef methodDef;
mcimadamore@1415 239 MethodReference methodReference;
mcimadamore@1415 240 static int count = 0;
mcimadamore@1415 241
mcimadamore@1415 242 static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
mcimadamore@1415 243 static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
mcimadamore@1415 244
mcimadamore@1415 245 SamConversionComboTest(FInterface f, Context c, MethodDef md) {
mcimadamore@1415 246 fInterface = f;
mcimadamore@1415 247 context = c;
mcimadamore@1415 248 methodDef = md;
mcimadamore@1415 249 methodReference = md.getMethodReference();
mcimadamore@1415 250 }
mcimadamore@1415 251
mcimadamore@1415 252 public static void main(String[] args) throws Exception {
mcimadamore@1415 253 for(Context ct : Context.values()) {
mcimadamore@1415 254 for (FInterface fi : FInterface.values()) {
mcimadamore@1415 255 for (MethodDef md: MethodDef.values()) {
mcimadamore@1415 256 new SamConversionComboTest(fi, ct, md).test();
mcimadamore@1415 257 }
mcimadamore@1415 258 }
mcimadamore@1415 259 }
mcimadamore@1415 260 System.out.println("total tests: " + count);
mcimadamore@1415 261 }
mcimadamore@1415 262 }

mercurial