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

Thu, 14 Feb 2013 13:22:26 +0100

author
attila
date
Thu, 14 Feb 2013 13:22:26 +0100
changeset 90
5a820fb11814
child 101
f8221ce53c2e
permissions
-rw-r--r--

8008085: Integrate Dynalink source code into Nashorn codebase
Reviewed-by: jlaskey, lagergren, sundar

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 import java.lang.reflect.Modifier;
attila@90 93
attila@90 94 /**
attila@90 95 * A wrapper around MethodHandles.Lookup that masks checked exceptions in those cases when you're looking up methods
attila@90 96 * within your own codebase (therefore it is an error if they are not present).
attila@90 97 *
attila@90 98 * @author Attila Szegedi
attila@90 99 */
attila@90 100 public class Lookup {
attila@90 101 private final MethodHandles.Lookup lookup;
attila@90 102
attila@90 103 /**
attila@90 104 * Creates a new instance, bound to an instance of {@link java.lang.invoke.MethodHandles.Lookup}.
attila@90 105 *
attila@90 106 * @param lookup the {@link java.lang.invoke.MethodHandles.Lookup} it delegates to.
attila@90 107 */
attila@90 108 public Lookup(MethodHandles.Lookup lookup) {
attila@90 109 this.lookup = lookup;
attila@90 110 }
attila@90 111
attila@90 112 /**
attila@90 113 * A canonical Lookup object that wraps {@link MethodHandles#publicLookup()}.
attila@90 114 */
attila@90 115 public static final Lookup PUBLIC = new Lookup(MethodHandles.publicLookup());
attila@90 116
attila@90 117 /**
attila@90 118 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflect(Method)}, converting any encountered
attila@90 119 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 120 *
attila@90 121 * @param m the method to unreflect
attila@90 122 * @return the unreflected method handle.
attila@90 123 */
attila@90 124 public MethodHandle unreflect(Method m) {
attila@90 125 try {
attila@90 126 return lookup.unreflect(m);
attila@90 127 } catch(IllegalAccessException e) {
attila@90 128 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect method " + m);
attila@90 129 ee.initCause(e);
attila@90 130 throw ee;
attila@90 131 }
attila@90 132 }
attila@90 133
attila@90 134
attila@90 135 /**
attila@90 136 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter(Field)}, converting any encountered
attila@90 137 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 138 *
attila@90 139 * @param f the field for which a getter is unreflected
attila@90 140 * @return the unreflected field getter handle.
attila@90 141 */
attila@90 142 public MethodHandle unreflectGetter(Field f) {
attila@90 143 try {
attila@90 144 return lookup.unreflectGetter(f);
attila@90 145 } catch(IllegalAccessException e) {
attila@90 146 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect getter for field " + f);
attila@90 147 ee.initCause(e);
attila@90 148 throw ee;
attila@90 149 }
attila@90 150 }
attila@90 151
attila@90 152 /**
attila@90 153 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#findGetter(Class, String, Class)}, converting any
attila@90 154 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and {@link NoSuchFieldException}
attila@90 155 * into a {@link NoSuchFieldError}.
attila@90 156 *
attila@90 157 * @param refc the class declaring the field
attila@90 158 * @param name the name of the field
attila@90 159 * @param type the type of the field
attila@90 160 * @return the unreflected field getter handle.
attila@90 161 * @throws IllegalAccessError if the field is inaccessible.
attila@90 162 * @throws NoSuchFieldError if the field does not exist.
attila@90 163 */
attila@90 164 public MethodHandle findGetter(Class<?>refc, String name, Class<?> type) {
attila@90 165 try {
attila@90 166 return lookup.findGetter(refc, name, type);
attila@90 167 } catch(IllegalAccessException e) {
attila@90 168 final IllegalAccessError ee = new IllegalAccessError("Failed to access getter for field " + refc.getName() +
attila@90 169 "." + name + " of type " + type.getName());
attila@90 170 ee.initCause(e);
attila@90 171 throw ee;
attila@90 172 } catch(NoSuchFieldException e) {
attila@90 173 final NoSuchFieldError ee = new NoSuchFieldError("Failed to find getter for field " + refc.getName() +
attila@90 174 "." + name + " of type " + type.getName());
attila@90 175 ee.initCause(e);
attila@90 176 throw ee;
attila@90 177 }
attila@90 178 }
attila@90 179
attila@90 180 /**
attila@90 181 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter(Field)}, converting any encountered
attila@90 182 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 183 *
attila@90 184 * @param f the field for which a setter is unreflected
attila@90 185 * @return the unreflected field setter handle.
attila@90 186 */
attila@90 187 public MethodHandle unreflectSetter(Field f) {
attila@90 188 try {
attila@90 189 return lookup.unreflectSetter(f);
attila@90 190 } catch(IllegalAccessException e) {
attila@90 191 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect setter for field " + f);
attila@90 192 ee.initCause(e);
attila@90 193 throw ee;
attila@90 194 }
attila@90 195 }
attila@90 196
attila@90 197 /**
attila@90 198 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor(Constructor)}, converting any
attila@90 199 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 200 *
attila@90 201 * @param c the constructor to unreflect
attila@90 202 * @return the unreflected constructor handle.
attila@90 203 */
attila@90 204 public MethodHandle unreflectConstructor(Constructor<?> c) {
attila@90 205 try {
attila@90 206 return lookup.unreflectConstructor(c);
attila@90 207 } catch(IllegalAccessException e) {
attila@90 208 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect constructor " + c);
attila@90 209 ee.initCause(e);
attila@90 210 throw ee;
attila@90 211 }
attila@90 212 }
attila@90 213
attila@90 214 /**
attila@90 215 * Performs a findSpecial on the underlying lookup, except for the backport where it rather uses unreflect. Converts
attila@90 216 * any encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and a
attila@90 217 * {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 218 *
attila@90 219 * @param declaringClass class declaring the method
attila@90 220 * @param name the name of the method
attila@90 221 * @param type the type of the method
attila@90 222 * @return a method handle for the method
attila@90 223 * @throws IllegalAccessError if the method is inaccessible.
attila@90 224 * @throws NoSuchMethodError if the method does not exist.
attila@90 225 */
attila@90 226 public MethodHandle findSpecial(Class<?> declaringClass, String name, MethodType type) {
attila@90 227 try {
attila@90 228 if(Backport.inUse) {
attila@90 229 final Method m = declaringClass.getDeclaredMethod(name, type.parameterArray());
attila@90 230 if(!Modifier.isPublic(declaringClass.getModifiers()) || !Modifier.isPublic(m.getModifiers())) {
attila@90 231 m.setAccessible(true);
attila@90 232 }
attila@90 233 return unreflect(m);
attila@90 234 } else {
attila@90 235 return lookup.findSpecial(declaringClass, name, type, declaringClass);
attila@90 236 }
attila@90 237 } catch(IllegalAccessException e) {
attila@90 238 final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription(
attila@90 239 declaringClass, name, type));
attila@90 240 ee.initCause(e);
attila@90 241 throw ee;
attila@90 242 } catch(NoSuchMethodException e) {
attila@90 243 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find special method " + methodDescription(
attila@90 244 declaringClass, name, type));
attila@90 245 ee.initCause(e);
attila@90 246 throw ee;
attila@90 247 }
attila@90 248 }
attila@90 249
attila@90 250 private static String methodDescription(Class<?> declaringClass, String name, MethodType type) {
attila@90 251 return declaringClass.getName() + "#" + name + type;
attila@90 252 }
attila@90 253
attila@90 254 /**
attila@90 255 * Performs a findStatic on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
attila@90 256 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 257 *
attila@90 258 * @param declaringClass class declaring the method
attila@90 259 * @param name the name of the method
attila@90 260 * @param type the type of the method
attila@90 261 * @return a method handle for the method
attila@90 262 * @throws IllegalAccessError if the method is inaccessible.
attila@90 263 * @throws NoSuchMethodError if the method does not exist.
attila@90 264 */
attila@90 265 public MethodHandle findStatic(Class<?> declaringClass, String name, MethodType type) {
attila@90 266 try {
attila@90 267 return lookup.findStatic(declaringClass, name, type);
attila@90 268 } catch(IllegalAccessException e) {
attila@90 269 final IllegalAccessError ee = new IllegalAccessError("Failed to access static method " + methodDescription(
attila@90 270 declaringClass, name, type));
attila@90 271 ee.initCause(e);
attila@90 272 throw ee;
attila@90 273 } catch(NoSuchMethodException e) {
attila@90 274 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find static method " + methodDescription(
attila@90 275 declaringClass, name, type));
attila@90 276 ee.initCause(e);
attila@90 277 throw ee;
attila@90 278 }
attila@90 279 }
attila@90 280
attila@90 281 /**
attila@90 282 * Performs a findVirtual on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
attila@90 283 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 284 *
attila@90 285 * @param declaringClass class declaring the method
attila@90 286 * @param name the name of the method
attila@90 287 * @param type the type of the method
attila@90 288 * @return a method handle for the method
attila@90 289 * @throws IllegalAccessError if the method is inaccessible.
attila@90 290 * @throws NoSuchMethodError if the method does not exist.
attila@90 291 */
attila@90 292 public MethodHandle findVirtual(Class<?> declaringClass, String name, MethodType type) {
attila@90 293 try {
attila@90 294 return lookup.findVirtual(declaringClass, name, type);
attila@90 295 } catch(IllegalAccessException e) {
attila@90 296 final IllegalAccessError ee = new IllegalAccessError("Failed to access virtual method " + methodDescription(
attila@90 297 declaringClass, name, type));
attila@90 298 ee.initCause(e);
attila@90 299 throw ee;
attila@90 300 } catch(NoSuchMethodException e) {
attila@90 301 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find virtual method " + methodDescription(
attila@90 302 declaringClass, name, type));
attila@90 303 ee.initCause(e);
attila@90 304 throw ee;
attila@90 305 }
attila@90 306 }
attila@90 307
attila@90 308 /**
attila@90 309 * Given a lookup, finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class.
attila@90 310 * Useful in classes' code for convenient linking to their own privates.
attila@90 311 * @param lookup the lookup for the class
attila@90 312 * @param name the name of the method
attila@90 313 * @param rtype the return type of the method
attila@90 314 * @param ptypes the parameter types of the method
attila@90 315 * @return the method handle for the method
attila@90 316 */
attila@90 317 public static MethodHandle findOwnSpecial(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 318 return new Lookup(lookup).findOwnSpecial(name, rtype, ptypes);
attila@90 319 }
attila@90 320
attila@90 321
attila@90 322 /**
attila@90 323 * Finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
attila@90 324 * code for convenient linking to their own privates. It's easier to use than {@code findSpecial} in that you can
attila@90 325 * just list the parameter types, and don't have to specify lookup class.
attila@90 326 * @param name the name of the method
attila@90 327 * @param rtype the return type of the method
attila@90 328 * @param ptypes the parameter types of the method
attila@90 329 * @return the method handle for the method
attila@90 330 */
attila@90 331 public MethodHandle findOwnSpecial(String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 332 return findSpecial(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
attila@90 333 }
attila@90 334
attila@90 335 /**
attila@90 336 * Given a lookup, finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class.
attila@90 337 * Useful in classes' code for convenient linking to their own privates. It's easier to use than {@code findStatic}
attila@90 338 * in that you can just list the parameter types, and don't have to specify lookup class.
attila@90 339 * @param lookup the lookup for the class
attila@90 340 * @param name the name of the method
attila@90 341 * @param rtype the return type of the method
attila@90 342 * @param ptypes the parameter types of the method
attila@90 343 * @return the method handle for the method
attila@90 344 */
attila@90 345 public static MethodHandle findOwnStatic(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 346 return new Lookup(lookup).findOwnStatic(name, rtype, ptypes);
attila@90 347 }
attila@90 348
attila@90 349 /**
attila@90 350 * Finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
attila@90 351 * code for convenient linking to their own privates. It's easier to use than {@code findStatic} in that you can
attila@90 352 * just list the parameter types, and don't have to specify lookup 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@90 358 public MethodHandle findOwnStatic(String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 359 return findStatic(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
attila@90 360 }
attila@90 361 }

mercurial