test/tools/javac/lambda/methodReference/SamConversion.java

changeset 1415
01c9d4161882
child 1435
9b26c96f5138
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/methodReference/SamConversion.java	Sat Nov 17 19:01:03 2012 +0000
     1.3 @@ -0,0 +1,337 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *   Test SAM conversion of method references in contexts of assignment, method/constructor argument,
    1.32 + *           return statement, array initializer, lambda expression body, conditional expression and cast.
    1.33 + * @compile SamConversion.java
    1.34 + * @run main SamConversion
    1.35 + */
    1.36 +
    1.37 +public class SamConversion {
    1.38 +
    1.39 +    static int assertionCount = 0;
    1.40 +
    1.41 +    static interface Foo {
    1.42 +        Integer m(int i);
    1.43 +    }
    1.44 +
    1.45 +    static interface Bar {
    1.46 +        int m(Integer i) throws MyException;
    1.47 +    }
    1.48 +
    1.49 +    private static void assertTrue(boolean b) {
    1.50 +        assertionCount++;
    1.51 +        if(!b)
    1.52 +            throw new AssertionError();
    1.53 +    }
    1.54 +
    1.55 +    private static int test1(Foo foo) {
    1.56 +        return foo.m(1);
    1.57 +    }
    1.58 +
    1.59 +    private static void test2(Bar bar, int result) {
    1.60 +        try {
    1.61 +            assertTrue(bar.m(1) == result);
    1.62 +        } catch (Exception e){
    1.63 +            assertTrue(false);
    1.64 +        }
    1.65 +    }
    1.66 +
    1.67 +    private static Bar test3(int i) {
    1.68 +        switch (i) {
    1.69 +        case 0:
    1.70 +            return A::method1;
    1.71 +        case 1:
    1.72 +            return new A()::method2;
    1.73 +        case 2:
    1.74 +            return A::method3;
    1.75 +        case 3:
    1.76 +            return new A()::method4;
    1.77 +        case 4:
    1.78 +            return new A()::method5;
    1.79 +        case 5:
    1.80 +            return A::method6;
    1.81 +        default:
    1.82 +            return null;
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Test SAM conversion of method reference in assignment context
    1.88 +     */
    1.89 +    private static void testAssignment() {
    1.90 +        Foo foo = A::method1; //static reference, parameter type matching and return type matching
    1.91 +        assertTrue(foo.m(1) == 2);
    1.92 +
    1.93 +        foo = new A()::method2; //instance reference, parameter type unboxing and return type boxing
    1.94 +        assertTrue(foo.m(1) == 3);
    1.95 +
    1.96 +        foo = A::method3; //static reference, parameter type matching and return type boxing
    1.97 +        assertTrue(foo.m(1) == 4);
    1.98 +
    1.99 +        foo = new A()::method4; //instance reference, parameter type unboxing and return type matching
   1.100 +        assertTrue(foo.m(1) == 5);
   1.101 +
   1.102 +        foo = new A()::method5; //instance reference, parameter type unboxing and return type matching
   1.103 +        assertTrue(foo.m(1) == 6);
   1.104 +
   1.105 +        Bar bar = A::method1;
   1.106 +        try {
   1.107 +            assertTrue(bar.m(1) == 2);
   1.108 +        } catch (Exception e) {
   1.109 +            assertTrue(false);
   1.110 +        }
   1.111 +
   1.112 +        bar = new A()::method2;
   1.113 +        try {
   1.114 +            assertTrue(bar.m(1) == 3);
   1.115 +        } catch (Exception e) {
   1.116 +            assertTrue(false);
   1.117 +        }
   1.118 +
   1.119 +        bar = A::method3;
   1.120 +        try {
   1.121 +            assertTrue(bar.m(1) == 4);
   1.122 +        } catch (Exception e) {
   1.123 +            assertTrue(false);
   1.124 +        }
   1.125 +
   1.126 +        bar = new A()::method4;
   1.127 +        try {
   1.128 +            assertTrue(bar.m(1) == 5);
   1.129 +        } catch (Exception e) {
   1.130 +            assertTrue(false);
   1.131 +        }
   1.132 +
   1.133 +        bar = new A()::method5;
   1.134 +        try {
   1.135 +            assertTrue(bar.m(1) == 6);
   1.136 +        } catch (Exception e) {
   1.137 +            assertTrue(false);
   1.138 +        }
   1.139 +
   1.140 +        bar = new A()::method6;
   1.141 +        try {
   1.142 +            bar.m(1);
   1.143 +            assertTrue(false);
   1.144 +        } catch (MyException e) {
   1.145 +        } catch (Exception e) {
   1.146 +            assertTrue(false);
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Test SAM conversion of method reference in method/constructor argument context
   1.152 +     */
   1.153 +    private static void testMethodArgument() {
   1.154 +        assertTrue(test1(A::method1) == 2);
   1.155 +        assertTrue(test1(new A()::method2) == 3);
   1.156 +        assertTrue(test1(A::method3) == 4);
   1.157 +        assertTrue(test1(new A()::method4) == 5);
   1.158 +        assertTrue(test1(new A()::method5) == 6);
   1.159 +        test2(A::method1, 2);
   1.160 +        test2(new A()::method2, 3);
   1.161 +        test2(A::method3, 4);
   1.162 +        test2(new A()::method4, 5);
   1.163 +        test2(new A()::method5, 6);
   1.164 +        A a = new A(A::method1); //A(Foo f) called
   1.165 +        assertTrue(a.method2(1) == 11);
   1.166 +        assertTrue(a.method4(1) == 11);
   1.167 +        assertTrue(a.method5(1) == 11);
   1.168 +        A a2 = new A(new A()::method2); //A(Bar b) called
   1.169 +        assertTrue(a2.method2(1) == 12);
   1.170 +        assertTrue(a2.method4(1) == 12);
   1.171 +        assertTrue(a2.method5(1) == 12);
   1.172 +    }
   1.173 +
   1.174 +    /**
   1.175 +     * Test SAM conversion of method reference in return statement context
   1.176 +     */
   1.177 +    private static void testReturnStatement() {
   1.178 +        Bar bar = test3(0);
   1.179 +        try {
   1.180 +            assertTrue(bar.m(1) == 2);
   1.181 +        } catch (Exception e) {
   1.182 +            assertTrue(false);
   1.183 +        }
   1.184 +        bar = test3(1);
   1.185 +        try {
   1.186 +            assertTrue(bar.m(1) == 3);
   1.187 +        } catch (Exception e) {
   1.188 +            assertTrue(false);
   1.189 +        }
   1.190 +        bar = test3(2);
   1.191 +        try {
   1.192 +            assertTrue(bar.m(1) == 4);
   1.193 +        } catch (Exception e) {
   1.194 +            assertTrue(false);
   1.195 +        }
   1.196 +        bar = test3(3);
   1.197 +        try {
   1.198 +            assertTrue(bar.m(1) == 5);
   1.199 +        } catch (Exception e) {
   1.200 +            assertTrue(false);
   1.201 +        }
   1.202 +        bar = test3(4);
   1.203 +        try {
   1.204 +            assertTrue(bar.m(1) == 6);
   1.205 +        } catch (Exception e) {
   1.206 +            assertTrue(false);
   1.207 +        }
   1.208 +        bar = test3(5);
   1.209 +        try {
   1.210 +            bar.m(1);
   1.211 +            assertTrue(false);
   1.212 +        } catch (MyException e) {
   1.213 +        } catch (Exception e) {
   1.214 +            assertTrue(false);
   1.215 +        }
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Test SAM conversion of method reference in cast context
   1.220 +     */
   1.221 +    private static void testCast() {
   1.222 +        assertTrue(((Foo)A::method1).m(1) == 2);
   1.223 +        try {
   1.224 +            assertTrue(((Bar)new A()::method2).m(1) == 3);
   1.225 +        } catch (Exception e) {
   1.226 +            assertTrue(false);
   1.227 +        }
   1.228 +    }
   1.229 +
   1.230 +    /**
   1.231 +     * Test SAM conversion of method reference in array initializer context
   1.232 +     */
   1.233 +    private static void testArrayInitializer() {
   1.234 +        Object[] oarray = {"a", 1, (Foo)A::method3}; //last element need a cast
   1.235 +        Object[] oarray2 = {"a", 1, (Bar)new A()::method4}; //last element need a cast
   1.236 +        Foo[] farray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5};
   1.237 +        Bar[] barray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5, A::method6};
   1.238 +    }
   1.239 +
   1.240 +    /**
   1.241 +     * Test SAM conversion of method reference in conditional expression context
   1.242 +     */
   1.243 +    private static void testConditionalExpression(boolean b) {
   1.244 +        Foo f = b ? A::method3 : new A()::method5;
   1.245 +        if(b)
   1.246 +            assertTrue(f.m(1) == 4);
   1.247 +        else
   1.248 +            assertTrue(f.m(1) == 6);
   1.249 +
   1.250 +        Bar bar = b ? A::method1 : A::method6;
   1.251 +        if(b) {
   1.252 +            try {
   1.253 +                assertTrue(bar.m(1) == 2);
   1.254 +            } catch (Exception e) {
   1.255 +                assertTrue(false);
   1.256 +            }
   1.257 +        }
   1.258 +        else {
   1.259 +            try {
   1.260 +                bar.m(1);
   1.261 +                assertTrue(false);
   1.262 +            } catch (MyException e) {
   1.263 +            } catch (Exception e) {
   1.264 +                assertTrue(false);
   1.265 +            }
   1.266 +        }
   1.267 +    }
   1.268 +
   1.269 +    /**
   1.270 +     * Test SAM conversion of method reference in lambda expression body
   1.271 +     */
   1.272 +    private static void testLambdaExpressionBody() {
   1.273 +        Foo f = n -> ((Foo)A::method3).m(n);
   1.274 +        assertTrue(f.m(1) == 4);
   1.275 +
   1.276 +        Bar b = n -> { return ((Foo)new A()::method5).m(n); };
   1.277 +        try {
   1.278 +            assertTrue(b.m(1) == 6);
   1.279 +        } catch (Exception e) {
   1.280 +            assertTrue(false);
   1.281 +        }
   1.282 +    }
   1.283 +
   1.284 +    public static void main(String[] args) {
   1.285 +        testAssignment();
   1.286 +        testMethodArgument();
   1.287 +        testReturnStatement();
   1.288 +        testCast();
   1.289 +        testArrayInitializer();
   1.290 +        testConditionalExpression(true);
   1.291 +        testConditionalExpression(false);
   1.292 +        testLambdaExpressionBody();
   1.293 +
   1.294 +        assertTrue(assertionCount == 38);
   1.295 +    }
   1.296 +
   1.297 +    static class MyException extends Exception {}
   1.298 +
   1.299 +    static class A {
   1.300 +
   1.301 +        int value = 0;
   1.302 +
   1.303 +        A() {
   1.304 +        }
   1.305 +
   1.306 +        A(Foo f) {
   1.307 +            value = f.m(9);
   1.308 +        }
   1.309 +
   1.310 +        A(Bar b) {
   1.311 +            try {
   1.312 +                value = b.m(9);
   1.313 +            } catch (MyException e){}
   1.314 +        }
   1.315 +
   1.316 +        static Integer method1(int n) {
   1.317 +            return n + 1;
   1.318 +        }
   1.319 +
   1.320 +        int method2(Integer n) {
   1.321 +            return value == 0 ? n + 2 : n + value;
   1.322 +        }
   1.323 +
   1.324 +        static int method3(int n) {
   1.325 +            return n + 3;
   1.326 +        }
   1.327 +
   1.328 +        Integer method4(Integer n) {
   1.329 +            return value == 0 ? n + 4 : n + value;
   1.330 +        }
   1.331 +
   1.332 +        Integer method5(Integer n) {
   1.333 +            return value == 0 ? new Integer(n + 5) : new Integer(n + value);
   1.334 +        }
   1.335 +
   1.336 +        static int method6(Integer n) throws MyException{
   1.337 +            throw new MyException();
   1.338 +        }
   1.339 +    }
   1.340 +}

mercurial