Mon, 18 Feb 2013 16:00:15 +0100
8008371: Fix Dynalink compiler warnings and whitespace
Reviewed-by: jlaskey, sundar
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 */
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
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.
40 You may not use this file except in compliance with either the Apache
41 License or the BSD License.
43 If you choose to use this file in compliance with the Apache License, the
44 following notice applies to you:
46 You may obtain a copy of the Apache License at
48 http://www.apache.org/licenses/LICENSE-2.0
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.
56 If you choose to use this file in compliance with the BSD License, the
57 following notice applies to you:
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.
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 */
84 package jdk.internal.dynalink.support;
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;
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;
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 }
112 /**
113 * A canonical Lookup object that wraps {@link MethodHandles#publicLookup()}.
114 */
115 public static final Lookup PUBLIC = new Lookup(MethodHandles.publicLookup());
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 }
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 }
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 }
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 }
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 }
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 }
235 return lookup.findSpecial(declaringClass, name, type, declaringClass);
236 } catch(IllegalAccessException e) {
237 final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription(
238 declaringClass, name, type));
239 ee.initCause(e);
240 throw ee;
241 } catch(NoSuchMethodException e) {
242 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find special method " + methodDescription(
243 declaringClass, name, type));
244 ee.initCause(e);
245 throw ee;
246 }
247 }
249 private static String methodDescription(Class<?> declaringClass, String name, MethodType type) {
250 return declaringClass.getName() + "#" + name + type;
251 }
253 /**
254 * Performs a findStatic on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
255 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
256 *
257 * @param declaringClass class declaring the method
258 * @param name the name of the method
259 * @param type the type of the method
260 * @return a method handle for the method
261 * @throws IllegalAccessError if the method is inaccessible.
262 * @throws NoSuchMethodError if the method does not exist.
263 */
264 public MethodHandle findStatic(Class<?> declaringClass, String name, MethodType type) {
265 try {
266 return lookup.findStatic(declaringClass, name, type);
267 } catch(IllegalAccessException e) {
268 final IllegalAccessError ee = new IllegalAccessError("Failed to access static method " + methodDescription(
269 declaringClass, name, type));
270 ee.initCause(e);
271 throw ee;
272 } catch(NoSuchMethodException e) {
273 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find static method " + methodDescription(
274 declaringClass, name, type));
275 ee.initCause(e);
276 throw ee;
277 }
278 }
280 /**
281 * Performs a findVirtual on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
282 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
283 *
284 * @param declaringClass class declaring the method
285 * @param name the name of the method
286 * @param type the type of the method
287 * @return a method handle for the method
288 * @throws IllegalAccessError if the method is inaccessible.
289 * @throws NoSuchMethodError if the method does not exist.
290 */
291 public MethodHandle findVirtual(Class<?> declaringClass, String name, MethodType type) {
292 try {
293 return lookup.findVirtual(declaringClass, name, type);
294 } catch(IllegalAccessException e) {
295 final IllegalAccessError ee = new IllegalAccessError("Failed to access virtual method " + methodDescription(
296 declaringClass, name, type));
297 ee.initCause(e);
298 throw ee;
299 } catch(NoSuchMethodException e) {
300 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find virtual method " + methodDescription(
301 declaringClass, name, type));
302 ee.initCause(e);
303 throw ee;
304 }
305 }
307 /**
308 * Given a lookup, finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class.
309 * Useful in classes' code for convenient linking to their own privates.
310 * @param lookup the lookup for the class
311 * @param name the name of the method
312 * @param rtype the return type of the method
313 * @param ptypes the parameter types of the method
314 * @return the method handle for the method
315 */
316 public static MethodHandle findOwnSpecial(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
317 return new Lookup(lookup).findOwnSpecial(name, rtype, ptypes);
318 }
321 /**
322 * Finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
323 * code for convenient linking to their own privates. It's easier to use than {@code findSpecial} in that you can
324 * just list the parameter types, and don't have to specify lookup class.
325 * @param name the name of the method
326 * @param rtype the return type of the method
327 * @param ptypes the parameter types of the method
328 * @return the method handle for the method
329 */
330 public MethodHandle findOwnSpecial(String name, Class<?> rtype, Class<?>... ptypes) {
331 return findSpecial(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
332 }
334 /**
335 * Given a lookup, finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class.
336 * Useful in classes' code for convenient linking to their own privates. It's easier to use than {@code findStatic}
337 * in that you can just list the parameter types, and don't have to specify lookup class.
338 * @param lookup the lookup for the class
339 * @param name the name of the method
340 * @param rtype the return type of the method
341 * @param ptypes the parameter types of the method
342 * @return the method handle for the method
343 */
344 public static MethodHandle findOwnStatic(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
345 return new Lookup(lookup).findOwnStatic(name, rtype, ptypes);
346 }
348 /**
349 * Finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
350 * code for convenient linking to their own privates. It's easier to use than {@code findStatic} in that you can
351 * just list the parameter types, and don't have to specify lookup class.
352 * @param name the name of the method
353 * @param rtype the return type of the method
354 * @param ptypes the parameter types of the method
355 * @return the method handle for the method
356 */
357 public MethodHandle findOwnStatic(String name, Class<?> rtype, Class<?>... ptypes) {
358 return findStatic(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
359 }
360 }