test/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestMethodHandle.java

Tue, 29 Mar 2016 10:48:49 +0000

author
dbuck
date
Tue, 29 Mar 2016 10:48:49 +0000
changeset 3102
e74dd6df4d4c
parent 0
959103a6100f
permissions
-rw-r--r--

8143647: Javac compiles method reference that allows results in an IllegalAccessError
Summary: Lambda implementation method synthesized by javac should not mention inaccessible types.
Reviewed-by: mcimadamore

     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  */
    26 /**
    27  * @test
    28  * @bug 8028739
    29  * @summary javac generates incorrect descriptor for MethodHandle::invoke
    30  * @run testng MethodReferenceTestMethodHandle
    31  */
    33 import java.lang.invoke.*;
    34 import java.util.*;
    36 import org.testng.annotations.Test;
    37 import static org.testng.Assert.assertEquals;
    39 @Test
    40 public class MethodReferenceTestMethodHandle {
    42   MethodHandles.Lookup lookup = MethodHandles.lookup();
    44   interface ReplaceItf {
    45       Object apply(String a, char b, char c) throws Throwable;
    46   }
    48   interface FormatItf {
    49       Object apply(String a, Object... args) throws Throwable;
    50   }
    52   interface AddItf {
    53       void apply(List st, int idx, Object v) throws Throwable;
    54   }
    56   public void testVirtual() throws Throwable {
    58       MethodType mt = MethodType.methodType(String.class, char.class, char.class);
    59       MethodHandle ms = lookup.findVirtual(String.class, "replace", mt);
    61       // --- String.replace(String, char, char) ---
    63       assertEquals("oome otring to oearch", ms.invoke("some string to search", 's', 'o'));
    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'));
    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   }
    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);
    78       // --- String.format(String, Object...) ---
    80       assertEquals("Testing One 2 3", fms.invoke("Testing %s %d %x", "One", new Integer(2), 3));
    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   }
    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<>();
    93       // --- List.add(int,String) ---
    95       pms.invoke(list, 0, "Hi");
    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 }

mercurial