Tue, 18 Dec 2012 22:16:45 +0000
8005193: New regression test test/tools/javac/lambda/BadMethodCall2.java fails
Summary: Bad golden file in negative test
Reviewed-by: jjh
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * @test
26 * @bug 8003280
27 * @summary Add lambda tests
28 * Test bridge methods for certain SAM conversions
29 * @compile LambdaTest6.java
30 * @run main LambdaTest6
31 */
33 import java.lang.reflect.Method;
34 import java.util.HashSet;
35 import java.util.Set;
37 public class LambdaTest6<T> {
39 interface H {Object m();}
41 interface K<U> {void m(U element);}
43 interface L extends K<String> {} //generic substitution
45 interface M {void m(String s);}
47 interface KM extends K<String>, M{} //generic substitution
49 interface N extends H {String m();} //covariant return
51 private static void assertTrue(boolean b) {
52 if(!b)
53 throw new AssertionError();
54 }
56 private Set<String> setOfStringObject() {
57 Set<String> s = new HashSet<>();
58 s.add("java.lang.String");
59 s.add("java.lang.Object");
60 return s;
61 }
63 private void test1()
64 {
65 L la = s -> { };
66 la.m("hi");
67 Class<? extends L> c1 = la.getClass();
68 Method[] methods = c1.getDeclaredMethods();
69 Set<String> types = setOfStringObject();
70 for(Method m : methods) {
71 assertTrue(m.getName().equals("m"));
72 Class[] parameterTypes = m.getParameterTypes();
73 assertTrue(parameterTypes.length == 1);
74 assertTrue(types.remove(parameterTypes[0].getName()));
75 }
76 assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
77 }
79 private void test2()
80 {
81 KM km = s -> { };
82 //km.m("hi");
83 Class<? extends KM> c2 = km.getClass();
84 Method[] methods = c2.getDeclaredMethods();
85 Set<String> types = setOfStringObject();
86 for(Method m : methods) {
87 assertTrue(m.getName().equals("m"));
88 Class[] parameterTypes = m.getParameterTypes();
89 assertTrue(parameterTypes.length == 1);
90 assertTrue(types.remove(parameterTypes[0].getName()));
91 }
92 assertTrue(types.isEmpty());
93 }
95 private void test3()
96 {
97 N na = ()-> "hi";
98 assertTrue( na.m().equals("hi") );
99 assertTrue( ((H)na).m().equals("hi") );
100 Class<? extends N> c3 = na.getClass();
101 Method[] methods = c3.getDeclaredMethods();
102 Set<String> types = setOfStringObject();
103 for(Method m : methods) {
104 assertTrue(m.getName().equals("m"));
105 Class returnType = m.getReturnType();
106 assertTrue(types.remove(returnType.getName()));
107 }
108 assertTrue(types.isEmpty());
109 }
112 public static void main(String[] args) {
113 LambdaTest6 test = new LambdaTest6();
114 test.test1();
115 test.test2();
116 test.test3();
117 }
118 }