1 /*
2  * Copyright (c) 2017, 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 8187089
27  * @run main BasicTest
28  */
29 
30 import java.lang.invoke.*;
31 import java.util.Arrays;
32 
33 public class BasicTest {
34     static final int MAX_PARAM_SLOTS = 200;
35     static int exceedMaxParamSlots = 0;
main(String[] args)36     public static void main(String[] args) throws Throwable {
37         int expectionTestCases = 0;
38 
39         Class<?>[] types = new Class<?>[200];
40         Arrays.fill(types, int.class);
41         test(MethodType.methodType(String.class, types));
42 
43         types = new Class<?>[100];
44         Arrays.fill(types, long.class);
45         test(MethodType.methodType(String.class, types));
46 
47         // test cases exceeding 200 parameter slots
48         expectionTestCases++;
49         types = new Class<?>[101];
50         Arrays.fill(types, 0, 50, long.class);
51         Arrays.fill(types, 50, 100, double.class);
52         types[100] = int.class;
53         test(MethodType.methodType(String.class, types));
54 
55         expectionTestCases++;
56         types = new Class<?>[201];
57         Arrays.fill(types, int.class);
58         test(MethodType.methodType(String.class, types));
59 
60         if (exceedMaxParamSlots != expectionTestCases) {
61             throw new RuntimeException("expected one test case exceeding 200 param slots");
62         }
63     }
64 
65     /**
66      * Tests if StringConcatException is thrown if the given concatType
67      * has more than 200 parameter slots
68      */
test(MethodType concatType)69     static void test(MethodType concatType) throws StringConcatException {
70         String recipe = "";
71         int slots = 0;
72         for (Class<?> c : concatType.parameterList()) {
73             recipe += "\1";
74             slots++;
75             if (c == double.class || c == long.class) {
76                 slots++;
77             }
78         }
79         if (slots > MAX_PARAM_SLOTS) {
80             exceedMaxParamSlots++;
81         }
82         System.out.format("Test %s parameter slots%n", slots);
83         try {
84             StringConcatFactory.makeConcat(MethodHandles.lookup(), "name", concatType);
85             if (slots > MAX_PARAM_SLOTS) {
86                 throw new RuntimeException("StringConcatException not thrown");
87             }
88         } catch (StringConcatException e) {
89             if (slots <= MAX_PARAM_SLOTS) throw e;
90         }
91 
92         try {
93             StringConcatFactory.makeConcatWithConstants(MethodHandles.lookup(), "name",
94                                                         concatType, recipe);
95             if (slots > MAX_PARAM_SLOTS) {
96                 throw new RuntimeException("StringConcatException not thrown");
97             }
98         } catch (StringConcatException e) {
99             if (slots <= MAX_PARAM_SLOTS) throw e;
100         }
101     }
102 }
103