Home | History | Annotate | Download | only in lambdadesugaringnplus
      1 // 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.
      4 package lambdadesugaringnplus;
      5 
      6 import java.io.Serializable;
      7 import java.lang.annotation.Annotation;
      8 import java.lang.annotation.Retention;
      9 import java.lang.annotation.RetentionPolicy;
     10 import lambdadesugaringnplus.other.ClassWithDefaultPackagePrivate;
     11 import lambdadesugaringnplus.other.InterfaceWithDefaultPackagePrivate;
     12 
     13 public 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 
    146   interface B38257037_I1 {
    147     default Number getNumber() {
    148       return new Integer(1);
    149     }
    150   }
    151 
    152   interface B38257037_I2 extends B38257037_I1 {
    153     @Override
    154     default Double getNumber() {
    155       return new Double(2.3);
    156     }
    157   }
    158 
    159   static class B38257037_C implements B38257037_I2 {
    160   }
    161 
    162   /**
    163    * Check that bridges are generated.
    164    */
    165   static class B38257037 {
    166     private B38257037_C c = new B38257037_C();
    167 
    168     public Double test1() {
    169       return c.getNumber();
    170     }
    171 
    172     public Number test2() {
    173       return ((B38257037_I1) c).getNumber();
    174     }
    175 
    176     public static void test() {
    177       B38257037 l = new B38257037();
    178       if (l.test1() == 2.3) {
    179         System.out.println("Check 1: OK");
    180       } else {
    181         System.out.println("Check 1: NOT OK");
    182       }
    183       if (l.test2().equals(new Double(2.3))) {
    184         System.out.println("Check 2: OK");
    185       } else {
    186         System.out.println("Check 2: NOT OK");
    187       }
    188     }
    189   }
    190 
    191   interface B38306708_I {
    192     class $CC{
    193       static void print() {
    194         System.out.println("$CC");
    195       }
    196     }
    197 
    198     default String m() {
    199       return "ITop.m()";
    200     }
    201   }
    202 
    203   static class B38306708 {
    204     public static void test() {
    205       B38306708_I.$CC.print();
    206     }
    207   }
    208 
    209   interface B38308515_I {
    210     default String m() {
    211       return "m instance";
    212     }
    213 
    214     static String m(B38308515_I i) {
    215       return "m static";
    216     }
    217   }
    218 
    219   static class B38308515_C implements B38308515_I {
    220   }
    221 
    222   static class B38308515 {
    223     static void test() {
    224       B38308515_C c = new B38308515_C();
    225       System.out.println(c.m());
    226       System.out.println(B38308515_I.m(c));
    227     }
    228   }
    229 
    230   static class B38302860 {
    231 
    232     @SomeAnnotation(1)
    233     private interface AnnotatedInterface {
    234 
    235       @SomeAnnotation(2)
    236       void annotatedAbstractMethod();
    237 
    238       @SomeAnnotation(3)
    239       default void annotatedDefaultMethod() {
    240       }
    241 
    242       @SomeAnnotation(4)
    243       static void annotatedStaticMethod() {
    244       }
    245     }
    246 
    247     @Retention(value = RetentionPolicy.RUNTIME)
    248     private @interface SomeAnnotation {
    249       int value();
    250     }
    251 
    252     private static boolean checkAnnotationValue(Annotation[] annotations, int value) {
    253       if (annotations.length != 1) {
    254         return false;
    255       }
    256       return annotations[0] instanceof SomeAnnotation
    257           && ((SomeAnnotation) annotations[0]).value() == value;
    258     }
    259 
    260     @SuppressWarnings("unchecked")
    261     static void test() throws Exception {
    262       if (checkAnnotationValue(AnnotatedInterface.class.getAnnotations(), 1)) {
    263         System.out.println("Check 1: OK");
    264       } else {
    265         System.out.println("Check 1: NOT OK");
    266       }
    267 
    268       if (checkAnnotationValue(
    269           AnnotatedInterface.class.getMethod("annotatedAbstractMethod").getAnnotations(), 2)) {
    270         System.out.println("Check 2: OK");
    271       } else {
    272         System.out.println("Check 2: NOT OK");
    273       }
    274 
    275       if (checkAnnotationValue(
    276           AnnotatedInterface.class.getMethod("annotatedDefaultMethod").getAnnotations(), 3)) {
    277         System.out.println("Check 3: OK");
    278       } else {
    279         System.out.println("Check 3: NOT OK");
    280       }
    281 
    282       if (checkAnnotationValue(
    283           getCompanionClassOrInterface().getMethod("annotatedStaticMethod").getAnnotations(), 4)) {
    284         System.out.println("Check 4: OK");
    285       } else {
    286         System.out.println("Check 4: NOT OK");
    287       }
    288     }
    289 
    290     private static Class getCompanionClassOrInterface() {
    291       try {
    292         return Class.forName("lambdadesugaringnplus."
    293             + "LambdasWithStaticAndDefaultMethods$B38302860$AnnotatedInterface-CC");
    294       } catch (Exception e) {
    295         return AnnotatedInterface.class;
    296       }
    297     }
    298   }
    299 
    300   static class B62168701 {
    301     interface I extends Serializable {
    302       String getValue();
    303     }
    304 
    305     interface J {
    306       static void dump() {
    307         I i = () -> "B62168701 -- OK";
    308         System.out.println(i.getValue());
    309       }
    310     }
    311 
    312     static void test() {
    313       J.dump();
    314     }
    315   }
    316 
    317   static void z(Z p) {
    318     System.out.println(p.foo(null));
    319   }
    320 
    321   static void g(G<String[]> g) {
    322     StringBuilder builder = new StringBuilder("{");
    323     String sep = "";
    324     for (String s : g.foo(new String[] { "Arg0", "Arg1", "Arg2" })) {
    325       builder.append(sep).append(s);
    326       sep = ", ";
    327     }
    328     builder.append("}");
    329     System.out.println(builder.toString());
    330   }
    331 
    332   interface SuperChain {
    333     default String iMain() {
    334       return "SuperChain::iMain()";
    335     }
    336   }
    337 
    338   interface SuperChainDerived extends SuperChain {
    339     default String iMain() {
    340       return "SuperChainDerived::iMain(" + SuperChain.super.iMain() + ")";
    341     }
    342   }
    343 
    344   interface OtherSuperChain {
    345     default String iMain() {
    346       return "OtherSuperChain::iMain()";
    347     }
    348   }
    349 
    350   static class ClassWithSuperChain implements SuperChainDerived, OtherSuperChain {
    351     public String iMain() {
    352       return "ClassWithSuperChain::iMain(" + SuperChainDerived.super.iMain() + ")" + iMainImpl();
    353     }
    354 
    355     public String iMainImpl() {
    356       return "ClassWithSuperChain::iMain(" + SuperChainDerived.super.iMain() +
    357           " + " + OtherSuperChain.super.iMain() + ")";
    358     }
    359   }
    360 
    361   public static void main(String[] args) throws Exception {
    362     C c = new C();
    363     I i = c;
    364 
    365     c.iRegular();
    366     c.iDefault();
    367     c.iDefaultOverridden();
    368     I.iStatic();
    369     i.iRegular();
    370     i.iDefault();
    371     i.iDefaultOverridden();
    372 
    373     p(i.stateless()::iRegular);
    374     p(i.stateful()::iRegular);
    375 
    376     g(a -> a);
    377     g(a -> {
    378       int size = a.length;
    379       for (int x = 0; x < size / 2; x++) {
    380         String t = a[x];
    381         a[x] = a[size - 1 - x];
    382         a[size - 1 - x] = t;
    383       }
    384       return a;
    385     });
    386 
    387     p(c::iRegular);
    388     p(c::iDefault);
    389     p(c::iDefaultOverridden);
    390     p(I::iStatic);
    391     p(i::iRegular);
    392     p(i::iDefault);
    393     p(i::iDefaultOverridden);
    394 
    395     II ii = i::iRegular;
    396     p(II::iStatic);
    397     p(ii::iRegular);
    398     p(ii::iDefault);
    399     p(ii::iDefaultOverridden);
    400 
    401     z(s -> "From Interface With Bridges");
    402 
    403     System.out.println(new ClassWithSuperChain().iMain());
    404 
    405     ClassWithDefaultPackagePrivate c2 = new ClassWithDefaultPackagePrivate();
    406     InterfaceWithDefaultPackagePrivate i2 = c2;
    407 
    408     c2.defaultFoo();
    409     i2.defaultFoo();
    410     InterfaceWithDefaultPackagePrivate.staticFoo();
    411 
    412     p(c2::defaultFoo);
    413     p(i2::defaultFoo);
    414     p(InterfaceWithDefaultPackagePrivate::staticFoo);
    415     p(c2.lambda()::foo);
    416 
    417     B38257361.test();
    418     B38257037.test();
    419     B38306708.test();
    420     B38308515.test();
    421     B38302860.test();
    422     B62168701.test();
    423   }
    424 }
    425