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

Wed, 03 Jul 2013 12:39:28 +0200

author
attila
date
Wed, 03 Jul 2013 12:39:28 +0200
changeset 404
18d467e94150
parent 101
f8221ce53c2e
child 488
9a3e3bb30db3
permissions
-rw-r--r--

8010946: AccessControl.doPrivileged is broken when called from js script
Reviewed-by: jlaskey, 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@404 125 return unreflect(lookup, m);
attila@404 126 }
attila@404 127
attila@404 128 /**
attila@404 129 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflect(Method)}, converting any encountered
attila@404 130 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@404 131 *
attila@404 132 * @param lookup the lookup used to unreflect
attila@404 133 * @param m the method to unreflect
attila@404 134 * @return the unreflected method handle.
attila@404 135 */
attila@404 136 public static MethodHandle unreflect(MethodHandles.Lookup lookup, Method m) {
attila@90 137 try {
attila@90 138 return lookup.unreflect(m);
attila@90 139 } catch(IllegalAccessException e) {
attila@90 140 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect method " + m);
attila@90 141 ee.initCause(e);
attila@90 142 throw ee;
attila@90 143 }
attila@90 144 }
attila@90 145
attila@90 146 /**
attila@90 147 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectGetter(Field)}, converting any encountered
attila@90 148 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 149 *
attila@90 150 * @param f the field for which a getter is unreflected
attila@90 151 * @return the unreflected field getter handle.
attila@90 152 */
attila@90 153 public MethodHandle unreflectGetter(Field f) {
attila@90 154 try {
attila@90 155 return lookup.unreflectGetter(f);
attila@90 156 } catch(IllegalAccessException e) {
attila@90 157 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect getter for field " + f);
attila@90 158 ee.initCause(e);
attila@90 159 throw ee;
attila@90 160 }
attila@90 161 }
attila@90 162
attila@90 163 /**
attila@90 164 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#findGetter(Class, String, Class)}, converting any
attila@90 165 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and {@link NoSuchFieldException}
attila@90 166 * into a {@link NoSuchFieldError}.
attila@90 167 *
attila@90 168 * @param refc the class declaring the field
attila@90 169 * @param name the name of the field
attila@90 170 * @param type the type of the field
attila@90 171 * @return the unreflected field getter handle.
attila@90 172 * @throws IllegalAccessError if the field is inaccessible.
attila@90 173 * @throws NoSuchFieldError if the field does not exist.
attila@90 174 */
attila@90 175 public MethodHandle findGetter(Class<?>refc, String name, Class<?> type) {
attila@90 176 try {
attila@90 177 return lookup.findGetter(refc, name, type);
attila@90 178 } catch(IllegalAccessException e) {
attila@90 179 final IllegalAccessError ee = new IllegalAccessError("Failed to access getter for field " + refc.getName() +
attila@90 180 "." + name + " of type " + type.getName());
attila@90 181 ee.initCause(e);
attila@90 182 throw ee;
attila@90 183 } catch(NoSuchFieldException e) {
attila@90 184 final NoSuchFieldError ee = new NoSuchFieldError("Failed to find getter for field " + refc.getName() +
attila@90 185 "." + name + " of type " + type.getName());
attila@90 186 ee.initCause(e);
attila@90 187 throw ee;
attila@90 188 }
attila@90 189 }
attila@90 190
attila@90 191 /**
attila@90 192 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectSetter(Field)}, converting any encountered
attila@90 193 * {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 194 *
attila@90 195 * @param f the field for which a setter is unreflected
attila@90 196 * @return the unreflected field setter handle.
attila@90 197 */
attila@90 198 public MethodHandle unreflectSetter(Field f) {
attila@90 199 try {
attila@90 200 return lookup.unreflectSetter(f);
attila@90 201 } catch(IllegalAccessException e) {
attila@90 202 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect setter for field " + f);
attila@90 203 ee.initCause(e);
attila@90 204 throw ee;
attila@90 205 }
attila@90 206 }
attila@90 207
attila@90 208 /**
attila@90 209 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor(Constructor)}, converting any
attila@90 210 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@90 211 *
attila@90 212 * @param c the constructor to unreflect
attila@90 213 * @return the unreflected constructor handle.
attila@90 214 */
attila@90 215 public MethodHandle unreflectConstructor(Constructor<?> c) {
attila@404 216 return unreflectConstructor(lookup, c);
attila@404 217 }
attila@404 218
attila@404 219 /**
attila@404 220 * Performs a {@link java.lang.invoke.MethodHandles.Lookup#unreflectConstructor(Constructor)}, converting any
attila@404 221 * encountered {@link IllegalAccessException} into an {@link IllegalAccessError}.
attila@404 222 *
attila@404 223 * @param lookup the lookup used to unreflect
attila@404 224 * @param c the constructor to unreflect
attila@404 225 * @return the unreflected constructor handle.
attila@404 226 */
attila@404 227 public static MethodHandle unreflectConstructor(MethodHandles.Lookup lookup, Constructor<?> c) {
attila@90 228 try {
attila@90 229 return lookup.unreflectConstructor(c);
attila@90 230 } catch(IllegalAccessException e) {
attila@90 231 final IllegalAccessError ee = new IllegalAccessError("Failed to unreflect constructor " + c);
attila@90 232 ee.initCause(e);
attila@90 233 throw ee;
attila@90 234 }
attila@90 235 }
attila@90 236
attila@90 237 /**
attila@90 238 * Performs a findSpecial on the underlying lookup, except for the backport where it rather uses unreflect. Converts
attila@90 239 * any encountered {@link IllegalAccessException} into an {@link IllegalAccessError} and a
attila@90 240 * {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 241 *
attila@90 242 * @param declaringClass class declaring the method
attila@90 243 * @param name the name of the method
attila@90 244 * @param type the type of the method
attila@90 245 * @return a method handle for the method
attila@90 246 * @throws IllegalAccessError if the method is inaccessible.
attila@90 247 * @throws NoSuchMethodError if the method does not exist.
attila@90 248 */
attila@90 249 public MethodHandle findSpecial(Class<?> declaringClass, String name, MethodType type) {
attila@90 250 try {
attila@90 251 if(Backport.inUse) {
attila@90 252 final Method m = declaringClass.getDeclaredMethod(name, type.parameterArray());
attila@90 253 if(!Modifier.isPublic(declaringClass.getModifiers()) || !Modifier.isPublic(m.getModifiers())) {
attila@90 254 m.setAccessible(true);
attila@90 255 }
attila@90 256 return unreflect(m);
attila@90 257 }
attila@101 258 return lookup.findSpecial(declaringClass, name, type, declaringClass);
attila@90 259 } catch(IllegalAccessException e) {
attila@90 260 final IllegalAccessError ee = new IllegalAccessError("Failed to access special method " + methodDescription(
attila@90 261 declaringClass, name, type));
attila@90 262 ee.initCause(e);
attila@90 263 throw ee;
attila@90 264 } catch(NoSuchMethodException e) {
attila@90 265 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find special method " + methodDescription(
attila@90 266 declaringClass, name, type));
attila@90 267 ee.initCause(e);
attila@90 268 throw ee;
attila@90 269 }
attila@90 270 }
attila@90 271
attila@90 272 private static String methodDescription(Class<?> declaringClass, String name, MethodType type) {
attila@90 273 return declaringClass.getName() + "#" + name + type;
attila@90 274 }
attila@90 275
attila@90 276 /**
attila@90 277 * Performs a findStatic on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
attila@90 278 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 279 *
attila@90 280 * @param declaringClass class declaring the method
attila@90 281 * @param name the name of the method
attila@90 282 * @param type the type of the method
attila@90 283 * @return a method handle for the method
attila@90 284 * @throws IllegalAccessError if the method is inaccessible.
attila@90 285 * @throws NoSuchMethodError if the method does not exist.
attila@90 286 */
attila@90 287 public MethodHandle findStatic(Class<?> declaringClass, String name, MethodType type) {
attila@90 288 try {
attila@90 289 return lookup.findStatic(declaringClass, name, type);
attila@90 290 } catch(IllegalAccessException e) {
attila@90 291 final IllegalAccessError ee = new IllegalAccessError("Failed to access static method " + methodDescription(
attila@90 292 declaringClass, name, type));
attila@90 293 ee.initCause(e);
attila@90 294 throw ee;
attila@90 295 } catch(NoSuchMethodException e) {
attila@90 296 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find static method " + methodDescription(
attila@90 297 declaringClass, name, type));
attila@90 298 ee.initCause(e);
attila@90 299 throw ee;
attila@90 300 }
attila@90 301 }
attila@90 302
attila@90 303 /**
attila@90 304 * Performs a findVirtual on the underlying lookup. Converts any encountered {@link IllegalAccessException} into an
attila@90 305 * {@link IllegalAccessError} and a {@link NoSuchMethodException} into a {@link NoSuchMethodError}.
attila@90 306 *
attila@90 307 * @param declaringClass class declaring the method
attila@90 308 * @param name the name of the method
attila@90 309 * @param type the type of the method
attila@90 310 * @return a method handle for the method
attila@90 311 * @throws IllegalAccessError if the method is inaccessible.
attila@90 312 * @throws NoSuchMethodError if the method does not exist.
attila@90 313 */
attila@90 314 public MethodHandle findVirtual(Class<?> declaringClass, String name, MethodType type) {
attila@90 315 try {
attila@90 316 return lookup.findVirtual(declaringClass, name, type);
attila@90 317 } catch(IllegalAccessException e) {
attila@90 318 final IllegalAccessError ee = new IllegalAccessError("Failed to access virtual method " + methodDescription(
attila@90 319 declaringClass, name, type));
attila@90 320 ee.initCause(e);
attila@90 321 throw ee;
attila@90 322 } catch(NoSuchMethodException e) {
attila@90 323 final NoSuchMethodError ee = new NoSuchMethodError("Failed to find virtual method " + methodDescription(
attila@90 324 declaringClass, name, type));
attila@90 325 ee.initCause(e);
attila@90 326 throw ee;
attila@90 327 }
attila@90 328 }
attila@90 329
attila@90 330 /**
attila@90 331 * Given a lookup, finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class.
attila@90 332 * Useful in classes' code for convenient linking to their own privates.
attila@90 333 * @param lookup the lookup for the class
attila@90 334 * @param name the name of the method
attila@90 335 * @param rtype the return type of the method
attila@90 336 * @param ptypes the parameter types of the method
attila@90 337 * @return the method handle for the method
attila@90 338 */
attila@90 339 public static MethodHandle findOwnSpecial(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 340 return new Lookup(lookup).findOwnSpecial(name, rtype, ptypes);
attila@90 341 }
attila@90 342
attila@90 343
attila@90 344 /**
attila@90 345 * Finds using {@link #findSpecial(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
attila@90 346 * code for convenient linking to their own privates. It's easier to use than {@code findSpecial} in that you can
attila@90 347 * just list the parameter types, and don't have to specify lookup class.
attila@90 348 * @param name the name of the method
attila@90 349 * @param rtype the return type of the method
attila@90 350 * @param ptypes the parameter types of the method
attila@90 351 * @return the method handle for the method
attila@90 352 */
attila@90 353 public MethodHandle findOwnSpecial(String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 354 return findSpecial(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
attila@90 355 }
attila@90 356
attila@90 357 /**
attila@90 358 * Given a lookup, finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class.
attila@90 359 * Useful in classes' code for convenient linking to their own privates. It's easier to use than {@code findStatic}
attila@90 360 * in that you can just list the parameter types, and don't have to specify lookup class.
attila@90 361 * @param lookup the lookup for the class
attila@90 362 * @param name the name of the method
attila@90 363 * @param rtype the return type of the method
attila@90 364 * @param ptypes the parameter types of the method
attila@90 365 * @return the method handle for the method
attila@90 366 */
attila@90 367 public static MethodHandle findOwnStatic(MethodHandles.Lookup lookup, String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 368 return new Lookup(lookup).findOwnStatic(name, rtype, ptypes);
attila@90 369 }
attila@90 370
attila@90 371 /**
attila@90 372 * Finds using {@link #findStatic(Class, String, MethodType)} a method on that lookup's class. Useful in classes'
attila@90 373 * code for convenient linking to their own privates. It's easier to use than {@code findStatic} in that you can
attila@90 374 * just list the parameter types, and don't have to specify lookup class.
attila@90 375 * @param name the name of the method
attila@90 376 * @param rtype the return type of the method
attila@90 377 * @param ptypes the parameter types of the method
attila@90 378 * @return the method handle for the method
attila@90 379 */
attila@90 380 public MethodHandle findOwnStatic(String name, Class<?> rtype, Class<?>... ptypes) {
attila@90 381 return findStatic(lookup.lookupClass(), name, MethodType.methodType(rtype, ptypes));
attila@90 382 }
attila@90 383 }

mercurial