• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 /* @test
27  * @summary unit tests for java.lang.invoke.MethodHandles
28  * @run testng/othervm -ea -esa test.java.lang.invoke.VarArgsTest
29  */
30 package test.java.lang.invoke;
31 
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import java.util.Arrays;
36 import java.util.List;
37 import static java.lang.invoke.MethodHandles.*;
38 import static java.lang.invoke.MethodType.*;
39 import static org.testng.AssertJUnit.*;
40 import org.testng.annotations.*;
41 
42 public class VarArgsTest {
43 
44     @Test
testWithVarargs()45     public void testWithVarargs() throws Throwable {
46         MethodHandle deepToString = publicLookup()
47             .findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class));
48         assertFalse(deepToString.isVarargsCollector());
49         MethodHandle ts = deepToString.withVarargs(false);
50         assertFalse(ts.isVarargsCollector());
51         MethodHandle ts1 = deepToString.withVarargs(true);
52         assertTrue(ts1.isVarargsCollector());
53         assertEquals("[won]", (String) ts1.invokeExact(new Object[]{"won"}));
54         assertEquals("[won]", (String) ts1.invoke(new Object[]{"won"}));
55         assertEquals("[won]", (String) ts1.invoke("won"));
56         assertEquals("[won, won]", (String) ts1.invoke("won", "won"));
57         assertEquals("[won, won]", (String) ts1.invoke(new Object[]{"won", "won"}));
58         assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"}));
59     }
60 
61     @Test
testWithVarargs2()62     public void testWithVarargs2() throws Throwable {
63         MethodHandle asList = publicLookup()
64             .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class));
65         MethodHandle asListWithVarargs = asList.withVarargs(asList.isVarargsCollector());
66         assert(asListWithVarargs.isVarargsCollector());
67         assertEquals("[]", asListWithVarargs.invoke().toString());
68         assertEquals("[1]", asListWithVarargs.invoke(1).toString());
69         assertEquals("[two, too]", asListWithVarargs.invoke("two", "too").toString());
70     }
71 
72     @Test
73     @ExpectedExceptions(IllegalArgumentException.class)
testWithVarargsIAE()74     public void testWithVarargsIAE() throws Throwable {
75         MethodHandle lenMH = publicLookup()
76             .findVirtual(String.class, "length", methodType(int.class));
77         MethodHandle lenMHWithVarargs = lenMH.withVarargs(true);
78     }
79 
80 }
81