-
Notifications
You must be signed in to change notification settings - Fork 371
/
Java8Test.java
1557 lines (1363 loc) · 42.5 KB
/
Java8Test.java
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.dev.jjs.test;
import com.google.gwt.core.client.GwtScriptOnly;
import com.google.gwt.dev.jjs.test.defaultmethods.ImplementsWithDefaultMethodAndStaticInitializer;
import com.google.gwt.dev.jjs.test.defaultmethods.SomeClass;
import com.google.gwt.junit.client.GWTTestCase;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jsinterop.annotations.JsFunction;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsType;
/**
* Tests Java 8 features. It is super sourced so that gwt can be compiles under Java 7.
*
* IMPORTANT: For each test here there must exist the corresponding method in the non super sourced
* version.
*
* Eventually this test will graduate and not be super sourced.
*/
@GwtScriptOnly
public class Java8Test extends GWTTestCase {
int local = 42;
static abstract class SameClass {
public int method1() { return 10; }
public abstract int method2();
}
interface Lambda<T> {
T run(int a, int b);
}
interface Lambda2<String> {
boolean run(String a, String b);
}
interface Lambda3<String> {
boolean run(String a);
}
class AcceptsLambda<T> {
public T accept(Lambda<T> foo) {
return foo.run(10, 20);
}
public boolean accept2(Lambda2<String> foo) {
return foo.run("a", "b");
}
public boolean accept3(Lambda3<String> foo) {
return foo.run("hello");
}
}
class Pojo {
private final int x;
private final int y;
public Pojo(int x, int y) {
this.x = x;
this.y = y;
}
public int fooInstance(int a, int b) {
return a + b + x + y;
}
}
interface DefaultInterface {
void method1();
// CHECKSTYLE_OFF
default int method2() { return 42; }
default int redeclaredAsAbstract() {
return 88;
}
default Integer addInts(int x, int y) { return x + y; }
default String print() { return "DefaultInterface"; }
// CHECKSTYLE_ON
}
interface DefaultInterface2 {
void method3();
// CHECKSTYLE_OFF
default int method4() { return 23; }
default int redeclaredAsAbstract() {
return 77;
}
// CHECKSTYLE_ON
}
interface DefaultInterfaceSubType extends DefaultInterface {
// CHECKSTYLE_OFF
default int method2() { return 43; }
default String print() {
return "DefaultInterfaceSubType " + DefaultInterface.super.print();
}
// CHECKSTYLE_ON
}
static abstract class DualImplementorSuper implements DefaultInterface {
public void method1() {
}
public abstract int redeclaredAsAbstract();
}
static class DualImplementorBoth extends VirtualUpRef implements DefaultInterface,
DefaultInterface2 {
public void method1() {
}
public void method3() {
}
}
static class DualImplementor extends DualImplementorSuper implements DefaultInterface2 {
public void method3() {
}
public int redeclaredAsAbstract() {
return DefaultInterface2.super.redeclaredAsAbstract();
}
}
// this doesn't implement DefaultInterface, but will provide implementation in subclasses
static class VirtualUpRef {
public int method2() {
return 99;
}
public int redeclaredAsAbstract() {
return 44;
}
}
class Inner {
int local = 22;
public void run() {
assertEquals(94, new AcceptsLambda<Integer>().accept((a,b) -> Java8Test.this.local + local + a + b).intValue());
}
}
static class Static {
static int staticField;
static {
staticField = 99;
}
static Integer staticMethod(int x, int y) { return x + y + staticField; }
}
static class StaticFailIfClinitRuns {
static {
fail("clinit() shouldn't run from just taking a reference to a method");
}
public static Integer staticMethod(int x, int y) {
return null;
}
}
static class DefaultInterfaceImpl implements DefaultInterface {
public void method1() {
}
}
static class DefaultInterfaceImpl2 implements DefaultInterface {
public void method1() {
}
public int method2() {
return 100;
}
}
static class DefaultInterfaceImplVirtualUpRef extends VirtualUpRef implements DefaultInterface {
public void method1() {
}
}
static class DefaultInterfaceImplVirtualUpRefTwoInterfaces extends VirtualUpRef
implements DefaultInterfaceSubType {
public void method1() {
}
// CHECKSTYLE_OFF
public String print() { return "DefaultInterfaceImplVirtualUpRefTwoInterfaces"; }
// CHECKSTYLE_ON
}
@Override
public String getModuleName() {
return "com.google.gwt.dev.jjs.Java8Test";
}
public void testLambdaNoCapture() {
assertEquals(30, new AcceptsLambda<Integer>().accept((a, b) -> a + b).intValue());
}
public void testLambdaCaptureLocal() {
int x = 10;
assertEquals(40, new AcceptsLambda<Integer>().accept((a,b) -> x + a + b).intValue());
}
public void testLambdaCaptureLocalWithInnerClass() {
int x = 10;
Lambda<Integer> l = (a,b) -> new Lambda<Integer>() {
@Override public Integer run(int a, int b) {
int t = x;
return t + a + b;
}
}.run(a,b);
assertEquals(40, new AcceptsLambda<Integer>().accept(l).intValue());
}
public void testLambdaCaptureLocalAndField() {
int x = 10;
assertEquals(82, new AcceptsLambda<Integer>().accept((a,b) -> x + local + a + b).intValue());
}
public void testLambdaCaptureLocalAndFieldWithInnerClass() {
int x = 10;
Lambda<Integer> l = (a,b) -> new Lambda<Integer>() {
@Override public Integer run(int j, int k) {
int t = x;
int s = local;
return t + s + a + b;
}
}.run(a,b);
assertEquals(82, new AcceptsLambda<Integer>().accept(l).intValue());
}
public void testCompileLambdaCaptureOuterInnerField() throws Exception {
new Inner().run();
}
public void testStaticReferenceBinding() throws Exception {
assertEquals(129, new AcceptsLambda<Integer>().accept(Static::staticMethod).intValue());
// if this next line runs a clinit, it fails
Lambda l = dummyMethodToMakeCheckStyleHappy(StaticFailIfClinitRuns::staticMethod);
try {
// but now it should fail
l.run(1,2);
fail("Clinit should have run for the first time");
} catch (AssertionError ae) {
// success, it was supposed to throw!
}
}
private static Lambda<Integer> dummyMethodToMakeCheckStyleHappy(Lambda<Integer> l) {
return l;
}
public void testInstanceReferenceBinding() throws Exception {
Pojo instance1 = new Pojo(1, 2);
Pojo instance2 = new Pojo(3, 4);
assertEquals(33, new AcceptsLambda<Integer>().accept(instance1::fooInstance).intValue());
assertEquals(37, new AcceptsLambda<Integer>().accept(instance2::fooInstance).intValue());
}
public void testImplicitQualifierReferenceBinding() throws Exception {
assertFalse(new AcceptsLambda<String>().accept2(String::equalsIgnoreCase));
assertTrue(new AcceptsLambda<String>().accept3("hello world"::contains));
}
public void testConstructorReferenceBinding() {
assertEquals(30, new AcceptsLambda<Pojo>().accept(Pojo::new).fooInstance(0, 0));
}
public void testStaticInterfaceMethod() {
assertEquals(99, (int) Static.staticMethod(0, 0));
}
interface ArrayCtor {
ArrayElem [][][] copy(int i);
}
interface ArrayCtorBoxed {
ArrayElem [][][] copy(Integer i);
}
static class ArrayElem {
}
public void testArrayConstructorReference() {
ArrayCtor ctor = ArrayElem[][][]::new;
ArrayElem[][][] array = ctor.copy(100);
assertEquals(100, array.length);
}
public void testArrayConstructorReferenceBoxed() {
ArrayCtorBoxed ctor = ArrayElem[][][]::new;
ArrayElem[][][] array = ctor.copy(100);
assertEquals(100, array.length);
}
interface ThreeArgs {
int foo(int x, int y, int z);
}
interface ThreeVarArgs {
int foo(int x, int y, int... z);
}
public static int addMany(int x, int y, int... nums) {
int sum = x + y;
for (int num : nums) {
sum += num;
}
return sum;
}
public void testVarArgsReferenceBinding() {
ThreeArgs t = Java8Test::addMany;
assertEquals(6, t.foo(1,2,3));
}
public void testVarArgsPassthroughReferenceBinding() {
ThreeVarArgs t = Java8Test::addMany;
assertEquals(6, t.foo(1,2,3));
}
public void testVarArgsPassthroughReferenceBindingProvidedArray() {
ThreeVarArgs t = Java8Test::addMany;
assertEquals(6, t.foo(1,2, new int[] {3}));
}
interface I {
int foo(Integer i);
}
public void testSuperReferenceExpression() {
class Y {
int foo(Integer i) {
return 42;
}
}
class X extends Y {
int foo(Integer i) {
return 23;
}
int goo() {
I i = super::foo;
return i.foo(0);
}
}
assertEquals(42, new X().goo());
}
static class X2 {
protected int field;
void foo() {
int local;
class Y extends X2 {
class Z extends X2 {
void f() {
Ctor c = X2::new;
X2 x = c.makeX(123456);
assertEquals(123456, x.field);
c = Y::new;
x = c.makeX(987654);
x = new Y(987654);
assertEquals(987655, x.field);
c = Z::new;
x = c.makeX(456789);
x = new Z(456789);
assertEquals(456791, x.field);
}
private Z(int z) {
super(z + 2);
}
Z() {
}
}
private Y(int y) {
super(y + 1);
}
private Y() {
}
}
new Y().new Z().f();
}
private X2(int x) {
this.field = x;
}
X2() {
}
}
public void testSuperReferenceExpressionWithVarArgs() {
class Base {
int foo(Object... objects) {
return 0;
}
}
class X extends Base {
int foo(Object... objects) {
throw new AssertionError();
}
void goo() {
I i = super::foo;
i.foo(10);
}
}
new X().goo();
}
interface Ctor {
X2 makeX(int x);
}
public void testPrivateConstructorReference() {
new X2().foo();
}
public void testDefaultInterfaceMethod() {
assertEquals(42, new DefaultInterfaceImpl().method2());
}
public void testDefaultInterfaceMethodVirtualUpRef() {
assertEquals(99, new DefaultInterfaceImplVirtualUpRef().method2());
assertEquals(99, new DefaultInterfaceImplVirtualUpRefTwoInterfaces().method2());
assertEquals("SimpleB", new com.google.gwt.dev.jjs.test.package3.SimpleC().m());
assertEquals("SimpleASimpleB", new com.google.gwt.dev.jjs.test.package1.SimpleD().m());
}
public void testDefaultInterfaceMethodMultiple() {
assertEquals(42, new DualImplementor().method2());
assertEquals(23, new DualImplementor().method4());
assertEquals(77, new DualImplementor().redeclaredAsAbstract());
assertEquals(44, new DualImplementorBoth().redeclaredAsAbstract());
DefaultInterfaceImplVirtualUpRefTwoInterfaces instanceImplementInterfaceSubType =
new DefaultInterfaceImplVirtualUpRefTwoInterfaces();
DefaultInterfaceSubType interfaceSubType1 = instanceImplementInterfaceSubType;
assertEquals("DefaultInterfaceImplVirtualUpRefTwoInterfaces",
instanceImplementInterfaceSubType.print());
assertEquals("DefaultInterfaceImplVirtualUpRefTwoInterfaces", interfaceSubType1.print());
DefaultInterfaceSubType interfaceSubType2 = new DefaultInterfaceSubType() {
@Override
public void method1() { }
};
assertEquals("DefaultInterfaceSubType DefaultInterface",
interfaceSubType2.print());
DefaultInterfaceSubType interfaceSubType3 = () -> { };
assertEquals("DefaultInterfaceSubType DefaultInterface",
interfaceSubType3.print());
}
public void testDefenderMethodByInterfaceInstance() {
DefaultInterfaceImpl2 interfaceImpl2 = new DefaultInterfaceImpl2();
DefaultInterface interface1 = interfaceImpl2;
assertEquals(100, interfaceImpl2.method2());
assertEquals(100, interface1.method2());
}
public void testDefaultMethodReference() {
DefaultInterfaceImplVirtualUpRef x = new DefaultInterfaceImplVirtualUpRef();
assertEquals(30, (int) new AcceptsLambda<Integer>().accept(x::addInts));
}
interface InterfaceWithTwoDefenderMethods {
// CHECKSTYLE_OFF
default String foo() { return "interface.foo"; }
default String bar() { return this.foo() + " " + foo(); }
// CHECKSTYLE_ON
}
class ClassImplementOneDefenderMethod implements InterfaceWithTwoDefenderMethods {
public String foo() {
return "class.foo";
}
}
public void testThisRefInDefenderMethod() {
ClassImplementOneDefenderMethod c = new ClassImplementOneDefenderMethod();
InterfaceWithTwoDefenderMethods i1 = c;
InterfaceWithTwoDefenderMethods i2 = new InterfaceWithTwoDefenderMethods() { };
assertEquals("class.foo class.foo", c.bar());
assertEquals("class.foo class.foo", i1.bar());
assertEquals("interface.foo interface.foo", i2.bar());
}
interface InterfaceImplementOneDefenderMethod extends InterfaceWithTwoDefenderMethods {
// CHECKSTYLE_OFF
default String foo() { return "interface1.foo"; }
// CHECKSTYLE_ON
}
interface InterfaceImplementZeroDefenderMethod extends InterfaceWithTwoDefenderMethods {
}
class ClassImplementsTwoInterfaces implements InterfaceImplementOneDefenderMethod,
InterfaceImplementZeroDefenderMethod {
}
public void testClassImplementsTwoInterfacesWithSameDefenderMethod() {
ClassImplementsTwoInterfaces c = new ClassImplementsTwoInterfaces();
assertEquals("interface1.foo", c.foo());
}
abstract class AbstractClass implements InterfaceWithTwoDefenderMethods {
}
class Child1 extends AbstractClass {
public String foo() {
return super.foo() + " child1.foo";
}
}
class Child2 extends AbstractClass {
}
public void testAbstractClassImplementsInterface() {
Child1 child1 = new Child1();
Child2 child2 = new Child2();
assertEquals("interface.foo child1.foo", child1.foo());
assertEquals("interface.foo", child2.foo());
}
interface InterfaceI {
// CHECKSTYLE_OFF
default String print() { return "interface1"; }
// CHECKSTYLE_ON
}
interface InterfaceII {
// CHECKSTYLE_OFF
default String print() { return "interface2"; }
// CHECKSTYLE_ON
}
class ClassI {
public String print() {
return "class1";
}
}
class ClassII extends ClassI implements InterfaceI, InterfaceII {
public String print() {
return super.print() + " " + InterfaceI.super.print() + " " + InterfaceII.super.print();
}
}
public void testSuperRefInDefenderMethod() {
ClassII c = new ClassII();
assertEquals("class1 interface1 interface2", c.print());
}
interface II {
// CHECKSTYLE_OFF
default String fun() { return "fun() in i: " + this.foo(); };
default String foo() { return "foo() in i.\n"; };
// CHECKSTYLE_ON
}
interface JJ extends II {
// CHECKSTYLE_OFF
default String fun() { return "fun() in j: " + this.foo() + II.super.fun(); };
default String foo() { return "foo() in j.\n"; }
// CHECKSTYLE_ON
}
class AA {
public String fun() {
return "fun() in a: " + this.foo();
}
public String foo() {
return "foo() in a.\n";
}
}
class BB extends AA implements JJ {
public String fun() {
return "fun() in b: " + this.foo() + super.fun() + JJ.super.fun();
}
public String foo() {
return "foo() in b.\n";
}
}
class CC extends BB implements JJ {
public String fun() {
return "fun() in c: " + super.fun();
}
}
public void testSuperThisRefsInDefenderMethod() {
CC c = new CC();
II i1 = c;
JJ j1 = c;
BB b = new BB();
II i2 = b;
JJ j2 = b;
JJ j3 = new JJ() { };
II i3 = j3;
II i4 = new II() { };
String c_fun = "fun() in c: fun() in b: foo() in b.\n"
+ "fun() in a: foo() in b.\n"
+ "fun() in j: foo() in b.\n"
+ "fun() in i: foo() in b.\n";
String b_fun = "fun() in b: foo() in b.\n"
+ "fun() in a: foo() in b.\n"
+ "fun() in j: foo() in b.\n"
+ "fun() in i: foo() in b.\n";
String j_fun = "fun() in j: foo() in j.\n"
+ "fun() in i: foo() in j.\n";
String i_fun = "fun() in i: foo() in i.\n";
assertEquals(c_fun, c.fun());
assertEquals(c_fun, i1.fun());
assertEquals(c_fun, j1.fun());
assertEquals(b_fun, b.fun());
assertEquals(b_fun, i2.fun());
assertEquals(b_fun, j2.fun());
assertEquals(j_fun, j3.fun());
assertEquals(j_fun, i3.fun());
assertEquals(i_fun, i4.fun());
}
interface OuterInterface {
// CHECKSTYLE_OFF
default String m() {
return "I.m;" + new InnerClass().n();
}
default String n() {
return "I.n;" + this.m();
}
// CHECKSTYLE_ON
class InnerClass {
public String n() {
return "A.n;" + m();
}
public String m() {
return "A.m;";
}
}
}
class OuterClass {
public String m() {
return "B.m;";
}
public String n1() {
OuterInterface i = new OuterInterface() { };
return "B.n1;" + i.n() + OuterClass.this.m();
}
public String n2() {
OuterInterface i = new OuterInterface() {
@Override
public String n() {
return this.m() + OuterClass.this.m();
}
};
return "B.n2;" + i.n() + OuterClass.this.m();
}
}
public void testNestedInterfaceClass() {
OuterClass outerClass = new OuterClass();
assertEquals("B.n1;I.n;I.m;A.n;A.m;B.m;", outerClass.n1());
assertEquals("B.n2;I.m;A.n;A.m;B.m;B.m;", outerClass.n2());
}
class EmptyA { }
interface EmptyI { }
interface EmptyJ { }
class EmptyB extends EmptyA implements EmptyI { }
class EmptyC extends EmptyA implements EmptyI, EmptyJ { }
public void testBaseIntersectionCast() {
EmptyA localB = new EmptyB();
EmptyA localC = new EmptyC();
EmptyB b2BI = (EmptyB & EmptyI) localB;
EmptyC c2CIJ = (EmptyC & EmptyI & EmptyJ) localC;
EmptyI ii1 = (EmptyB & EmptyI) localB;
EmptyI ii2 = (EmptyC & EmptyI) localC;
EmptyI ii3 = (EmptyC & EmptyJ) localC;
EmptyI ii4 = (EmptyC & EmptyI & EmptyJ) localC;
EmptyJ jj1 = (EmptyC & EmptyI & EmptyJ) localC;
EmptyJ jj2 = (EmptyC & EmptyI) localC;
EmptyJ jj3 = (EmptyC & EmptyJ) localC;
EmptyJ jj4 = (EmptyI & EmptyJ) localC;
try {
EmptyC b2CIJ = (EmptyC & EmptyI & EmptyJ) localB;
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// Expected.
}
try {
EmptyB c2BI = (EmptyB & EmptyI) localC;
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// Expected.
}
try {
EmptyJ jj = (EmptyB & EmptyJ) localB;
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// Expected.
}
}
interface SimpleI {
int fun();
}
interface SimpleJ {
int foo();
int bar();
}
interface SimpleK {
}
public void testIntersectionCastWithLambdaExpr() {
SimpleI simpleI1 = (SimpleI & EmptyI) () -> { return 11; };
assertEquals(11, simpleI1.fun());
SimpleI simpleI2 = (EmptyI & SimpleI) () -> { return 22; };
assertEquals(22, simpleI2.fun());
EmptyI emptyI = (EmptyI & SimpleI) () -> { return 33; };
try {
((EmptyA & SimpleI) () -> { return 33; }).fun();
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// expected.
}
try {
((SimpleI & SimpleJ) () -> { return 44; }).fun();
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// expected.
}
try {
((SimpleI & SimpleJ) () -> { return 44; }).foo();
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// expected.
}
try {
((SimpleI & SimpleJ) () -> { return 44; }).bar();
fail("Should have thrown a ClassCastException");
} catch (ClassCastException e) {
// expected.
}
assertEquals(55, ((SimpleI & SimpleK) () -> { return 55; }).fun());
}
class SimpleA {
public int bar() {
return 11;
}
}
class SimpleB extends SimpleA implements SimpleI {
public int fun() {
return 22;
}
}
class SimpleC extends SimpleA implements SimpleI {
public int fun() {
return 33;
}
public int bar() {
return 44;
}
}
public void testIntersectionCastPolymorphism() {
SimpleA bb = new SimpleB();
assertEquals(22, ((SimpleB & SimpleI) bb).fun());
assertEquals(11, ((SimpleB & SimpleI) bb).bar());
SimpleA cc = new SimpleC();
assertEquals(33, ((SimpleC & SimpleI) cc).fun());
assertEquals(44, ((SimpleC & SimpleI) cc).bar());
assertEquals(33, ((SimpleA & SimpleI) cc).fun());
SimpleI ii = (SimpleC & SimpleI) cc;
assertEquals(33, ii.fun());
}
interface ClickHandler {
int onClick(int a);
}
private int addClickHandler(ClickHandler clickHandler) {
return clickHandler.onClick(1);
}
private int addClickHandler(int a) {
return addClickHandler(x -> { int temp = a; return temp; });
}
public void testLambdaCaptureParameter() {
assertEquals(2, addClickHandler(2));
}
interface TestLambda_Inner {
void f();
}
interface TestLambda_Outer {
void accept(TestLambda_Inner t);
}
public void testLambda_call(TestLambda_Outer a) {
a.accept(() -> { });
}
public void testLambdaNestingCaptureLocal() {
int[] success = new int[] {0};
testLambda_call(sam1 -> { testLambda_call(sam2 -> { success[0] = 10; }); });
assertEquals(10, success[0]);
}
public void testLambdaNestingInAnonymousCaptureLocal() {
int[] x = new int[] {42};
new Runnable() {
public void run() {
Lambda<Integer> l = (a, b) -> x[0] = x[0] + a + b;
l.run(1, 2);
}
}.run();
assertEquals(45, x[0]);
}
public void testLambdaNestingInMultipleMixedAnonymousCaptureLocal() {
// checks that lambda has access to local variable and arguments when placed in mixed scopes
// Local Class -> Local Class -> Local Anonymous -> lambda -> Local Anonymous
class A {
int a() {
int[] x = new int[] {42};
class B {
void b() {
I i = new I() {
public int foo(Integer arg) {
Runnable r = () -> {
new Runnable() {
public void run() {
Lambda<Integer> l = (a, b) -> x[0] = x[0] + a + b + arg;
l.run(1, 2);
}
}.run();
};
r.run();
return x[0];
}
};
i.foo(1);
}
}
B b = new B();
b.b();
return x[0];
}
}
A a = new A();
assertEquals(46, a.a());
}
public void testLambdaNestingInMultipleMixedAnonymousCaptureLocal_withInterference() {
// checks that lambda has access to NEAREST local variable and arguments when placed in mixed
// scopes Local Class -> Local Class -> Local Anonymous -> lambda -> Local Anonymous
class A {
int a() {
int[] x = new int[] {42};
class B {
int b() {
int[] x = new int[] {22};
I i = new I() {
public int foo(Integer arg) {
Runnable r = () -> {
new Runnable() {
public void run() {
Lambda<Integer> l = (a, b) -> x[0] = x[0] + a + b + arg;
l.run(1, 2);
}
}.run();
};
r.run();
return x[0];
}
};
return i.foo(1);
}
}
B b = new B();
return b.b();
}
}
A a = new A();
assertEquals(26, a.a());
}
public void testLambdaNestingInMultipleMixedAnonymousCaptureLocalAndField() {
// checks that lambda has access to local variable, field and arguments when placed in mixed
// scopes - Local Class -> Local Class -> Local Anonymous -> lambda -> Local Anonymous
class A {
int fA = 1;
int a() {
int[] x = new int[] {42};
class B {
int fB = 2;
int b() {
I i = new I() {
int fI = 3;
public int foo(Integer arg) {
Runnable r = () -> {
new Runnable() {
public void run() {
Lambda<Integer> l = (a, b) -> x[0] = x[0] + a + b + arg + fA + fB + fI;
l.run(1, 2);
}
}.run();
};
r.run();
return x[0];
}
};
return i.foo(1);
}
}
B b = new B();
return b.b();
}
}
A a = new A();
assertEquals(52, a.a());
}
public void testLambdaNestingInMultipleAnonymousCaptureLocal() {
// checks that lambda has access to local variable and arguments when placed in local anonymous
// class with multile nesting
int[] x = new int[] {42};
int result = new I() {
public int foo(Integer i1) {
return new I() {
public int foo(Integer i2) {
return new I() {
public int foo(Integer i3) {
Lambda<Integer> l = (a, b) -> x[0] = x[0] + a + b + i1 + i2 + i3;
return l.run(1, 2);
}
}.foo(3);
}
}.foo(2);
}
}.foo(1);
assertEquals(51, x[0]);
}
static class TestLambda_ClassA {
int[] f = new int[] {42};
class B {
void m() {
Runnable r = () -> f[0] = f[0] + 1;
r.run();
}
}
int a() {
B b = new B();
b.m();
return f[0];
}
}
public void testLambdaNestingCaptureField_InnerClassCapturingOuterClassVariable() {
TestLambda_ClassA a = new TestLambda_ClassA();
assertEquals(43, a.a());
}
public void testInnerClassCaptureLocalFromOuterLambda() {
int[] x = new int[] {42};
Lambda<Integer> l = (a, b) -> {
int[] x1 = new int[] {32};
Lambda<Integer> r = (rA, rB) -> {
int[] x2 = new int[] {22};
I i = new I() {
public int foo(Integer arg) {
x1[0] = x1[0] + 1;
x[0] = x[0] + 1;
return x2[0] = x2[0] + rA + rB + a + b;
}
};
return i.foo(1);
};
return r.run(3, 4) + x1[0];
};
// x1[0](32) + 1 + x2[0](22) + rA(3) + rB(4) + a(1) + b(2)
assertEquals(65, l.run(1, 2).intValue());
assertEquals(43, x[0]);
}