1/*
2 * Copyright (c) 2015, 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
24package compiler.calls.common;
25
26import java.lang.invoke.CallSite;
27import java.lang.invoke.ConstantCallSite;
28import java.lang.invoke.MethodHandles;
29import java.lang.invoke.MethodType;
30
31/**
32 * A test class checking InvokeDynamic instruction.
33 * This is not quite "ready-to-use" class, since javac can't generate indy
34 * directly(only as part of lambda init) so, this class bytecode should be
35 * patched with method "caller" which uses indy. Other methods can be written in
36 * java for easier support and readability.
37 */
38
39public class InvokeDynamic extends CallsBase {
40    private static final Object LOCK = new Object();
41
42    public static void main(String args[]) {
43        new InvokeDynamic().runTest(args);
44    }
45
46    /**
47     * Caller method to call "callee" method. Must be overwritten with InvokeDynamicPatcher
48     */
49    @Override
50    public void caller() {
51    }
52
53    /**
54     * A bootstrap method for invokedynamic
55     * @param lookup a lookup object
56     * @param methodName methodName
57     * @param type method type
58     * @return CallSite for method
59     */
60    public static CallSite bootstrapMethod(MethodHandles.Lookup lookup,
61            String methodName, MethodType type) throws IllegalAccessException,
62            NoSuchMethodException {
63        MethodType mtype = MethodType.methodType(boolean.class,
64                new Class<?>[]{int.class, long.class, float.class,
65                    double.class, String.class});
66        return new ConstantCallSite(lookup.findVirtual(lookup.lookupClass(),
67                methodName, mtype));
68    }
69
70    /**
71     * A callee method, assumed to be called by "caller"
72     */
73    public boolean callee(int param1, long param2, float param3, double param4,
74            String param5) {
75        calleeVisited = true;
76        CallsBase.checkValues(param1, param2, param3, param4, param5);
77        return true;
78    }
79
80    /**
81     * A native callee method, assumed to be called by "caller"
82     */
83    public native boolean calleeNative(int param1, long param2, float param3,
84            double param4, String param5);
85
86    /**
87     * Returns object to lock execution on
88     * @return lock object
89     */
90    @Override
91    protected Object getLockObject() {
92        return LOCK;
93    }
94
95    @Override
96    protected void callerNative() {
97        throw new Error("No native call for invokedynamic");
98    }
99}
100