src/jdk/internal/dynalink/support/Lookup.java

changeset 90
5a820fb11814
child 101
f8221ce53c2e
equal deleted inserted replaced
89:43e32b36153c 90:5a820fb11814
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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.
24 */
25
26 /*
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file, and Oracle licenses the original version of this file under the BSD
31 * license:
32 */
33 /*
34 Copyright 2009-2013 Attila Szegedi
35
36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
37 and the BSD License (the "BSD License"), with licensee being free to
38 choose either of the two at their discretion.
39
40 You may not use this file except in compliance with either the Apache
41 License or the BSD License.
42
43 If you choose to use this file in compliance with the Apache License, the
44 following notice applies to you:
45
46 You may obtain a copy of the Apache License at
47
48 http://www.apache.org/licenses/LICENSE-2.0
49
50 Unless required by applicable law or agreed to in writing, software
51 distributed under the License is distributed on an "AS IS" BASIS,
52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
53 implied. See the License for the specific language governing
54 permissions and limitations under the License.
55
56 If you choose to use this file in compliance with the BSD License, the
57 following notice applies to you:
58
59 Redistribution and use in source and binary forms, with or without
60 modification, are permitted provided that the following conditions are
61 met:
62 * Redistributions of source code must retain the above copyright
63 notice, this list of conditions and the following disclaimer.
64 * Redistributions in binary form must reproduce the above copyright
65 notice, this list of conditions and the following disclaimer in the
66 documentation and/or other materials provided with the distribution.
67 * Neither the name of the copyright holder nor the names of
68 contributors may be used to endorse or promote products derived from
69 this software without specific prior written permission.
70
71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 */
83
84 package jdk.internal.dynalink.support;
85
86 import java.lang.invoke.MethodHandle;
87 import java.lang.invoke.MethodHandles;
88 import java.lang.invoke.MethodType;
89 import java.lang.reflect.Constructor;
90 import java.lang.reflect.Field;
91 import java.lang.reflect.Method;
92 import java.lang.reflect.Modifier;
93
94 /**
95 * A wrapper around MethodHandles.Lookup that masks checked exceptions in those cases when you're looking up methods
96 * within your own codebase (therefore it is an error if they are not present).
97 *
98 * @author Attila Szegedi
99 */
100 public class Lookup {
101 private final MethodHandles.Lookup lookup;
102
103 /**
104 * Creates a new instance, bound to an instance of {@link java.lang.invoke.MethodHandles.Lookup}.
105 *
106 * @param lookup the {@link java.lang.invoke.MethodHandles.Lookup} it delegates to.
107 */
108 public Lookup(MethodHandles.Lookup lookup) {
109 this.lookup = lookup;
110 }
111
112 /**
113 * A canonical Lookup object that wraps {@link MethodHandles#publicLookup()}.
114 */
115 public static final Lookup PUBLIC = new Lookup(MethodHandles.publicLookup());
116
117 /**
118 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflect(Method)}, converting any encountered
119 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
120 *
121 * @param m the method to unreflect
122 * @return the unreflected method handle.
123 */
124 public MethodHandle unreflect(Method m) {
125 try {
126 return lookup.unreflect(m);
127 } catch(IllegalAccessException e) {
128 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect method " + m);
129 ee.initCause(e);
130 throw ee;
131 }
132 }
133
134
135 /**
136 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter(Field)}, converting any encountered
137 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
138 *
139 * @param f the field for which a getter is unreflected
140 * @return the unreflected field getter handle.
141 */
142 public MethodHandle unreflectGetter(Field f) {
143 try {
144 return lookup.unreflectGetter(f);
145 } catch(IllegalAccessException e) {
146 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect getter for field " + f);
147 ee.initCause(e);
148 throw ee;
149 }
150 }
151
152 /**
153 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#findGetter(Class, String, Class)}, converting any
154 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and {@link NoSuchFieldException}
155 * into a {@link NoSuchFieldError}.
156 *
157 * @param refc the class declaring the field
158 * @param name the name of the field
159 * @param type the type of the field
160 * @return the unreflected field getter handle.
161 * @throws IllegalAccessError if the field is inaccessible.
162 * @throws NoSuchFieldError if the field does not exist.
163 */
164 public MethodHandle findGetter(Class<?>refc, String name, Class<?> type) {
165 try {
166 return lookup.findGetter(refc, name, type);
167 } catch(IllegalAccessException e) {
168 final IllegalAccessError ee = new IllegalAccessError("Failed to access getter for field " + refc.getName() +
169 "." + name + " of type " + type.getName());
170 ee.initCause(e);
171 throw ee;
172 } catch(NoSuchFieldException e) {
173 final NoSuchFieldError ee = new NoSuchFieldError("Failed to find getter for field " + refc.getName() +
174 "." + name + " of type " + type.getName());
175 ee.initCause(e);
176 throw ee;
177 }
178 }
179
180 /**
181 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter(Field)}, converting any encountered
182 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
183 *
184 * @param f the field for which a setter is unreflected
185 * @return the unreflected field setter handle.
186 */
187 public MethodHandle unreflectSetter(Field f) {
188 try {
189 return lookup.unreflectSetter(f);
190 } catch(IllegalAccessException e) {
191 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect setter for field " + f);
192 ee.initCause(e);
193 throw ee;
194 }
195 }
196
197 /**
198 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor(Constructor)}, converting any
199 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError}.
200 *
201 * @param c the constructor to unreflect
202 * @return the unreflected constructor handle.
203 */
204 public MethodHandle unreflectConstructor(Constructor<?> c) {
205 try {
206 return lookup.unreflectConstructor(c);
207 } catch(IllegalAccessException e) {
208 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect constructor " + c);
209 ee.initCause(e);
210 throw ee;
211 }
212 }
213
214 /**
215 * Performs a findSpecial on the underlying lookup, except for the backport where it rather uses unreflect. Converts
216 * any encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and a
217 * {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
218 *
219 * @param declaringClass class declaring the method
220 * @param name the name of the method
221 * @param type the type of the method
222 * @return a method handle for the method
223 * @throws IllegalAccessError if the method is inaccessible.
224 * @throws NoSuchMethodError if the method does not exist.
225 */
226 public MethodHandle findSpecial(Class<?> declaringClass, String name, MethodType type) {
227 try {
228 if(Backport.inUse) {
229 final Method m = declaringClass.getDeclaredMethod(name, type.parameterArray());
230 if(!Modifier.isPublic(declaringClass.getModifiers()) || !Modifier.isPublic(m.getModifiers())) {
231 m.setAccessible(true);
232 }
233 return unreflect(m);
234 } else {
235 return lookup.findSpecial(declaringClass, name, type, declaringClass);
236 }
237 } catch(IllegalAccessException e) {
238 final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription(
239 declaringClass, name, type));
240 ee.initCause(e);
241 throw ee;
242 } catch(NoSuchMethodException e) {
243 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find special method " + methodDescription(
244 declaringClass, name, type));
245 ee.initCause(e);
246 throw ee;
247 }
248 }
249
250 private static String methodDescription(Class<?> declaringClass, String name, MethodType type) {
251 return declaringClass.getName() + "#" + name + type;
252 }
253
254 /**
255 * Performs a findStatic on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
256 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
257 *
258 * @param declaringClass class declaring the method
259 * @param name the name of the method
260 * @param type the type of the method
261 * @return a method handle for the method
262 * @throws IllegalAccessError if the method is inaccessible.
263 * @throws NoSuchMethodError if the method does not exist.
264 */
265 public MethodHandle findStatic(Class<?> declaringClass, String name, MethodType type) {
266 try {
267 return lookup.findStatic(declaringClass, name, type);
268 } catch(IllegalAccessException e) {
269 final IllegalAccessError ee = new IllegalAccessError("Failed to access static method " + methodDescription(
270 declaringClass, name, type));
271 ee.initCause(e);
272 throw ee;
273 } catch(NoSuchMethodException e) {
274 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find static method " + methodDescription(
275 declaringClass, name, type));
276 ee.initCause(e);
277 throw ee;
278 }
279 }
280
281 /**
282 * Performs a findVirtual on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
283 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
284 *
285 * @param declaringClass class declaring the method
286 * @param name the name of the method
287 * @param type the type of the method
288 * @return a method handle for the method
289 * @throws IllegalAccessError if the method is inaccessible.
290 * @throws NoSuchMethodError if the method does not exist.
291 */
292 public MethodHandle findVirtual(Class<?> declaringClass, String name, MethodType type) {
293 try {
294 return lookup.findVirtual(declaringClass, name, type);
295 } catch(IllegalAccessException e) {
296 final IllegalAccessError ee = new IllegalAccessError("Failed to access virtual method " + methodDescription(
297 declaringClass, name, type));
298 ee.initCause(e);
299 throw ee;
300 } catch(NoSuchMethodException e) {
301 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find virtual method " + methodDescription(
302 declaringClass, name, type));
303 ee.initCause(e);
304 throw ee;
305 }
306 }
307
308 /**
309 * Given a lookup, finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class.
310 * Useful in classes' code for convenient linking to their own privates.
311 * @param lookup the lookup for the class
312 * @param name the name of the method
313 * @param rtype the return type of the method
314 * @param ptypes the parameter types of the method
315 * @return the method handle for the method
316 */
317 public static MethodHandle findOwnSpecial(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
318 return new Lookup(lookup).findOwnSpecial(name, rtype, ptypes);
319 }
320
321
322 /**
323 * Finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
324 * code for convenient linking to their own privates. It's easier to use than {@code findSpecial} in that you can
325 * just list the parameter types, and don't have to specify lookup class.
326 * @param name the name of the method
327 * @param rtype the return type of the method
328 * @param ptypes the parameter types of the method
329 * @return the method handle for the method
330 */
331 public MethodHandle findOwnSpecial(String name, Class<?> rtype, Class<?>... ptypes) {
332 return findSpecial(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
333 }
334
335 /**
336 * Given a lookup, finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class.
337 * Useful in classes' code for convenient linking to their own privates. It's easier to use than {@code findStatic}
338 * in that you can just list the parameter types, and don't have to specify lookup class.
339 * @param lookup the lookup for the class
340 * @param name the name of the method
341 * @param rtype the return type of the method
342 * @param ptypes the parameter types of the method
343 * @return the method handle for the method
344 */
345 public static MethodHandle findOwnStatic(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
346 return new Lookup(lookup).findOwnStatic(name, rtype, ptypes);
347 }
348
349 /**
350 * Finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
351 * code for convenient linking to their own privates. It's easier to use than {@code findStatic} in that you can
352 * just list the parameter types, and don't have to specify lookup class.
353 * @param name the name of the method
354 * @param rtype the return type of the method
355 * @param ptypes the parameter types of the method
356 * @return the method handle for the method
357 */
358 public MethodHandle findOwnStatic(String name, Class<?> rtype, Class<?>... ptypes) {
359 return findStatic(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
360 }
361 }

mercurial