Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/java.base/share/classes/java/lang/invoke/ConstantCallSite.java
12520 views
1
/*[INCLUDE-IF Sidecar17 & !OPENJDK_METHODHANDLES]*/
2
/*******************************************************************************
3
* Copyright (c) 2011, 2020 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
22
*******************************************************************************/
23
package java.lang.invoke;
24
25
/**
26
* A ConstantCallSite is permanently bound to its initial target MethodHandle.
27
* Any call to {@link #setTarget(MethodHandle)} will result in an UnsupportedOperationException.
28
*
29
* @since 1.7
30
*/
31
public class ConstantCallSite extends CallSite {
32
private final MethodHandle target;
33
34
/**
35
* Create a ConstantCallSite with a target MethodHandle that cannot change.
36
*
37
* @param permanentTarget - the target MethodHandle to permanently associate with this CallSite.
38
*/
39
public ConstantCallSite(MethodHandle permanentTarget) {
40
super(permanentTarget.type());
41
// .type() call ensures non-null
42
target = permanentTarget;
43
}
44
45
/**
46
* Create a ConstantCallSite and assign the hook MethodHandle's result to its permanent target.
47
* The hook MethodHandle is invoked as though by (@link MethodHandle#invoke(this)) and must return a MethodHandle that will be installed
48
* as the ConstantCallSite's target.
49
* <p>
50
* The hook MethodHandle is required if the ConstantCallSite's target needs to have access to the ConstantCallSite instance. This is an
51
* action that user code cannot perform on its own.
52
* <p>
53
* The hook must return a MethodHandle that is exactly of type <i>targetType</i>.
54
* <p>
55
* Until the result of the hook has been installed in the ConstantCallSite, any call to getTarget() or dynamicInvoker() will throw an
56
* IllegalStateException. It is always valid to call type().
57
*
58
* @param targetType - the type of the ConstantCallSite's target
59
* @param hook - the hook handle, with signature (ConstantCallSite)MethodHandle
60
* @throws Throwable anything thrown by the hook.
61
* @throws WrongMethodTypeException if the hook has the wrong signature or returns a MethodHandle with the wrong signature
62
* @throws NullPointerException if the hook is null or returns null
63
* @throws ClassCastException if the result of the hook is not a MethodHandle
64
*/
65
protected ConstantCallSite(MethodType targetType, MethodHandle hook) throws Throwable, WrongMethodTypeException, NullPointerException, ClassCastException {
66
super(targetType);
67
MethodHandle handle = null;
68
if (hook != null) {
69
handle = (MethodHandle) hook.invoke(this);
70
}
71
handle.getClass(); // Throw NPE if null
72
if (handle.type != targetType) {
73
throw WrongMethodTypeException.newWrongMethodTypeException(targetType, handle.type);
74
}
75
target = handle;
76
}
77
78
/**
79
* Return the target MethodHandle of this CallSite.
80
* @throws IllegalStateException - if the target has not yet been assigned in the ConstantCallSite constructor
81
*/
82
@Override
83
public final MethodHandle dynamicInvoker() throws IllegalStateException {
84
return getTarget();
85
}
86
87
/**
88
* Return the target MethodHandle of this CallSite.
89
* The target is defined as though it where a final field.
90
*
91
* @throws IllegalStateException - if the target has not yet been assigned in the ConstantCallSite constructor
92
*/
93
@Override
94
public final MethodHandle getTarget() throws IllegalStateException {
95
if (target == null) {
96
throw new IllegalStateException();
97
}
98
return target;
99
}
100
101
/**
102
* Throws UnsupportedOperationException as a ConstantCallSite is permanently
103
* bound to its initial target MethodHandle.
104
*/
105
@Override
106
public final void setTarget(MethodHandle newTarget) {
107
throw new UnsupportedOperationException();
108
}
109
}
110
111
112