author | jrose |
Thu, 12 May 2011 19:27:49 -0700 | |
changeset 9646 | 5ebbe5ab084f |
parent 9645 | dabb5e4edc4c |
child 9730 | e4b334d47f4b |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2 |
* Copyright (c) 2008, 2011, 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.*; |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
29 |
import sun.invoke.WrapperInstance; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
30 |
import sun.invoke.util.ValueConversions; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
31 |
import sun.invoke.util.VerifyAccess; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
32 |
import sun.invoke.util.Wrapper; |
4537 | 33 |
import java.util.List; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
34 |
import java.util.ArrayList; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
35 |
import java.util.Arrays; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
36 |
import sun.reflect.Reflection; |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
37 |
import static java.lang.invoke.MethodHandleStatics.*; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
38 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
39 |
/** |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
40 |
* 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
|
41 |
* method handles. They fall into several categories: |
4537 | 42 |
* <ul> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
43 |
* <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
|
44 |
* <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
|
45 |
* <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
|
46 |
* <li>Wrapper methods which can convert between method handles and interface types. |
4537 | 47 |
* </ul> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
48 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
49 |
* @author John Rose, JSR 292 EG |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
50 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
51 |
public class MethodHandles { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
52 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
53 |
private MethodHandles() { } // do not instantiate |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
54 |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
55 |
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
|
56 |
static { MethodHandleImpl.initStatics(); } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
57 |
// See IMPL_LOOKUP below. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
58 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
59 |
//// Method handle creation from ordinary methods. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
60 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
61 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
62 |
* Returns a {@link Lookup lookup object} on the caller, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
63 |
* 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
|
64 |
* including direct method handles to private fields and methods. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
65 |
* 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
|
66 |
* Do not store it in place where untrusted code can access it. |
4537 | 67 |
*/ |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
68 |
public static Lookup lookup() { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
69 |
return new Lookup(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
70 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
71 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
72 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
73 |
* 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
|
74 |
* It can only be used to create method handles to |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
75 |
* publicly accessible fields and methods. |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
76 |
* <p> |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
77 |
* 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
|
78 |
* 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
|
79 |
* <p> |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
80 |
* 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
|
81 |
* {@linkplain Lookup#in <code>publicLookup().in(C.class)</code>}. |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
82 |
* Since all classes have equal access to public names, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
83 |
* such a change would confer no new access rights. |
4537 | 84 |
*/ |
85 |
public static Lookup publicLookup() { |
|
86 |
return Lookup.PUBLIC_LOOKUP; |
|
87 |
} |
|
88 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
89 |
/** |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
90 |
* 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
|
91 |
* when the creation requires access checking. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
92 |
* Method handles do not perform |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
93 |
* 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
|
94 |
* Therefore, method handle access |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
95 |
* 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
|
96 |
* The caller class against which those restrictions are enforced |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
97 |
* is known as the {@linkplain #lookupClass lookup class}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
98 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
99 |
* 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
|
100 |
* {@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
|
101 |
* 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
|
102 |
* determined, and securely stored in the {@code Lookup} object. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
103 |
* 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
|
104 |
* 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
|
105 |
* 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
|
106 |
* even private ones. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
107 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
108 |
* 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
|
109 |
* use cases for methods, constructors, and fields. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
110 |
* 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
|
111 |
* the behavior the resulting method handles: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
112 |
* <code> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
113 |
* <table border=1 cellpadding=5 summary="lookup method behaviors"> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
114 |
* <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
|
115 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
116 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findGetter lookup.findGetter(C.class,"f",FT.class)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
117 |
* <td>FT f;</td><td>(T) this.f;</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
118 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
119 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
120 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticGetter lookup.findStaticGetter(C.class,"f",FT.class)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
121 |
* <td>static<br>FT f;</td><td>(T) C.f;</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
122 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
123 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
124 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSetter lookup.findSetter(C.class,"f",FT.class)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
125 |
* <td>FT f;</td><td>this.f = x;</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
126 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
127 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
128 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStaticSetter lookup.findStaticSetter(C.class,"f",FT.class)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
129 |
* <td>static<br>FT f;</td><td>C.f = arg;</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
130 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
131 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
132 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findVirtual lookup.findVirtual(C.class,"m",MT)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
133 |
* <td>T m(A*);</td><td>(T) this.m(arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
134 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
135 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
136 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findStatic lookup.findStatic(C.class,"m",MT)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
137 |
* <td>static<br>T m(A*);</td><td>(T) C.m(arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
138 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
139 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
140 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findSpecial lookup.findSpecial(C.class,"m",MT,this.class)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
141 |
* <td>T m(A*);</td><td>(T) super.m(arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
142 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
143 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
144 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#findConstructor lookup.findConstructor(C.class,MT)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
145 |
* <td>C(A*);</td><td>(T) new C(arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
146 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
147 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
148 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectGetter lookup.unreflectGetter(aField)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
149 |
* <td>(static)?<br>FT f;</td><td>(FT) aField.get(thisOrNull);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
150 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
151 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
152 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectSetter lookup.unreflectSetter(aField)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
153 |
* <td>(static)?<br>FT f;</td><td>aField.set(thisOrNull, arg);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
154 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
155 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
156 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
157 |
* <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
158 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
159 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
160 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflectConstructor lookup.unreflectConstructor(aConstructor)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
161 |
* <td>C(A*);</td><td>(C) aConstructor.newInstance(arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
162 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
163 |
* <tr> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
164 |
* <td>{@linkplain java.lang.invoke.MethodHandles.Lookup#unreflect lookup.unreflect(aMethod)}</td> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
165 |
* <td>(static)?<br>T m(A*);</td><td>(T) aMethod.invoke(thisOrNull, arg*);</td> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
166 |
* </tr> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
167 |
* </table> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
168 |
* </code> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
169 |
* 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
|
170 |
* documented as a parameter named {@code refc} in the lookup methods. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
171 |
* The method or constructor type {@code MT} is composed from the return type {@code T} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
172 |
* and the sequence of argument types {@code A*}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
173 |
* 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
|
174 |
* 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
|
175 |
* if it is present, it is always the leading argument to the method handle invocation. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
176 |
* 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
|
177 |
* 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
|
178 |
* 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
|
179 |
* and {@code this} otherwise. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
180 |
* 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
|
181 |
* for reflective objects corresponding to the given members. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
182 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
183 |
* The equivalence between looked-up method handles and underlying |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
184 |
* class members can break down in a few ways: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
185 |
* <ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
186 |
* <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
|
187 |
* 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
|
188 |
* Java expression or bytecoded constant. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
189 |
* <li>Likewise, if {@code T} or {@code MT} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
190 |
* 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
|
191 |
* the lookup can still succeed. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
192 |
* 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
|
193 |
* {@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
|
194 |
* <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
|
195 |
* on various grounds (<a href="#secmgr">see below</a>). |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
196 |
* 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
|
197 |
* security manager checks. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
198 |
* </ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
199 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
200 |
* <h3><a name="access"></a>Access checking</h3> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
201 |
* 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
|
202 |
* when a method handle is created. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
203 |
* This is a key difference from the Core Reflection API, since |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
204 |
* {@link java.lang.reflect.Method#invoke Method.invoke} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
205 |
* performs access checking against every caller, on every call. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
206 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
207 |
* 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
|
208 |
* compares its recorded lookup class against all requests to |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
209 |
* create method handles. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
210 |
* 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
|
211 |
* 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
|
212 |
* lookup class. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
213 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
214 |
* 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
|
215 |
* such as a metaobject protocol. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
216 |
* A shared {@code Lookup} object delegates the capability |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
217 |
* 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
|
218 |
* Even if privileged code uses the {@code Lookup} object, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
219 |
* 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
|
220 |
* original lookup class. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
221 |
* <p> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
222 |
* A lookup can fail, because |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
223 |
* 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
|
224 |
* 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
|
225 |
* 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
|
226 |
* 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
|
227 |
* 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
|
228 |
* the following: |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
229 |
* <ul> |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
230 |
* <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
|
231 |
* <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
|
232 |
* <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
|
233 |
* </ul> |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
234 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
235 |
* 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
|
236 |
* 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
|
237 |
* 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
|
238 |
* 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
|
239 |
* 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
|
240 |
* 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
|
241 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
242 |
* 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
|
243 |
* 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
|
244 |
* in the same top-level declaration. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
245 |
* For example, a nested class {@code C.D} |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
246 |
* 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
|
247 |
* {@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
|
248 |
* 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
|
249 |
* 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
|
250 |
* {@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
|
251 |
* 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
|
252 |
* 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
|
253 |
* classes, without special elevation of privilege. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
254 |
* <p> |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
255 |
* Although bytecode instructions can only refer to classes in |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
256 |
* 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
|
257 |
* 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
|
258 |
* 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
|
259 |
* Core Reflection API, and are impossible to bytecode instructions |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
260 |
* such as {@code invokestatic} or {@code getfield}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
261 |
* 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
|
262 |
* to allow applications to check such cross-loader references. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
263 |
* 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
|
264 |
* and the Core Reflection API |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
265 |
* (as found on {@link java.lang.Class Class}). |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
266 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
267 |
* Access checks only apply to named and reflected methods, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
268 |
* constructors, and fields. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
269 |
* Other method handle creation methods, such as |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
270 |
* {@link #convertArguments MethodHandles.convertArguments}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
271 |
* do not require any access checks, and are done |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
272 |
* with static methods of {@link MethodHandles}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
273 |
* independently of any {@code Lookup} object. |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
274 |
* |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
275 |
* <h3>Security manager interactions</h3> |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
276 |
* <a name="secmgr"></a> |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
277 |
* 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
|
278 |
* additional checks. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
279 |
* From one to four calls are made to the security manager. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
280 |
* 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
|
281 |
* {@link java.lang.SecurityException SecurityException}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
282 |
* Define {@code smgr} as the security manager, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
283 |
* {@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
|
284 |
* 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
|
285 |
* member is actually defined. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
286 |
* The calls are made according to the following rules: |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
287 |
* <ul> |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
288 |
* <li>In all cases, {@link SecurityManager#checkMemberAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
289 |
* smgr.checkMemberAccess(refc, Member.PUBLIC)} is called. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
290 |
* <li>If the class loader of the lookup class is not |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
291 |
* 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
|
292 |
* then {@link SecurityManager#checkPackageAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
293 |
* smgr.checkPackageAccess(refcPkg)} is called, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
294 |
* where {@code refcPkg} is the package of {@code refc}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
295 |
* <li>If the retrieved member is not public, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
296 |
* {@link SecurityManager#checkMemberAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
297 |
* smgr.checkMemberAccess(defc, Member.DECLARED)} is called. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
298 |
* (Note that {@code defc} might be the same as {@code refc}.) |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
299 |
* <li>If the retrieved member is not public, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
300 |
* and if {@code defc} and {@code refc} are in different class loaders, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
301 |
* and if the class loader of the lookup class is not |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
302 |
* the same as or an ancestor of the class loader of {@code defc}, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
303 |
* then {@link SecurityManager#checkPackageAccess |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
304 |
* smgr.checkPackageAccess(defcPkg)} is called, |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
305 |
* where {@code defcPkg} is the package of {@code defc}. |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
306 |
* </ul> |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
307 |
* In all cases, the requesting class presented to the security |
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
308 |
* manager will be the lookup class from the current {@code Lookup} object. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
309 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
310 |
public static final |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
311 |
class Lookup { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
312 |
/** 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
|
313 |
private final Class<?> lookupClass; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
314 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
315 |
/** 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
|
316 |
private final int allowedModes; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
317 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
318 |
/** A single-bit mask representing {@code public} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
319 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
320 |
* 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
|
321 |
* {@code public} {@linkplain java.lang.reflect.Modifier#PUBLIC modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
322 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
323 |
public static final int PUBLIC = Modifier.PUBLIC; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
324 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
325 |
/** A single-bit mask representing {@code private} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
326 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
327 |
* 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
|
328 |
* {@code private} {@linkplain java.lang.reflect.Modifier#PRIVATE modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
329 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
330 |
public static final int PRIVATE = Modifier.PRIVATE; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
331 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
332 |
/** A single-bit mask representing {@code protected} access, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
333 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
334 |
* 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
|
335 |
* {@code protected} {@linkplain java.lang.reflect.Modifier#PROTECTED modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
336 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
337 |
public static final int PROTECTED = Modifier.PROTECTED; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
338 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
339 |
/** A single-bit mask representing {@code package} access (default access), |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
340 |
* which may contribute to the result of {@link #lookupModes lookupModes}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
341 |
* 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
|
342 |
* any particular {@linkplain java.lang.reflect.Modifier modifier bit}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
343 |
*/ |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
344 |
public static final int PACKAGE = Modifier.STATIC; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
345 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
346 |
private static final int ALL_MODES = (PUBLIC | PRIVATE | PROTECTED | PACKAGE); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
347 |
private static final int TRUSTED = -1; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
348 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
349 |
private static int fixmods(int mods) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
350 |
mods &= (ALL_MODES - PACKAGE); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
351 |
return (mods != 0) ? mods : PACKAGE; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
352 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
353 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
354 |
/** 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
|
355 |
* which checks are performed for visibility and access permissions. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
356 |
* <p> |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
357 |
* The class implies a maximum level of access permission, |
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
358 |
* 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
|
359 |
* {@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
|
360 |
* can be accessed. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
361 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
362 |
public Class<?> lookupClass() { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
363 |
return lookupClass; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
364 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
365 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
366 |
// This is just for calling out to MethodHandleImpl. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
367 |
private Class<?> lookupClassOrNull() { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
368 |
return (allowedModes == TRUSTED) ? null : lookupClass; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
369 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
370 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
371 |
/** 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
|
372 |
* The result is a bit-mask of the bits |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
373 |
* {@linkplain #PUBLIC PUBLIC (0x01)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
374 |
* {@linkplain #PRIVATE PRIVATE (0x02)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
375 |
* {@linkplain #PROTECTED PROTECTED (0x04)}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
376 |
* and {@linkplain #PACKAGE PACKAGE (0x08)}. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
377 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
378 |
* A freshly-created lookup object |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
379 |
* 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
|
380 |
* 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
|
381 |
* 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
|
382 |
* {@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
|
383 |
* may have some mode bits set to zero. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
384 |
* 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
|
385 |
* 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
|
386 |
* lookup object, and also by the new lookup class. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
387 |
*/ |
7052
963a5baf2ba3
6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
5727
diff
changeset
|
388 |
public int lookupModes() { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
389 |
return allowedModes & ALL_MODES; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
390 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
391 |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
392 |
/** 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
|
393 |
* for method handle creation. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
394 |
* 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
|
395 |
* which in turn is called by a method not in this package. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
396 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
397 |
* Also, don't make it private, lest javac interpose |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
398 |
* an access$N method. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
399 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
400 |
Lookup() { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
401 |
this(getCallerClassAtEntryPoint(), ALL_MODES); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
402 |
// 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
|
403 |
checkUnprivilegedlookupClass(lookupClass); |
4537 | 404 |
} |
405 |
||
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
406 |
Lookup(Class<?> lookupClass) { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
407 |
this(lookupClass, ALL_MODES); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
408 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
409 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
410 |
private Lookup(Class<?> lookupClass, int allowedModes) { |
4537 | 411 |
this.lookupClass = lookupClass; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
412 |
this.allowedModes = allowedModes; |
4537 | 413 |
} |
414 |
||
415 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
416 |
* Creates a lookup on the specified new lookup class. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
417 |
* The resulting object will report the specified |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
418 |
* class as its own {@link #lookupClass lookupClass}. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
419 |
* <p> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
420 |
* However, the resulting {@code Lookup} object is guaranteed |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
421 |
* 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
|
422 |
* 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
|
423 |
* <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
|
424 |
* 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
|
425 |
* (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
|
426 |
* <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
|
427 |
* 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
|
428 |
* <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
|
429 |
* 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
|
430 |
* <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
|
431 |
* 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
|
432 |
* (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
|
433 |
* </ul> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
434 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
435 |
* @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
|
436 |
* @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
|
437 |
* @throws NullPointerException if the argument is null |
4537 | 438 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
439 |
public Lookup in(Class<?> requestedLookupClass) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
440 |
requestedLookupClass.getClass(); // null check |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
441 |
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
|
442 |
return new Lookup(requestedLookupClass, ALL_MODES); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
443 |
if (requestedLookupClass == this.lookupClass) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
444 |
return this; // keep same capabilities |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
445 |
int newModes = (allowedModes & (ALL_MODES & ~PROTECTED)); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
446 |
if ((newModes & PACKAGE) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
447 |
&& !VerifyAccess.isSamePackage(this.lookupClass, requestedLookupClass)) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
448 |
newModes &= ~(PACKAGE|PRIVATE); |
4537 | 449 |
} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
450 |
// Allow nestmate lookups to be created without special privilege: |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
451 |
if ((newModes & PRIVATE) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
452 |
&& !VerifyAccess.isSamePackageMember(this.lookupClass, requestedLookupClass)) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
453 |
newModes &= ~PRIVATE; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
454 |
} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
455 |
if (newModes == PUBLIC |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
456 |
&& !VerifyAccess.isClassAccessible(requestedLookupClass, this.lookupClass)) { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
457 |
// 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
|
458 |
// No permissions. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
459 |
newModes = 0; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
460 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
461 |
checkUnprivilegedlookupClass(requestedLookupClass); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
462 |
return new Lookup(requestedLookupClass, newModes); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
463 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
464 |
|
4537 | 465 |
// Make sure outer class is initialized first. |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
466 |
static { IMPL_NAMES.getClass(); } |
4537 | 467 |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
468 |
/** Version of lookup which is trusted minimally. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
469 |
* 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
|
470 |
* publicly accessible members. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
471 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
472 |
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
|
473 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
474 |
/** Package-private version of lookup which is trusted. */ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
475 |
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
|
476 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
477 |
private static void checkUnprivilegedlookupClass(Class<?> lookupClass) { |
2764
2e45af54c0f9
6839839: access checking logic is wrong at three points in MethodHandles
jrose
parents:
2763
diff
changeset
|
478 |
String name = lookupClass.getName(); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
479 |
if (name.startsWith("java.lang.invoke.")) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
480 |
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
481 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
482 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
483 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
484 |
* 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
|
485 |
* (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
|
486 |
* 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
|
487 |
* 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
|
488 |
* 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
|
489 |
* allowed access, and is chosen as follows: |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
490 |
* <ul> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
491 |
* <li>If no access is allowed, the suffix is "/noaccess". |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
492 |
* <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
|
493 |
* <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
|
494 |
* <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
|
495 |
* </ul> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
496 |
* 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
|
497 |
* access (public, package, private, and protected) is allowed. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
498 |
* In this case, no suffix is added. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
499 |
* 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
|
500 |
* {@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
|
501 |
* 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
|
502 |
* 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
|
503 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
504 |
* (It may seem strange that protected access should be |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
505 |
* stronger than private access. Viewed independently from |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
506 |
* 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
|
507 |
* because it requires a direct subclass relationship between |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
508 |
* caller and callee.) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
509 |
* @see #in |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
510 |
*/ |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
511 |
@Override |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
512 |
public String toString() { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
513 |
String cname = lookupClass.getName(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
514 |
switch (allowedModes) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
515 |
case 0: // no privileges |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
516 |
return cname + "/noaccess"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
517 |
case PUBLIC: |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
518 |
return cname + "/public"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
519 |
case PUBLIC|PACKAGE: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
520 |
return cname + "/package"; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
521 |
case ALL_MODES & ~PROTECTED: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
522 |
return cname + "/private"; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
523 |
case ALL_MODES: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
524 |
return cname; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
525 |
case TRUSTED: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
526 |
return "/trusted"; // internal only; not exported |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
527 |
default: // Should not happen, but it's a bitfield... |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
528 |
cname = cname + "/" + Integer.toHexString(allowedModes); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
529 |
assert(false) : cname; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
530 |
return cname; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
531 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
532 |
} |
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 |
// call this from an entry point method in Lookup with extraFrames=0. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
535 |
private static Class<?> getCallerClassAtEntryPoint() { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
536 |
final int CALLER_DEPTH = 4; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
537 |
// 0: Reflection.getCC, 1: getCallerClassAtEntryPoint, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
538 |
// 2: Lookup.<init>, 3: MethodHandles.*, 4: caller |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
539 |
// Note: This should be the only use of getCallerClass in this file. |
4537 | 540 |
assert(Reflection.getCallerClass(CALLER_DEPTH-1) == MethodHandles.class); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
541 |
return Reflection.getCallerClass(CALLER_DEPTH); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
542 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
543 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
544 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
545 |
* Produces a method handle for a static method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
546 |
* The type of the method handle will be that of the method. |
4537 | 547 |
* (Since static methods do not take receivers, there is no |
548 |
* 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
|
549 |
* 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
|
550 |
* 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
|
551 |
* 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
|
552 |
* immediately, before the method handle is returned. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
553 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
554 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
555 |
* {@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
|
556 |
* 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
|
557 |
* @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
|
558 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
559 |
* @param type the type of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
560 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
561 |
* @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
|
562 |
* @throws IllegalAccessException if access checking fails, or if the method is not {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
563 |
* @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
|
564 |
* <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
|
565 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
566 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
567 |
public |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
568 |
MethodHandle findStatic(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
569 |
MemberName method = resolveOrFail(refc, name, type, true); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
570 |
checkMethod(refc, method, true); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
571 |
return MethodHandleImpl.findMethod(method, false, lookupClassOrNull()); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
572 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
573 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
574 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
575 |
* Produces a method handle for a virtual method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
576 |
* 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
|
577 |
* with the receiver type (usually {@code refc}) prepended. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
578 |
* 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
|
579 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
580 |
* 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
|
581 |
* 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
|
582 |
* implementation to enter. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
583 |
* (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
|
584 |
* {@code invokevirtual} or {@code invokeinterface} instruction.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
585 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
586 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
587 |
* {@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
|
588 |
* 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
|
589 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
590 |
* Because of the general equivalence between {@code invokevirtual} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
591 |
* instructions and method handles produced by {@code findVirtual}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
592 |
* 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
|
593 |
* {@code invokeExact} or {@code invoke}, the resulting |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
594 |
* 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
|
595 |
* {@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
|
596 |
* {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
597 |
* with the same {@code type} argument. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
598 |
* |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
599 |
* @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
|
600 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
601 |
* @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
|
602 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
603 |
* @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
|
604 |
* @throws IllegalAccessException if access checking fails, or if the method is {@code static} |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
605 |
* @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
|
606 |
* <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
|
607 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
608 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
609 |
public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
610 |
MemberName method = resolveOrFail(refc, name, type, false); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
611 |
checkMethod(refc, method, false); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
612 |
MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
613 |
return restrictProtectedReceiver(method, mh); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
614 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
615 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
616 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
617 |
* 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
|
618 |
* the constructor of the specified type. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
619 |
* 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
|
620 |
* 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
|
621 |
* 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
|
622 |
* 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
|
623 |
* immediately, before the method handle is returned. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
624 |
* <p> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
625 |
* 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
|
626 |
* 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
|
627 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
628 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
629 |
* {@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
|
630 |
* 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
|
631 |
* @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
|
632 |
* @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
|
633 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
634 |
* @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
|
635 |
* @throws IllegalAccessException if access checking fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
636 |
* @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
|
637 |
* <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
|
638 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
639 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
640 |
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
|
641 |
String name = "<init>"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
642 |
MemberName ctor = resolveOrFail(refc, name, type, false, false, lookupClassOrNull()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
643 |
assert(ctor.isConstructor()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
644 |
checkAccess(refc, ctor); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
645 |
MethodHandle rawMH = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull()); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
646 |
MethodHandle allocMH = MethodHandleImpl.makeAllocator(rawMH); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
647 |
return fixVarargs(allocMH, rawMH); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
648 |
} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
649 |
|
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
650 |
/** Return a version of MH which matches matchMH w.r.t. isVarargsCollector. */ |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
651 |
private static MethodHandle fixVarargs(MethodHandle mh, MethodHandle matchMH) { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
652 |
boolean va1 = mh.isVarargsCollector(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
653 |
boolean va2 = matchMH.isVarargsCollector(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
654 |
if (va1 == va2) { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
655 |
return mh; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
656 |
} else if (va2) { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
657 |
MethodType type = mh.type(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
658 |
int arity = type.parameterCount(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
659 |
return mh.asVarargsCollector(type.parameterType(arity-1)); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
660 |
} else { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
661 |
throw new InternalError("already varargs, but template is not: "+mh); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
662 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
663 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
664 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
665 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
666 |
* Produces an early-bound method handle for a virtual method, |
2763 | 667 |
* as if called from an {@code invokespecial} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
668 |
* instruction from {@code caller}. |
2763 | 669 |
* 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
|
670 |
* with a suitably restricted receiver type (such as {@code caller}) prepended. |
2763 | 671 |
* 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
|
672 |
* to the caller. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
673 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
674 |
* 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
|
675 |
* but will not dispatch on the receiver's type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
676 |
* (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
|
677 |
* {@code invokespecial} instruction.) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
678 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
679 |
* 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
|
680 |
* 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
|
681 |
* privileges, the access fails. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
682 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
683 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
684 |
* {@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
|
685 |
* 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
|
686 |
* @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
|
687 |
* @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
|
688 |
* @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
|
689 |
* @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
|
690 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
691 |
* @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
|
692 |
* @throws IllegalAccessException if access checking fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
693 |
* @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
|
694 |
* <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
|
695 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
696 |
*/ |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
697 |
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
|
698 |
Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
699 |
checkSpecialCaller(specialCaller); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
700 |
MemberName method = resolveOrFail(refc, name, type, false, false, specialCaller); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
701 |
checkMethod(refc, method, false); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
702 |
MethodHandle mh = MethodHandleImpl.findMethod(method, false, specialCaller); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
703 |
return restrictReceiver(method, mh, specialCaller); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
704 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
705 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
706 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
707 |
* 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
|
708 |
* 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
|
709 |
* value type. |
5726 | 710 |
* 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
|
711 |
* the field. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
712 |
* 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
|
713 |
* @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
|
714 |
* @param name the field's name |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
715 |
* @param type the field's type |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
716 |
* @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
|
717 |
* @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
|
718 |
* @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
|
719 |
* @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
|
720 |
* <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
|
721 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
722 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
723 |
public MethodHandle findGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
5726 | 724 |
return makeAccessor(refc, name, type, false, false); |
725 |
} |
|
726 |
||
727 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
728 |
* Produces a method handle giving write access to a non-static field. |
5726 | 729 |
* The type of the method handle will have a void return type. |
730 |
* The method handle will take two arguments, the instance containing |
|
731 |
* the field, and the value to be stored. |
|
732 |
* The second argument will be of the field's value type. |
|
733 |
* 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
|
734 |
* @param refc the class or interface from which the method is accessed |
5726 | 735 |
* @param name the field's name |
736 |
* @param type the field's type |
|
737 |
* @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
|
738 |
* @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
|
739 |
* @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
|
740 |
* @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
|
741 |
* <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
|
742 |
* @throws NullPointerException if any argument is null |
5726 | 743 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
744 |
public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
5726 | 745 |
return makeAccessor(refc, name, type, false, true); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
746 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
747 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
748 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
749 |
* Produces a method handle giving read access to a static field. |
5726 | 750 |
* The type of the method handle will have a return type of the field's |
751 |
* value type. |
|
752 |
* The method handle will take no arguments. |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
753 |
* 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
|
754 |
* @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
|
755 |
* @param name the field's name |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
756 |
* @param type the field's type |
5726 | 757 |
* @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
|
758 |
* @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
|
759 |
* @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
|
760 |
* @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
|
761 |
* <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
|
762 |
* @throws NullPointerException if any argument is null |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
763 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
764 |
public MethodHandle findStaticGetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
5726 | 765 |
return makeAccessor(refc, name, type, true, false); |
766 |
} |
|
767 |
||
768 |
/** |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
769 |
* Produces a method handle giving write access to a static field. |
5726 | 770 |
* The type of the method handle will have a void return type. |
771 |
* The method handle will take a single |
|
772 |
* argument, of the field's value type, the value to be stored. |
|
773 |
* 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
|
774 |
* @param refc the class or interface from which the method is accessed |
5726 | 775 |
* @param name the field's name |
776 |
* @param type the field's type |
|
777 |
* @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
|
778 |
* @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
|
779 |
* @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
|
780 |
* @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
|
781 |
* <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
|
782 |
* @throws NullPointerException if any argument is null |
5726 | 783 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
784 |
public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { |
5726 | 785 |
return makeAccessor(refc, name, type, true, true); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
786 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
787 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
788 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
789 |
* 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
|
790 |
* 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
|
791 |
* 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
|
792 |
* The method and all its argument types must be accessible to the lookup class. |
4537 | 793 |
* The type of the method handle will be that of the method, |
794 |
* without any insertion of an additional receiver parameter. |
|
795 |
* The given receiver will be bound into the method handle, |
|
796 |
* so that every call to the method handle will invoke the |
|
797 |
* requested method on the given receiver. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
798 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
799 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
800 |
* {@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
|
801 |
* 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
|
802 |
* <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
|
803 |
* (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
|
804 |
* 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
|
805 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
806 |
* This is equivalent to the following code: |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
807 |
* <blockquote><pre> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
808 |
MethodHandle mh0 = {@link #findVirtual findVirtual}(defc, name, type); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
809 |
MethodHandle mh1 = mh0.{@link MethodHandle#bindTo bindTo}(receiver); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
810 |
MethodType mt1 = mh1.type(); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
811 |
if (mh0.isVarargsCollector() && mt1.parameterCount() > 0) { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
812 |
mh1 = mh1.asVarargsCollector(mt1.parameterType(mt1.parameterCount()-1)); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
813 |
return mh1; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
814 |
* </pre></blockquote> |
4537 | 815 |
* where {@code defc} is either {@code receiver.getClass()} or a super |
816 |
* type of that class, in which the requested method is accessible |
|
817 |
* to the lookup class. |
|
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
818 |
* (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
|
819 |
* @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
|
820 |
* @param name the name of the method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
821 |
* @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
|
822 |
* @return the desired method handle |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
823 |
* @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
|
824 |
* @throws IllegalAccessException if access checking fails |
8349
e1ba54c43609
7014755: JSR 292 member lookup interaction with security manager
jrose
parents:
8347
diff
changeset
|
825 |
* @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
|
826 |
* <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
|
827 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
828 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
829 |
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
|
830 |
Class<? extends Object> refc = receiver.getClass(); // may get NPE |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
831 |
MemberName method = resolveOrFail(refc, name, type, false); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
832 |
checkMethod(refc, method, false); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
833 |
MethodHandle dmh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
834 |
MethodHandle bmh = MethodHandleImpl.bindReceiver(dmh, receiver); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
835 |
if (bmh == null) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
836 |
throw method.makeAccessException("no access", this); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
837 |
if (dmh.type().parameterCount() == 0) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
838 |
return dmh; // bound the trailing parameter; no varargs possible |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
839 |
return fixVarargs(bmh, dmh); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
840 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
841 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
842 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
843 |
* 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
|
844 |
* 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
|
845 |
* 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
|
846 |
* 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
|
847 |
* 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
|
848 |
* 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
|
849 |
* 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
|
850 |
* 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
|
851 |
* 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
|
852 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
853 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
854 |
* {@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
|
855 |
* 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
|
856 |
* @param m the reflected method |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
857 |
* @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
|
858 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
859 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
860 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
861 |
public MethodHandle unreflect(Method m) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
862 |
MemberName method = new MemberName(m); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
863 |
assert(method.isMethod()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
864 |
if (!m.isAccessible()) checkMethod(method.getDeclaringClass(), method, method.isStatic()); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
865 |
MethodHandle mh = MethodHandleImpl.findMethod(method, true, lookupClassOrNull()); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
866 |
if (!m.isAccessible()) mh = restrictProtectedReceiver(method, mh); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
867 |
return mh; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
868 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
869 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
870 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
871 |
* Produces a method handle for a reflected method. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
872 |
* 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
|
873 |
* 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
|
874 |
* 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
|
875 |
* 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
|
876 |
* 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
|
877 |
* 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
|
878 |
* 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
|
879 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
880 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
881 |
* {@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
|
882 |
* 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
|
883 |
* @param m the reflected method |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
884 |
* @param specialCaller the class nominally calling the method |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
885 |
* @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
|
886 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
887 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
888 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
889 |
public MethodHandle unreflectSpecial(Method m, Class<?> specialCaller) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
890 |
checkSpecialCaller(specialCaller); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
891 |
MemberName method = new MemberName(m); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
892 |
assert(method.isMethod()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
893 |
// ignore m.isAccessible: this is a new kind of access |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
894 |
checkMethod(m.getDeclaringClass(), method, false); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
895 |
MethodHandle mh = MethodHandleImpl.findMethod(method, false, lookupClassOrNull()); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
896 |
return restrictReceiver(method, mh, specialCaller); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
897 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
898 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
899 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
900 |
* Produces a method handle for a reflected constructor. |
4537 | 901 |
* The type of the method handle will be that of the constructor, |
902 |
* 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
|
903 |
* The method handle will perform a {@code newInstance} operation, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
904 |
* 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
|
905 |
* arguments passed to the method handle. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
906 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
907 |
* If the constructor's {@code accessible} flag is not set, |
4537 | 908 |
* 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
|
909 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
910 |
* The returned method handle will have |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
911 |
* {@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
|
912 |
* 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
|
913 |
* @param c the reflected constructor |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
914 |
* @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
|
915 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
916 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
917 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
918 |
public MethodHandle unreflectConstructor(Constructor c) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
919 |
MemberName ctor = new MemberName(c); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
920 |
assert(ctor.isConstructor()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
921 |
if (!c.isAccessible()) checkAccess(c.getDeclaringClass(), ctor); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
922 |
MethodHandle rawCtor = MethodHandleImpl.findMethod(ctor, false, lookupClassOrNull()); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
923 |
MethodHandle allocator = MethodHandleImpl.makeAllocator(rawCtor); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
924 |
return fixVarargs(allocator, rawCtor); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
925 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
926 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
927 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
928 |
* 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
|
929 |
* The type of the method handle will have a return type of the field's |
4537 | 930 |
* value type. |
931 |
* If the field is static, the method handle will take no arguments. |
|
932 |
* Otherwise, its single argument will be the instance containing |
|
933 |
* the field. |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
934 |
* 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
|
935 |
* 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
|
936 |
* @param f the reflected field |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
937 |
* @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
|
938 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
939 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
940 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
941 |
public MethodHandle unreflectGetter(Field f) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
942 |
return makeAccessor(f.getDeclaringClass(), new MemberName(f), f.isAccessible(), false); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
943 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
944 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
945 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
946 |
* 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
|
947 |
* The type of the method handle will have a void return type. |
4537 | 948 |
* If the field is static, the method handle will take a single |
949 |
* argument, of the field's value type, the value to be stored. |
|
950 |
* Otherwise, the two arguments will be the instance containing |
|
951 |
* 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
|
952 |
* 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
|
953 |
* 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
|
954 |
* @param f the reflected field |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
955 |
* @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
|
956 |
* @throws IllegalAccessException if access checking fails |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
957 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
958 |
*/ |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
959 |
public MethodHandle unreflectSetter(Field f) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
960 |
return makeAccessor(f.getDeclaringClass(), new MemberName(f), f.isAccessible(), true); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
961 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
962 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
963 |
/// Helper methods, all package-private. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
964 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
965 |
MemberName resolveOrFail(Class<?> refc, String name, Class<?> type, boolean isStatic) throws NoSuchFieldException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
966 |
checkSymbolicClass(refc); // do this before attempting to resolve |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
967 |
name.getClass(); type.getClass(); // NPE |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
968 |
int mods = (isStatic ? Modifier.STATIC : 0); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
969 |
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull(), |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
970 |
NoSuchFieldException.class); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
971 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
972 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
973 |
MemberName resolveOrFail(Class<?> refc, String name, MethodType type, boolean isStatic) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
974 |
checkSymbolicClass(refc); // do this before attempting to resolve |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
975 |
name.getClass(); type.getClass(); // NPE |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
976 |
int mods = (isStatic ? Modifier.STATIC : 0); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
977 |
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull(), |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
978 |
NoSuchMethodException.class); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
979 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
980 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
981 |
MemberName resolveOrFail(Class<?> refc, String name, MethodType type, boolean isStatic, |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
982 |
boolean searchSupers, Class<?> specialCaller) throws NoSuchMethodException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
983 |
checkSymbolicClass(refc); // do this before attempting to resolve |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
984 |
name.getClass(); type.getClass(); // NPE |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
985 |
int mods = (isStatic ? Modifier.STATIC : 0); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
986 |
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), searchSupers, specialCaller, |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
987 |
NoSuchMethodException.class); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
988 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
989 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
990 |
void checkSymbolicClass(Class<?> refc) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
991 |
Class<?> caller = lookupClassOrNull(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
992 |
if (caller != null && !VerifyAccess.isClassAccessible(refc, caller)) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
993 |
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
|
994 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
995 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
996 |
void checkMethod(Class<?> refc, MemberName m, boolean wantStatic) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
997 |
String message; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
998 |
if (m.isConstructor()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
999 |
message = "expected a method, not a constructor"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1000 |
else if (!m.isMethod()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1001 |
message = "expected a method"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1002 |
else if (wantStatic != m.isStatic()) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1003 |
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
|
1004 |
else |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1005 |
{ checkAccess(refc, m); return; } |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1006 |
throw m.makeAccessException(message, this); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1007 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1008 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1009 |
void checkAccess(Class<?> refc, MemberName m) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1010 |
int allowedModes = this.allowedModes; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1011 |
if (allowedModes == TRUSTED) return; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1012 |
int mods = m.getModifiers(); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1013 |
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
|
1014 |
return; // common case |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1015 |
int requestedModes = fixmods(mods); // adjust 0 => PACKAGE |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1016 |
if ((requestedModes & allowedModes) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1017 |
&& VerifyAccess.isMemberAccessible(refc, m.getDeclaringClass(), |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1018 |
mods, lookupClass())) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1019 |
return; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1020 |
if (((requestedModes & ~allowedModes) & PROTECTED) != 0 |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1021 |
&& VerifyAccess.isSamePackage(m.getDeclaringClass(), lookupClass())) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1022 |
// Protected members can also be checked as if they were package-private. |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1023 |
return; |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1024 |
throw m.makeAccessException(accessFailedMessage(refc, m), this); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1025 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1026 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1027 |
String accessFailedMessage(Class<?> refc, MemberName m) { |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1028 |
Class<?> defc = m.getDeclaringClass(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1029 |
int mods = m.getModifiers(); |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1030 |
// check the class first: |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1031 |
boolean classOK = (Modifier.isPublic(defc.getModifiers()) && |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1032 |
(defc == refc || |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1033 |
Modifier.isPublic(refc.getModifiers()))); |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1034 |
if (!classOK && (allowedModes & PACKAGE) != 0) { |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1035 |
classOK = (VerifyAccess.isClassAccessible(defc, lookupClass()) && |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1036 |
(defc == refc || |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1037 |
VerifyAccess.isClassAccessible(refc, lookupClass()))); |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1038 |
} |
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1039 |
if (!classOK) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1040 |
return "class is not public"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1041 |
if (Modifier.isPublic(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1042 |
return "access to public member failed"; // (how?) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1043 |
if (Modifier.isPrivate(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1044 |
return "member is private"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1045 |
if (Modifier.isProtected(mods)) |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1046 |
return "member is protected"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1047 |
return "member is private to package"; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1048 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1049 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1050 |
private static final boolean ALLOW_NESTMATE_ACCESS = false; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1051 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1052 |
void checkSpecialCaller(Class<?> specialCaller) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1053 |
if (allowedModes == TRUSTED) return; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1054 |
if ((allowedModes & PRIVATE) == 0 |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1055 |
|| (specialCaller != lookupClass() |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1056 |
&& !(ALLOW_NESTMATE_ACCESS && |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1057 |
VerifyAccess.isSamePackageMember(specialCaller, lookupClass())))) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1058 |
throw new MemberName(specialCaller). |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1059 |
makeAccessException("no private access for invokespecial", this); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1060 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1061 |
|
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1062 |
MethodHandle restrictProtectedReceiver(MemberName method, MethodHandle mh) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1063 |
// 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
|
1064 |
// 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
|
1065 |
if (!method.isProtected() || method.isStatic() |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1066 |
|| allowedModes == TRUSTED |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1067 |
|| method.getDeclaringClass() == lookupClass() |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1068 |
|| (ALLOW_NESTMATE_ACCESS && |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1069 |
VerifyAccess.isSamePackageMember(method.getDeclaringClass(), lookupClass()))) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1070 |
return mh; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1071 |
else |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1072 |
return restrictReceiver(method, mh, lookupClass()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1073 |
} |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1074 |
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
|
1075 |
assert(!method.isStatic()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1076 |
Class<?> defc = method.getDeclaringClass(); // receiver type of mh is too wide |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1077 |
if (defc.isInterface() || !defc.isAssignableFrom(caller)) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1078 |
throw method.makeAccessException("caller class must be a subclass below the method", caller); |
4537 | 1079 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1080 |
MethodType rawType = mh.type(); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1081 |
if (rawType.parameterType(0) == caller) return mh; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1082 |
MethodType narrowType = rawType.changeParameterType(0, caller); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1083 |
MethodHandle narrowMH = MethodHandleImpl.convertArguments(mh, narrowType, rawType, 0); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1084 |
return fixVarargs(narrowMH, mh); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1085 |
} |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1086 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1087 |
MethodHandle makeAccessor(Class<?> refc, String name, Class<?> type, |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1088 |
boolean isStatic, boolean isSetter) throws NoSuchFieldException, IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1089 |
MemberName field = resolveOrFail(refc, name, type, isStatic); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1090 |
if (isStatic != field.isStatic()) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1091 |
throw field.makeAccessException(isStatic |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1092 |
? "expected a static field" |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1093 |
: "expected a non-static field", this); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1094 |
return makeAccessor(refc, field, false, isSetter); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1095 |
} |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1096 |
|
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1097 |
MethodHandle makeAccessor(Class<?> refc, MemberName field, |
8347
e5daa5772ffd
7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents:
8346
diff
changeset
|
1098 |
boolean trusted, boolean isSetter) throws IllegalAccessException { |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1099 |
assert(field.isField()); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1100 |
if (trusted) |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1101 |
return MethodHandleImpl.accessField(field, isSetter, lookupClassOrNull()); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1102 |
checkAccess(refc, field); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1103 |
MethodHandle mh = MethodHandleImpl.accessField(field, isSetter, lookupClassOrNull()); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1104 |
return restrictProtectedReceiver(field, mh); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
1105 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1106 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1107 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1108 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1109 |
* 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
|
1110 |
* 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
|
1111 |
* 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
|
1112 |
* and the second will be {@code int}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1113 |
* @param arrayClass an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1114 |
* @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
|
1115 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1116 |
* @throws IllegalArgumentException if arrayClass is not an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1117 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1118 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1119 |
MethodHandle arrayElementGetter(Class<?> arrayClass) throws IllegalArgumentException { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1120 |
return MethodHandleImpl.accessArrayElement(arrayClass, false); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1121 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1122 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1123 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1124 |
* 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
|
1125 |
* 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
|
1126 |
* 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
|
1127 |
* The first and second arguments will be the array type and int. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1128 |
* @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
|
1129 |
* @throws NullPointerException if the argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1130 |
* @throws IllegalArgumentException if arrayClass is not an array type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1131 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1132 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1133 |
MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentException { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1134 |
return MethodHandleImpl.accessArrayElement(arrayClass, true); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1135 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1136 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1137 |
/// method handle invocation (reflective style) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1138 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1139 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1140 |
* Produces a method handle which will invoke any method handle of the |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1141 |
* given {@code type} on a standard set of {@code Object} type arguments |
4537 | 1142 |
* and a single trailing {@code Object[]} array. |
1143 |
* The resulting invoker will be a method handle with the following |
|
1144 |
* arguments: |
|
1145 |
* <ul> |
|
1146 |
* <li>a single {@code MethodHandle} target |
|
1147 |
* <li>zero or more {@code Object} values (counted by {@code objectArgCount}) |
|
1148 |
* <li>an {@code Object[]} array containing more arguments |
|
1149 |
* </ul> |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1150 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1151 |
* The invoker will behave 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
|
1152 |
* the indicated {@code type}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1153 |
* 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
|
1154 |
* 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
|
1155 |
* 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
|
1156 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1157 |
* The type of the returned invoker will not be the given {@code type}, but rather |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1158 |
* will have all parameter and return types replaced by {@code Object}, except for |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1159 |
* the last parameter type, which will be the array type {@code Object[]}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1160 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1161 |
* Before invoking its target, the invoker will spread the varargs array, apply |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1162 |
* 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
|
1163 |
* The return value of the invoker will be an {@code Object} reference, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1164 |
* boxing a primitive value if the original type returns a primitive, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1165 |
* and always null if the original type returns void. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1166 |
* <p> |
4537 | 1167 |
* This method is equivalent to the following code (though it may be more efficient): |
1168 |
* <p><blockquote><pre> |
|
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1169 |
MethodHandle invoker = MethodHandles.invoker(type); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1170 |
int spreadArgCount = type.parameterCount - objectArgCount; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1171 |
invoker = invoker.asSpreader(Object[].class, spreadArgCount); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1172 |
return invoker; |
4537 | 1173 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1174 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1175 |
* This method throws no reflective or security exceptions. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1176 |
* @param type the desired target type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1177 |
* @param objectArgCount number of fixed (non-varargs) {@code Object} arguments |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1178 |
* @return a method handle suitable for invoking any method handle of the given type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1179 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1180 |
static public |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1181 |
MethodHandle spreadInvoker(MethodType type, int objectArgCount) { |
4537 | 1182 |
if (objectArgCount < 0 || objectArgCount > type.parameterCount()) |
1183 |
throw new IllegalArgumentException("bad argument count "+objectArgCount); |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1184 |
return type.invokers().spreadInvoker(objectArgCount); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1185 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1186 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1187 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1188 |
* 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
|
1189 |
* 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
|
1190 |
* 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
|
1191 |
* 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
|
1192 |
* an additional leading argument of type {@code MethodHandle}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1193 |
* <p> |
4537 | 1194 |
* This method is equivalent to the following code (though it may be more efficient): |
1195 |
* <p><blockquote><pre> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1196 |
publicLookup().findVirtual(MethodHandle.class, "invokeExact", type) |
4537 | 1197 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1198 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1199 |
* <p style="font-size:smaller;"> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1200 |
* <em>Discussion:</em> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1201 |
* 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
|
1202 |
* of unknown types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1203 |
* 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
|
1204 |
* handle {@code M}, extract its type {@code T}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1205 |
* 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
|
1206 |
* 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
|
1207 |
* (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
|
1208 |
* is unknown.) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1209 |
* If spreading, collecting, or other argument transformations are required, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1210 |
* 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
|
1211 |
* 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
|
1212 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1213 |
* <em>(Note: The invoker method is not available via the Core Reflection API. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1214 |
* An attempt to call {@linkplain java.lang.reflect.Method#invoke Method.invoke} |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1215 |
* 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
|
1216 |
* {@link java.lang.UnsupportedOperationException UnsupportedOperationException}.)</em> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1217 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1218 |
* This method throws no reflective or security exceptions. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1219 |
* @param type the desired target type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1220 |
* @return a method handle suitable for invoking any method handle of the given type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1221 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1222 |
static public |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1223 |
MethodHandle exactInvoker(MethodType type) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1224 |
return type.invokers().exactInvoker(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1225 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1226 |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1227 |
/** |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1228 |
* 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
|
1229 |
* 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
|
1230 |
* The resulting invoker will have a type which is |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1231 |
* 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
|
1232 |
* an additional leading argument of type {@code MethodHandle}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1233 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1234 |
* Before invoking its target, the invoker will apply reference casts as |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1235 |
* 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
|
1236 |
* 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
|
1237 |
* 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
|
1238 |
* 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
|
1239 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1240 |
* 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
|
1241 |
* <p><blockquote><pre> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1242 |
publicLookup().findVirtual(MethodHandle.class, "invoke", type) |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1243 |
* </pre></blockquote> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1244 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1245 |
* This method throws no reflective or security exceptions. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1246 |
* @param type the desired target type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1247 |
* @return a method handle suitable for invoking any method handle convertible to the given type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1248 |
*/ |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1249 |
static public |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1250 |
MethodHandle invoker(MethodType type) { |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1251 |
return type.invokers().generalInvoker(); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1252 |
} |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1253 |
|
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1254 |
/** |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1255 |
* <em>Temporary alias</em> for {@link #invoker}, for backward compatibility with some versions of JSR 292. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1256 |
* @deprecated Will be removed for JSR 292 Proposed Final Draft. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1257 |
*/ |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1258 |
public static |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1259 |
MethodHandle genericInvoker(MethodType type) { |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1260 |
return invoker(type); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1261 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1262 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1263 |
/** |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1264 |
* Perform value checking, exactly as if for an adapted method handle. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1265 |
* It is assumed that the given value is either null, of type T0, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1266 |
* or (if T0 is primitive) of the wrapper type corresponding to T0. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1267 |
* The following checks and conversions are made: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1268 |
* <ul> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1269 |
* <li>If T0 and T1 are references, then a cast to T1 is applied. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1270 |
* (The types do not need to be related in any particular way.) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1271 |
* <li>If T0 and T1 are primitives, then a widening or narrowing |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1272 |
* conversion is applied, if one exists. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1273 |
* <li>If T0 is a primitive and T1 a reference, and |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1274 |
* T0 has a wrapper type TW, a boxing conversion to TW is applied, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1275 |
* possibly followed by a reference conversion. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1276 |
* T1 must be TW or a supertype. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1277 |
* <li>If T0 is a reference and T1 a primitive, and |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1278 |
* T1 has a wrapper type TW, an unboxing conversion is applied, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1279 |
* possibly preceded by a reference conversion. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1280 |
* T0 must be TW or a supertype. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1281 |
* <li>If T1 is void, the return value is discarded |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1282 |
* <li>If T0 is void and T1 a reference, a null value is introduced. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1283 |
* <li>If T0 is void and T1 a primitive, a zero value is introduced. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1284 |
* </ul> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1285 |
* If the value is discarded, null will be returned. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1286 |
* @param valueType |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1287 |
* @param value |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1288 |
* @return the value, converted if necessary |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1289 |
* @throws java.lang.ClassCastException if a cast fails |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1290 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1291 |
static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1292 |
<T0, T1> T1 checkValue(Class<T0> t0, Class<T1> t1, Object value) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1293 |
throws ClassCastException |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1294 |
{ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1295 |
if (t0 == t1) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1296 |
// no conversion needed; just reassert the same type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1297 |
if (t0.isPrimitive()) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1298 |
return Wrapper.asPrimitiveType(t1).cast(value); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1299 |
else |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1300 |
return Wrapper.OBJECT.convert(value, t1); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1301 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1302 |
boolean prim0 = t0.isPrimitive(), prim1 = t1.isPrimitive(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1303 |
if (!prim0) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1304 |
// check contract with caller |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1305 |
Wrapper.OBJECT.convert(value, t0); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1306 |
if (!prim1) { |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1307 |
return Wrapper.OBJECT.convert(value, t1); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1308 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1309 |
// convert reference to primitive by unboxing |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1310 |
Wrapper w1 = Wrapper.forPrimitiveType(t1); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1311 |
return w1.convert(value, t1); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1312 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1313 |
// check contract with caller: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1314 |
Wrapper.asWrapperType(t0).cast(value); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1315 |
Wrapper w1 = Wrapper.forPrimitiveType(t1); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1316 |
return w1.convert(value, t1); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1317 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1318 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1319 |
static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1320 |
Object checkValue(Class<?> T1, Object value) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1321 |
throws ClassCastException |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1322 |
{ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1323 |
Class<?> T0; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1324 |
if (value == null) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1325 |
T0 = Object.class; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1326 |
else |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1327 |
T0 = value.getClass(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1328 |
return checkValue(T0, T1, value); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1329 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1330 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1331 |
/// method handle modification (creation from other method handles) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1332 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1333 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1334 |
* Produces a method handle which adapts the type of the |
4537 | 1335 |
* given method handle to a new type by pairwise argument conversion. |
1336 |
* The original type and new type must have the same number of arguments. |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1337 |
* The resulting method handle is guaranteed to report a type |
4537 | 1338 |
* which is equal to the desired new type. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1339 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1340 |
* If the original type and new type are equal, returns target. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1341 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1342 |
* The following conversions are applied as needed both to |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1343 |
* arguments and return types. Let T0 and T1 be the differing |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1344 |
* new and old parameter types (or old and new return types) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1345 |
* for corresponding values passed by the new and old method types. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1346 |
* Given those types T0, T1, one of the following conversions is applied |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1347 |
* if possible: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1348 |
* <ul> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1349 |
* <li>If T0 and T1 are references, then a cast to T1 is applied. |
4537 | 1350 |
* (The types do not need to be related in any particular way.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1351 |
* <li>If T0 and T1 are primitives, then a Java method invocation |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1352 |
* conversion (JLS 5.3) is applied, if one exists. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1353 |
* <li>If T0 is a primitive and T1 a reference, a boxing |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1354 |
* conversion is applied if one exists, possibly followed by |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1355 |
* a reference conversion to a superclass. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1356 |
* T1 must be a wrapper class or a supertype of one. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1357 |
* <li>If T0 is a reference and T1 a primitive, an unboxing |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1358 |
* conversion will be applied at runtime, possibly followed |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1359 |
* by a Java method invocation conversion (JLS 5.3) |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1360 |
* on the primitive value. (These are the widening conversions.) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1361 |
* T0 must be a wrapper class or a supertype of one. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1362 |
* (In the case where T0 is Object, these are the conversions |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1363 |
* allowed by java.lang.reflect.Method.invoke.) |
4537 | 1364 |
* <li>If the return type T1 is void, any returned value is discarded |
1365 |
* <li>If the return type T0 is void and T1 a reference, a null value is introduced. |
|
1366 |
* <li>If the return type T0 is void and T1 a primitive, a zero value is introduced. |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1367 |
* </ul> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1368 |
* @param target the method handle to invoke after arguments are retyped |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1369 |
* @param newType the expected type of the new method handle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1370 |
* @return a method handle which delegates to {@code target} after performing |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1371 |
* any necessary argument conversions, and arranges for any |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1372 |
* necessary return value conversions |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1373 |
* @throws NullPointerException if either argument is null |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1374 |
* @throws WrongMethodTypeException if the conversion cannot be made |
4537 | 1375 |
* @see MethodHandle#asType |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1376 |
* @see MethodHandles#explicitCastArguments |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1377 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1378 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1379 |
MethodHandle convertArguments(MethodHandle target, MethodType newType) { |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1380 |
return MethodHandleImpl.convertArguments(target, newType, 1); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1381 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1382 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1383 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1384 |
* Produces a method handle which adapts the type of the |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1385 |
* given method handle to a new type by pairwise argument conversion. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1386 |
* 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
|
1387 |
* 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
|
1388 |
* which is equal to the desired new type. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1389 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1390 |
* 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
|
1391 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1392 |
* The same conversions are allowed as for {@link #convertArguments convertArguments}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1393 |
* and some additional conversions are also applied if those conversions fail. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1394 |
* Given types T0, T1, one of the following conversions is applied |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1395 |
* in addition, if the conversions specified for {@code convertArguments} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1396 |
* would be insufficient: |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1397 |
* <ul> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1398 |
* <li>If T0 and T1 are references, and T1 is an interface type, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1399 |
* then the value of type T0 is passed as a T1 without a cast. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1400 |
* (This treatment of interfaces follows the usage of the bytecode verifier.) |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1401 |
* <li>If T0 and T1 are primitives and one is boolean, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1402 |
* the boolean is treated as a one-bit unsigned integer. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1403 |
* (This treatment follows the usage of the bytecode verifier.) |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1404 |
* A conversion from another primitive type behaves as if |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1405 |
* it first converts to byte, and then masks all but the low bit. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1406 |
* <li>If a primitive value would be converted by {@code convertArguments} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1407 |
* using Java method invocation conversion (JLS 5.3), |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1408 |
* Java casting conversion (JLS 5.5) may be used also. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1409 |
* This allows primitives to be narrowed as well as widened. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1410 |
* </ul> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1411 |
* @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
|
1412 |
* @param newType the expected type of the new method handle |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1413 |
* @return a method handle which delegates to {@code target} after performing |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1414 |
* any necessary argument conversions, and arranges for any |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1415 |
* necessary return value conversions |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1416 |
* @throws NullPointerException if either argument is null |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1417 |
* @throws WrongMethodTypeException if the conversion cannot be made |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1418 |
* @see MethodHandle#asType |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1419 |
* @see MethodHandles#convertArguments |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1420 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1421 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1422 |
MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) { |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1423 |
return MethodHandleImpl.convertArguments(target, newType, 2); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1424 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1425 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1426 |
/* |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1427 |
FIXME: Reconcile javadoc with 10/22/2010 EG notes on conversion: |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1428 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1429 |
Both converters arrange for their method handles to convert arguments |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1430 |
and return values. The conversion rules are the same for arguments |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1431 |
and return values, and depend only on source and target types, S and |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1432 |
T. The conversions allowed by castConvertArguments are a strict |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1433 |
superset of those performed by convertArguments. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1434 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1435 |
In all cases, if S and T are references, a simple checkcast is done. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1436 |
If neither S nor T is a primitive, no attempt is made to unbox and |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1437 |
box. A failed conversion throws ClassCastException. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1438 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1439 |
If T is void, the value is dropped. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1440 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1441 |
For compatibility with reflection, if S is void and T is a reference, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1442 |
a null value is produced. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1443 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1444 |
For compatibility with reflection, if S is a reference and T is a |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1445 |
primitive, S is first unboxed and then undergoes primitive conversion. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1446 |
In the case of 'convertArguments', only assignment conversion is |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1447 |
performed (no narrowing primitive conversion). |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1448 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1449 |
If S is a primitive, S is boxed, and then the above rules are applied. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1450 |
If S and T are both primitives, the boxing will be undetectable; only |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1451 |
the primitive conversions will be apparent to the user. The key point |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1452 |
is that if S is a primitive type, the implementation may box it and |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1453 |
treat is as Object, without loss of information, or it may use a "fast |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1454 |
path" which does not use boxing. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1455 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1456 |
Notwithstanding the rules above, for compatibility with the verifier, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1457 |
if T is an interface, it is treated as if it were Object. [KEEP THIS?] |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1458 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1459 |
Also, for compatibility with the verifier, a boolean may be undergo |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1460 |
widening or narrowing conversion to any other primitive type. [KEEP THIS?] |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1461 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1462 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1463 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1464 |
* 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
|
1465 |
* 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
|
1466 |
* 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
|
1467 |
* which is equal to the desired new type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1468 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1469 |
* The given array controls the reordering. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1470 |
* 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
|
1471 |
* {@code newType.parameterCount()}, and call {@code #O} the number |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1472 |
* of outgoing parameters (the value {@code target.type().parameterCount()}). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1473 |
* 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
|
1474 |
* 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
|
1475 |
* 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
|
1476 |
* 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
|
1477 |
* argument, where {@code I} is {@code reorder[N]}. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1478 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1479 |
* No argument or return value conversions are applied. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1480 |
* The type of each incoming argument, as determined by {@code newType}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1481 |
* must be identical to the type of the corresponding outgoing argument |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1482 |
* or arguments in the target method handle. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1483 |
* 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
|
1484 |
* type of the original target. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1485 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1486 |
* The reordering array need not specify an actual permutation. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1487 |
* 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
|
1488 |
* 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
|
1489 |
* 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
|
1490 |
* 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
|
1491 |
* 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
|
1492 |
* are may be any type, as determined only by {@code newType}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1493 |
* <blockquote><pre> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1494 |
MethodType intfn1 = MethodType.methodType(int.class, int.class); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1495 |
MethodType intfn2 = MethodType.methodType(int.class, int.class, int.class); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1496 |
MethodHandle sub = ... {int x, int y => x-y} ...; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1497 |
assert(sub.type().equals(intfn2)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1498 |
MethodHandle sub1 = MethodHandles.permuteArguments(sub, intfn2, 0, 1); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1499 |
MethodHandle rsub = MethodHandles.permuteArguments(sub, intfn2, 1, 0); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1500 |
assert((int)rsub.invokeExact(1, 100) == 99); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1501 |
MethodHandle add = ... {int x, int y => x+y} ...; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1502 |
assert(add.type().equals(intfn2)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1503 |
MethodHandle twice = MethodHandles.permuteArguments(add, intfn1, 0, 0); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1504 |
assert(twice.type().equals(intfn1)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1505 |
assert((int)twice.invokeExact(21) == 42); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1506 |
* </pre></blockquote> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1507 |
* @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
|
1508 |
* @param newType the expected type of the new method handle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1509 |
* @param reorder a string which controls the reordering |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1510 |
* @return a method handle which delegates to {@code target} after it |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1511 |
* 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
|
1512 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1513 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1514 |
public static |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1515 |
MethodHandle permuteArguments(MethodHandle target, MethodType newType, int... reorder) { |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1516 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1517 |
checkReorder(reorder, newType, oldType); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1518 |
return MethodHandleImpl.permuteArguments(target, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1519 |
newType, oldType, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1520 |
reorder); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1521 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1522 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1523 |
private static void checkReorder(int[] reorder, MethodType newType, MethodType oldType) { |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1524 |
if (newType.returnType() != oldType.returnType()) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1525 |
throw newIllegalArgumentException("return types do not match", |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1526 |
oldType, newType); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1527 |
if (reorder.length == oldType.parameterCount()) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1528 |
int limit = newType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1529 |
boolean bad = false; |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1530 |
for (int j = 0; j < reorder.length; j++) { |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1531 |
int i = reorder[j]; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1532 |
if (i < 0 || i >= limit) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1533 |
bad = true; break; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1534 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1535 |
Class<?> src = newType.parameterType(i); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1536 |
Class<?> dst = oldType.parameterType(j); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1537 |
if (src != dst) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1538 |
throw newIllegalArgumentException("parameter types do not match after reorder", |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1539 |
oldType, newType); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1540 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1541 |
if (!bad) return; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1542 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1543 |
throw newIllegalArgumentException("bad reorder array: "+Arrays.toString(reorder)); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1544 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1545 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1546 |
/** |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1547 |
* Equivalent to the following code: |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1548 |
* <p><blockquote><pre> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1549 |
* int spreadPos = newType.parameterCount() - 1; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1550 |
* Class<?> spreadType = newType.parameterType(spreadPos); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1551 |
* int spreadCount = target.type().parameterCount() - spreadPos; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1552 |
* MethodHandle adapter = target.asSpreader(spreadType, spreadCount); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1553 |
* adapter = adapter.asType(newType); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1554 |
* return adapter; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1555 |
* </pre></blockquote> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1556 |
* @param target the method handle to invoke after argument spreading |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1557 |
* @param newType the expected type of the new method handle |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1558 |
* @return a method handle which spreads its final argument, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1559 |
* before calling the original method handle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1560 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1561 |
/*non-public*/ static |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1562 |
MethodHandle spreadArguments(MethodHandle target, MethodType newType) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1563 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1564 |
int inargs = newType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1565 |
int outargs = oldType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1566 |
int spreadPos = inargs - 1; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1567 |
int numSpread = (outargs - spreadPos); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1568 |
MethodHandle res = null; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1569 |
if (spreadPos >= 0 && numSpread >= 0) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1570 |
res = MethodHandleImpl.spreadArguments(target, newType, spreadPos); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1571 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1572 |
if (res == null) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1573 |
throw newIllegalArgumentException("cannot spread "+newType+" to " +oldType); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1574 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1575 |
return res; |
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 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1578 |
/** |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1579 |
* Equivalent to the following code: |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1580 |
* <p><blockquote><pre> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1581 |
* int collectPos = target.type().parameterCount() - 1; |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1582 |
* Class<?> collectType = target.type().parameterType(collectPos); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1583 |
* if (!collectType.isArray()) collectType = Object[].class; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1584 |
* int collectCount = newType.parameterCount() - collectPos; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1585 |
* MethodHandle adapter = target.asCollector(collectType, collectCount); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1586 |
* adapter = adapter.asType(newType); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1587 |
* return adapter; |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1588 |
* </pre></blockquote> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1589 |
* @param target the method handle to invoke after argument collection |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1590 |
* @param newType the expected type of the new method handle |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1591 |
* @return a method handle which collects some trailing argument |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1592 |
* into an array, before calling the original method handle |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1593 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1594 |
/*non-public*/ static |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1595 |
MethodHandle collectArguments(MethodHandle target, MethodType newType) { |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1596 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1597 |
int inargs = newType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1598 |
int outargs = oldType.parameterCount(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1599 |
int collectPos = outargs - 1; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1600 |
int numCollect = (inargs - collectPos); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1601 |
if (collectPos < 0 || numCollect < 0) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1602 |
throw newIllegalArgumentException("wrong number of arguments"); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1603 |
MethodHandle res = MethodHandleImpl.collectArguments(target, newType, collectPos, null); |
4537 | 1604 |
if (res == null) { |
1605 |
throw newIllegalArgumentException("cannot collect from "+newType+" to " +oldType); |
|
1606 |
} |
|
1607 |
return res; |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1608 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1609 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1610 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1611 |
* 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
|
1612 |
* constant value every time it is invoked. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1613 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1614 |
* 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
|
1615 |
* 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
|
1616 |
* else reference conversions are attempted. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1617 |
* <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1618 |
* unless the type is {@code void}, in which case it is {@code identity(type)}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1619 |
* @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
|
1620 |
* @param value the value to return |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1621 |
* @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
|
1622 |
* @throws NullPointerException if the {@code type} argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1623 |
* @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
|
1624 |
* @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
|
1625 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1626 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1627 |
MethodHandle constant(Class<?> type, Object value) { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1628 |
if (type.isPrimitive()) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1629 |
if (type == void.class) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1630 |
throw newIllegalArgumentException("void type"); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1631 |
Wrapper w = Wrapper.forPrimitiveType(type); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1632 |
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
|
1633 |
} else { |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1634 |
return identity(type).bindTo(type.cast(value)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1635 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1636 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1637 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1638 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1639 |
* 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
|
1640 |
* <p>The identity function for {@code void} takes no arguments and returns no values. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1641 |
* @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
|
1642 |
* @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
|
1643 |
* @throws NullPointerException if the argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1644 |
* @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
|
1645 |
*/ |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1646 |
public static |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1647 |
MethodHandle identity(Class<?> type) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1648 |
if (type == void.class) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1649 |
throw newIllegalArgumentException("void type"); |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1650 |
else if (type == Object.class) |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1651 |
return ValueConversions.identity(); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1652 |
else if (type.isPrimitive()) |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1653 |
return ValueConversions.identity(Wrapper.forPrimitiveType(type)); |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1654 |
else |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1655 |
return AdapterMethodHandle.makeRetypeRaw( |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1656 |
MethodType.methodType(type, type), ValueConversions.identity()); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1657 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1658 |
|
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1659 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1660 |
* Produces a method handle which calls the original method handle {@code target}, |
4537 | 1661 |
* after inserting the given argument(s) at the given position. |
1662 |
* The formal parameters to {@code target} which will be supplied by those |
|
1663 |
* arguments are called <em>bound parameters</em>, because the new method |
|
1664 |
* will contain bindings for those parameters take from {@code values}. |
|
1665 |
* The type of the new method handle will drop the types for the bound |
|
1666 |
* parameters from the original target type, since the new method handle |
|
1667 |
* 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
|
1668 |
* <p> |
4537 | 1669 |
* Each given argument object must match the corresponding bound parameter type. |
1670 |
* If a bound parameter type is a primitive, the argument object |
|
1671 |
* 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
|
1672 |
* <p> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1673 |
* The <i>pos</i> may range between zero and <i>N</i> (inclusively), |
4537 | 1674 |
* where <i>N</i> is the number of argument types in resulting method handle |
1675 |
* (after bound parameter types are dropped). |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1676 |
* @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
|
1677 |
* @param pos where to insert the argument (zero for the first) |
4537 | 1678 |
* @param values the series of arguments to insert |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1679 |
* @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
|
1680 |
* before calling the original method handle |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1681 |
* @throws NullPointerException if the {@code target} argument or the {@code values} array is null |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1682 |
* @see MethodHandle#bindTo |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1683 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1684 |
public static |
4537 | 1685 |
MethodHandle insertArguments(MethodHandle target, int pos, Object... values) { |
1686 |
int insCount = values.length; |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1687 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1688 |
int outargs = oldType.parameterCount(); |
4537 | 1689 |
int inargs = outargs - insCount; |
1690 |
if (inargs < 0) |
|
1691 |
throw newIllegalArgumentException("too many values to insert"); |
|
1692 |
if (pos < 0 || pos > inargs) |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1693 |
throw newIllegalArgumentException("no argument type to append"); |
4537 | 1694 |
MethodHandle result = target; |
1695 |
for (int i = 0; i < insCount; i++) { |
|
1696 |
Object value = values[i]; |
|
1697 |
Class<?> valueType = oldType.parameterType(pos+i); |
|
1698 |
value = checkValue(valueType, value); |
|
1699 |
if (pos == 0 && !valueType.isPrimitive()) { |
|
1700 |
// At least for now, make bound method handles a special case. |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1701 |
MethodHandle bmh = MethodHandleImpl.bindReceiver(result, value); |
4537 | 1702 |
if (bmh != null) { |
1703 |
result = bmh; |
|
1704 |
continue; |
|
1705 |
} |
|
1706 |
// else fall through to general adapter machinery |
|
1707 |
} |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1708 |
result = MethodHandleImpl.bindArgument(result, pos, value); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1709 |
} |
4537 | 1710 |
return result; |
1711 |
} |
|
1712 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1713 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1714 |
* Produces a method handle which calls the original method handle, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1715 |
* after dropping the given argument(s) at the given position. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1716 |
* The type of the new method handle will insert the given argument |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1717 |
* type(s), at that position, into the original handle's type. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1718 |
* <p> |
4537 | 1719 |
* The <i>pos</i> may range between zero and <i>N</i>, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1720 |
* where <i>N</i> is the number of argument types in <i>target</i>, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1721 |
* meaning to drop the first or last argument (respectively), |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1722 |
* or an argument somewhere in between. |
4537 | 1723 |
* <p> |
1724 |
* <b>Example:</b> |
|
1725 |
* <p><blockquote><pre> |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1726 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1727 |
import static java.lang.invoke.MethodType.*; |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1728 |
... |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1729 |
MethodHandle cat = lookup().findVirtual(String.class, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1730 |
"concat", methodType(String.class, String.class)); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1731 |
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
|
1732 |
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
|
1733 |
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
|
1734 |
assertEquals(bigType, d0.type()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1735 |
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
|
1736 |
* </pre></blockquote> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1737 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1738 |
* 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
|
1739 |
* <p><blockquote><pre> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1740 |
* {@link #dropArguments(MethodHandle,int,Class...) dropArguments}(target, pos, valueTypes.toArray(new Class[0])) |
4537 | 1741 |
* </pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1742 |
* @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
|
1743 |
* @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
|
1744 |
* @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
|
1745 |
* @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
|
1746 |
* before calling the original method handle |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1747 |
* @throws NullPointerException if the {@code target} argument is null, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1748 |
* or if the {@code valueTypes} list or any of its elements is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1749 |
* @throws IllegalArgumentException if any of the {@code valueTypes} is {@code void.class} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1750 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1751 |
public static |
4537 | 1752 |
MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) { |
1753 |
if (valueTypes.size() == 0) return target; |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1754 |
MethodType oldType = target.type(); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1755 |
int outargs = oldType.parameterCount(); |
4537 | 1756 |
int inargs = outargs + valueTypes.size(); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1757 |
if (pos < 0 || pos >= inargs) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1758 |
throw newIllegalArgumentException("no argument type to remove"); |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1759 |
ArrayList<Class<?>> ptypes = |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1760 |
new ArrayList<Class<?>>(oldType.parameterList()); |
4537 | 1761 |
ptypes.addAll(pos, valueTypes); |
1762 |
MethodType newType = MethodType.methodType(oldType.returnType(), ptypes); |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
1763 |
return MethodHandleImpl.dropArguments(target, newType, pos); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1764 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1765 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1766 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1767 |
* Produces a method handle which calls the original method handle, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1768 |
* after dropping the given argument(s) at the given position. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1769 |
* The type of the new method handle will insert the given argument |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1770 |
* type(s), at that position, into the original handle's type. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1771 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1772 |
* The <i>pos</i> may range between zero and <i>N</i>, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1773 |
* where <i>N</i> is the number of argument types in <i>target</i>, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1774 |
* meaning to drop the first or last argument (respectively), |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1775 |
* or an argument somewhere in between. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1776 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1777 |
* <b>Example:</b> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1778 |
* <p><blockquote><pre> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1779 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1780 |
import static java.lang.invoke.MethodType.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1781 |
... |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1782 |
MethodHandle cat = lookup().findVirtual(String.class, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1783 |
"concat", methodType(String.class, String.class)); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1784 |
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
|
1785 |
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
|
1786 |
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
|
1787 |
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
|
1788 |
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
|
1789 |
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
|
1790 |
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
|
1791 |
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
|
1792 |
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
|
1793 |
* </pre></blockquote> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1794 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1795 |
* 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
|
1796 |
* <p><blockquote><pre> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1797 |
* {@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
|
1798 |
* </pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1799 |
* @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
|
1800 |
* @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
|
1801 |
* @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
|
1802 |
* @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
|
1803 |
* before calling the original method handle |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1804 |
* @throws NullPointerException if the {@code target} argument is null, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1805 |
* or if the {@code valueTypes} array or any of its elements is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1806 |
* @throws IllegalArgumentException if any of the {@code valueTypes} is {@code void.class} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1807 |
*/ |
4537 | 1808 |
public static |
1809 |
MethodHandle dropArguments(MethodHandle target, int pos, Class<?>... valueTypes) { |
|
1810 |
return dropArguments(target, pos, Arrays.asList(valueTypes)); |
|
1811 |
} |
|
1812 |
||
1813 |
/** |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1814 |
* Adapts a target method handle {@code target} by pre-processing |
4537 | 1815 |
* one or more of its arguments, each with its own unary filter function, |
1816 |
* and then calling the target with each pre-processed argument |
|
1817 |
* replaced by the result of its corresponding filter function. |
|
1818 |
* <p> |
|
1819 |
* 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
|
1820 |
* specified in the elements of the {@code filters} array. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1821 |
* Null arguments in the array are ignored, and the corresponding arguments left unchanged. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1822 |
* (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
|
1823 |
* Each filter is applied to the corresponding argument of the adapter. |
4537 | 1824 |
* <p> |
1825 |
* If a filter {@code F} applies to the {@code N}th argument of |
|
1826 |
* the method handle, then {@code F} must be a method handle which |
|
1827 |
* takes exactly one argument. The type of {@code F}'s sole argument |
|
1828 |
* replaces the corresponding argument type of the target |
|
1829 |
* in the resulting adapted method handle. |
|
1830 |
* The return type of {@code F} must be identical to the corresponding |
|
1831 |
* parameter type of the target. |
|
1832 |
* <p> |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1833 |
* It is an error if there are elements of {@code filters} |
4537 | 1834 |
* 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
|
1835 |
* <b>Example:</b> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1836 |
* <p><blockquote><pre> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1837 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1838 |
import static java.lang.invoke.MethodType.*; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1839 |
... |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1840 |
MethodHandle cat = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1841 |
"concat", methodType(String.class, String.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1842 |
MethodHandle upcase = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1843 |
"toUpperCase", methodType(String.class)); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1844 |
assertEquals("xy", (String) cat.invokeExact("x", "y")); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1845 |
MethodHandle f0 = filterArguments(cat, 0, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1846 |
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1847 |
MethodHandle f1 = filterArguments(cat, 1, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1848 |
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1849 |
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1850 |
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY |
4537 | 1851 |
* </pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1852 |
* |
4537 | 1853 |
* @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
|
1854 |
* @param pos the position of the first argument to filter |
4537 | 1855 |
* @param filters method handles to call initially on filtered arguments |
1856 |
* @return method handle which incorporates the specified argument filtering logic |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1857 |
* @throws NullPointerException if the {@code target} argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1858 |
* or if the {@code filters} array is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1859 |
* @throws IllegalArgumentException if a non-null element of {@code filters} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1860 |
* does not match a corresponding argument type of {@code target} as described above, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1861 |
* or if the {@code pos+filters.length} is greater than {@code target.type().parameterCount()} |
4537 | 1862 |
*/ |
1863 |
public static |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1864 |
MethodHandle filterArguments(MethodHandle target, int pos, MethodHandle... filters) { |
4537 | 1865 |
MethodType targetType = target.type(); |
1866 |
MethodHandle adapter = target; |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1867 |
MethodType adapterType = null; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1868 |
assert((adapterType = targetType) != null); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1869 |
int maxPos = targetType.parameterCount(); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1870 |
if (pos + filters.length > maxPos) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1871 |
throw newIllegalArgumentException("too many filters"); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1872 |
int curPos = pos-1; // pre-incremented |
4537 | 1873 |
for (MethodHandle filter : filters) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1874 |
curPos += 1; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1875 |
if (filter == null) continue; // ignore null elements of filters |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1876 |
adapter = filterArgument(adapter, curPos, filter); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1877 |
assert((adapterType = adapterType.changeParameterType(curPos, filter.type().parameterType(0))) != null); |
4537 | 1878 |
} |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1879 |
assert(adapterType.equals(adapter.type())); |
4537 | 1880 |
return adapter; |
1881 |
} |
|
1882 |
||
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1883 |
/*non-public*/ static |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1884 |
MethodHandle filterArgument(MethodHandle target, int pos, MethodHandle filter) { |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1885 |
MethodType targetType = target.type(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1886 |
MethodType filterType = filter.type(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1887 |
if (filterType.parameterCount() != 1 |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1888 |
|| filterType.returnType() != targetType.parameterType(pos)) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1889 |
throw newIllegalArgumentException("target and filter types do not match", targetType, filterType); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1890 |
return MethodHandleImpl.filterArgument(target, pos, filter); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1891 |
} |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1892 |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
1893 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1894 |
* Adapts a target method handle {@code target} by post-processing |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1895 |
* its return value with a unary filter function. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1896 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1897 |
* If a filter {@code F} applies to the return value of |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1898 |
* the target method handle, then {@code F} must be a method handle which |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1899 |
* takes exactly one argument. The return type of {@code F} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1900 |
* replaces the return type of the target |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1901 |
* in the resulting adapted method handle. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1902 |
* The argument type of {@code F} must be identical to the |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1903 |
* return type of the target. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1904 |
* <b>Example:</b> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1905 |
* <p><blockquote><pre> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1906 |
import static java.lang.invoke.MethodHandles.*; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1907 |
import static java.lang.invoke.MethodType.*; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1908 |
... |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1909 |
MethodHandle cat = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1910 |
"concat", methodType(String.class, String.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1911 |
MethodHandle length = lookup().findVirtual(String.class, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1912 |
"length", methodType(int.class)); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1913 |
System.out.println((String) cat.invokeExact("x", "y")); // xy |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1914 |
MethodHandle f0 = filterReturnValue(cat, length); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1915 |
System.out.println((int) f0.invokeExact("x", "y")); // 2 |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1916 |
* </pre></blockquote> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1917 |
* @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
|
1918 |
* @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
|
1919 |
* @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
|
1920 |
* @throws NullPointerException if either argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1921 |
* @throws IllegalArgumentException if {@code filter} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1922 |
* does not match the return type of {@code target} as described above |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1923 |
*/ |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1924 |
public static |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1925 |
MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) { |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1926 |
MethodType targetType = target.type(); |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1927 |
MethodType filterType = filter.type(); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1928 |
Class<?> rtype = targetType.returnType(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1929 |
int filterValues = filterType.parameterCount(); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1930 |
if (filterValues == 0 |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1931 |
? (rtype != void.class) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1932 |
: (rtype != filterType.parameterType(0))) |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1933 |
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
|
1934 |
// result = fold( lambda(retval, arg...) { filter(retval) }, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1935 |
// lambda( arg...) { target(arg...) } ) |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1936 |
MethodType newType = targetType.changeReturnType(filterType.returnType()); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1937 |
MethodHandle result = null; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1938 |
if (AdapterMethodHandle.canCollectArguments(filterType, targetType, 0, false)) { |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1939 |
result = AdapterMethodHandle.makeCollectArguments(filter, target, 0, false); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1940 |
if (result != null) return result; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1941 |
} |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1942 |
// FIXME: Too many nodes here. |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1943 |
assert(MethodHandleNatives.workaroundWithoutRicochetFrames()); // this class is deprecated |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1944 |
MethodHandle returner = dropArguments(filter, filterValues, targetType.parameterList()); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1945 |
result = foldArguments(returner, target); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1946 |
assert(result.type().equals(newType)); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1947 |
return result; |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1948 |
} |
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1949 |
|
4537 | 1950 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1951 |
* Adapts a target method handle {@code target} by pre-processing |
4537 | 1952 |
* some of its arguments, and then calling the target with |
1953 |
* the result of the pre-processing, plus all original arguments. |
|
1954 |
* <p> |
|
1955 |
* The pre-processing is performed by a second method handle, the {@code combiner}. |
|
1956 |
* The first {@code N} arguments passed to the adapter, |
|
1957 |
* are copied to the combiner, which then produces a result. |
|
1958 |
* (Here, {@code N} is defined as the parameter count of the adapter.) |
|
1959 |
* After this, control passes to the {@code target}, with both the result |
|
1960 |
* of the combiner, and all the original incoming arguments. |
|
1961 |
* <p> |
|
1962 |
* The first argument type of the target must be identical with the |
|
1963 |
* return type of the combiner. |
|
1964 |
* The resulting adapter is the same type as the target, except that the |
|
1965 |
* initial argument type of the target is dropped. |
|
1966 |
* <p> |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1967 |
* (Note that {@link #dropArguments(MethodHandle,int,List) dropArguments} can be used to remove any arguments |
4537 | 1968 |
* that either the {@code combiner} or {@code target} does not wish to receive. |
1969 |
* 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
|
1970 |
* consider using {@link MethodHandle#asCollector asCollector} instead, since those |
4537 | 1971 |
* arguments will not need to be live on the stack on entry to the |
1972 |
* target.) |
|
1973 |
* <p> |
|
1974 |
* The first argument of the target must be identical with the |
|
1975 |
* return value of the combiner. |
|
1976 |
* <p> Here is pseudocode for the resulting adapter: |
|
1977 |
* <blockquote><pre> |
|
1978 |
* // there are N arguments in the A sequence |
|
1979 |
* T target(V, A[N]..., B...); |
|
1980 |
* V combiner(A...); |
|
1981 |
* T adapter(A... a, B... b) { |
|
1982 |
* V v = combiner(a...); |
|
1983 |
* return target(v, a..., b...); |
|
1984 |
* } |
|
1985 |
* </pre></blockquote> |
|
1986 |
* @param target the method handle to invoke after arguments are combined |
|
1987 |
* @param combiner method handle to call initially on the incoming arguments |
|
1988 |
* @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
|
1989 |
* @throws NullPointerException if either argument is null |
4537 | 1990 |
* @throws IllegalArgumentException if the first argument type of |
1991 |
* {@code target} is not the same as {@code combiner}'s return type, |
|
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
1992 |
* or if the following argument types of {@code target} |
4537 | 1993 |
* are not identical with the argument types of {@code combiner} |
1994 |
*/ |
|
1995 |
public static |
|
1996 |
MethodHandle foldArguments(MethodHandle target, MethodHandle combiner) { |
|
1997 |
MethodType targetType = target.type(); |
|
1998 |
MethodType combinerType = combiner.type(); |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1999 |
int foldPos = 0; // always at the head, at present |
4537 | 2000 |
int foldArgs = combinerType.parameterCount(); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2001 |
int foldVals = combinerType.returnType() == void.class ? 0 : 1; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2002 |
int afterInsertPos = foldPos + foldVals; |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2003 |
boolean ok = (targetType.parameterCount() >= afterInsertPos + foldArgs); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2004 |
if (ok && !(combinerType.parameterList() |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2005 |
.equals(targetType.parameterList().subList(afterInsertPos, |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2006 |
afterInsertPos + foldArgs)))) |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2007 |
ok = false; |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2008 |
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
|
2009 |
ok = false; |
4537 | 2010 |
if (!ok) |
2011 |
throw misMatchedTypes("target and combiner types", targetType, combinerType); |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2012 |
MethodType newType = targetType.dropParameterTypes(foldPos, afterInsertPos); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2013 |
MethodHandle res = MethodHandleImpl.foldArguments(target, newType, foldPos, combiner); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2014 |
if (res == null) throw newIllegalArgumentException("cannot fold from "+newType+" to " +targetType); |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
2015 |
return res; |
4537 | 2016 |
} |
2017 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2018 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2019 |
* 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
|
2020 |
* 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
|
2021 |
* 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
|
2022 |
* All three method handles must have the same corresponding |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2023 |
* argument and return types, except that the return type |
4537 | 2024 |
* of the test must be boolean, and the test is allowed |
2025 |
* 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
|
2026 |
* <p> Here is pseudocode for the resulting adapter: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2027 |
* <blockquote><pre> |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2028 |
* boolean test(A...); |
4537 | 2029 |
* T target(A...,B...); |
2030 |
* T fallback(A...,B...); |
|
2031 |
* T adapter(A... a,B... b) { |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2032 |
* if (test(a...)) |
4537 | 2033 |
* return target(a..., b...); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2034 |
* else |
4537 | 2035 |
* return fallback(a..., b...); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2036 |
* } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2037 |
* </pre></blockquote> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2038 |
* 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
|
2039 |
* 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
|
2040 |
* 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
|
2041 |
* @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
|
2042 |
* @param target method handle to call if test passes |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2043 |
* @param fallback method handle to call if test fails |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2044 |
* @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
|
2045 |
* @throws NullPointerException if any argument is null |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2046 |
* @throws IllegalArgumentException if {@code test} does not return boolean, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2047 |
* or if all three method types do not match (with the return |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2048 |
* type of {@code test} changed to match that of {@code target}). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2049 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2050 |
public static |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2051 |
MethodHandle guardWithTest(MethodHandle test, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2052 |
MethodHandle target, |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2053 |
MethodHandle fallback) { |
4537 | 2054 |
MethodType gtype = test.type(); |
2055 |
MethodType ttype = target.type(); |
|
2056 |
MethodType ftype = fallback.type(); |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2057 |
if (!ttype.equals(ftype)) |
4537 | 2058 |
throw misMatchedTypes("target and fallback types", ttype, ftype); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2059 |
if (gtype.returnType() != boolean.class) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2060 |
throw newIllegalArgumentException("guard type is not a predicate "+gtype); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2061 |
List<Class<?>> targs = ttype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2062 |
List<Class<?>> gargs = gtype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2063 |
if (!targs.equals(gargs)) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2064 |
int gpc = gargs.size(), tpc = targs.size(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2065 |
if (gpc >= tpc || !targs.subList(0, gpc).equals(gargs)) |
4537 | 2066 |
throw misMatchedTypes("target and test types", ttype, gtype); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2067 |
test = dropArguments(test, gpc, targs.subList(gpc, tpc)); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2068 |
gtype = test.type(); |
4537 | 2069 |
} |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2070 |
return MethodHandleImpl.makeGuardWithTest(test, target, fallback); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2071 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2072 |
|
4537 | 2073 |
static RuntimeException misMatchedTypes(String what, MethodType t1, MethodType t2) { |
2074 |
return newIllegalArgumentException(what + " must match: " + t1 + " != " + t2); |
|
2075 |
} |
|
2076 |
||
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2077 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2078 |
* Makes a method handle which adapts a target method handle, |
4537 | 2079 |
* by running it inside an exception handler. |
2080 |
* If the target returns normally, the adapter returns that value. |
|
2081 |
* If an exception matching the specified type is thrown, the fallback |
|
2082 |
* 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
|
2083 |
* <p> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2084 |
* The target and handler must have the same corresponding |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2085 |
* 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
|
2086 |
* (similarly to the predicate in {@link #guardWithTest guardWithTest}). |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2087 |
* 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
|
2088 |
* <p> Here is pseudocode for the resulting adapter: |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2089 |
* <blockquote><pre> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2090 |
* T target(A..., B...); |
4537 | 2091 |
* T handler(ExType, A...); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2092 |
* T adapter(A... a, B... b) { |
4537 | 2093 |
* try { |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2094 |
* return target(a..., b...); |
4537 | 2095 |
* } catch (ExType ex) { |
2096 |
* return handler(ex, a...); |
|
2097 |
* } |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2098 |
* } |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2099 |
* </pre></blockquote> |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2100 |
* 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
|
2101 |
* 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
|
2102 |
* 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
|
2103 |
* <p> |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2104 |
* 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
|
2105 |
* always throws. (This might happen, for instance, because the handler |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2106 |
* is simulating a {@code finally} clause). |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2107 |
* 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
|
2108 |
* with {@link #throwException throwException}, |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2109 |
* in order to create a method handle of the correct return type. |
4537 | 2110 |
* @param target method handle to call |
2111 |
* @param exType the type of exception which the handler will catch |
|
2112 |
* @param handler method handle to call if a matching exception is thrown |
|
2113 |
* @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
|
2114 |
* @throws NullPointerException if any argument is null |
4537 | 2115 |
* @throws IllegalArgumentException if {@code handler} does not accept |
2116 |
* the given exception type, or if the method handle types do |
|
2117 |
* not match in their return types and their |
|
2118 |
* corresponding parameters |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2119 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2120 |
public static |
4537 | 2121 |
MethodHandle catchException(MethodHandle target, |
2122 |
Class<? extends Throwable> exType, |
|
2123 |
MethodHandle handler) { |
|
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2124 |
MethodType ttype = target.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2125 |
MethodType htype = handler.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2126 |
if (htype.parameterCount() < 1 || |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2127 |
!htype.parameterType(0).isAssignableFrom(exType)) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2128 |
throw newIllegalArgumentException("handler does not accept exception type "+exType); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2129 |
if (htype.returnType() != ttype.returnType()) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2130 |
throw misMatchedTypes("target and handler return types", ttype, htype); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2131 |
List<Class<?>> targs = ttype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2132 |
List<Class<?>> hargs = htype.parameterList(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2133 |
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
|
2134 |
if (!targs.equals(hargs)) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2135 |
int hpc = hargs.size(), tpc = targs.size(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2136 |
if (hpc >= tpc || !targs.subList(0, hpc).equals(hargs)) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2137 |
throw misMatchedTypes("target and handler types", ttype, htype); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2138 |
handler = dropArguments(handler, hpc, hargs.subList(hpc, tpc)); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2139 |
htype = handler.type(); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2140 |
} |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2141 |
return MethodHandleImpl.makeGuardWithCatch(target, exType, handler); |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2142 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2143 |
|
4537 | 2144 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2145 |
* Produces a method handle which will throw exceptions of the given {@code exType}. |
4537 | 2146 |
* The method handle will accept a single argument of {@code exType}, |
2147 |
* and immediately throw it as an exception. |
|
2148 |
* The method type will nominally specify a return of {@code returnType}. |
|
2149 |
* The return type may be anything convenient: It doesn't matter to the |
|
2150 |
* method handle's behavior, since it will never return normally. |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2151 |
* @return method handle which can throw the given exceptions |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2152 |
* @throws NullPointerException if either argument is null |
4537 | 2153 |
*/ |
2154 |
public static |
|
2155 |
MethodHandle throwException(Class<?> returnType, Class<? extends Throwable> exType) { |
|
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2156 |
return MethodHandleImpl.throwException(MethodType.methodType(returnType, exType)); |
4537 | 2157 |
} |
7053 | 2158 |
|
2159 |
/** |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2160 |
* Produces an instance of the given single-method interface which redirects |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2161 |
* its calls to the given method handle. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2162 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2163 |
* A single-method interface is an interface which declares a unique method. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2164 |
* When determining the unique method of a single-method interface, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2165 |
* the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode}) |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2166 |
* are disregarded. For example, {@link java.util.Comparator} is a single-method interface, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2167 |
* even though it re-declares the {@code Object.equals} method. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2168 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2169 |
* The type must be public. No additional access checks are performed. |
7053 | 2170 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2171 |
* The resulting instance of the required type will respond to |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2172 |
* invocation of the type's single abstract method by calling |
7053 | 2173 |
* the given {@code target} on the incoming arguments, |
2174 |
* and returning or throwing whatever the {@code target} |
|
2175 |
* returns or throws. The invocation will be as if by |
|
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
2176 |
* {@code target.invoke}. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2177 |
* The target's type will be checked before the |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2178 |
* instance is created, as if by a call to {@code asType}, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2179 |
* which may result in a {@code WrongMethodTypeException}. |
7053 | 2180 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2181 |
* The wrapper instance will implement the requested interface |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2182 |
* and its super-types, but no other single-method interfaces. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2183 |
* This means that the instance will not unexpectedly |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2184 |
* pass an {@code instanceof} test for any unrequested type. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2185 |
* <p style="font-size:smaller;"> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2186 |
* <em>Implementation Note:</em> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2187 |
* Therefore, each instance must implement a unique single-method interface. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2188 |
* Implementations may not bundle together |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2189 |
* multiple single-method interfaces onto single implementation classes |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2190 |
* in the style of {@link java.awt.AWTEventMulticaster}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2191 |
* <p> |
7053 | 2192 |
* The method handle may throw an <em>undeclared exception</em>, |
2193 |
* which means any checked exception (or other checked throwable) |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2194 |
* not declared by the requested type's single abstract method. |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2195 |
* If this happens, the throwable will be wrapped in an instance of |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2196 |
* {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2197 |
* and thrown in that wrapped form. |
7053 | 2198 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2199 |
* Like {@link java.lang.Integer#valueOf Integer.valueOf}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2200 |
* {@code asInstance} is a factory method whose results are defined |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2201 |
* by their behavior. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2202 |
* It is not guaranteed to return a new instance for every call. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2203 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2204 |
* Because of the possibility of {@linkplain java.lang.reflect.Method#isBridge bridge methods} |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2205 |
* and other corner cases, the interface may also have several abstract methods |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2206 |
* with the same name but having distinct descriptors (types of returns and parameters). |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2207 |
* In this case, all the methods are bound in common to the one given {@code target}. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2208 |
* The type check and effective {@code asType} conversion is applied to each |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2209 |
* method type descriptor, and all abstract methods are bound to the {@code target} in common. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2210 |
* Beyond this type check, no further checks are made to determine that the |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2211 |
* abstract methods are related in any way. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2212 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2213 |
* Future versions of this API may accept additional types, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2214 |
* such as abstract classes with single abstract methods. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2215 |
* Future versions of this API may also equip wrapper instances |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2216 |
* with one or more additional public "marker" interfaces. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2217 |
* |
7053 | 2218 |
* @param target the method handle to invoke from the wrapper |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2219 |
* @param smType the desired type of the wrapper, a single-method interface |
7053 | 2220 |
* @return a correctly-typed wrapper for the given {@code target} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2221 |
* @throws NullPointerException if either argument is null |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2222 |
* @throws IllegalArgumentException if the {@code smType} is not a |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2223 |
* valid argument to this method |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2224 |
* @throws WrongMethodTypeException if the {@code target} cannot |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2225 |
* be converted to the type required by the requested interface |
7053 | 2226 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2227 |
// Other notes to implementors: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2228 |
// <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2229 |
// No stable mapping is promised between the single-method interface and |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2230 |
// the implementation class C. Over time, several implementation |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2231 |
// classes might be used for the same type. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2232 |
// <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2233 |
// If the implementation is able |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2234 |
// to prove that a wrapper of the required type |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2235 |
// has already been created for a given |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2236 |
// method handle, or for another method handle with the |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2237 |
// same behavior, the implementation may return that wrapper in place of |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2238 |
// a new wrapper. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2239 |
// <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2240 |
// This method is designed to apply to common use cases |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2241 |
// where a single method handle must interoperate with |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2242 |
// an interface that implements a function-like |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2243 |
// API. Additional variations, such as single-abstract-method classes with |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2244 |
// private constructors, or interfaces with multiple but related |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2245 |
// entry points, must be covered by hand-written or automatically |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2246 |
// generated adapter classes. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2247 |
// |
7053 | 2248 |
public static |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2249 |
<T> T asInstance(final MethodHandle target, final Class<T> smType) { |
7053 | 2250 |
// POC implementation only; violates the above contract several ways |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2251 |
final Method sm = getSingleMethod(smType); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2252 |
if (sm == null) |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2253 |
throw new IllegalArgumentException("not a single-method interface: "+smType.getName()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2254 |
MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2255 |
MethodHandle checkTarget = target.asType(smMT); // make throw WMT |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2256 |
checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class)); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2257 |
final MethodHandle vaTarget = checkTarget.asSpreader(Object[].class, smMT.parameterCount()); |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2258 |
return smType.cast(Proxy.newProxyInstance( |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2259 |
smType.getClassLoader(), |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2260 |
new Class[]{ smType, WrapperInstance.class }, |
7053 | 2261 |
new InvocationHandler() { |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2262 |
private Object getArg(String name) { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2263 |
if ((Object)name == "getWrapperInstanceTarget") return target; |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2264 |
if ((Object)name == "getWrapperInstanceType") return smType; |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2265 |
throw new AssertionError(); |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2266 |
} |
7053 | 2267 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2268 |
if (method.getDeclaringClass() == WrapperInstance.class) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2269 |
return getArg(method.getName()); |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2270 |
if (method.equals(sm)) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2271 |
return vaTarget.invokeExact(args); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2272 |
if (isObjectMethod(method)) |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2273 |
return callObjectMethod(this, method, args); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2274 |
throw new InternalError(); |
7053 | 2275 |
} |
2276 |
})); |
|
2277 |
} |
|
2278 |
||
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2279 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2280 |
* Determines if the given object was produced by a call to {@link #asInstance asInstance}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2281 |
* @param x any reference |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2282 |
* @return true if the reference is not null and points to an object produced by {@code asInstance} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2283 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2284 |
public static |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2285 |
boolean isWrapperInstance(Object x) { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2286 |
return x instanceof WrapperInstance; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2287 |
} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2288 |
|
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2289 |
private static WrapperInstance asWrapperInstance(Object x) { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2290 |
try { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2291 |
if (x != null) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2292 |
return (WrapperInstance) x; |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2293 |
} catch (ClassCastException ex) { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2294 |
} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2295 |
throw new IllegalArgumentException("not a wrapper instance"); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2296 |
} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2297 |
|
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2298 |
/** |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2299 |
* Produces or recovers a target method handle which is behaviorally |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2300 |
* equivalent to the unique method of this wrapper instance. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2301 |
* The object {@code x} must have been produced by a call to {@link #asInstance asInstance}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2302 |
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2303 |
* @param x any reference |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2304 |
* @return a method handle implementing the unique method |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2305 |
* @throws IllegalArgumentException if the reference x is not to a wrapper instance |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2306 |
*/ |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2307 |
public static |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2308 |
MethodHandle wrapperInstanceTarget(Object x) { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2309 |
return asWrapperInstance(x).getWrapperInstanceTarget(); |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2310 |
} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2311 |
|
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2312 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2313 |
* Recovers the unique single-method interface type for which this wrapper instance was created. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2314 |
* The object {@code x} must have been produced by a call to {@link #asInstance asInstance}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2315 |
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2316 |
* @param x any reference |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2317 |
* @return the single-method interface type for which the wrapper was created |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2318 |
* @throws IllegalArgumentException if the reference x is not to a wrapper instance |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2319 |
*/ |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2320 |
public static |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2321 |
Class<?> wrapperInstanceType(Object x) { |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
2322 |
return asWrapperInstance(x).getWrapperInstanceType(); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2323 |
} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
2324 |
|
7053 | 2325 |
private static |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2326 |
boolean isObjectMethod(Method m) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2327 |
switch (m.getName()) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2328 |
case "toString": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2329 |
return (m.getReturnType() == String.class |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2330 |
&& m.getParameterTypes().length == 0); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2331 |
case "hashCode": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2332 |
return (m.getReturnType() == int.class |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2333 |
&& m.getParameterTypes().length == 0); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2334 |
case "equals": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2335 |
return (m.getReturnType() == boolean.class |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2336 |
&& m.getParameterTypes().length == 1 |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2337 |
&& m.getParameterTypes()[0] == Object.class); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2338 |
} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2339 |
return false; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2340 |
} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2341 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2342 |
private static |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2343 |
Object callObjectMethod(Object self, Method m, Object[] args) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2344 |
assert(isObjectMethod(m)) : m; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2345 |
switch (m.getName()) { |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2346 |
case "toString": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2347 |
return self.getClass().getName() + "@" + Integer.toHexString(self.hashCode()); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2348 |
case "hashCode": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2349 |
return System.identityHashCode(self); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2350 |
case "equals": |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2351 |
return (self == args[0]); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2352 |
} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2353 |
return null; |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2354 |
} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2355 |
|
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7555
diff
changeset
|
2356 |
private static |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2357 |
Method getSingleMethod(Class<?> smType) { |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2358 |
Method sm = null; |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2359 |
for (Method m : smType.getMethods()) { |
7053 | 2360 |
int mod = m.getModifiers(); |
2361 |
if (Modifier.isAbstract(mod)) { |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2362 |
if (sm != null && !isObjectMethod(sm)) |
7053 | 2363 |
return null; // too many abstract methods |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2364 |
sm = m; |
7053 | 2365 |
} |
2366 |
} |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2367 |
if (!smType.isInterface() && getSingleConstructor(smType) == null) |
7053 | 2368 |
return null; // wrong kind of constructor |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2369 |
return sm; |
7053 | 2370 |
} |
2371 |
||
2372 |
private static |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2373 |
Constructor getSingleConstructor(Class<?> smType) { |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
2374 |
for (Constructor c : smType.getDeclaredConstructors()) { |
7053 | 2375 |
if (c.getParameterTypes().length == 0) { |
2376 |
int mod = c.getModifiers(); |
|
2377 |
if (Modifier.isPublic(mod) || Modifier.isProtected(mod)) |
|
2378 |
return c; |
|
2379 |
} |
|
2380 |
} |
|
2381 |
return null; |
|
2382 |
} |
|
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2383 |
|
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2384 |
/*non-public*/ |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
2385 |
static MethodHandle asVarargsCollector(MethodHandle target, Class<?> arrayType) { |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8349
diff
changeset
|
2386 |
return MethodHandleImpl.asVarargsCollector(target, arrayType); |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7053
diff
changeset
|
2387 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
2388 |
} |