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 java.lang.invoke;
25
26import java.lang.invoke.MethodHandles.Lookup;
27import jdk.internal.vm.annotation.DontInline;
28import jdk.internal.vm.annotation.ForceInline;
29/**
30 * Helper class to inject into java.lang.invoke that provides access to
31 * package-private methods in this package.
32 */
33
34public class MethodHandleHelper {
35
36    private MethodHandleHelper() { }
37
38    public static final Lookup IMPL_LOOKUP = Lookup.IMPL_LOOKUP;
39    public static final Class<?> MHN_CALL_SITE_CONTEXT_CLASS
40            = MethodHandleNatives.CallSiteContext.class;
41
42    public static void customize(MethodHandle mh) {
43        mh.customize();
44    }
45
46    @ForceInline
47    public static Object internalMemberName(MethodHandle mh) throws Throwable {
48        return mh.internalMemberName();
49    }
50
51    @ForceInline
52    public static void linkToStatic(MethodHandle mh, float arg, Object name) throws Throwable {
53        MethodHandle.linkToStatic(mh, arg, name);
54    }
55
56    @ForceInline
57    public static void invokeBasicV(MethodHandle mh, float arg) throws Throwable {
58        mh.invokeBasic(arg);
59    }
60
61    @ForceInline
62    public static Object invokeBasicL(MethodHandle mh) throws Throwable {
63        return mh.invokeBasic();
64    }
65
66    @ForceInline
67    public static int invokeBasicI(MethodHandle mh) throws Throwable {
68        return (int) mh.invokeBasic();
69    }
70
71    public static MethodHandle varargsArray(int nargs) {
72        return MethodHandleImpl.varargsArray(nargs);
73    }
74
75    public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
76        return MethodHandleImpl.varargsArray(arrayType, nargs);
77    }
78
79    public static LambdaForm getLambdaForm(MethodHandle mh) {
80        return mh.form;
81    }
82
83    public static class NonInlinedReinvoker extends DelegatingMethodHandle {
84        private final MethodHandle target;
85
86        private NonInlinedReinvoker(MethodHandle target, LambdaForm lf) {
87            super(target.type(), lf);
88            this.target = target;
89        }
90        @Override
91        public MethodHandle getTarget() {
92            return target;
93        }
94
95        @Override
96        public MethodHandle asTypeUncached(MethodType newType) {
97            return asTypeCache = target.asType(newType);
98        }
99
100        public static MethodHandle make(MethodHandle target) {
101            LambdaForm lform = DelegatingMethodHandle.makeReinvokerForm(
102                    target, -1, DelegatingMethodHandle.class,
103                /*forceInline=*/false, DelegatingMethodHandle.NF_getTarget, null);
104            return new NonInlinedReinvoker(target, lform);
105        }
106    }
107}
108