langtools/test/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestMethodHandle.java
changeset 21892 8ab5aa58a900
child 32337 c9d3ab9f601c
equal deleted inserted replaced
21891:42435b2c96cf 21892:8ab5aa58a900
       
     1 /*
       
     2  * Copyright (c) 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /**
       
    27  * @test
       
    28  * @bug 8028739
       
    29  * @summary javac generates incorrect descriptor for MethodHandle::invoke
       
    30  * @run testng MethodReferenceTestMethodHandle
       
    31  */
       
    32 
       
    33 import java.lang.invoke.*;
       
    34 import java.util.*;
       
    35 
       
    36 import org.testng.annotations.Test;
       
    37 import static org.testng.Assert.assertEquals;
       
    38 
       
    39 @Test
       
    40 public class MethodReferenceTestMethodHandle {
       
    41 
       
    42   MethodHandles.Lookup lookup = MethodHandles.lookup();
       
    43 
       
    44   interface ReplaceItf {
       
    45       Object apply(String a, char b, char c) throws Throwable;
       
    46   }
       
    47 
       
    48   interface FormatItf {
       
    49       Object apply(String a, Object... args) throws Throwable;
       
    50   }
       
    51 
       
    52   interface AddItf {
       
    53       void apply(List st, int idx, Object v) throws Throwable;
       
    54   }
       
    55 
       
    56   public void testVirtual() throws Throwable {
       
    57 
       
    58       MethodType mt = MethodType.methodType(String.class, char.class, char.class);
       
    59       MethodHandle ms = lookup.findVirtual(String.class, "replace", mt);
       
    60 
       
    61       // --- String.replace(String, char, char) ---
       
    62 
       
    63       assertEquals("oome otring to oearch", ms.invoke("some string to search", 's', 'o'));
       
    64 
       
    65       ReplaceItf f1 = (a, b, c) -> ms.invoke(a,b,c);
       
    66       assertEquals("oome otring to oearch", f1.apply("some string to search", 's', 'o'));
       
    67 
       
    68       ReplaceItf f2 = ms::invoke;
       
    69       assertEquals("oome otring to oearch", f2.apply("some string to search", 's', 'o'));
       
    70       assertEquals("oome otring to oearch", f2.apply("some string to search", new Character('s'), 'o'));
       
    71       assertEquals("oome otring to oearch", ((ReplaceItf) ms::invoke).apply("some string to search", 's', 'o'));
       
    72   }
       
    73 
       
    74   public void testStatic() throws Throwable {
       
    75       MethodType fmt = MethodType.methodType(String.class, String.class, (new Object[1]).getClass());
       
    76       MethodHandle fms = lookup.findStatic(String.class, "format", fmt);
       
    77 
       
    78       // --- String.format(String, Object...) ---
       
    79 
       
    80       assertEquals("Testing One 2 3", fms.invoke("Testing %s %d %x", "One", new Integer(2), 3));
       
    81 
       
    82       FormatItf ff2 = fms::invoke;
       
    83       assertEquals("Testing One 2 3", ff2.apply("Testing %s %d %x", "One", new Integer(2), 3));
       
    84       assertEquals("Testing One 2 3", ((FormatItf) fms::invoke).apply("Testing %s %d %x", "One", new Integer(2), 3));
       
    85       assertEquals("Testing One 2 3 four", ff2.apply("Testing %s %d %x %s", "One", new Integer(2), 3, "four"));
       
    86   }
       
    87 
       
    88   public void testVoid() throws Throwable {
       
    89       MethodType pmt = MethodType.methodType(void.class, int.class, Object.class);
       
    90       MethodHandle pms = lookup.findVirtual(List.class, "add", pmt);
       
    91       List<String> list = new ArrayList<>();
       
    92 
       
    93       // --- List.add(int,String) ---
       
    94 
       
    95       pms.invoke(list, 0, "Hi");
       
    96 
       
    97       AddItf pf2 = pms::invoke;
       
    98       pf2.apply(list, 1, "there");
       
    99       AddItf pf3 = pms::invokeExact;
       
   100       pf3.apply(list, 2, "you");
       
   101       assertEquals("Hi", list.get(0));
       
   102       assertEquals("there", list.get(1));
       
   103       assertEquals("you", list.get(2));
       
   104    }
       
   105 }