author | jrose |
Sat, 05 Oct 2013 05:30:39 -0700 | |
changeset 20529 | b49b07206f7d |
parent 20528 | 0b1e2130d3f7 |
child 20530 | b54a1f5cd35f |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
16030
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14222
diff
changeset
|
2 |
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
4 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
10 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
16 |
* |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
20 |
* |
5506 | 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. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
24 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
25 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
26 |
package java.lang.invoke; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
27 |
|
7053 | 28 |
import java.lang.reflect.*; |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
29 |
import java.util.List; |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
30 |
import java.util.ArrayList; |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
31 |
import java.util.Arrays; |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
32 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
33 |
import sun.invoke.util.ValueConversions; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
34 |
import sun.invoke.util.VerifyAccess; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
35 |
import sun.invoke.util.Wrapper; |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
36 |
import sun.reflect.CallerSensitive; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
37 |
import sun.reflect.Reflection; |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
38 |
import sun.reflect.misc.ReflectUtil; |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
39 |
import sun.security.util.SecurityConstants; |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
40 |
import static java.lang.invoke.MethodHandleStatics.*; |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
41 |
import static java.lang.invoke.MethodHandleNatives.Constants.*; |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
42 |
import java.util.concurrent.ConcurrentHashMap; |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
43 |
import sun.security.util.SecurityConstants; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
44 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
45 |
/** |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
46 |
* This class consists exclusively of static methods that operate on or return |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
47 |
* method handles. They fall into several categories: |
4537 | 48 |
* <ul> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
49 |
* <li>Lookup methods which help create method handles for methods and fields. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
50 |
* <li>Combinator methods, which combine or transform pre-existing method handles into new ones. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
51 |
* <li>Other factory methods to create method handles that emulate other common JVM operations or control flow patterns. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
52 |
* <li>Wrapper methods which can convert between method handles and interface types. |
4537 | 53 |
* </ul> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
54 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
55 |
* @author John Rose, JSR 292 EG |
19804 | 56 |
* @since 1.7 |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
57 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
58 |
public class MethodHandles { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
59 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
60 |
private MethodHandles() { } // do not instantiate |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
61 |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
62 |
private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
63 |
static { MethodHandleImpl.initStatics(); } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
64 |
// See IMPL_LOOKUP below. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
65 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
66 |
//// Method handle creation from ordinary methods. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
67 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
68 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
69 |
* Returns a {@link Lookup lookup object} on the caller, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
70 |
* which has the capability to access any method handle that the caller has access to, |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
71 |
* including direct method handles to private fields and methods. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
72 |
* This lookup object is a <em>capability</em> which may be delegated to trusted agents. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
73 |
* Do not store it in place where untrusted code can access it. |
18569 | 74 |
* @return a lookup object for the caller of this method |
4537 | 75 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
76 |
@CallerSensitive |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
77 |
public static Lookup lookup() { |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
78 |
return new Lookup(Reflection.getCallerClass()); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
79 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
80 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
81 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
82 |
* Returns a {@link Lookup lookup object} which is trusted minimally. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
83 |
* It can only be used to create method handles to |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
84 |
* publicly accessible fields and methods. |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
85 |
* <p> |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
86 |
* As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class} |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
87 |
* of this lookup object will be {@link java.lang.Object}. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
88 |
* <p> |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
89 |
* The lookup class can be changed to any other class {@code C} using an expression of the form |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
90 |
* {@linkplain Lookup#in <code>publicLookup().in(C.class)</code>}. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
91 |
* Since all classes have equal access to public names, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
92 |
* such a change would confer no new access rights. |
18569 | 93 |
* @return a lookup object which is trusted minimally |
4537 | 94 |
*/ |
95 |
public static Lookup publicLookup() { |
|
96 |
return Lookup.PUBLIC_LOOKUP; |
|
97 |
} |
|
98 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
99 |
/** |
19804 | 100 |
* Performs an unchecked "crack" of a direct method handle. |
101 |
* The result is as if the user had obtained a lookup object capable enough |
|
102 |
* to crack the target method handle, called |
|
103 |
* {@link java.lang.invoke.MethodHandles.Lookup#revealDirect Lookup.revealDirect} |
|
104 |
* on the target to obtain its symbolic reference, and then called |
|
105 |
* {@link java.lang.invoke.MethodHandleInfo#reflectAs MethodHandleInfo.reflectAs} |
|
106 |
* to resolve the symbolic reference to a member. |
|
107 |
* <p> |
|
108 |
* If there is a security manager, its {@code checkPermission} method |
|
109 |
* is called with a {@code ReflectPermission("suppressAccessChecks")} permission. |
|
110 |
* @param <T> the desired type of the result, either {@link Member} or a subtype |
|
111 |
* @param target a direct method handle to crack into symbolic reference components |
|
112 |
* @param expected a class object representing the desired result type {@code T} |
|
113 |
* @return a reference to the method, constructor, or field object |
|
114 |
* @exception SecurityException if the caller is not privileged to call {@code setAccessible} |
|
115 |
* @exception NullPointerException if either argument is {@code null} |
|
116 |
* @exception IllegalArgumentException if the target is not a direct method handle |
|
117 |
* @exception ClassCastException if the member is not of the expected type |
|
118 |
* @since 1.8 |
|
119 |
*/ |
|
120 |
public static <T extends Member> T |
|
121 |
reflectAs(Class<T> expected, MethodHandle target) { |
|
122 |
SecurityManager smgr = System.getSecurityManager(); |
|
123 |
if (smgr != null) smgr.checkPermission(ACCESS_PERMISSION); |
|
124 |
Lookup lookup = Lookup.IMPL_LOOKUP; // use maximally privileged lookup |
|
125 |
return lookup.revealDirect(target).reflectAs(expected, lookup); |
|
126 |
} |
|
127 |
// Copied from AccessibleObject, as used by Method.setAccessible, etc.: |
|
128 |
static final private java.security.Permission ACCESS_PERMISSION = |
|
129 |
new ReflectPermission("suppressAccessChecks"); |
|
130 |
||
131 |
/** |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
132 |
* A <em>lookup object</em> is a factory for creating method handles, |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
133 |
* when the creation requires access checking. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
134 |
* Method handles do not perform |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
135 |
* access checks when they are called, but rather when they are created. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
136 |
* Therefore, method handle access |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
137 |
* restrictions must be enforced when a method handle is created. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
138 |
* The caller class against which those restrictions are enforced |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
139 |
* is known as the {@linkplain #lookupClass lookup class}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
140 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
141 |
* A lookup class which needs to create method handles will call |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
142 |
* {@link MethodHandles#lookup MethodHandles.lookup} to create a factory for itself. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
143 |
* When the {@code Lookup} factory object is created, the identity of the lookup class is |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
144 |
* determined, and securely stored in the {@code Lookup} object. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
145 |
* The lookup class (or its delegates) may then use factory methods |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
146 |
* on the {@code Lookup} object to create method handles for access-checked members. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
147 |
* This includes all methods, constructors, and fields which are allowed to the lookup class, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
148 |
* even private ones. |
18569 | 149 |
* |
150 |
* <h1><a name="lookups"></a>Lookup Factory Methods</h1> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
151 |
* The factory methods on a {@code Lookup} object correspond to all major |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
152 |
* use cases for methods, constructors, and fields. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
153 |
* Here is a summary of the correspondence between these factory methods and |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
154 |
* the behavior the resulting method handles: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
155 |
* <table border=1 cellpadding=5 summary="lookup method behaviors"> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
156 |
* <tr><th>lookup expression</th><th>member</th><th>behavior</th></tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
157 |
* <tr> |
18569 | 158 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td> |
159 |
* <td>{@code FT f;}</td><td>{@code (T) this.f;}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
160 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
161 |
* <tr> |
18569 | 162 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td> |
163 |
* <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
164 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
165 |
* <tr> |
18569 | 166 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td> |
167 |
* <td>{@code FT f;}</td><td>{@code this.f = x;}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
168 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
169 |
* <tr> |
18569 | 170 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td> |
171 |
* <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
172 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
173 |
* <tr> |
18569 | 174 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td> |
175 |
* <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
176 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
177 |
* <tr> |
18569 | 178 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td> |
179 |
* <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
180 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
181 |
* <tr> |
18569 | 182 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td> |
183 |
* <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
184 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
185 |
* <tr> |
18569 | 186 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td> |
187 |
* <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
188 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
189 |
* <tr> |
18569 | 190 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td> |
191 |
* <td>({@code static})?<br>{@code FT f;}</td><td>{@code (FT) aField.get(thisOrNull);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
192 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
193 |
* <tr> |
18569 | 194 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td> |
195 |
* <td>({@code static})?<br>{@code FT f;}</td><td>{@code aField.set(thisOrNull, arg);}</td> |
|
196 |
* </tr> |
|
197 |
* <tr> |
|
198 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td> |
|
199 |
* <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
200 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
201 |
* <tr> |
18569 | 202 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td> |
203 |
* <td>{@code C(A*);}</td><td>{@code (C) aConstructor.newInstance(arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
204 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
205 |
* <tr> |
18569 | 206 |
* <td>{@link java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td> |
207 |
* <td>({@code static})?<br>{@code T m(A*);}</td><td>{@code (T) aMethod.invoke(thisOrNull, arg*);}</td> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
208 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
209 |
* </table> |
18569 | 210 |
* |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
211 |
* Here, the type {@code C} is the class or interface being searched for a member, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
212 |
* documented as a parameter named {@code refc} in the lookup methods. |
18569 | 213 |
* The method type {@code MT} is composed from the return type {@code T} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
214 |
* and the sequence of argument types {@code A*}. |
18569 | 215 |
* The constructor also has a sequence of argument types {@code A*} and |
216 |
* is deemed to return the newly-created object of type {@code C}. |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
217 |
* Both {@code MT} and the field type {@code FT} are documented as a parameter named {@code type}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
218 |
* The formal parameter {@code this} stands for the self-reference of type {@code C}; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
219 |
* if it is present, it is always the leading argument to the method handle invocation. |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
220 |
* (In the case of some {@code protected} members, {@code this} may be |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
221 |
* restricted in type to the lookup class; see below.) |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
222 |
* The name {@code arg} stands for all the other method handle arguments. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
223 |
* In the code examples for the Core Reflection API, the name {@code thisOrNull} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
224 |
* stands for a null reference if the accessed method or field is static, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
225 |
* and {@code this} otherwise. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
226 |
* The names {@code aMethod}, {@code aField}, and {@code aConstructor} stand |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
227 |
* for reflective objects corresponding to the given members. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
228 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
229 |
* In cases where the given member is of variable arity (i.e., a method or constructor) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
230 |
* the returned method handle will also be of {@linkplain MethodHandle#asVarargsCollector variable arity}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
231 |
* In all other cases, the returned method handle will be of fixed arity. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
232 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
233 |
* The equivalence between looked-up method handles and underlying |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
234 |
* class members can break down in a few ways: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
235 |
* <ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
236 |
* <li>If {@code C} is not symbolically accessible from the lookup class's loader, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
237 |
* the lookup can still succeed, even when there is no equivalent |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
238 |
* Java expression or bytecoded constant. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
239 |
* <li>Likewise, if {@code T} or {@code MT} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
240 |
* is not symbolically accessible from the lookup class's loader, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
241 |
* the lookup can still succeed. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
242 |
* For example, lookups for {@code MethodHandle.invokeExact} and |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
243 |
* {@code MethodHandle.invoke} will always succeed, regardless of requested type. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
244 |
* <li>If there is a security manager installed, it can forbid the lookup |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
245 |
* on various grounds (<a href="#secmgr">see below</a>). |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
246 |
* By contrast, the {@code ldc} instruction is not subject to |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
247 |
* security manager checks. |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
248 |
* <li>If the looked-up method has a |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
249 |
* <a href="MethodHandle.html#maxarity">very large arity</a>, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
250 |
* the method handle creation may fail, due to the method handle |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
251 |
* type having too many parameters. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
252 |
* </ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
253 |
* |
18569 | 254 |
* <h1><a name="access"></a>Access checking</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
255 |
* Access checks are applied in the factory methods of {@code Lookup}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
256 |
* when a method handle is created. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
257 |
* This is a key difference from the Core Reflection API, since |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
258 |
* {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
259 |
* performs access checking against every caller, on every call. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
260 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
261 |
* All access checks start from a {@code Lookup} object, which |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
262 |
* compares its recorded lookup class against all requests to |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
263 |
* create method handles. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
264 |
* A single {@code Lookup} object can be used to create any number |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
265 |
* of access-checked method handles, all checked against a single |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
266 |
* lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
267 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
268 |
* A {@code Lookup} object can be shared with other trusted code, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
269 |
* such as a metaobject protocol. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
270 |
* A shared {@code Lookup} object delegates the capability |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
271 |
* to create method handles on private members of the lookup class. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
272 |
* Even if privileged code uses the {@code Lookup} object, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
273 |
* the access checking is confined to the privileges of the |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
274 |
* original lookup class. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
275 |
* <p> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
276 |
* A lookup can fail, because |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
277 |
* the containing class is not accessible to the lookup class, or |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
278 |
* because the desired class member is missing, or because the |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
279 |
* desired class member is not accessible to the lookup class. |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
280 |
* In any of these cases, a {@code ReflectiveOperationException} will be |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
281 |
* thrown from the attempted lookup. The exact class will be one of |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
282 |
* the following: |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
283 |
* <ul> |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
284 |
* <li>NoSuchMethodException — if a method is requested but does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
285 |
* <li>NoSuchFieldException — if a field is requested but does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
286 |
* <li>IllegalAccessException — if the member exists but an access check fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
287 |
* </ul> |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
288 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
289 |
* In general, the conditions under which a method handle may be |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
290 |
* looked up for a method {@code M} are exactly equivalent to the conditions |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
291 |
* under which the lookup class could have compiled and resolved a call to {@code M}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
292 |
* And the effect of invoking the method handle resulting from the lookup |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
293 |
* is exactly equivalent to executing the compiled and resolved call to {@code M}. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
294 |
* The same point is true of fields and constructors. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
295 |
* <p> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
296 |
* If the desired member is {@code protected}, the usual JVM rules apply, |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
297 |
* including the requirement that the lookup class must be either be in the |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
298 |
* same package as the desired member, or must inherit that member. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
299 |
* (See the Java Virtual Machine Specification, sections 4.9.2, 5.4.3.5, and 6.4.) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
300 |
* In addition, if the desired member is a non-static field or method |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
301 |
* in a different package, the resulting method handle may only be applied |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
302 |
* to objects of the lookup class or one of its subclasses. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
303 |
* This requirement is enforced by narrowing the type of the leading |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
304 |
* {@code this} parameter from {@code C} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
305 |
* (which will necessarily be a superclass of the lookup class) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
306 |
* to the lookup class itself. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
307 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
308 |
* In some cases, access between nested classes is obtained by the Java compiler by creating |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
309 |
* an wrapper method to access a private method of another class |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
310 |
* in the same top-level declaration. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
311 |
* For example, a nested class {@code C.D} |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
312 |
* can access private members within other related classes such as |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
313 |
* {@code C}, {@code C.D.E}, or {@code C.B}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
314 |
* but the Java compiler may need to generate wrapper methods in |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
315 |
* those related classes. In such cases, a {@code Lookup} object on |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
316 |
* {@code C.E} would be unable to those private members. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
317 |
* A workaround for this limitation is the {@link Lookup#in Lookup.in} method, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
318 |
* which can transform a lookup on {@code C.E} into one on any of those other |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
319 |
* classes, without special elevation of privilege. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
320 |
* <p> |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
321 |
* Although bytecode instructions can only refer to classes in |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
322 |
* a related class loader, this API can search for methods in any |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
323 |
* class, as long as a reference to its {@code Class} object is |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
324 |
* available. Such cross-loader references are also possible with the |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
325 |
* Core Reflection API, and are impossible to bytecode instructions |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
326 |
* such as {@code invokestatic} or {@code getfield}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
327 |
* There is a {@linkplain java.lang.SecurityManager security manager API} |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
328 |
* to allow applications to check such cross-loader references. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
329 |
* These checks apply to both the {@code MethodHandles.Lookup} API |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
330 |
* and the Core Reflection API |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
331 |
* (as found on {@link java.lang.Class Class}). |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
332 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
333 |
* Access checks only apply to named and reflected methods, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
334 |
* constructors, and fields. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
335 |
* Other method handle creation methods, such as |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
336 |
* {@link MethodHandle#asType MethodHandle.asType}, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
337 |
* do not require any access checks, and are done |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
338 |
* with static methods of {@link MethodHandles}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
339 |
* independently of any {@code Lookup} object. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
340 |
* |
18569 | 341 |
* <h1>Security manager interactions</h1> |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
342 |
* <a name="secmgr"></a> |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
343 |
* If a security manager is present, member lookups are subject to |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
344 |
* additional checks. |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
345 |
* From one to three calls are made to the security manager. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
346 |
* Any of these calls can refuse access by throwing a |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
347 |
* {@link java.lang.SecurityException SecurityException}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
348 |
* Define {@code smgr} as the security manager, |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
349 |
* {@code lookc} as the lookup class of the current lookup object, |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
350 |
* {@code refc} as the containing class in which the member |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
351 |
* is being sought, and {@code defc} as the class in which the |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
352 |
* member is actually defined. |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
353 |
* The value {@code lookc} is defined as <em>not present</em> |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
354 |
* if the current lookup object does not have |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
355 |
* {@linkplain java.lang.invoke.MethodHandles.Lookup#PRIVATE private access}. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
356 |
* The calls are made according to the following rules: |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
357 |
* <ul> |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
358 |
* <li>If {@code lookc} is not present, or if its class loader is not |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
359 |
* the same as or an ancestor of the class loader of {@code refc}, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
360 |
* then {@link SecurityManager#checkPackageAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
361 |
* smgr.checkPackageAccess(refcPkg)} is called, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
362 |
* where {@code refcPkg} is the package of {@code refc}. |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
363 |
* <li>If the retrieved member is not public and |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
364 |
* {@code lookc} is not present, then |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
365 |
* {@link SecurityManager#checkPermission smgr.checkPermission} |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
366 |
* with {@code RuntimePermission("accessDeclaredMembers")} is called. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
367 |
* <li>If the retrieved member is not public, |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
368 |
* and if {@code defc} and {@code refc} are different, |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
369 |
* then {@link SecurityManager#checkPackageAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
370 |
* smgr.checkPackageAccess(defcPkg)} is called, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
371 |
* where {@code defcPkg} is the package of {@code defc}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
372 |
* </ul> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
373 |
*/ |
14222 | 374 |
// FIXME in MR1: clarify that the bytecode behavior of a caller-ID method (like Class.forName) is relative to the lookupClass used to create the method handle, not the dynamic caller of the method handle |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
375 |
public static final |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
376 |
class Lookup { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
377 |
/** The class on behalf of whom the lookup is being performed. */ |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
378 |
private final Class<?> lookupClass; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
379 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
380 |
/** The allowed sorts of members which may be looked up (PUBLIC, etc.). */ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
381 |
private final int allowedModes; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
382 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
383 |
/** A single-bit mask representing {@code public} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
384 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
385 |
* The value, {@code 0x01}, happens to be the same as the value of the |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
386 |
* {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
387 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
388 |
public static final int PUBLIC = Modifier.PUBLIC; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
389 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
390 |
/** A single-bit mask representing {@code private} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
391 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
392 |
* The value, {@code 0x02}, happens to be the same as the value of the |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
393 |
* {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
394 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
395 |
public static final int PRIVATE = Modifier.PRIVATE; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
396 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
397 |
/** A single-bit mask representing {@code protected} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
398 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
399 |
* The value, {@code 0x04}, happens to be the same as the value of the |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
400 |
* {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
401 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
402 |
public static final int PROTECTED = Modifier.PROTECTED; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
403 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
404 |
/** A single-bit mask representing {@code package} access (default access), |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
405 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
406 |
* The value is {@code 0x08}, which does not correspond meaningfully to |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
407 |
* any particular {@linkplain java.lang.reflect.Modifier modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
408 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
409 |
public static final int PACKAGE = Modifier.STATIC; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
410 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
411 |
private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
412 |
private static final int TRUSTED = -1; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
413 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
414 |
private static int fixmods(int mods) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
415 |
mods &= (ALL_MODES - PACKAGE); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
416 |
return (mods != 0) ? mods : PACKAGE; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
417 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
418 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
419 |
/** Tells which class is performing the lookup. It is this class against |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
420 |
* which checks are performed for visibility and access permissions. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
421 |
* <p> |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
422 |
* The class implies a maximum level of access permission, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
423 |
* but the permissions may be additionally limited by the bitmask |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
424 |
* {@link #lookupModes lookupModes}, which controls whether non-public members |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
425 |
* can be accessed. |
18569 | 426 |
* @return the lookup class, on behalf of which this lookup object finds members |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
427 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
428 |
public Class<?> lookupClass() { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
429 |
return lookupClass; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
430 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
431 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
432 |
// This is just for calling out to MethodHandleImpl. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
433 |
private Class<?> lookupClassOrNull() { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
434 |
return (allowedModes == TRUSTED) ? null : lookupClass; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
435 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
436 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
437 |
/** Tells which access-protection classes of members this lookup object can produce. |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
438 |
* The result is a bit-mask of the bits |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
439 |
* {@linkplain #PUBLIC PUBLIC (0x01)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
440 |
* {@linkplain #PRIVATE PRIVATE (0x02)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
441 |
* {@linkplain #PROTECTED PROTECTED (0x04)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
442 |
* and {@linkplain #PACKAGE PACKAGE (0x08)}. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
443 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
444 |
* A freshly-created lookup object |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
445 |
* on the {@linkplain java.lang.invoke.MethodHandles#lookup() caller's class} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
446 |
* has all possible bits set, since the caller class can access all its own members. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
447 |
* A lookup object on a new lookup class |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
448 |
* {@linkplain java.lang.invoke.MethodHandles.Lookup#in created from a previous lookup object} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
449 |
* may have some mode bits set to zero. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
450 |
* The purpose of this is to restrict access via the new lookup object, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
451 |
* so that it can access only names which can be reached by the original |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
452 |
* lookup object, and also by the new lookup class. |
18569 | 453 |
* @return the lookup modes, which limit the kinds of access performed by this lookup object |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
454 |
*/ |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
455 |
public int lookupModes() { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
456 |
return allowedModes & ALL_MODES; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
457 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
458 |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
459 |
/** Embody the current class (the lookupClass) as a lookup class |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
460 |
* for method handle creation. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
461 |
* Must be called by from a method in this package, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
462 |
* which in turn is called by a method not in this package. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
463 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
464 |
Lookup(Class<?> lookupClass) { |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
465 |
this(lookupClass, ALL_MODES); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
466 |
// make sure we haven't accidentally picked up a privileged class: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
467 |
checkUnprivilegedlookupClass(lookupClass); |
4537 | 468 |
} |
469 |
||
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
470 |
private Lookup(Class<?> lookupClass, int allowedModes) { |
4537 | 471 |
this.lookupClass = lookupClass; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
472 |
this.allowedModes = allowedModes; |
4537 | 473 |
} |
474 |
||
475 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
476 |
* Creates a lookup on the specified new lookup class. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
477 |
* The resulting object will report the specified |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
478 |
* class as its own {@link #lookupClass lookupClass}. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
479 |
* <p> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
480 |
* However, the resulting {@code Lookup} object is guaranteed |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
481 |
* to have no more access capabilities than the original. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
482 |
* In particular, access capabilities can be lost as follows:<ul> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
483 |
* <li>If the new lookup class differs from the old one, |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
484 |
* protected members will not be accessible by virtue of inheritance. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
485 |
* (Protected members may continue to be accessible because of package sharing.) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
486 |
* <li>If the new lookup class is in a different package |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
487 |
* than the old one, protected and default (package) members will not be accessible. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
488 |
* <li>If the new lookup class is not within the same package member |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
489 |
* as the old one, private members will not be accessible. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
490 |
* <li>If the new lookup class is not accessible to the old lookup class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
491 |
* then no members, not even public members, will be accessible. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
492 |
* (In all other cases, public members will continue to be accessible.) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
493 |
* </ul> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
494 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
495 |
* @param requestedLookupClass the desired lookup class for the new lookup object |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
496 |
* @return a lookup object which reports the desired lookup class |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
497 |
* @throws NullPointerException if the argument is null |
4537 | 498 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
499 |
public Lookup in(Class<?> requestedLookupClass) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
500 |
requestedLookupClass.getClass(); // null check |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
501 |
if (allowedModes == TRUSTED) // IMPL_LOOKUP can make any lookup at all |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
502 |
return new Lookup(requestedLookupClass, ALL_MODES); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
503 |
if (requestedLookupClass == this.lookupClass) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
504 |
return this; // keep same capabilities |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
505 |
int newModes = (allowedModes & (ALL_MODES & ~PROTECTED)); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
506 |
if ((newModes & PACKAGE) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
507 |
&& !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
508 |
newModes &= ~(PACKAGE|PRIVATE); |
4537 | 509 |
} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
510 |
// Allow nestmate lookups to be created without special privilege: |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
511 |
if ((newModes & PRIVATE) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
512 |
&& !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
513 |
newModes &= ~PRIVATE; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
514 |
} |
13044
8411854afc2b
7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
11535
diff
changeset
|
515 |
if ((newModes & PUBLIC) != 0 |
8411854afc2b
7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
11535
diff
changeset
|
516 |
&& !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass, allowedModes)) { |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
517 |
// The requested class it not accessible from the lookup class. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
518 |
// No permissions. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
519 |
newModes = 0; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
520 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
521 |
checkUnprivilegedlookupClass(requestedLookupClass); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
522 |
return new Lookup(requestedLookupClass, newModes); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
523 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
524 |
|
4537 | 525 |
// Make sure outer class is initialized first. |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
526 |
static { IMPL_NAMES.getClass(); } |
4537 | 527 |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
528 |
/** Version of lookup which is trusted minimally. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
529 |
* It can only be used to create method handles to |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
530 |
* publicly accessible members. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
531 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
532 |
static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, PUBLIC); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
533 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
534 |
/** Package-private version of lookup which is trusted. */ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
535 |
static final Lookup IMPL_LOOKUP = new Lookup(Object.class, TRUSTED); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
536 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
537 |
private static void checkUnprivilegedlookupClass(Class<?> lookupClass) { |
2764
2e45af54c0f9
6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents:
2763
diff
changeset
|
538 |
String name = lookupClass.getName(); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
539 |
if (name.startsWith("java.lang.invoke.")) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
540 |
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
541 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
542 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
543 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
544 |
* Displays the name of the class from which lookups are to be made. |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
545 |
* (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
546 |
* If there are restrictions on the access permitted to this lookup, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
547 |
* this is indicated by adding a suffix to the class name, consisting |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
548 |
* of a slash and a keyword. The keyword represents the strongest |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
549 |
* allowed access, and is chosen as follows: |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
550 |
* <ul> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
551 |
* <li>If no access is allowed, the suffix is "/noaccess". |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
552 |
* <li>If only public access is allowed, the suffix is "/public". |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
553 |
* <li>If only public and package access are allowed, the suffix is "/package". |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
554 |
* <li>If only public, package, and private access are allowed, the suffix is "/private". |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
555 |
* </ul> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
556 |
* If none of the above cases apply, it is the case that full |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
557 |
* access (public, package, private, and protected) is allowed. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
558 |
* In this case, no suffix is added. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
559 |
* This is true only of an object obtained originally from |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
560 |
* {@link java.lang.invoke.MethodHandles#lookup MethodHandles.lookup}. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
561 |
* Objects created by {@link java.lang.invoke.MethodHandles.Lookup#in Lookup.in} |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
562 |
* always have restricted access, and will display a suffix. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
563 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
564 |
* (It may seem strange that protected access should be |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
565 |
* stronger than private access. Viewed independently from |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
566 |
* package access, protected access is the first to be lost, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
567 |
* because it requires a direct subclass relationship between |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
568 |
* caller and callee.) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
569 |
* @see #in |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
570 |
*/ |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
571 |
@Override |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
572 |
public String toString() { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
573 |
String cname = lookupClass.getName(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
574 |
switch (allowedModes) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
575 |
case 0: // no privileges |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
576 |
return cname + "/noaccess"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
577 |
case PUBLIC: |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
578 |
return cname + "/public"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
579 |
case PUBLIC|PACKAGE: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
580 |
return cname + "/package"; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
581 |
case ALL_MODES & ~PROTECTED: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
582 |
return cname + "/private"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
583 |
case ALL_MODES: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
584 |
return cname; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
585 |
case TRUSTED: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
586 |
return "/trusted"; // internal only; not exported |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
587 |
default: // Should not happen, but it's a bitfield... |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
588 |
cname = cname + "/" + Integer.toHexString(allowedModes); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
589 |
assert(false) : cname; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
590 |
return cname; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
591 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
592 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
593 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
594 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
595 |
* Produces a method handle for a static method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
596 |
* The type of the method handle will be that of the method. |
4537 | 597 |
* (Since static methods do not take receivers, there is no |
598 |
* additional receiver argument inserted into the method handle type, |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
599 |
* as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
600 |
* The method and all its argument types must be accessible to the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
601 |
* If the method's class has not yet been initialized, that is done |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
602 |
* immediately, before the method handle is returned. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
603 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
604 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
605 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
606 |
* the method's variable arity modifier bit ({@code 0x0080}) is set. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
607 |
* @param refc the class from which the method is accessed |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
608 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
609 |
* @param type the type of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
610 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
611 |
* @throws NoSuchMethodException if the method does not exist |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
612 |
* @throws IllegalAccessException if access checking fails, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
613 |
* or if the method is not {@code static}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
614 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
615 |
* is set and {@code asVarargsCollector} fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
616 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
617 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
618 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
619 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
620 |
public |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
621 |
MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
622 |
MemberName method = resolveOrFail(REF_invokeStatic, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
623 |
checkSecurityManager(refc, method); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
624 |
return getDirectMethod(REF_invokeStatic, refc, method, findBoundCallerClass(method)); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
625 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
626 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
627 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
628 |
* Produces a method handle for a virtual method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
629 |
* The type of the method handle will be that of the method, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
630 |
* with the receiver type (usually {@code refc}) prepended. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
631 |
* The method and all its argument types must be accessible to the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
632 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
633 |
* When called, the handle will treat the first argument as a receiver |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
634 |
* and dispatch on the receiver's type to determine which method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
635 |
* implementation to enter. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
636 |
* (The dispatching action is identical with that performed by an |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
637 |
* {@code invokevirtual} or {@code invokeinterface} instruction.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
638 |
* <p> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
639 |
* The first argument will be of type {@code refc} if the lookup |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
640 |
* class has full privileges to access the member. Otherwise |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
641 |
* the member must be {@code protected} and the first argument |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
642 |
* will be restricted in type to the lookup class. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
643 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
644 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
645 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
646 |
* the method's variable arity modifier bit ({@code 0x0080}) is set. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
647 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
648 |
* Because of the general equivalence between {@code invokevirtual} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
649 |
* instructions and method handles produced by {@code findVirtual}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
650 |
* if the class is {@code MethodHandle} and the name string is |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
651 |
* {@code invokeExact} or {@code invoke}, the resulting |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
652 |
* method handle is equivalent to one produced by |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
653 |
* {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker} or |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
654 |
* {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
655 |
* with the same {@code type} argument. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
656 |
* |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
657 |
* @param refc the class or interface from which the method is accessed |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
658 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
659 |
* @param type the type of the method, with the receiver argument omitted |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
660 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
661 |
* @throws NoSuchMethodException if the method does not exist |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
662 |
* @throws IllegalAccessException if access checking fails, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
663 |
* or if the method is {@code static} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
664 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
665 |
* is set and {@code asVarargsCollector} fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
666 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
667 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
668 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
669 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
670 |
public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
671 |
if (refc == MethodHandle.class) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
672 |
MethodHandle mh = findVirtualForMH(name, type); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
673 |
if (mh != null) return mh; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
674 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
675 |
byte refKind = (refc.isInterface() ? REF_invokeInterface : REF_invokeVirtual); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
676 |
MemberName method = resolveOrFail(refKind, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
677 |
checkSecurityManager(refc, method); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
678 |
return getDirectMethod(refKind, refc, method, findBoundCallerClass(method)); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
679 |
} |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
680 |
private MethodHandle findVirtualForMH(String name, MethodType type) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
681 |
// these names require special lookups because of the implicit MethodType argument |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
682 |
if ("invoke".equals(name)) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
683 |
return invoker(type); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
684 |
if ("invokeExact".equals(name)) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
685 |
return exactInvoker(type); |
19804 | 686 |
assert(!MemberName.isMethodHandleInvokeName(name)); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
687 |
return null; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
688 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
689 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
690 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
691 |
* Produces a method handle which creates an object and initializes it, using |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
692 |
* the constructor of the specified type. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
693 |
* The parameter types of the method handle will be those of the constructor, |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
694 |
* while the return type will be a reference to the constructor's class. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
695 |
* The constructor and all its argument types must be accessible to the lookup class. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
696 |
* If the constructor's class has not yet been initialized, that is done |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
697 |
* immediately, before the method handle is returned. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
698 |
* <p> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
699 |
* Note: The requested type must have a return type of {@code void}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
700 |
* This is consistent with the JVM's treatment of constructor type descriptors. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
701 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
702 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
703 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
704 |
* the constructor's variable arity modifier bit ({@code 0x0080}) is set. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
705 |
* @param refc the class or interface from which the method is accessed |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
706 |
* @param type the type of the method, with the receiver argument omitted, and a void return type |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
707 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
708 |
* @throws NoSuchMethodException if the constructor does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
709 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
710 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
711 |
* is set and {@code asVarargsCollector} fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
712 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
713 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
714 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
715 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
716 |
public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
717 |
String name = "<init>"; |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
718 |
MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
719 |
checkSecurityManager(refc, ctor); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
720 |
return getDirectConstructor(refc, ctor); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
721 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
722 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
723 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
724 |
* Produces an early-bound method handle for a virtual method, |
2763 | 725 |
* as if called from an {@code invokespecial} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
726 |
* instruction from {@code caller}. |
2763 | 727 |
* The type of the method handle will be that of the method, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
728 |
* with a suitably restricted receiver type (such as {@code caller}) prepended. |
2763 | 729 |
* The method and all its argument types must be accessible |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
730 |
* to the caller. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
731 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
732 |
* When called, the handle will treat the first argument as a receiver, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
733 |
* but will not dispatch on the receiver's type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
734 |
* (This direct invocation action is identical with that performed by an |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
735 |
* {@code invokespecial} instruction.) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
736 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
737 |
* If the explicitly specified caller class is not identical with the |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
738 |
* lookup class, or if this lookup object does not have private access |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
739 |
* privileges, the access fails. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
740 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
741 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
742 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
743 |
* the method's variable arity modifier bit ({@code 0x0080}) is set. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
744 |
* @param refc the class or interface from which the method is accessed |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
745 |
* @param name the name of the method (which must not be "<init>") |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
746 |
* @param type the type of the method, with the receiver argument omitted |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
747 |
* @param specialCaller the proposed calling class to perform the {@code invokespecial} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
748 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
749 |
* @throws NoSuchMethodException if the method does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
750 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
751 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
752 |
* is set and {@code asVarargsCollector} fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
753 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
754 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
755 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
756 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
757 |
public MethodHandle findSpecial(Class<?> refc, String name, MethodType type, |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
758 |
Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
759 |
checkSpecialCaller(specialCaller); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
760 |
Lookup specialLookup = this.in(specialCaller); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
761 |
MemberName method = specialLookup.resolveOrFail(REF_invokeSpecial, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
762 |
checkSecurityManager(refc, method); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
763 |
return specialLookup.getDirectMethod(REF_invokeSpecial, refc, method, findBoundCallerClass(method)); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
764 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
765 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
766 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
767 |
* Produces a method handle giving read access to a non-static field. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
768 |
* The type of the method handle will have a return type of the field's |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
769 |
* value type. |
5726 | 770 |
* The method handle's single argument will be the instance containing |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
771 |
* the field. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
772 |
* Access checking is performed immediately on behalf of the lookup class. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
773 |
* @param refc the class or interface from which the method is accessed |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
774 |
* @param name the field's name |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
775 |
* @param type the field's type |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
776 |
* @return a method handle which can load values from the field |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
777 |
* @throws NoSuchFieldException if the field does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
778 |
* @throws IllegalAccessException if access checking fails, or if the field is {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
779 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
780 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
781 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
782 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
783 |
public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
784 |
MemberName field = resolveOrFail(REF_getField, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
785 |
checkSecurityManager(refc, field); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
786 |
return getDirectField(REF_getField, refc, field); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
787 |
} |
5726 | 788 |
|
789 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
790 |
* Produces a method handle giving write access to a non-static field. |
5726 | 791 |
* The type of the method handle will have a void return type. |
792 |
* The method handle will take two arguments, the instance containing |
|
793 |
* the field, and the value to be stored. |
|
794 |
* The second argument will be of the field's value type. |
|
795 |
* Access checking is performed immediately on behalf of the lookup class. |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
796 |
* @param refc the class or interface from which the method is accessed |
5726 | 797 |
* @param name the field's name |
798 |
* @param type the field's type |
|
799 |
* @return a method handle which can store values into the field |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
800 |
* @throws NoSuchFieldException if the field does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
801 |
* @throws IllegalAccessException if access checking fails, or if the field is {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
802 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
803 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
804 |
* @throws NullPointerException if any argument is null |
5726 | 805 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
806 |
public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
807 |
MemberName field = resolveOrFail(REF_putField, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
808 |
checkSecurityManager(refc, field); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
809 |
return getDirectField(REF_putField, refc, field); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
810 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
811 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
812 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
813 |
* Produces a method handle giving read access to a static field. |
5726 | 814 |
* The type of the method handle will have a return type of the field's |
815 |
* value type. |
|
816 |
* The method handle will take no arguments. |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
817 |
* Access checking is performed immediately on behalf of the lookup class. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
818 |
* @param refc the class or interface from which the method is accessed |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
819 |
* @param name the field's name |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
820 |
* @param type the field's type |
5726 | 821 |
* @return a method handle which can load values from the field |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
822 |
* @throws NoSuchFieldException if the field does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
823 |
* @throws IllegalAccessException if access checking fails, or if the field is not {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
824 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
825 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
826 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
827 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
828 |
public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
829 |
MemberName field = resolveOrFail(REF_getStatic, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
830 |
checkSecurityManager(refc, field); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
831 |
return getDirectField(REF_getStatic, refc, field); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
832 |
} |
5726 | 833 |
|
834 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
835 |
* Produces a method handle giving write access to a static field. |
5726 | 836 |
* The type of the method handle will have a void return type. |
837 |
* The method handle will take a single |
|
838 |
* argument, of the field's value type, the value to be stored. |
|
839 |
* Access checking is performed immediately on behalf of the lookup class. |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
840 |
* @param refc the class or interface from which the method is accessed |
5726 | 841 |
* @param name the field's name |
842 |
* @param type the field's type |
|
843 |
* @return a method handle which can store values into the field |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
844 |
* @throws NoSuchFieldException if the field does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
845 |
* @throws IllegalAccessException if access checking fails, or if the field is not {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
846 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
847 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
848 |
* @throws NullPointerException if any argument is null |
5726 | 849 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
850 |
public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
851 |
MemberName field = resolveOrFail(REF_putStatic, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
852 |
checkSecurityManager(refc, field); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
853 |
return getDirectField(REF_putStatic, refc, field); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
854 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
855 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
856 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
857 |
* Produces an early-bound method handle for a non-static method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
858 |
* The receiver must have a supertype {@code defc} in which a method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
859 |
* of the given name and type is accessible to the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
860 |
* The method and all its argument types must be accessible to the lookup class. |
4537 | 861 |
* The type of the method handle will be that of the method, |
862 |
* without any insertion of an additional receiver parameter. |
|
863 |
* The given receiver will be bound into the method handle, |
|
864 |
* so that every call to the method handle will invoke the |
|
865 |
* requested method on the given receiver. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
866 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
867 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
868 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
869 |
* the method's variable arity modifier bit ({@code 0x0080}) is set |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
870 |
* <em>and</em> the trailing array argument is not the only argument. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
871 |
* (If the trailing array argument is the only argument, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
872 |
* the given receiver value will be bound to it.) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
873 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
874 |
* This is equivalent to the following code: |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
875 |
* <blockquote><pre> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
876 |
import static java.lang.invoke.MethodHandles.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
877 |
import static java.lang.invoke.MethodType.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
878 |
... |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
879 |
MethodHandle mh0 = lookup().{@link #findVirtual findVirtual}(defc, name, type); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
880 |
MethodHandle mh1 = mh0.{@link MethodHandle#bindTo bindTo}(receiver); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
881 |
MethodType mt1 = mh1.type(); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
882 |
if (mh0.isVarargsCollector()) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
883 |
mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1)); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
884 |
return mh1; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
885 |
* </pre></blockquote> |
4537 | 886 |
* where {@code defc} is either {@code receiver.getClass()} or a super |
887 |
* type of that class, in which the requested method is accessible |
|
888 |
* to the lookup class. |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
889 |
* (Note that {@code bindTo} does not preserve variable arity.) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
890 |
* @param receiver the object from which the method is accessed |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
891 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
892 |
* @param type the type of the method, with the receiver argument omitted |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
893 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
894 |
* @throws NoSuchMethodException if the method does not exist |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
895 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
896 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
897 |
* is set and {@code asVarargsCollector} fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
898 |
* @exception SecurityException if a security manager is present and it |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
899 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
900 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
901 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
902 |
public MethodHandle bind(Object receiver, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
903 |
Class<? extends Object> refc = receiver.getClass(); // may get NPE |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
904 |
MemberName method = resolveOrFail(REF_invokeSpecial, refc, name, type); |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
905 |
checkSecurityManager(refc, method); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
906 |
MethodHandle mh = getDirectMethodNoRestrict(REF_invokeSpecial, refc, method, findBoundCallerClass(method)); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
907 |
return mh.bindReceiver(receiver).setVarargs(method); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
908 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
909 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
910 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
911 |
* Makes a direct method handle to <i>m</i>, if the lookup class has permission. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
912 |
* If <i>m</i> is non-static, the receiver argument is treated as an initial argument. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
913 |
* If <i>m</i> is virtual, overriding is respected on every call. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
914 |
* Unlike the Core Reflection API, exceptions are <em>not</em> wrapped. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
915 |
* The type of the method handle will be that of the method, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
916 |
* with the receiver type prepended (but only if it is non-static). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
917 |
* If the method's {@code accessible} flag is not set, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
918 |
* access checking is performed immediately on behalf of the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
919 |
* If <i>m</i> is not public, do not share the resulting handle with untrusted parties. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
920 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
921 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
922 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
923 |
* the method's variable arity modifier bit ({@code 0x0080}) is set. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
924 |
* @param m the reflected method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
925 |
* @return a method handle which can invoke the reflected method |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
926 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
927 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
928 |
* is set and {@code asVarargsCollector} fails |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
929 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
930 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
931 |
public MethodHandle unreflect(Method m) throws IllegalAccessException { |
19804 | 932 |
if (m.getDeclaringClass() == MethodHandle.class) { |
933 |
MethodHandle mh = unreflectForMH(m); |
|
934 |
if (mh != null) return mh; |
|
935 |
} |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
936 |
MemberName method = new MemberName(m); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
937 |
byte refKind = method.getReferenceKind(); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
938 |
if (refKind == REF_invokeSpecial) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
939 |
refKind = REF_invokeVirtual; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
940 |
assert(method.isMethod()); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
941 |
Lookup lookup = m.isAccessible() ? IMPL_LOOKUP : this; |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
942 |
return lookup.getDirectMethod(refKind, method.getDeclaringClass(), method, findBoundCallerClass(method)); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
943 |
} |
19804 | 944 |
private MethodHandle unreflectForMH(Method m) { |
945 |
// these names require special lookups because they throw UnsupportedOperationException |
|
946 |
if (MemberName.isMethodHandleInvokeName(m.getName())) |
|
947 |
return MethodHandleImpl.fakeMethodHandleInvoke(new MemberName(m)); |
|
948 |
return null; |
|
949 |
} |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
950 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
951 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
952 |
* Produces a method handle for a reflected method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
953 |
* It will bypass checks for overriding methods on the receiver, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
954 |
* as if by a {@code invokespecial} instruction from within the {@code specialCaller}. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
955 |
* The type of the method handle will be that of the method, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
956 |
* with the special caller type prepended (and <em>not</em> the receiver of the method). |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
957 |
* If the method's {@code accessible} flag is not set, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
958 |
* access checking is performed immediately on behalf of the lookup class, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
959 |
* as if {@code invokespecial} instruction were being linked. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
960 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
961 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
962 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
963 |
* the method's variable arity modifier bit ({@code 0x0080}) is set. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
964 |
* @param m the reflected method |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
965 |
* @param specialCaller the class nominally calling the method |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
966 |
* @return a method handle which can invoke the reflected method |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
967 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
968 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
969 |
* is set and {@code asVarargsCollector} fails |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
970 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
971 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
972 |
public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
973 |
checkSpecialCaller(specialCaller); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
974 |
Lookup specialLookup = this.in(specialCaller); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
975 |
MemberName method = new MemberName(m, true); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
976 |
assert(method.isMethod()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
977 |
// ignore m.isAccessible: this is a new kind of access |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
978 |
return specialLookup.getDirectMethod(REF_invokeSpecial, method.getDeclaringClass(), method, findBoundCallerClass(method)); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
979 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
980 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
981 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
982 |
* Produces a method handle for a reflected constructor. |
4537 | 983 |
* The type of the method handle will be that of the constructor, |
984 |
* with the return type changed to the declaring class. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
985 |
* The method handle will perform a {@code newInstance} operation, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
986 |
* creating a new instance of the constructor's class on the |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
987 |
* arguments passed to the method handle. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
988 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
989 |
* If the constructor's {@code accessible} flag is not set, |
4537 | 990 |
* access checking is performed immediately on behalf of the lookup class. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
991 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
992 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
993 |
* {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
994 |
* the constructor's variable arity modifier bit ({@code 0x0080}) is set. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
995 |
* @param c the reflected constructor |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
996 |
* @return a method handle which can invoke the reflected constructor |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
997 |
* @throws IllegalAccessException if access checking fails |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
998 |
* or if the method's variable arity modifier bit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
999 |
* is set and {@code asVarargsCollector} fails |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1000 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1001 |
*/ |
11534
9949ffb8eb3a
7117167: Misc warnings in java.lang.invoke and sun.invoke.*
jrose
parents:
10082
diff
changeset
|
1002 |
@SuppressWarnings("rawtypes") // Will be Constructor<?> after JSR 292 MR |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1003 |
public MethodHandle unreflectConstructor(Constructor c) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1004 |
MemberName ctor = new MemberName(c); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1005 |
assert(ctor.isConstructor()); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1006 |
Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1007 |
return lookup.getDirectConstructor(ctor.getDeclaringClass(), ctor); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1008 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1009 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1010 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1011 |
* Produces a method handle giving read access to a reflected field. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1012 |
* The type of the method handle will have a return type of the field's |
4537 | 1013 |
* value type. |
1014 |
* If the field is static, the method handle will take no arguments. |
|
1015 |
* Otherwise, its single argument will be the instance containing |
|
1016 |
* the field. |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1017 |
* If the field's {@code accessible} flag is not set, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1018 |
* access checking is performed immediately on behalf of the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1019 |
* @param f the reflected field |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1020 |
* @return a method handle which can load values from the reflected field |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1021 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1022 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1023 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1024 |
public MethodHandle unreflectGetter(Field f) throws IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1025 |
return unreflectField(f, false); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1026 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1027 |
private MethodHandle unreflectField(Field f, boolean isSetter) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1028 |
MemberName field = new MemberName(f, isSetter); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1029 |
assert(isSetter |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1030 |
? MethodHandleNatives.refKindIsSetter(field.getReferenceKind()) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1031 |
: MethodHandleNatives.refKindIsGetter(field.getReferenceKind())); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1032 |
Lookup lookup = f.isAccessible() ? IMPL_LOOKUP : this; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1033 |
return lookup.getDirectField(field.getReferenceKind(), f.getDeclaringClass(), field); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1034 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1035 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1036 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1037 |
* Produces a method handle giving write access to a reflected field. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1038 |
* The type of the method handle will have a void return type. |
4537 | 1039 |
* If the field is static, the method handle will take a single |
1040 |
* argument, of the field's value type, the value to be stored. |
|
1041 |
* Otherwise, the two arguments will be the instance containing |
|
1042 |
* the field, and the value to be stored. |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1043 |
* If the field's {@code accessible} flag is not set, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1044 |
* access checking is performed immediately on behalf of the lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1045 |
* @param f the reflected field |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1046 |
* @return a method handle which can store values into the reflected field |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1047 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1048 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1049 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1050 |
public MethodHandle unreflectSetter(Field f) throws IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1051 |
return unreflectField(f, true); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1052 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1053 |
|
19804 | 1054 |
/** |
1055 |
* Cracks a direct method handle created by this lookup object or a similar one. |
|
1056 |
* Security and access checks are performed to ensure that this lookup object |
|
1057 |
* is capable of reproducing the target method handle. |
|
1058 |
* This means that the cracking may fail if target is a direct method handle |
|
1059 |
* but was created by an unrelated lookup object. |
|
1060 |
* @param target a direct method handle to crack into symbolic reference components |
|
1061 |
* @return a symbolic reference which can be used to reconstruct this method handle from this lookup object |
|
1062 |
* @exception SecurityException if a security manager is present and it |
|
1063 |
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a> |
|
1064 |
* @throws IllegalArgumentException if the target is not a direct method handle or if access checking fails |
|
1065 |
* @exception NullPointerException if the target is {@code null} |
|
1066 |
* @since 1.8 |
|
1067 |
*/ |
|
1068 |
public MethodHandleInfo revealDirect(MethodHandle target) { |
|
1069 |
MemberName member = target.internalMemberName(); |
|
1070 |
if (member == null || (!member.isResolved() && !member.isMethodHandleInvoke())) |
|
1071 |
throw newIllegalArgumentException("not a direct method handle"); |
|
1072 |
Class<?> defc = member.getDeclaringClass(); |
|
1073 |
byte refKind = member.getReferenceKind(); |
|
1074 |
assert(MethodHandleNatives.refKindIsValid(refKind)); |
|
1075 |
if (refKind == REF_invokeSpecial && !target.isInvokeSpecial()) |
|
1076 |
// Devirtualized method invocation is usually formally virtual. |
|
1077 |
// To avoid creating extra MemberName objects for this common case, |
|
1078 |
// we encode this extra degree of freedom using MH.isInvokeSpecial. |
|
1079 |
refKind = REF_invokeVirtual; |
|
1080 |
if (refKind == REF_invokeVirtual && defc.isInterface()) |
|
1081 |
// Symbolic reference is through interface but resolves to Object method (toString, etc.) |
|
1082 |
refKind = REF_invokeInterface; |
|
1083 |
// Check SM permissions and member access before cracking. |
|
1084 |
try { |
|
1085 |
checkSecurityManager(defc, member); |
|
1086 |
checkAccess(refKind, defc, member); |
|
1087 |
} catch (IllegalAccessException ex) { |
|
1088 |
throw new IllegalArgumentException(ex); |
|
1089 |
} |
|
1090 |
// Produce the handle to the results. |
|
1091 |
return new InfoFromMemberName(this, member, refKind); |
|
1092 |
} |
|
1093 |
||
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1094 |
/// Helper methods, all package-private. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1095 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1096 |
MemberName resolveOrFail(byte refKind, Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1097 |
checkSymbolicClass(refc); // do this before attempting to resolve |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1098 |
name.getClass(); // NPE |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1099 |
type.getClass(); // NPE |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1100 |
return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(), |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1101 |
NoSuchFieldException.class); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1102 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1103 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1104 |
MemberName resolveOrFail(byte refKind, Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1105 |
checkSymbolicClass(refc); // do this before attempting to resolve |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1106 |
name.getClass(); // NPE |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1107 |
type.getClass(); // NPE |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1108 |
return IMPL_NAMES.resolveOrFail(refKind, new MemberName(refc, name, type, refKind), lookupClassOrNull(), |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1109 |
NoSuchMethodException.class); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1110 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1111 |
|
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1112 |
MemberName resolveOrFail(byte refKind, MemberName member) throws ReflectiveOperationException { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1113 |
checkSymbolicClass(member.getDeclaringClass()); // do this before attempting to resolve |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1114 |
member.getName().getClass(); // NPE |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1115 |
member.getType().getClass(); // NPE |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1116 |
return IMPL_NAMES.resolveOrFail(refKind, member, lookupClassOrNull(), |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1117 |
ReflectiveOperationException.class); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1118 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1119 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1120 |
void checkSymbolicClass(Class<?> refc) throws IllegalAccessException { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1121 |
refc.getClass(); // NPE |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1122 |
Class<?> caller = lookupClassOrNull(); |
13044
8411854afc2b
7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
11535
diff
changeset
|
1123 |
if (caller != null && !VerifyAccess.isClassAccessible(refc, caller, allowedModes)) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1124 |
throw new MemberName(refc).makeAccessException("symbolic reference class is not public", this); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1125 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1126 |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1127 |
/** |
16118 | 1128 |
* Find my trustable caller class if m is a caller sensitive method. |
1129 |
* If this lookup object has private access, then the caller class is the lookupClass. |
|
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1130 |
* Otherwise, if m is caller-sensitive, throw IllegalAccessException. |
16118 | 1131 |
*/ |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1132 |
Class<?> findBoundCallerClass(MemberName m) throws IllegalAccessException { |
16118 | 1133 |
Class<?> callerClass = null; |
1134 |
if (MethodHandleNatives.isCallerSensitive(m)) { |
|
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1135 |
// Only full-power lookup is allowed to resolve caller-sensitive methods |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1136 |
if (isFullPowerLookup()) { |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1137 |
callerClass = lookupClass; |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1138 |
} else { |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1139 |
throw new IllegalAccessException("Attempt to lookup caller-sensitive method using restricted lookup object"); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1140 |
} |
16118 | 1141 |
} |
1142 |
return callerClass; |
|
1143 |
} |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1144 |
|
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1145 |
private boolean isFullPowerLookup() { |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1146 |
return (allowedModes & PRIVATE) != 0; |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1147 |
} |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1148 |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1149 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1150 |
* Perform necessary <a href="MethodHandles.Lookup.html#secmgr">access checks</a>. |
16118 | 1151 |
* Determines a trustable caller class to compare with refc, the symbolic reference class. |
1152 |
* If this lookup object has private access, then the caller class is the lookupClass. |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1153 |
*/ |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1154 |
void checkSecurityManager(Class<?> refc, MemberName m) { |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1155 |
SecurityManager smgr = System.getSecurityManager(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1156 |
if (smgr == null) return; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1157 |
if (allowedModes == TRUSTED) return; |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1158 |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1159 |
// Step 1: |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1160 |
if (!isFullPowerLookup() || |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1161 |
!VerifyAccess.classLoaderIsAncestor(lookupClass, refc)) { |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1162 |
ReflectUtil.checkPackageAccess(refc); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1163 |
} |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1164 |
|
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
1165 |
// Step 2: |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1166 |
if (m.isPublic()) return; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1167 |
Class<?> defc = m.getDeclaringClass(); |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1168 |
{ |
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
1169 |
if (!isFullPowerLookup()) { |
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
1170 |
smgr.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1171 |
} |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1172 |
} |
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
16898
diff
changeset
|
1173 |
|
18766
28c62f5e9a47
8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents:
18569
diff
changeset
|
1174 |
// Step 3: |
18264
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1175 |
if (defc != refc) { |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1176 |
ReflectUtil.checkPackageAccess(defc); |
286a7973575a
8009424: Restrict publicLookup with additional checks
twisti
parents:
16906
diff
changeset
|
1177 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1178 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1179 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1180 |
void checkMethod(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1181 |
boolean wantStatic = (refKind == REF_invokeStatic); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1182 |
String message; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1183 |
if (m.isConstructor()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1184 |
message = "expected a method, not a constructor"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1185 |
else if (!m.isMethod()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1186 |
message = "expected a method"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1187 |
else if (wantStatic != m.isStatic()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1188 |
message = wantStatic ? "expected a static method" : "expected a non-static method"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1189 |
else |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1190 |
{ checkAccess(refKind, refc, m); return; } |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1191 |
throw m.makeAccessException(message, this); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1192 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1193 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1194 |
void checkField(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1195 |
boolean wantStatic = !MethodHandleNatives.refKindHasReceiver(refKind); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1196 |
String message; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1197 |
if (wantStatic != m.isStatic()) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1198 |
message = wantStatic ? "expected a static field" : "expected a non-static field"; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1199 |
else |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1200 |
{ checkAccess(refKind, refc, m); return; } |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1201 |
throw m.makeAccessException(message, this); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1202 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1203 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1204 |
void checkAccess(byte refKind, Class<?> refc, MemberName m) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1205 |
assert(m.referenceKindIsConsistentWith(refKind) && |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1206 |
MethodHandleNatives.refKindIsValid(refKind) && |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1207 |
(MethodHandleNatives.refKindIsField(refKind) == m.isField())); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1208 |
int allowedModes = this.allowedModes; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1209 |
if (allowedModes == TRUSTED) return; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1210 |
int mods = m.getModifiers(); |
20528
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1211 |
if (Modifier.isProtected(mods) && |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1212 |
refKind == REF_invokeVirtual && |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1213 |
m.getDeclaringClass() == Object.class && |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1214 |
m.getName().equals("clone") && |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1215 |
refc.isArray()) { |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1216 |
// The JVM does this hack also. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1217 |
// (See ClassVerifier::verify_invoke_instructions |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1218 |
// and LinkResolver::check_method_accessability.) |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1219 |
// Because the JVM does not allow separate methods on array types, |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1220 |
// there is no separate method for int[].clone. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1221 |
// All arrays simply inherit Object.clone. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1222 |
// But for access checking logic, we make Object.clone |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1223 |
// (normally protected) appear to be public. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1224 |
// Later on, when the DirectMethodHandle is created, |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1225 |
// its leading argument will be restricted to the |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1226 |
// requested array type. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1227 |
// N.B. The return type is not adjusted, because |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1228 |
// that is *not* the bytecode behavior. |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1229 |
mods ^= Modifier.PROTECTED | Modifier.PUBLIC; |
0b1e2130d3f7
8001105: findVirtual of Object[].clone produces internal error
jrose
parents:
20527
diff
changeset
|
1230 |
} |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1231 |
if (Modifier.isFinal(mods) && |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1232 |
MethodHandleNatives.refKindIsSetter(refKind)) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1233 |
throw m.makeAccessException("unexpected set of a final field", this); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1234 |
if (Modifier.isPublic(mods) && Modifier.isPublic(refc.getModifiers()) && allowedModes != 0) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1235 |
return; // common case |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1236 |
int requestedModes = fixmods(mods); // adjust 0 => PACKAGE |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1237 |
if ((requestedModes & allowedModes) != 0) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1238 |
if (VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(), |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1239 |
mods, lookupClass(), allowedModes)) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1240 |
return; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1241 |
} else { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1242 |
// Protected members can also be checked as if they were package-private. |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1243 |
if ((requestedModes & PROTECTED) != 0 && (allowedModes & PACKAGE) != 0 |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1244 |
&& VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass())) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1245 |
return; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1246 |
} |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1247 |
throw m.makeAccessException(accessFailedMessage(refc, m), this); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1248 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1249 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1250 |
String accessFailedMessage(Class<?> refc, MemberName m) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1251 |
Class<?> defc = m.getDeclaringClass(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1252 |
int mods = m.getModifiers(); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1253 |
// check the class first: |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1254 |
boolean classOK = (Modifier.isPublic(defc.getModifiers()) && |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1255 |
(defc == refc || |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1256 |
Modifier.isPublic(refc.getModifiers()))); |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1257 |
if (!classOK && (allowedModes & PACKAGE) != 0) { |
13044
8411854afc2b
7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
11535
diff
changeset
|
1258 |
classOK = (VerifyAccess.isClassAccessible(defc, lookupClass(), ALL_MODES) && |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1259 |
(defc == refc || |
13044
8411854afc2b
7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
11535
diff
changeset
|
1260 |
VerifyAccess.isClassAccessible(refc, lookupClass(), ALL_MODES))); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1261 |
} |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1262 |
if (!classOK) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1263 |
return "class is not public"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1264 |
if (Modifier.isPublic(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1265 |
return "access to public member failed"; // (how?) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1266 |
if (Modifier.isPrivate(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1267 |
return "member is private"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1268 |
if (Modifier.isProtected(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1269 |
return "member is protected"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1270 |
return "member is private to package"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1271 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1272 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1273 |
private static final boolean ALLOW_NESTMATE_ACCESS = false; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1274 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1275 |
private void checkSpecialCaller(Class<?> specialCaller) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1276 |
int allowedModes = this.allowedModes; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1277 |
if (allowedModes == TRUSTED) return; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1278 |
if ((allowedModes & PRIVATE) == 0 |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1279 |
|| (specialCaller != lookupClass() |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1280 |
&& !(ALLOW_NESTMATE_ACCESS && |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1281 |
VerifyAccess.isSamePackageMember(specialCaller, lookupClass())))) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1282 |
throw new MemberName(specialCaller). |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1283 |
makeAccessException("no private access for invokespecial", this); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1284 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1285 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1286 |
private boolean restrictProtectedReceiver(MemberName method) { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1287 |
// The accessing class only has the right to use a protected member |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1288 |
// on itself or a subclass. Enforce that restriction, from JVMS 5.4.4, etc. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1289 |
if (!method.isProtected() || method.isStatic() |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1290 |
|| allowedModes == TRUSTED |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1291 |
|| method.getDeclaringClass() == lookupClass() |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
1292 |
|| VerifyAccess.isSamePackage(method.getDeclaringClass(), lookupClass()) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1293 |
|| (ALLOW_NESTMATE_ACCESS && |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1294 |
VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass()))) |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1295 |
return false; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1296 |
return true; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1297 |
} |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1298 |
private MethodHandle restrictReceiver(MemberName method, MethodHandle mh, Class<?> caller) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1299 |
assert(!method.isStatic()); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1300 |
// receiver type of mh is too wide; narrow to caller |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1301 |
if (!method.getDeclaringClass().isAssignableFrom(caller)) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1302 |
throw method.makeAccessException("caller class must be a subclass below the method", caller); |
4537 | 1303 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1304 |
MethodType rawType = mh.type(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1305 |
if (rawType.parameterType(0) == caller) return mh; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1306 |
MethodType narrowType = rawType.changeParameterType(0, caller); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1307 |
return mh.viewAsType(narrowType); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1308 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1309 |
|
16118 | 1310 |
private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method, Class<?> callerClass) throws IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1311 |
return getDirectMethodCommon(refKind, refc, method, |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1312 |
(refKind == REF_invokeSpecial || |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1313 |
(MethodHandleNatives.refKindHasReceiver(refKind) && |
16118 | 1314 |
restrictProtectedReceiver(method))), callerClass); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1315 |
} |
16118 | 1316 |
private MethodHandle getDirectMethodNoRestrict(byte refKind, Class<?> refc, MemberName method, Class<?> callerClass) throws IllegalAccessException { |
1317 |
return getDirectMethodCommon(refKind, refc, method, false, callerClass); |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1318 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1319 |
private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberName method, |
16118 | 1320 |
boolean doRestrict, Class<?> callerClass) throws IllegalAccessException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1321 |
checkMethod(refKind, refc, method); |
19804 | 1322 |
assert(!method.isMethodHandleInvoke()); |
16898 | 1323 |
|
1324 |
Class<?> refcAsSuper; |
|
1325 |
if (refKind == REF_invokeSpecial && |
|
1326 |
refc != lookupClass() && |
|
19804 | 1327 |
!refc.isInterface() && |
16898 | 1328 |
refc != (refcAsSuper = lookupClass().getSuperclass()) && |
1329 |
refc.isAssignableFrom(lookupClass())) { |
|
1330 |
assert(!method.getName().equals("<init>")); // not this code path |
|
1331 |
// Per JVMS 6.5, desc. of invokespecial instruction: |
|
1332 |
// If the method is in a superclass of the LC, |
|
1333 |
// and if our original search was above LC.super, |
|
1334 |
// repeat the search (symbolic lookup) from LC.super. |
|
1335 |
// FIXME: MemberName.resolve should handle this instead. |
|
1336 |
MemberName m2 = new MemberName(refcAsSuper, |
|
1337 |
method.getName(), |
|
1338 |
method.getMethodType(), |
|
1339 |
REF_invokeSpecial); |
|
1340 |
m2 = IMPL_NAMES.resolveOrNull(refKind, m2, lookupClassOrNull()); |
|
1341 |
if (m2 == null) throw new InternalError(method.toString()); |
|
1342 |
method = m2; |
|
1343 |
refc = refcAsSuper; |
|
1344 |
// redo basic checks |
|
1345 |
checkMethod(refKind, refc, method); |
|
1346 |
} |
|
1347 |
||
16030
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14222
diff
changeset
|
1348 |
MethodHandle mh = DirectMethodHandle.make(refKind, refc, method); |
16118 | 1349 |
mh = maybeBindCaller(method, mh, callerClass); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1350 |
mh = mh.setVarargs(method); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1351 |
if (doRestrict) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1352 |
mh = restrictReceiver(method, mh, lookupClass()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1353 |
return mh; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1354 |
} |
16118 | 1355 |
private MethodHandle maybeBindCaller(MemberName method, MethodHandle mh, |
1356 |
Class<?> callerClass) |
|
1357 |
throws IllegalAccessException { |
|
14222 | 1358 |
if (allowedModes == TRUSTED || !MethodHandleNatives.isCallerSensitive(method)) |
1359 |
return mh; |
|
1360 |
Class<?> hostClass = lookupClass; |
|
1361 |
if ((allowedModes & PRIVATE) == 0) // caller must use full-power lookup |
|
16118 | 1362 |
hostClass = callerClass; // callerClass came from a security manager style stack walk |
14222 | 1363 |
MethodHandle cbmh = MethodHandleImpl.bindCaller(mh, hostClass); |
1364 |
// Note: caller will apply varargs after this step happens. |
|
1365 |
return cbmh; |
|
1366 |
} |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1367 |
private MethodHandle getDirectField(byte refKind, Class<?> refc, MemberName field) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1368 |
checkField(refKind, refc, field); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1369 |
MethodHandle mh = DirectMethodHandle.make(refc, field); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1370 |
boolean doRestrict = (MethodHandleNatives.refKindHasReceiver(refKind) && |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1371 |
restrictProtectedReceiver(field)); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1372 |
if (doRestrict) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1373 |
mh = restrictReceiver(field, mh, lookupClass()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1374 |
return mh; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1375 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1376 |
private MethodHandle getDirectConstructor(Class<?> refc, MemberName ctor) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1377 |
assert(ctor.isConstructor()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1378 |
checkAccess(REF_newInvokeSpecial, refc, ctor); |
14222 | 1379 |
assert(!MethodHandleNatives.isCallerSensitive(ctor)); // maybeBindCaller not relevant here |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1380 |
return DirectMethodHandle.make(ctor).setVarargs(ctor); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1381 |
} |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1382 |
|
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1383 |
/** Hook called from the JVM (via MethodHandleNatives) to link MH constants: |
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1384 |
*/ |
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1385 |
/*non-public*/ |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1386 |
MethodHandle linkMethodHandleConstant(byte refKind, Class<?> defc, String name, Object type) throws ReflectiveOperationException { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1387 |
if (!(type instanceof Class || type instanceof MethodType)) |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1388 |
throw new InternalError("unresolved MemberName"); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1389 |
MemberName member = new MemberName(refKind, defc, name, type); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1390 |
MethodHandle mh = LOOKASIDE_TABLE.get(member); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1391 |
if (mh != null) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1392 |
checkSymbolicClass(defc); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1393 |
return mh; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1394 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1395 |
MemberName resolved = resolveOrFail(refKind, member); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1396 |
mh = getDirectMethodHandle(refKind, defc, resolved); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1397 |
if (mh instanceof DirectMethodHandle |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1398 |
&& canBeCached(refKind, defc, resolved)) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1399 |
MemberName key = mh.internalMemberName(); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1400 |
if (key != null) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1401 |
key = key.asNormalOriginal(); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1402 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1403 |
if (member.equals(key)) { // better safe than sorry |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1404 |
LOOKASIDE_TABLE.put(key, (DirectMethodHandle) mh); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1405 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1406 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1407 |
return mh; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1408 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1409 |
private |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1410 |
boolean canBeCached(byte refKind, Class<?> defc, MemberName member) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1411 |
if (refKind == REF_invokeSpecial) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1412 |
return false; |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1413 |
} |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1414 |
if (!Modifier.isPublic(defc.getModifiers()) || |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1415 |
!Modifier.isPublic(member.getDeclaringClass().getModifiers()) || |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1416 |
!member.isPublic() || |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1417 |
member.isCallerSensitive()) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1418 |
return false; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1419 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1420 |
ClassLoader loader = defc.getClassLoader(); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1421 |
if (!sun.misc.VM.isSystemDomainLoader(loader)) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1422 |
ClassLoader sysl = ClassLoader.getSystemClassLoader(); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1423 |
boolean found = false; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1424 |
while (sysl != null) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1425 |
if (loader == sysl) { found = true; break; } |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1426 |
sysl = sysl.getParent(); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1427 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1428 |
if (!found) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1429 |
return false; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1430 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1431 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1432 |
try { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1433 |
MemberName resolved2 = publicLookup().resolveOrFail(refKind, |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1434 |
new MemberName(refKind, defc, member.getName(), member.getType())); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1435 |
checkSecurityManager(defc, resolved2); |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1436 |
} catch (ReflectiveOperationException | SecurityException ex) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1437 |
return false; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1438 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1439 |
return true; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1440 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1441 |
private |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1442 |
MethodHandle getDirectMethodHandle(byte refKind, Class<?> defc, MemberName member) throws ReflectiveOperationException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1443 |
if (MethodHandleNatives.refKindIsField(refKind)) { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1444 |
return getDirectField(refKind, defc, member); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1445 |
} else if (MethodHandleNatives.refKindIsMethod(refKind)) { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1446 |
return getDirectMethod(refKind, defc, member, lookupClass); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1447 |
} else if (refKind == REF_newInvokeSpecial) { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1448 |
return getDirectConstructor(defc, member); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1449 |
} |
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1450 |
// oops |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1451 |
throw newIllegalArgumentException("bad MethodHandle constant #"+member); |
9780
6fc3b49cfee4
7050328: (jsr-292) findConstructor throws ExceptionInInitializerError if run under SecurityManager
jrose
parents:
9752
diff
changeset
|
1452 |
} |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1453 |
|
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19804
diff
changeset
|
1454 |
static ConcurrentHashMap<MemberName, DirectMethodHandle> LOOKASIDE_TABLE = new ConcurrentHashMap<>(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1455 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1456 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1457 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1458 |
* Produces a method handle giving read access to elements of an array. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1459 |
* The type of the method handle will have a return type of the array's |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1460 |
* element type. Its first argument will be the array type, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1461 |
* and the second will be {@code int}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1462 |
* @param arrayClass an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1463 |
* @return a method handle which can load values from the given array type |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1464 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1465 |
* @throws IllegalArgumentException if arrayClass is not an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1466 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1467 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1468 |
MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1469 |
return MethodHandleImpl.makeArrayElementAccessor(arrayClass, false); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1470 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1471 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1472 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1473 |
* Produces a method handle giving write access to elements of an array. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1474 |
* The type of the method handle will have a void return type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1475 |
* Its last argument will be the array's element type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1476 |
* The first and second arguments will be the array type and int. |
18569 | 1477 |
* @param arrayClass the class of an array |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1478 |
* @return a method handle which can store values into the array type |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1479 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1480 |
* @throws IllegalArgumentException if arrayClass is not an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1481 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1482 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1483 |
MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1484 |
return MethodHandleImpl.makeArrayElementAccessor(arrayClass, true); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1485 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1486 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1487 |
/// method handle invocation (reflective style) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1488 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1489 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1490 |
* Produces a method handle which will invoke any method handle of the |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1491 |
* given {@code type}, with a given number of trailing arguments replaced by |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1492 |
* a single trailing {@code Object[]} array. |
4537 | 1493 |
* The resulting invoker will be a method handle with the following |
1494 |
* arguments: |
|
1495 |
* <ul> |
|
1496 |
* <li>a single {@code MethodHandle} target |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1497 |
* <li>zero or more leading values (counted by {@code leadingArgCount}) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1498 |
* <li>an {@code Object[]} array containing trailing arguments |
4537 | 1499 |
* </ul> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1500 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1501 |
* The invoker will invoke its target like a call to {@link MethodHandle#invoke invoke} with |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1502 |
* the indicated {@code type}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1503 |
* That is, if the target is exactly of the given {@code type}, it will behave |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1504 |
* like {@code invokeExact}; otherwise it behave as if {@link MethodHandle#asType asType} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1505 |
* is used to convert the target to the required {@code type}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1506 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1507 |
* The type of the returned invoker will not be the given {@code type}, but rather |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1508 |
* will have all parameters except the first {@code leadingArgCount} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1509 |
* replaced by a single array of type {@code Object[]}, which will be |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1510 |
* the final parameter. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1511 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1512 |
* Before invoking its target, the invoker will spread the final array, apply |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1513 |
* reference casts as necessary, and unbox and widen primitive arguments. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1514 |
* <p> |
4537 | 1515 |
* This method is equivalent to the following code (though it may be more efficient): |
1516 |
* <p><blockquote><pre> |
|
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1517 |
MethodHandle invoker = MethodHandles.invoker(type); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1518 |
int spreadArgCount = type.parameterCount() - leadingArgCount; |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1519 |
invoker = invoker.asSpreader(Object[].class, spreadArgCount); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1520 |
return invoker; |
4537 | 1521 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1522 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1523 |
* This method throws no reflective or security exceptions. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1524 |
* @param type the desired target type |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1525 |
* @param leadingArgCount number of fixed arguments, to be passed unchanged to the target |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1526 |
* @return a method handle suitable for invoking any method handle of the given type |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1527 |
* @throws NullPointerException if {@code type} is null |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1528 |
* @throws IllegalArgumentException if {@code leadingArgCount} is not in |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1529 |
* the range from 0 to {@code type.parameterCount()} inclusive, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1530 |
* or if the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1531 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1532 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1533 |
static public |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1534 |
MethodHandle spreadInvoker(MethodType type, int leadingArgCount) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1535 |
if (leadingArgCount < 0 || leadingArgCount > type.parameterCount()) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1536 |
throw new IllegalArgumentException("bad argument count "+leadingArgCount); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1537 |
return type.invokers().spreadInvoker(leadingArgCount); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1538 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1539 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1540 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1541 |
* Produces a special <em>invoker method handle</em> which can be used to |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1542 |
* invoke any method handle of the given type, as if by {@link MethodHandle#invokeExact invokeExact}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1543 |
* The resulting invoker will have a type which is |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1544 |
* exactly equal to the desired type, except that it will accept |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1545 |
* an additional leading argument of type {@code MethodHandle}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1546 |
* <p> |
4537 | 1547 |
* This method is equivalent to the following code (though it may be more efficient): |
1548 |
* <p><blockquote><pre> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1549 |
publicLookup().findVirtual(MethodHandle.class, "invokeExact", type) |
4537 | 1550 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1551 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1552 |
* <p style="font-size:smaller;"> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1553 |
* <em>Discussion:</em> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1554 |
* Invoker method handles can be useful when working with variable method handles |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1555 |
* of unknown types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1556 |
* For example, to emulate an {@code invokeExact} call to a variable method |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1557 |
* handle {@code M}, extract its type {@code T}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1558 |
* look up the invoker method {@code X} for {@code T}, |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1559 |
* and call the invoker method, as {@code X.invoke(T, A...)}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1560 |
* (It would not work to call {@code X.invokeExact}, since the type {@code T} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1561 |
* is unknown.) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1562 |
* If spreading, collecting, or other argument transformations are required, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1563 |
* they can be applied once to the invoker {@code X} and reused on many {@code M} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1564 |
* method handle values, as long as they are compatible with the type of {@code X}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1565 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1566 |
* <em>(Note: The invoker method is not available via the Core Reflection API. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1567 |
* An attempt to call {@linkplain java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke} |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1568 |
* on the declared {@code invokeExact} or {@code invoke} method will raise an |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1569 |
* {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1570 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1571 |
* This method throws no reflective or security exceptions. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1572 |
* @param type the desired target type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1573 |
* @return a method handle suitable for invoking any method handle of the given type |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1574 |
* @throws IllegalArgumentException if the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1575 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1576 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1577 |
static public |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1578 |
MethodHandle exactInvoker(MethodType type) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1579 |
return type.invokers().exactInvoker(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1580 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1581 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1582 |
/** |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1583 |
* Produces a special <em>invoker method handle</em> which can be used to |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1584 |
* invoke any method handle compatible with the given type, as if by {@link MethodHandle#invoke invoke}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1585 |
* The resulting invoker will have a type which is |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1586 |
* exactly equal to the desired type, except that it will accept |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1587 |
* an additional leading argument of type {@code MethodHandle}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1588 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1589 |
* Before invoking its target, if the target differs from the expected type, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1590 |
* the invoker will apply reference casts as |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1591 |
* necessary and box, unbox, or widen primitive values, as if by {@link MethodHandle#asType asType}. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1592 |
* Similarly, the return value will be converted as necessary. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1593 |
* If the target is a {@linkplain MethodHandle#asVarargsCollector variable arity method handle}, |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1594 |
* the required arity conversion will be made, again as if by {@link MethodHandle#asType asType}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1595 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1596 |
* A {@linkplain MethodType#genericMethodType general method type}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1597 |
* mentions only {@code Object} arguments and return values. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1598 |
* An invoker for such a type is capable of calling any method handle |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1599 |
* of the same arity as the general type. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1600 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1601 |
* This method is equivalent to the following code (though it may be more efficient): |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1602 |
* <p><blockquote><pre> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1603 |
publicLookup().findVirtual(MethodHandle.class, "invoke", type) |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1604 |
* </pre></blockquote> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1605 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1606 |
* This method throws no reflective or security exceptions. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1607 |
* @param type the desired target type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1608 |
* @return a method handle suitable for invoking any method handle convertible to the given type |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1609 |
* @throws IllegalArgumentException if the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1610 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1611 |
*/ |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1612 |
static public |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1613 |
MethodHandle invoker(MethodType type) { |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1614 |
return type.invokers().generalInvoker(); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1615 |
} |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1616 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1617 |
static /*non-public*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1618 |
MethodHandle basicInvoker(MethodType type) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1619 |
return type.form().basicInvoker(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1620 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1621 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1622 |
/// method handle modification (creation from other method handles) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1623 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1624 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1625 |
* Produces a method handle which adapts the type of the |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1626 |
* given method handle to a new type by pairwise argument and return type conversion. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1627 |
* The original type and new type must have the same number of arguments. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1628 |
* The resulting method handle is guaranteed to report a type |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1629 |
* which is equal to the desired new type. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1630 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1631 |
* If the original type and new type are equal, returns target. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1632 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1633 |
* The same conversions are allowed as for {@link MethodHandle#asType MethodHandle.asType}, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1634 |
* and some additional conversions are also applied if those conversions fail. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1635 |
* Given types <em>T0</em>, <em>T1</em>, one of the following conversions is applied |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1636 |
* if possible, before or instead of any conversions done by {@code asType}: |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1637 |
* <ul> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1638 |
* <li>If <em>T0</em> and <em>T1</em> are references, and <em>T1</em> is an interface type, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1639 |
* then the value of type <em>T0</em> is passed as a <em>T1</em> without a cast. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1640 |
* (This treatment of interfaces follows the usage of the bytecode verifier.) |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1641 |
* <li>If <em>T0</em> is boolean and <em>T1</em> is another primitive, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1642 |
* the boolean is converted to a byte value, 1 for true, 0 for false. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1643 |
* (This treatment follows the usage of the bytecode verifier.) |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1644 |
* <li>If <em>T1</em> is boolean and <em>T0</em> is another primitive, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1645 |
* <em>T0</em> is converted to byte via Java casting conversion (JLS 5.5), |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1646 |
* and the low order bit of the result is tested, as if by {@code (x & 1) != 0}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1647 |
* <li>If <em>T0</em> and <em>T1</em> are primitives other than boolean, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1648 |
* then a Java casting conversion (JLS 5.5) is applied. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1649 |
* (Specifically, <em>T0</em> will convert to <em>T1</em> by |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1650 |
* widening and/or narrowing.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1651 |
* <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1652 |
* conversion will be applied at runtime, possibly followed |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1653 |
* by a Java casting conversion (JLS 5.5) on the primitive value, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1654 |
* possibly followed by a conversion from byte to boolean by testing |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1655 |
* the low-order bit. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1656 |
* <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1657 |
* and if the reference is null at runtime, a zero value is introduced. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1658 |
* </ul> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1659 |
* @param target the method handle to invoke after arguments are retyped |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1660 |
* @param newType the expected type of the new method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1661 |
* @return a method handle which delegates to the target after performing |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1662 |
* any necessary argument conversions, and arranges for any |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1663 |
* necessary return value conversions |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1664 |
* @throws NullPointerException if either argument is null |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1665 |
* @throws WrongMethodTypeException if the conversion cannot be made |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1666 |
* @see MethodHandle#asType |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1667 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1668 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1669 |
MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1670 |
if (!target.type().isCastableTo(newType)) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1671 |
throw new WrongMethodTypeException("cannot explicitly cast "+target+" to "+newType); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1672 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1673 |
return MethodHandleImpl.makePairwiseConvert(target, newType, 2); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1674 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1675 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1676 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1677 |
* Produces a method handle which adapts the calling sequence of the |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1678 |
* given method handle to a new type, by reordering the arguments. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1679 |
* The resulting method handle is guaranteed to report a type |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1680 |
* which is equal to the desired new type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1681 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1682 |
* The given array controls the reordering. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1683 |
* Call {@code #I} the number of incoming parameters (the value |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1684 |
* {@code newType.parameterCount()}, and call {@code #O} the number |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1685 |
* of outgoing parameters (the value {@code target.type().parameterCount()}). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1686 |
* Then the length of the reordering array must be {@code #O}, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1687 |
* and each element must be a non-negative number less than {@code #I}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1688 |
* For every {@code N} less than {@code #O}, the {@code N}-th |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1689 |
* outgoing argument will be taken from the {@code I}-th incoming |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1690 |
* argument, where {@code I} is {@code reorder[N]}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1691 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1692 |
* No argument or return value conversions are applied. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1693 |
* The type of each incoming argument, as determined by {@code newType}, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1694 |
* must be identical to the type of the corresponding outgoing parameter |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1695 |
* or parameters in the target method handle. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1696 |
* The return type of {@code newType} must be identical to the return |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1697 |
* type of the original target. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1698 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1699 |
* The reordering array need not specify an actual permutation. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1700 |
* An incoming argument will be duplicated if its index appears |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1701 |
* more than once in the array, and an incoming argument will be dropped |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1702 |
* if its index does not appear in the array. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1703 |
* As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1704 |
* incoming arguments which are not mentioned in the reordering array |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1705 |
* are may be any type, as determined only by {@code newType}. |
18156 | 1706 |
* <blockquote><pre>{@code |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1707 |
import static java.lang.invoke.MethodHandles.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1708 |
import static java.lang.invoke.MethodType.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1709 |
... |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1710 |
MethodType intfn1 = methodType(int.class, int.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1711 |
MethodType intfn2 = methodType(int.class, int.class, int.class); |
18569 | 1712 |
MethodHandle sub = ... (int x, int y) -> (x-y) ...; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1713 |
assert(sub.type().equals(intfn2)); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1714 |
MethodHandle sub1 = permuteArguments(sub, intfn2, 0, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1715 |
MethodHandle rsub = permuteArguments(sub, intfn2, 1, 0); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1716 |
assert((int)rsub.invokeExact(1, 100) == 99); |
18569 | 1717 |
MethodHandle add = ... (int x, int y) -> (x+y) ...; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1718 |
assert(add.type().equals(intfn2)); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1719 |
MethodHandle twice = permuteArguments(add, intfn1, 0, 0); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1720 |
assert(twice.type().equals(intfn1)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1721 |
assert((int)twice.invokeExact(21) == 42); |
18156 | 1722 |
* }</pre></blockquote> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1723 |
* @param target the method handle to invoke after arguments are reordered |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1724 |
* @param newType the expected type of the new method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1725 |
* @param reorder an index array which controls the reordering |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1726 |
* @return a method handle which delegates to the target after it |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1727 |
* drops unused arguments and moves and/or duplicates the other arguments |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1728 |
* @throws NullPointerException if any argument is null |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1729 |
* @throws IllegalArgumentException if the index array length is not equal to |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1730 |
* the arity of the target, or if any index array element |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1731 |
* not a valid index for a parameter of {@code newType}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1732 |
* or if two corresponding parameter types in |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1733 |
* {@code target.type()} and {@code newType} are not identical, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1734 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1735 |
public static |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1736 |
MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1737 |
checkReorder(reorder, newType, target.type()); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1738 |
return target.permuteArguments(newType, reorder); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1739 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1740 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1741 |
private static void checkReorder(int[] reorder, MethodType newType, MethodType oldType) { |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1742 |
if (newType.returnType() != oldType.returnType()) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1743 |
throw newIllegalArgumentException("return types do not match", |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1744 |
oldType, newType); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1745 |
if (reorder.length == oldType.parameterCount()) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1746 |
int limit = newType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1747 |
boolean bad = false; |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1748 |
for (int j = 0; j < reorder.length; j++) { |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1749 |
int i = reorder[j]; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1750 |
if (i < 0 || i >= limit) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1751 |
bad = true; break; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1752 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1753 |
Class<?> src = newType.parameterType(i); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1754 |
Class<?> dst = oldType.parameterType(j); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1755 |
if (src != dst) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1756 |
throw newIllegalArgumentException("parameter types do not match after reorder", |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1757 |
oldType, newType); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1758 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1759 |
if (!bad) return; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1760 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1761 |
throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder)); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1762 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1763 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1764 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1765 |
* Produces a method handle of the requested return type which returns the given |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1766 |
* constant value every time it is invoked. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1767 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1768 |
* Before the method handle is returned, the passed-in value is converted to the requested type. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1769 |
* If the requested type is primitive, widening primitive conversions are attempted, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1770 |
* else reference conversions are attempted. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1771 |
* <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1772 |
* @param type the return type of the desired method handle |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1773 |
* @param value the value to return |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1774 |
* @return a method handle of the given return type and no arguments, which always returns the given value |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1775 |
* @throws NullPointerException if the {@code type} argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1776 |
* @throws ClassCastException if the value cannot be converted to the required return type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1777 |
* @throws IllegalArgumentException if the given type is {@code void.class} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1778 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1779 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1780 |
MethodHandle constant(Class<?> type, Object value) { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1781 |
if (type.isPrimitive()) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1782 |
if (type == void.class) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1783 |
throw newIllegalArgumentException("void type"); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1784 |
Wrapper w = Wrapper.forPrimitiveType(type); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1785 |
return insertArguments(identity(type), 0, w.convert(value, type)); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1786 |
} else { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1787 |
return identity(type).bindTo(type.cast(value)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1788 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1789 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1790 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1791 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1792 |
* Produces a method handle which returns its sole argument when invoked. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1793 |
* @param type the type of the sole parameter and return value of the desired method handle |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1794 |
* @return a unary method handle which accepts and returns the given type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1795 |
* @throws NullPointerException if the argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1796 |
* @throws IllegalArgumentException if the given type is {@code void.class} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1797 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1798 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1799 |
MethodHandle identity(Class<?> type) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1800 |
if (type == void.class) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1801 |
throw newIllegalArgumentException("void type"); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1802 |
else if (type == Object.class) |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1803 |
return ValueConversions.identity(); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1804 |
else if (type.isPrimitive()) |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1805 |
return ValueConversions.identity(Wrapper.forPrimitiveType(type)); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1806 |
else |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1807 |
return MethodHandleImpl.makeReferenceIdentity(type); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1808 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1809 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1810 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1811 |
* Provides a target method handle with one or more <em>bound arguments</em> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1812 |
* in advance of the method handle's invocation. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1813 |
* The formal parameters to the target corresponding to the bound |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1814 |
* arguments are called <em>bound parameters</em>. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1815 |
* Returns a new method handle which saves away the bound arguments. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1816 |
* When it is invoked, it receives arguments for any non-bound parameters, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1817 |
* binds the saved arguments to their corresponding parameters, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1818 |
* and calls the original target. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1819 |
* <p> |
4537 | 1820 |
* The type of the new method handle will drop the types for the bound |
1821 |
* parameters from the original target type, since the new method handle |
|
1822 |
* will no longer require those arguments to be supplied by its callers. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1823 |
* <p> |
4537 | 1824 |
* Each given argument object must match the corresponding bound parameter type. |
1825 |
* If a bound parameter type is a primitive, the argument object |
|
1826 |
* must be a wrapper, and will be unboxed to produce the primitive value. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1827 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1828 |
* The {@code pos} argument selects which parameters are to be bound. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1829 |
* It may range between zero and <i>N-L</i> (inclusively), |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1830 |
* where <i>N</i> is the arity of the target method handle |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1831 |
* and <i>L</i> is the length of the values array. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1832 |
* @param target the method handle to invoke after the argument is inserted |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1833 |
* @param pos where to insert the argument (zero for the first) |
4537 | 1834 |
* @param values the series of arguments to insert |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1835 |
* @return a method handle which inserts an additional argument, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1836 |
* before calling the original method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1837 |
* @throws NullPointerException if the target or the {@code values} array is null |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1838 |
* @see MethodHandle#bindTo |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1839 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1840 |
public static |
4537 | 1841 |
MethodHandle insertArguments(MethodHandle target, int pos, Object... values) { |
1842 |
int insCount = values.length; |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1843 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1844 |
int outargs = oldType.parameterCount(); |
4537 | 1845 |
int inargs = outargs - insCount; |
1846 |
if (inargs < 0) |
|
1847 |
throw newIllegalArgumentException("too many values to insert"); |
|
1848 |
if (pos < 0 || pos > inargs) |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1849 |
throw newIllegalArgumentException("no argument type to append"); |
4537 | 1850 |
MethodHandle result = target; |
1851 |
for (int i = 0; i < insCount; i++) { |
|
1852 |
Object value = values[i]; |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1853 |
Class<?> ptype = oldType.parameterType(pos+i); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1854 |
if (ptype.isPrimitive()) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1855 |
char btype = 'I'; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1856 |
Wrapper w = Wrapper.forPrimitiveType(ptype); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1857 |
switch (w) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1858 |
case LONG: btype = 'J'; break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1859 |
case FLOAT: btype = 'F'; break; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1860 |
case DOUBLE: btype = 'D'; break; |
4537 | 1861 |
} |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1862 |
// perform unboxing and/or primitive conversion |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1863 |
value = w.convert(value, ptype); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1864 |
result = result.bindArgument(pos, btype, value); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1865 |
continue; |
4537 | 1866 |
} |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1867 |
value = ptype.cast(value); // throw CCE if needed |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1868 |
if (pos == 0) { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1869 |
result = result.bindReceiver(value); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1870 |
} else { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1871 |
result = result.bindArgument(pos, 'L', value); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1872 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1873 |
} |
4537 | 1874 |
return result; |
1875 |
} |
|
1876 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1877 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1878 |
* Produces a method handle which will discard some dummy arguments |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1879 |
* before calling some other specified <i>target</i> method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1880 |
* The type of the new method handle will be the same as the target's type, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1881 |
* except it will also include the dummy argument types, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1882 |
* at some given position. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1883 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1884 |
* The {@code pos} argument may range between zero and <i>N</i>, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1885 |
* where <i>N</i> is the arity of the target. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1886 |
* If {@code pos} is zero, the dummy arguments will precede |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1887 |
* the target's real arguments; if {@code pos} is <i>N</i> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1888 |
* they will come after. |
4537 | 1889 |
* <p> |
1890 |
* <b>Example:</b> |
|
1891 |
* <p><blockquote><pre> |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1892 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1893 |
import static java.lang.invoke.MethodType.*; |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1894 |
... |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1895 |
MethodHandle cat = lookup().findVirtual(String.class, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1896 |
"concat", methodType(String.class, String.class)); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1897 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1898 |
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1899 |
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2)); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1900 |
assertEquals(bigType, d0.type()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1901 |
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1902 |
* </pre></blockquote> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1903 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1904 |
* This method is also equivalent to the following code: |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1905 |
* <p><blockquote><pre> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1906 |
* {@link #dropArguments(MethodHandle,int,Class...) dropArguments}(target, pos, valueTypes.toArray(new Class[0])) |
4537 | 1907 |
* </pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1908 |
* @param target the method handle to invoke after the arguments are dropped |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1909 |
* @param valueTypes the type(s) of the argument(s) to drop |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1910 |
* @param pos position of first argument to drop (zero for the leftmost) |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1911 |
* @return a method handle which drops arguments of the given types, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1912 |
* before calling the original method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1913 |
* @throws NullPointerException if the target is null, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1914 |
* or if the {@code valueTypes} list or any of its elements is null |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1915 |
* @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1916 |
* or if {@code pos} is negative or greater than the arity of the target, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1917 |
* or if the new method handle's type would have too many parameters |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1918 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1919 |
public static |
4537 | 1920 |
MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) { |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1921 |
MethodType oldType = target.type(); // get NPE |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1922 |
int dropped = valueTypes.size(); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1923 |
MethodType.checkSlotCount(dropped); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1924 |
if (dropped == 0) return target; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1925 |
int outargs = oldType.parameterCount(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1926 |
int inargs = outargs + dropped; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1927 |
if (pos < 0 || pos >= inargs) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1928 |
throw newIllegalArgumentException("no argument type to remove"); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1929 |
ArrayList<Class<?>> ptypes = new ArrayList<>(oldType.parameterList()); |
4537 | 1930 |
ptypes.addAll(pos, valueTypes); |
1931 |
MethodType newType = MethodType.methodType(oldType.returnType(), ptypes); |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
1932 |
return target.dropArguments(newType, pos, dropped); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1933 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1934 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1935 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1936 |
* Produces a method handle which will discard some dummy arguments |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1937 |
* before calling some other specified <i>target</i> method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1938 |
* The type of the new method handle will be the same as the target's type, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1939 |
* except it will also include the dummy argument types, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1940 |
* at some given position. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1941 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1942 |
* The {@code pos} argument may range between zero and <i>N</i>, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1943 |
* where <i>N</i> is the arity of the target. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1944 |
* If {@code pos} is zero, the dummy arguments will precede |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1945 |
* the target's real arguments; if {@code pos} is <i>N</i> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1946 |
* they will come after. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1947 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1948 |
* <b>Example:</b> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1949 |
* <p><blockquote><pre> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1950 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1951 |
import static java.lang.invoke.MethodType.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1952 |
... |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1953 |
MethodHandle cat = lookup().findVirtual(String.class, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1954 |
"concat", methodType(String.class, String.class)); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1955 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1956 |
MethodHandle d0 = dropArguments(cat, 0, String.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1957 |
assertEquals("yz", (String) d0.invokeExact("x", "y", "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1958 |
MethodHandle d1 = dropArguments(cat, 1, String.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1959 |
assertEquals("xz", (String) d1.invokeExact("x", "y", "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1960 |
MethodHandle d2 = dropArguments(cat, 2, String.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1961 |
assertEquals("xy", (String) d2.invokeExact("x", "y", "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1962 |
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1963 |
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z")); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1964 |
* </pre></blockquote> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1965 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1966 |
* This method is also equivalent to the following code: |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1967 |
* <p><blockquote><pre> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1968 |
* {@link #dropArguments(MethodHandle,int,List) dropArguments}(target, pos, Arrays.asList(valueTypes)) |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1969 |
* </pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1970 |
* @param target the method handle to invoke after the arguments are dropped |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1971 |
* @param valueTypes the type(s) of the argument(s) to drop |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1972 |
* @param pos position of first argument to drop (zero for the leftmost) |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1973 |
* @return a method handle which drops arguments of the given types, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1974 |
* before calling the original method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1975 |
* @throws NullPointerException if the target is null, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1976 |
* or if the {@code valueTypes} array or any of its elements is null |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1977 |
* @throws IllegalArgumentException if any element of {@code valueTypes} is {@code void.class}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1978 |
* or if {@code pos} is negative or greater than the arity of the target, |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1979 |
* or if the new method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
1980 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1981 |
*/ |
4537 | 1982 |
public static |
1983 |
MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) { |
|
1984 |
return dropArguments(target, pos, Arrays.asList(valueTypes)); |
|
1985 |
} |
|
1986 |
||
1987 |
/** |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1988 |
* Adapts a target method handle by pre-processing |
4537 | 1989 |
* one or more of its arguments, each with its own unary filter function, |
1990 |
* and then calling the target with each pre-processed argument |
|
1991 |
* replaced by the result of its corresponding filter function. |
|
1992 |
* <p> |
|
1993 |
* The pre-processing is performed by one or more method handles, |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1994 |
* specified in the elements of the {@code filters} array. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1995 |
* The first element of the filter array corresponds to the {@code pos} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1996 |
* argument of the target, and so on in sequence. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1997 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1998 |
* Null arguments in the array are treated as identity functions, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1999 |
* and the corresponding arguments left unchanged. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2000 |
* (If there are no non-null elements in the array, the original target is returned.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2001 |
* Each filter is applied to the corresponding argument of the adapter. |
4537 | 2002 |
* <p> |
2003 |
* If a filter {@code F} applies to the {@code N}th argument of |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2004 |
* the target, then {@code F} must be a method handle which |
4537 | 2005 |
* takes exactly one argument. The type of {@code F}'s sole argument |
2006 |
* replaces the corresponding argument type of the target |
|
2007 |
* in the resulting adapted method handle. |
|
2008 |
* The return type of {@code F} must be identical to the corresponding |
|
2009 |
* parameter type of the target. |
|
2010 |
* <p> |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2011 |
* It is an error if there are elements of {@code filters} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2012 |
* (null or not) |
4537 | 2013 |
* which do not correspond to argument positions in the target. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2014 |
* <b>Example:</b> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2015 |
* <p><blockquote><pre> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2016 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2017 |
import static java.lang.invoke.MethodType.*; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2018 |
... |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2019 |
MethodHandle cat = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2020 |
"concat", methodType(String.class, String.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2021 |
MethodHandle upcase = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2022 |
"toUpperCase", methodType(String.class)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2023 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2024 |
MethodHandle f0 = filterArguments(cat, 0, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2025 |
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2026 |
MethodHandle f1 = filterArguments(cat, 1, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2027 |
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2028 |
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2029 |
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
4537 | 2030 |
* </pre></blockquote> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2031 |
* <p> Here is pseudocode for the resulting adapter: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2032 |
* <blockquote><pre> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2033 |
* V target(P... p, A[i]... a[i], B... b); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2034 |
* A[i] filter[i](V[i]); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2035 |
* T adapter(P... p, V[i]... v[i], B... b) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2036 |
* return target(p..., f[i](v[i])..., b...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2037 |
* } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2038 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2039 |
* |
4537 | 2040 |
* @param target the method handle to invoke after arguments are filtered |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2041 |
* @param pos the position of the first argument to filter |
4537 | 2042 |
* @param filters method handles to call initially on filtered arguments |
2043 |
* @return method handle which incorporates the specified argument filtering logic |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2044 |
* @throws NullPointerException if the target is null |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2045 |
* or if the {@code filters} array is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2046 |
* @throws IllegalArgumentException if a non-null element of {@code filters} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2047 |
* does not match a corresponding argument type of target as described above, |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
2048 |
* or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()}, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
2049 |
* or if the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20528
diff
changeset
|
2050 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
4537 | 2051 |
*/ |
2052 |
public static |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2053 |
MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) { |
4537 | 2054 |
MethodType targetType = target.type(); |
2055 |
MethodHandle adapter = target; |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2056 |
MethodType adapterType = null; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2057 |
assert((adapterType = targetType) != null); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2058 |
int maxPos = targetType.parameterCount(); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2059 |
if (pos + filters.length > maxPos) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2060 |
throw newIllegalArgumentException("too many filters"); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2061 |
int curPos = pos-1; // pre-incremented |
4537 | 2062 |
for (MethodHandle filter : filters) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2063 |
curPos += 1; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2064 |
if (filter == null) continue; // ignore null elements of filters |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2065 |
adapter = filterArgument(adapter, curPos, filter); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2066 |
assert((adapterType = adapterType.changeParameterType(curPos, filter.type().parameterType(0))) != null); |
4537 | 2067 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2068 |
assert(adapterType.equals(adapter.type())); |
4537 | 2069 |
return adapter; |
2070 |
} |
|
2071 |
||
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2072 |
/*non-public*/ static |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2073 |
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) { |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2074 |
MethodType targetType = target.type(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2075 |
MethodType filterType = filter.type(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2076 |
if (filterType.parameterCount() != 1 |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2077 |
|| filterType.returnType() != targetType.parameterType(pos)) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2078 |
throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
2079 |
return MethodHandleImpl.makeCollectArguments(target, filter, pos, false); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2080 |
} |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2081 |
|
13610
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2082 |
// FIXME: Make this public in M1. |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2083 |
/*non-public*/ static |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2084 |
MethodHandle collectArguments(MethodHandle target, int pos, MethodHandle collector) { |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2085 |
MethodType targetType = target.type(); |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2086 |
MethodType filterType = collector.type(); |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2087 |
if (filterType.returnType() != void.class && |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2088 |
filterType.returnType() != targetType.parameterType(pos)) |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2089 |
throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2090 |
return MethodHandleImpl.makeCollectArguments(target, collector, pos, false); |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2091 |
} |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
2092 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2093 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2094 |
* Adapts a target method handle by post-processing |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2095 |
* its return value (if any) with a filter (another method handle). |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2096 |
* The result of the filter is returned from the adapter. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2097 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2098 |
* If the target returns a value, the filter must accept that value as |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2099 |
* its only argument. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2100 |
* If the target returns void, the filter must accept no arguments. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2101 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2102 |
* The return type of the filter |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2103 |
* replaces the return type of the target |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2104 |
* in the resulting adapted method handle. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2105 |
* The argument type of the filter (if any) must be identical to the |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2106 |
* return type of the target. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2107 |
* <b>Example:</b> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2108 |
* <p><blockquote><pre> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2109 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2110 |
import static java.lang.invoke.MethodType.*; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2111 |
... |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2112 |
MethodHandle cat = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2113 |
"concat", methodType(String.class, String.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2114 |
MethodHandle length = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2115 |
"length", methodType(int.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2116 |
System.out.println((String) cat.invokeExact("x", "y")); // xy |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2117 |
MethodHandle f0 = filterReturnValue(cat, length); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2118 |
System.out.println((int) f0.invokeExact("x", "y")); // 2 |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2119 |
* </pre></blockquote> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2120 |
* <p> Here is pseudocode for the resulting adapter: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2121 |
* <blockquote><pre> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2122 |
* V target(A...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2123 |
* T filter(V); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2124 |
* T adapter(A... a) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2125 |
* V v = target(a...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2126 |
* return filter(v); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2127 |
* } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2128 |
* // and if the target has a void return: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2129 |
* void target2(A...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2130 |
* T filter2(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2131 |
* T adapter2(A... a) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2132 |
* target2(a...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2133 |
* return filter2(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2134 |
* } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2135 |
* // and if the filter has a void return: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2136 |
* V target3(A...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2137 |
* void filter3(V); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2138 |
* void adapter3(A... a) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2139 |
* V v = target3(a...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2140 |
* filter3(v); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2141 |
* } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2142 |
* </pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2143 |
* @param target the method handle to invoke before filtering the return value |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2144 |
* @param filter method handle to call on the return value |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2145 |
* @return method handle which incorporates the specified return value filtering logic |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2146 |
* @throws NullPointerException if either argument is null |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2147 |
* @throws IllegalArgumentException if the argument list of {@code filter} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2148 |
* does not match the return type of target as described above |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2149 |
*/ |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2150 |
public static |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2151 |
MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2152 |
MethodType targetType = target.type(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2153 |
MethodType filterType = filter.type(); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2154 |
Class<?> rtype = targetType.returnType(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2155 |
int filterValues = filterType.parameterCount(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2156 |
if (filterValues == 0 |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2157 |
? (rtype != void.class) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2158 |
: (rtype != filterType.parameterType(0))) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2159 |
throw newIllegalArgumentException("target and filter types do not match", target, filter); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2160 |
// result = fold( lambda(retval, arg...) { filter(retval) }, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2161 |
// lambda( arg...) { target(arg...) } ) |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
2162 |
return MethodHandleImpl.makeCollectArguments(filter, target, 0, false); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2163 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2164 |
|
4537 | 2165 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2166 |
* Adapts a target method handle by pre-processing |
4537 | 2167 |
* some of its arguments, and then calling the target with |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2168 |
* the result of the pre-processing, inserted into the original |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2169 |
* sequence of arguments. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2170 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2171 |
* The pre-processing is performed by {@code combiner}, a second method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2172 |
* Of the arguments passed to the adapter, the first {@code N} arguments |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2173 |
* are copied to the combiner, which is then called. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2174 |
* (Here, {@code N} is defined as the parameter count of the combiner.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2175 |
* After this, control passes to the target, with any result |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2176 |
* from the combiner inserted before the original {@code N} incoming |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2177 |
* arguments. |
4537 | 2178 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2179 |
* If the combiner returns a value, the first parameter type of the target |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2180 |
* must be identical with the return type of the combiner, and the next |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2181 |
* {@code N} parameter types of the target must exactly match the parameters |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2182 |
* of the combiner. |
4537 | 2183 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2184 |
* If the combiner has a void return, no result will be inserted, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2185 |
* and the first {@code N} parameter types of the target |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2186 |
* must exactly match the parameters of the combiner. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2187 |
* <p> |
4537 | 2188 |
* The resulting adapter is the same type as the target, except that the |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2189 |
* first parameter type is dropped, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2190 |
* if it corresponds to the result of the combiner. |
4537 | 2191 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2192 |
* (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2193 |
* that either the combiner or the target does not wish to receive. |
4537 | 2194 |
* If some of the incoming arguments are destined only for the combiner, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2195 |
* consider using {@link MethodHandle#asCollector asCollector} instead, since those |
4537 | 2196 |
* arguments will not need to be live on the stack on entry to the |
2197 |
* target.) |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2198 |
* <b>Example:</b> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2199 |
* <p><blockquote><pre> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2200 |
import static java.lang.invoke.MethodHandles.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2201 |
import static java.lang.invoke.MethodType.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2202 |
... |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2203 |
MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2204 |
"println", methodType(void.class, String.class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2205 |
.bindTo(System.out); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2206 |
MethodHandle cat = lookup().findVirtual(String.class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2207 |
"concat", methodType(String.class, String.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2208 |
assertEquals("boojum", (String) cat.invokeExact("boo", "jum")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2209 |
MethodHandle catTrace = foldArguments(cat, trace); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2210 |
// also prints "boo": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2211 |
assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2212 |
* </pre></blockquote> |
4537 | 2213 |
* <p> Here is pseudocode for the resulting adapter: |
2214 |
* <blockquote><pre> |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2215 |
* // there are N arguments in A... |
4537 | 2216 |
* T target(V, A[N]..., B...); |
2217 |
* V combiner(A...); |
|
2218 |
* T adapter(A... a, B... b) { |
|
2219 |
* V v = combiner(a...); |
|
2220 |
* return target(v, a..., b...); |
|
2221 |
* } |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2222 |
* // and if the combiner has a void return: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2223 |
* T target2(A[N]..., B...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2224 |
* void combiner2(A...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2225 |
* T adapter2(A... a, B... b) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2226 |
* combiner2(a...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2227 |
* return target2(a..., b...); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2228 |
* } |
4537 | 2229 |
* </pre></blockquote> |
2230 |
* @param target the method handle to invoke after arguments are combined |
|
2231 |
* @param combiner method handle to call initially on the incoming arguments |
|
2232 |
* @return method handle which incorporates the specified argument folding logic |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2233 |
* @throws NullPointerException if either argument is null |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2234 |
* @throws IllegalArgumentException if {@code combiner}'s return type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2235 |
* is non-void and not the same as the first argument type of |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2236 |
* the target, or if the initial {@code N} argument types |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2237 |
* of the target |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2238 |
* (skipping one matching the {@code combiner}'s return type) |
4537 | 2239 |
* are not identical with the argument types of {@code combiner} |
2240 |
*/ |
|
2241 |
public static |
|
2242 |
MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) { |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2243 |
int pos = 0; |
4537 | 2244 |
MethodType targetType = target.type(); |
2245 |
MethodType combinerType = combiner.type(); |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2246 |
int foldPos = pos; |
4537 | 2247 |
int foldArgs = combinerType.parameterCount(); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2248 |
int foldVals = combinerType.returnType() == void.class ? 0 : 1; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2249 |
int afterInsertPos = foldPos + foldVals; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2250 |
boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2251 |
if (ok && !(combinerType.parameterList() |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2252 |
.equals(targetType.parameterList().subList(afterInsertPos, |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2253 |
afterInsertPos + foldArgs)))) |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2254 |
ok = false; |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2255 |
if (ok && foldVals != 0 && !combinerType.returnType().equals(targetType.parameterType(0))) |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2256 |
ok = false; |
4537 | 2257 |
if (!ok) |
2258 |
throw misMatchedTypes("target and combiner types", targetType, combinerType); |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2259 |
MethodType newType = targetType.dropParameterTypes(foldPos, afterInsertPos); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
2260 |
return MethodHandleImpl.makeCollectArguments(target, combiner, foldPos, true); |
4537 | 2261 |
} |
2262 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2263 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2264 |
* Makes a method handle which adapts a target method handle, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2265 |
* by guarding it with a test, a boolean-valued method handle. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2266 |
* If the guard fails, a fallback handle is called instead. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2267 |
* All three method handles must have the same corresponding |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2268 |
* argument and return types, except that the return type |
4537 | 2269 |
* of the test must be boolean, and the test is allowed |
2270 |
* to have fewer arguments than the other two method handles. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2271 |
* <p> Here is pseudocode for the resulting adapter: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2272 |
* <blockquote><pre> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2273 |
* boolean test(A...); |
4537 | 2274 |
* T target(A...,B...); |
2275 |
* T fallback(A...,B...); |
|
2276 |
* T adapter(A... a,B... b) { |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2277 |
* if (test(a...)) |
4537 | 2278 |
* return target(a..., b...); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2279 |
* else |
4537 | 2280 |
* return fallback(a..., b...); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2281 |
* } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2282 |
* </pre></blockquote> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2283 |
* Note that the test arguments ({@code a...} in the pseudocode) cannot |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2284 |
* be modified by execution of the test, and so are passed unchanged |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2285 |
* from the caller to the target or fallback as appropriate. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2286 |
* @param test method handle used for test, must return boolean |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2287 |
* @param target method handle to call if test passes |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2288 |
* @param fallback method handle to call if test fails |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2289 |
* @return method handle which incorporates the specified if/then/else logic |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2290 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2291 |
* @throws IllegalArgumentException if {@code test} does not return boolean, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2292 |
* or if all three method types do not match (with the return |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
2293 |
* type of {@code test} changed to match that of the target). |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2294 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2295 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2296 |
MethodHandle guardWithTest(MethodHandle test, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2297 |
MethodHandle target, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2298 |
MethodHandle fallback) { |
4537 | 2299 |
MethodType gtype = test.type(); |
2300 |
MethodType ttype = target.type(); |
|
2301 |
MethodType ftype = fallback.type(); |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2302 |
if (!ttype.equals(ftype)) |
4537 | 2303 |
throw misMatchedTypes("target and fallback types", ttype, ftype); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2304 |
if (gtype.returnType() != boolean.class) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2305 |
throw newIllegalArgumentException("guard type is not a predicate "+gtype); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2306 |
List<Class<?>> targs = ttype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2307 |
List<Class<?>> gargs = gtype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2308 |
if (!targs.equals(gargs)) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2309 |
int gpc = gargs.size(), tpc = targs.size(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2310 |
if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs)) |
4537 | 2311 |
throw misMatchedTypes("target and test types", ttype, gtype); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2312 |
test = dropArguments(test, gpc, targs.subList(gpc, tpc)); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2313 |
gtype = test.type(); |
4537 | 2314 |
} |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2315 |
return MethodHandleImpl.makeGuardWithTest(test, target, fallback); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2316 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2317 |
|
4537 | 2318 |
static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) { |
2319 |
return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2); |
|
2320 |
} |
|
2321 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2322 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2323 |
* Makes a method handle which adapts a target method handle, |
4537 | 2324 |
* by running it inside an exception handler. |
2325 |
* If the target returns normally, the adapter returns that value. |
|
2326 |
* If an exception matching the specified type is thrown, the fallback |
|
2327 |
* handle is called instead on the exception, plus the original arguments. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2328 |
* <p> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2329 |
* The target and handler must have the same corresponding |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2330 |
* argument and return types, except that handler may omit trailing arguments |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2331 |
* (similarly to the predicate in {@link #guardWithTest guardWithTest}). |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2332 |
* Also, the handler must have an extra leading parameter of {@code exType} or a supertype. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2333 |
* <p> Here is pseudocode for the resulting adapter: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2334 |
* <blockquote><pre> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2335 |
* T target(A..., B...); |
4537 | 2336 |
* T handler(ExType, A...); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2337 |
* T adapter(A... a, B... b) { |
4537 | 2338 |
* try { |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2339 |
* return target(a..., b...); |
4537 | 2340 |
* } catch (ExType ex) { |
2341 |
* return handler(ex, a...); |
|
2342 |
* } |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2343 |
* } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2344 |
* </pre></blockquote> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2345 |
* Note that the saved arguments ({@code a...} in the pseudocode) cannot |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2346 |
* be modified by execution of the target, and so are passed unchanged |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2347 |
* from the caller to the handler, if the handler is invoked. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2348 |
* <p> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2349 |
* The target and handler must return the same type, even if the handler |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2350 |
* always throws. (This might happen, for instance, because the handler |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2351 |
* is simulating a {@code finally} clause). |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2352 |
* To create such a throwing handler, compose the handler creation logic |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2353 |
* with {@link #throwException throwException}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2354 |
* in order to create a method handle of the correct return type. |
4537 | 2355 |
* @param target method handle to call |
2356 |
* @param exType the type of exception which the handler will catch |
|
2357 |
* @param handler method handle to call if a matching exception is thrown |
|
2358 |
* @return method handle which incorporates the specified try/catch logic |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2359 |
* @throws NullPointerException if any argument is null |
4537 | 2360 |
* @throws IllegalArgumentException if {@code handler} does not accept |
2361 |
* the given exception type, or if the method handle types do |
|
2362 |
* not match in their return types and their |
|
2363 |
* corresponding parameters |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2364 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2365 |
public static |
4537 | 2366 |
MethodHandle catchException(MethodHandle target, |
2367 |
Class<? extends Throwable> exType, |
|
2368 |
MethodHandle handler) { |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2369 |
MethodType ttype = target.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2370 |
MethodType htype = handler.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2371 |
if (htype.parameterCount() < 1 || |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2372 |
!htype.parameterType(0).isAssignableFrom(exType)) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2373 |
throw newIllegalArgumentException("handler does not accept exception type "+exType); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2374 |
if (htype.returnType() != ttype.returnType()) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2375 |
throw misMatchedTypes("target and handler return types", ttype, htype); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2376 |
List<Class<?>> targs = ttype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2377 |
List<Class<?>> hargs = htype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2378 |
hargs = hargs.subList(1, hargs.size()); // omit leading parameter from handler |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2379 |
if (!targs.equals(hargs)) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2380 |
int hpc = hargs.size(), tpc = targs.size(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2381 |
if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs)) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2382 |
throw misMatchedTypes("target and handler types", ttype, htype); |
9730
e4b334d47f4b
7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents:
9646
diff
changeset
|
2383 |
handler = dropArguments(handler, 1+hpc, targs.subList(hpc, tpc)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2384 |
htype = handler.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2385 |
} |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2386 |
return MethodHandleImpl.makeGuardWithCatch(target, exType, handler); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2387 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2388 |
|
4537 | 2389 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2390 |
* Produces a method handle which will throw exceptions of the given {@code exType}. |
4537 | 2391 |
* The method handle will accept a single argument of {@code exType}, |
2392 |
* and immediately throw it as an exception. |
|
2393 |
* The method type will nominally specify a return of {@code returnType}. |
|
2394 |
* The return type may be anything convenient: It doesn't matter to the |
|
2395 |
* method handle's behavior, since it will never return normally. |
|
18569 | 2396 |
* @param returnType the return type of the desired method handle |
2397 |
* @param exType the parameter type of the desired method handle |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2398 |
* @return method handle which can throw the given exceptions |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2399 |
* @throws NullPointerException if either argument is null |
4537 | 2400 |
*/ |
2401 |
public static |
|
2402 |
MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) { |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
2403 |
if (!Throwable.class.isAssignableFrom(exType)) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
13044
diff
changeset
|
2404 |
throw new ClassCastException(exType.getName()); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2405 |
return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType)); |
4537 | 2406 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2407 |
} |