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

Tue, 22 Dec 2015 10:56:12 -0800

author
asaha
date
Tue, 22 Dec 2015 10:56:12 -0800
changeset 1712
e9b46178f2e3
parent 962
ac62e33a99b0
child 1205
4112748288bb
permissions
-rw-r--r--

Added tag jdk8u71-b15 for changeset c577bcadb46d

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

mercurial