1 /*
   2  * Copyright (c) 2011, 2013, 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  */
  23 
  24 /**
  25  * @test
  26  * @bug 8003280
  27  * @summary Add lambda tests
  28  *   Test bridge methods for certain SAM conversions
  29  *   Tests the set of generate fields
  30  *   Test the set of generated methods
  31  * @compile LambdaTest6.java
  32  * @run main LambdaTest6
  33  * @run main/othervm -Djdk.internal.lambda.disableEagerInitialization=true LambdaTest6
  34  */
  35 
  36 import java.lang.reflect.Field;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Modifier;
  39 import java.util.HashSet;
  40 import java.util.Set;
  41 
  42 public class LambdaTest6<T> {
  43 
  44     interface H {Object m();}
  45 
  46     interface K<U> {void m(U element);}
  47 
  48     interface L extends K<String> {} //generic substitution
  49 
  50     interface M {void m(String s);}
  51 
  52     interface KM extends K<String>, M{} //generic substitution
  53 
  54     interface N extends H {String m();} //covariant return
  55 
  56     private static void assertTrue(boolean b) {
  57         if(!b)
  58             throw new AssertionError();
  59     }
  60 
  61     private Set<String> setOfStringObject() {
  62         Set<String> s = new HashSet<>();
  63         s.add("java.lang.String");
  64         s.add("java.lang.Object");
  65         return s;
  66     }
  67 
  68     private static Set<String> allowedMethods() {
  69         Set<String> s = new HashSet<>();
  70         s.add("m");
  71         return s;
  72     }
  73 
  74     private static Set<String> allowedStaticFields() {
  75         Set<String> s = new HashSet<>();
  76         if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) {
  77             s.add("LAMBDA_INSTANCE$");
  78         }
  79         return s;
  80     }
  81 
  82     private static Set<String> checkNonCapturingFields(Class<?> c) {
  83         Set<String> s = new HashSet<>();
  84         for (Field f : c.getDeclaredFields()) {
  85             if (f.getName().equals("LAMBDA_INSTANCE$") && Modifier.isStatic(f.getModifiers())) {
  86                 if (Boolean.getBoolean("jdk.internal.lambda.disableEagerInitialization")) {
  87                     continue;
  88                 }
  89             }
  90             if (Modifier.isStatic(f.getModifiers())) {
  91                 s.add("static " + f.getName());
  92             } else {
  93                 s.add(f.getName());
  94             }
  95         }
  96         return s;
  97     }
  98 
  99     private static boolean matchingMethodNames(Method[] methods) {
 100         Set<String> methodNames = new HashSet<>();
 101         for (Method m : methods) {
 102             methodNames.add(m.getName());
 103         }
 104         return methodNames.equals(allowedMethods());
 105     }
 106 
 107     private void test1()
 108     {
 109         L la = s -> { };
 110         la.m("hi");
 111         Class<? extends L> c1 = la.getClass();
 112         Method[] methods = c1.getDeclaredMethods();
 113         assertTrue(matchingMethodNames(methods));
 114         assertTrue(checkNonCapturingFields(c1).isEmpty());
 115         Set<String> types = setOfStringObject();
 116         for(Method m : methods) {
 117             if ("m".equals(m.getName())) {
 118                 Class[] parameterTypes = m.getParameterTypes();
 119                 assertTrue(parameterTypes.length == 1);
 120                 assertTrue(types.remove(parameterTypes[0].getName()));
 121             }
 122         }
 123         assertTrue(types.isEmpty() || (types.size() == 1 && types.contains("java.lang.String")));
 124     }
 125 
 126     private void test2()
 127     {
 128         KM km = s -> { };
 129         //km.m("hi");
 130         Class<? extends KM> c2 = km.getClass();
 131         Method[] methods = c2.getDeclaredMethods();
 132         assertTrue(matchingMethodNames(methods));
 133         assertTrue(checkNonCapturingFields(c2).isEmpty());
 134         Set<String> types = setOfStringObject();
 135         for(Method m : methods) {
 136             if ("m".equals(m.getName())) {
 137                 Class[] parameterTypes = m.getParameterTypes();
 138                 assertTrue(parameterTypes.length == 1);
 139                 assertTrue(types.remove(parameterTypes[0].getName()));
 140             }
 141         }
 142         assertTrue(types.isEmpty());
 143     }
 144 
 145     private void test3()
 146     {
 147         N na = ()-> "hi";
 148         assertTrue( na.m().equals("hi") );
 149         assertTrue( ((H)na).m().equals("hi") );
 150         Class<? extends N> c3 = na.getClass();
 151         Method[] methods = c3.getDeclaredMethods();
 152         assertTrue(matchingMethodNames(methods));
 153         assertTrue(checkNonCapturingFields(c3).isEmpty());
 154         Set<String> types = setOfStringObject();
 155         for(Method m : methods) {
 156             if ("m".equals(m.getName())) {
 157                 Class returnType = m.getReturnType();
 158                 assertTrue(types.remove(returnType.getName()));
 159             }
 160         }
 161         assertTrue(types.size() == 1); //there's a bridge
 162     }
 163 
 164 
 165     public static void main(String[] args) {
 166         LambdaTest6 test = new LambdaTest6();
 167         test.test1();
 168         test.test2();
 169         test.test3();
 170     }
 171 }