blob: 46cd555da44cf395961c3213b827f26723c3e44c [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4package lambdadesugaringnplus;
5
Denis Vnukovb4e55582017-05-31 08:54:29 -07006import java.io.Serializable;
Mads Ager418d1ca2017-05-22 09:35:49 +02007import java.lang.annotation.Annotation;
8import java.lang.annotation.Retention;
9import java.lang.annotation.RetentionPolicy;
10import lambdadesugaringnplus.other.ClassWithDefaultPackagePrivate;
11import lambdadesugaringnplus.other.InterfaceWithDefaultPackagePrivate;
12
13public class LambdasWithStaticAndDefaultMethods {
14 interface I {
15 String iRegular();
16
17 static String iStatic() {
18 return "I::iStatic()";
19 }
20
21 default String iDefault() {
22 return "I::iDefault()";
23 }
24
25 default String iDefaultOverridden() {
26 return "I::iDefaultOverridden()";
27 }
28
29 default II stateless() {
30 return () -> "I::stateless()";
31 }
32
33 default II stateful() {
34 return () -> "I::captureThis(" + stateless().iRegular() + ")";
35 }
36 }
37
38 static class C implements I {
39 @Override
40 public String iRegular() {
41 return "C::iRegular()";
42 }
43
44 @Override
45 public String iDefaultOverridden() {
46 return "C::iDefaultOverridden()";
47 }
48 }
49
50 interface II extends I {
51 static String iStatic() {
52 II ii = I::iStatic;
53 return "II::iStatic(" + ((I) I::iStatic).iRegular() +
54 "|" + ((II) ii::iDefaultOverridden).iRegular() +
55 "|" + ((II) String::new).iRegular() +
56 "|" + ((II) ii::iRegular).iRegular() + ")";
57 }
58
59 default String iDefaultOverridden() {
60 return "II::iDefault(" + ((I) this::iDefault).iRegular() +
61 "|" + ((II) "One-Two-Three"::intern).iRegular() +
62 "|" + ((II) this::iDefault).iRegular() + ")";
63 }
64 }
65
66 interface P {
67 String get();
68 }
69
70 static void p(P p) {
71 System.out.println(p.get());
72 }
73
74 interface X<T> {
75 String foo(T t);
76 }
77
78 interface Y<T extends I> extends X<T> {
79 String foo(T t);
80 }
81
82 interface Z extends Y<II> {
83 String foo(II t);
84 }
85
86 interface G<T> {
87 T foo(T t);
88 }
89
90 interface B38257361_I1<T extends Number> {
91 default T copy(T t) {
92 return t;
93 }
94 }
95
96 interface B38257361_I2 extends B38257361_I1<Integer> {
97 @Override
98 default Integer copy(Integer t) {
99 return B38257361_I1.super.copy(t);
100 }
101 }
102
103 static class B38257361_C implements B38257361_I2 {
104 }
105
106 static class B38257361 {
107 private B38257361_C c = new B38257361_C();
108
109 public Integer test(Integer i) {
110 return c.copy(i);
111 }
112
113 @SuppressWarnings({"rawtypes", "unchecked"})
114 public Number test(Number n) {
115 return ((B38257361_I1) c).copy(n);
116 }
117
118 public static void test() {
119 B38257361 l = new B38257361();
120 Integer i = new Integer(1);
121 if (i.equals(l.test(i))) {
122 System.out.println("Check 1: OK");
123 } else {
124 System.out.println("Check 1: NOT OK");
125 }
126 if (i.equals(l.test((Number) i))) {
127 System.out.println("Check 2: OK");
128 } else {
129 System.out.println("Check 2: NOT OK");
130 }
131 try {
132 Double d = new Double(1);
133 if (d.equals(l.test((Number) d))) {
134 System.out.println("Check 3: NOT OK, classCastException expected");
135 } else {
136 System.out.println("Check 3: NOT OK, classCastException expected");
137 }
138 System.out.println("Error, ClassCastException is expected");
139 } catch (ClassCastException e) {
140 // Class cast into the bridge method is expected
141 System.out.println("OK, ClassCastException is expected");
142 }
143 }
144 }
145
Denis Vnukovad257b62018-05-03 08:33:17 -0700146 static class B78901754 {
147 public static class A {
148 public final String msg;
149
150 public A(String msg) {
151 this.msg = msg;
152 }
153 }
154
155 public static class B extends A {
156 public B(String msg) {
157 super(msg);
158 }
159 }
160
161 public interface IAA {
162 A[] foo(A[] p);
163 }
164
165 public interface IAB {
166 A[] foo(B[] p);
167 }
168
169 public interface IBA {
170 B[] foo(A[] p);
171 }
172
173 public interface IBB {
174 B[] foo(B[] p);
175 }
176
177 public static A[] fooAA(A[] p) {
178 return new A[]{new A("fooAA")};
179 }
180
181 public static A[] fooBA(B[] p) {
182 return new A[]{new A("fooBA")};
183 }
184
185 public static B[] fooAB(A[] p) {
186 return new B[]{new B("fooAB")};
187 }
188
189 public static B[] fooBB(B[] p) {
190 return new B[]{new B("fooBB")};
191 }
192
193 public static void testAA(IAA i) {
194 System.out.println(i.foo(null)[0].msg);
195 }
196
197 public static void testAB(IAB i) {
198 System.out.println(i.foo(null)[0].msg);
199 }
200
201 public static void testBA(IBA i) {
202 System.out.println(i.foo(null)[0].msg);
203 }
204
205 public static void testBB(IBB i) {
206 System.out.println(i.foo(null)[0].msg);
207 }
208
209 public static void test() {
210 testAA(B78901754::fooAA);
211 testAA(B78901754::fooAB);
212 // testAA(B78901754::fooBA); javac error: incompatible types: A[] cannot be converted to B[]
213 // testAA(B78901754::fooBB); javac error: incompatible types: A[] cannot be converted to B[]
214
215 testAB(B78901754::fooAA);
216 testAB(B78901754::fooAB);
217 testAB(B78901754::fooBA);
218 testAB(B78901754::fooBB);
219
220 // testBA(B78901754::fooAA); javac error: A[] cannot be converted to B[]
221 testBA(B78901754::fooAB);
222 // testBA(B78901754::fooBA); javac error: incompatible types: A[] cannot be converted to B[]
223 // testBA(B78901754::fooBB); javac error: incompatible types: A[] cannot be converted to B[]
224
225 // testBB(B78901754::fooAA); javac error: A[] cannot be converted to B[]
226 testBB(B78901754::fooAB);
227 // testBB(B78901754::fooBA); javac error: A[] cannot be converted to B[]
228 testBB(B78901754::fooBB);
229 }
230 }
231
Mads Ager418d1ca2017-05-22 09:35:49 +0200232 interface B38257037_I1 {
233 default Number getNumber() {
234 return new Integer(1);
235 }
236 }
237
238 interface B38257037_I2 extends B38257037_I1 {
239 @Override
240 default Double getNumber() {
241 return new Double(2.3);
242 }
243 }
244
245 static class B38257037_C implements B38257037_I2 {
246 }
247
248 /**
249 * Check that bridges are generated.
250 */
251 static class B38257037 {
252 private B38257037_C c = new B38257037_C();
253
254 public Double test1() {
255 return c.getNumber();
256 }
257
258 public Number test2() {
259 return ((B38257037_I1) c).getNumber();
260 }
261
262 public static void test() {
263 B38257037 l = new B38257037();
264 if (l.test1() == 2.3) {
265 System.out.println("Check 1: OK");
266 } else {
267 System.out.println("Check 1: NOT OK");
268 }
269 if (l.test2().equals(new Double(2.3))) {
270 System.out.println("Check 2: OK");
271 } else {
272 System.out.println("Check 2: NOT OK");
273 }
274 }
275 }
276
277 interface B38306708_I {
278 class $CC{
279 static void print() {
280 System.out.println("$CC");
281 }
282 }
283
284 default String m() {
285 return "ITop.m()";
286 }
287 }
288
289 static class B38306708 {
290 public static void test() {
291 B38306708_I.$CC.print();
292 }
293 }
294
295 interface B38308515_I {
296 default String m() {
297 return "m instance";
298 }
299
300 static String m(B38308515_I i) {
301 return "m static";
302 }
303 }
304
305 static class B38308515_C implements B38308515_I {
306 }
307
308 static class B38308515 {
309 static void test() {
310 B38308515_C c = new B38308515_C();
311 System.out.println(c.m());
312 System.out.println(B38308515_I.m(c));
313 }
314 }
315
316 static class B38302860 {
317
318 @SomeAnnotation(1)
319 private interface AnnotatedInterface {
320
321 @SomeAnnotation(2)
322 void annotatedAbstractMethod();
323
324 @SomeAnnotation(3)
325 default void annotatedDefaultMethod() {
326 }
327
328 @SomeAnnotation(4)
329 static void annotatedStaticMethod() {
330 }
331 }
332
333 @Retention(value = RetentionPolicy.RUNTIME)
334 private @interface SomeAnnotation {
335 int value();
336 }
337
338 private static boolean checkAnnotationValue(Annotation[] annotations, int value) {
339 if (annotations.length != 1) {
340 return false;
341 }
342 return annotations[0] instanceof SomeAnnotation
343 && ((SomeAnnotation) annotations[0]).value() == value;
344 }
345
346 @SuppressWarnings("unchecked")
347 static void test() throws Exception {
348 if (checkAnnotationValue(AnnotatedInterface.class.getAnnotations(), 1)) {
349 System.out.println("Check 1: OK");
350 } else {
351 System.out.println("Check 1: NOT OK");
352 }
353
354 if (checkAnnotationValue(
355 AnnotatedInterface.class.getMethod("annotatedAbstractMethod").getAnnotations(), 2)) {
356 System.out.println("Check 2: OK");
357 } else {
358 System.out.println("Check 2: NOT OK");
359 }
360
361 if (checkAnnotationValue(
362 AnnotatedInterface.class.getMethod("annotatedDefaultMethod").getAnnotations(), 3)) {
363 System.out.println("Check 3: OK");
364 } else {
365 System.out.println("Check 3: NOT OK");
366 }
367
368 if (checkAnnotationValue(
369 getCompanionClassOrInterface().getMethod("annotatedStaticMethod").getAnnotations(), 4)) {
370 System.out.println("Check 4: OK");
371 } else {
372 System.out.println("Check 4: NOT OK");
373 }
374 }
375
376 private static Class getCompanionClassOrInterface() {
377 try {
378 return Class.forName("lambdadesugaringnplus."
Sebastien Hertz681eb292018-04-10 17:30:28 +0200379 + "LambdasWithStaticAndDefaultMethods$B38302860$AnnotatedInterface$-CC");
Mads Ager418d1ca2017-05-22 09:35:49 +0200380 } catch (Exception e) {
381 return AnnotatedInterface.class;
382 }
383 }
384 }
385
Denis Vnukovb4e55582017-05-31 08:54:29 -0700386 static class B62168701 {
387 interface I extends Serializable {
388 String getValue();
389 }
390
391 interface J {
392 static void dump() {
393 I i = () -> "B62168701 -- OK";
394 System.out.println(i.getValue());
395 }
396 }
397
398 static void test() {
399 J.dump();
400 }
401 }
402
Mads Ager418d1ca2017-05-22 09:35:49 +0200403 static void z(Z p) {
404 System.out.println(p.foo(null));
405 }
406
407 static void g(G<String[]> g) {
408 StringBuilder builder = new StringBuilder("{");
409 String sep = "";
410 for (String s : g.foo(new String[] { "Arg0", "Arg1", "Arg2" })) {
411 builder.append(sep).append(s);
412 sep = ", ";
413 }
414 builder.append("}");
415 System.out.println(builder.toString());
416 }
417
418 interface SuperChain {
419 default String iMain() {
420 return "SuperChain::iMain()";
421 }
422 }
423
424 interface SuperChainDerived extends SuperChain {
425 default String iMain() {
426 return "SuperChainDerived::iMain(" + SuperChain.super.iMain() + ")";
427 }
428 }
429
430 interface OtherSuperChain {
431 default String iMain() {
432 return "OtherSuperChain::iMain()";
433 }
434 }
435
436 static class ClassWithSuperChain implements SuperChainDerived, OtherSuperChain {
437 public String iMain() {
438 return "ClassWithSuperChain::iMain(" + SuperChainDerived.super.iMain() + ")" + iMainImpl();
439 }
440
441 public String iMainImpl() {
442 return "ClassWithSuperChain::iMain(" + SuperChainDerived.super.iMain() +
443 " + " + OtherSuperChain.super.iMain() + ")";
444 }
445 }
446
447 public static void main(String[] args) throws Exception {
448 C c = new C();
449 I i = c;
450
451 c.iRegular();
452 c.iDefault();
453 c.iDefaultOverridden();
454 I.iStatic();
455 i.iRegular();
456 i.iDefault();
457 i.iDefaultOverridden();
458
459 p(i.stateless()::iRegular);
460 p(i.stateful()::iRegular);
461
462 g(a -> a);
463 g(a -> {
464 int size = a.length;
465 for (int x = 0; x < size / 2; x++) {
466 String t = a[x];
467 a[x] = a[size - 1 - x];
468 a[size - 1 - x] = t;
469 }
470 return a;
471 });
472
473 p(c::iRegular);
474 p(c::iDefault);
475 p(c::iDefaultOverridden);
476 p(I::iStatic);
477 p(i::iRegular);
478 p(i::iDefault);
479 p(i::iDefaultOverridden);
480
481 II ii = i::iRegular;
482 p(II::iStatic);
483 p(ii::iRegular);
484 p(ii::iDefault);
485 p(ii::iDefaultOverridden);
486
487 z(s -> "From Interface With Bridges");
488
489 System.out.println(new ClassWithSuperChain().iMain());
490
491 ClassWithDefaultPackagePrivate c2 = new ClassWithDefaultPackagePrivate();
492 InterfaceWithDefaultPackagePrivate i2 = c2;
493
494 c2.defaultFoo();
495 i2.defaultFoo();
496 InterfaceWithDefaultPackagePrivate.staticFoo();
497
498 p(c2::defaultFoo);
499 p(i2::defaultFoo);
500 p(InterfaceWithDefaultPackagePrivate::staticFoo);
501 p(c2.lambda()::foo);
502
503 B38257361.test();
504 B38257037.test();
505 B38306708.test();
506 B38308515.test();
507 B38302860.test();
Denis Vnukovb4e55582017-05-31 08:54:29 -0700508 B62168701.test();
Denis Vnukovad257b62018-05-03 08:33:17 -0700509 B78901754.test();
Mads Ager418d1ca2017-05-22 09:35:49 +0200510 }
511}