1.1 --- a/rt/emul/compact/src/main/java/java/lang/invoke/MethodHandleInfo.java Sun Aug 17 20:09:05 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,284 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.lang.invoke;
1.30 -
1.31 -import java.lang.reflect.*;
1.32 -import java.util.*;
1.33 -import java.lang.invoke.MethodHandleNatives.Constants;
1.34 -import java.lang.invoke.MethodHandles.Lookup;
1.35 -import static java.lang.invoke.MethodHandleStatics.*;
1.36 -
1.37 -/**
1.38 - * A symbolic reference obtained by cracking a direct method handle
1.39 - * into its consitutent symbolic parts.
1.40 - * To crack a direct method handle, call {@link Lookup#revealDirect Lookup.revealDirect}.
1.41 - * <h1><a name="directmh"></a>Direct Method Handles</h1>
1.42 - * A <em>direct method handle</em> represents a method, constructor, or field without
1.43 - * any intervening argument bindings or other transformations.
1.44 - * The method, constructor, or field referred to by a direct method handle is called
1.45 - * its <em>underlying member</em>.
1.46 - * Direct method handles may be obtained in any of these ways:
1.47 - * <ul>
1.48 - * <li>By executing an {@code ldc} instruction on a {@code CONSTANT_MethodHandle} constant.
1.49 - * (See the Java Virtual Machine Specification, sections 4.4.8 and 5.4.3.)
1.50 - * <li>By calling one of the <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a>,
1.51 - * such as {@link Lookup#findVirtual Lookup.findVirtual},
1.52 - * to resolve a symbolic reference into a method handle.
1.53 - * A symbolic reference consists of a class, name string, and type.
1.54 - * <li>By calling the factory method {@link Lookup#unreflect Lookup.unreflect}
1.55 - * or {@link Lookup#unreflectSpecial Lookup.unreflectSpecial}
1.56 - * to convert a {@link Method} into a method handle.
1.57 - * <li>By calling the factory method {@link Lookup#unreflectConstructor Lookup.unreflectConstructor}
1.58 - * to convert a {@link Constructor} into a method handle.
1.59 - * <li>By calling the factory method {@link Lookup#unreflectGetter Lookup.unreflectGetter}
1.60 - * or {@link Lookup#unreflectSetter Lookup.unreflectSetter}
1.61 - * to convert a {@link Field} into a method handle.
1.62 - * </ul>
1.63 - *
1.64 - * <h1>Restrictions on Cracking</h1>
1.65 - * Given a suitable {@code Lookup} object, it is possible to crack any direct method handle
1.66 - * to recover a symbolic reference for the underlying method, constructor, or field.
1.67 - * Cracking must be done via a {@code Lookup} object equivalent to that which created
1.68 - * the target method handle, or which has enough access permissions to recreate
1.69 - * an equivalent method handle.
1.70 - * <p>
1.71 - * If the underlying method is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>,
1.72 - * the direct method handle will have been "bound" to a particular caller class, the
1.73 - * {@linkplain java.lang.invoke.MethodHandles.Lookup#lookupClass() lookup class}
1.74 - * of the lookup object used to create it.
1.75 - * Cracking this method handle with a different lookup class will fail
1.76 - * even if the underlying method is public (like {@code Class.forName}).
1.77 - * <p>
1.78 - * The requirement of lookup object matching provides a "fast fail" behavior
1.79 - * for programs which may otherwise trust erroneous revelation of a method
1.80 - * handle with symbolic information (or caller binding) from an unexpected scope.
1.81 - * Use {@link java.lang.invoke.MethodHandles#reflectAs} to override this limitation.
1.82 - *
1.83 - * <h1><a name="refkinds"></a>Reference kinds</h1>
1.84 - * The <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a>
1.85 - * correspond to all major use cases for methods, constructors, and fields.
1.86 - * These use cases may be distinguished using small integers as follows:
1.87 - * <table border=1 cellpadding=5 summary="reference kinds">
1.88 - * <tr><th>reference kind</th><th>descriptive name</th><th>scope</th><th>member</th><th>behavior</th></tr>
1.89 - * <tr>
1.90 - * <td>{@code 1}</td><td>{@code REF_getField}</td><td>{@code class}</td>
1.91 - * <td>{@code FT f;}</td><td>{@code (T) this.f;}</td>
1.92 - * </tr>
1.93 - * <tr>
1.94 - * <td>{@code 2}</td><td>{@code REF_getStatic}</td><td>{@code class} or {@code interface}</td>
1.95 - * <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td>
1.96 - * </tr>
1.97 - * <tr>
1.98 - * <td>{@code 3}</td><td>{@code REF_putField}</td><td>{@code class}</td>
1.99 - * <td>{@code FT f;}</td><td>{@code this.f = x;}</td>
1.100 - * </tr>
1.101 - * <tr>
1.102 - * <td>{@code 4}</td><td>{@code REF_putStatic}</td><td>{@code class}</td>
1.103 - * <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td>
1.104 - * </tr>
1.105 - * <tr>
1.106 - * <td>{@code 5}</td><td>{@code REF_invokeVirtual}</td><td>{@code class}</td>
1.107 - * <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
1.108 - * </tr>
1.109 - * <tr>
1.110 - * <td>{@code 6}</td><td>{@code REF_invokeStatic}</td><td>{@code class} or {@code interface}</td>
1.111 - * <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td>
1.112 - * </tr>
1.113 - * <tr>
1.114 - * <td>{@code 7}</td><td>{@code REF_invokeSpecial}</td><td>{@code class} or {@code interface}</td>
1.115 - * <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td>
1.116 - * </tr>
1.117 - * <tr>
1.118 - * <td>{@code 8}</td><td>{@code REF_newInvokeSpecial}</td><td>{@code class}</td>
1.119 - * <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td>
1.120 - * </tr>
1.121 - * <tr>
1.122 - * <td>{@code 9}</td><td>{@code REF_invokeInterface}</td><td>{@code interface}</td>
1.123 - * <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td>
1.124 - * </tr>
1.125 - * </table>
1.126 - * @since 1.8
1.127 - */
1.128 -public
1.129 -interface MethodHandleInfo {
1.130 - /**
1.131 - * A direct method handle reference kind,
1.132 - * as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>.
1.133 - */
1.134 - public static final int
1.135 - REF_getField = Constants.REF_getField,
1.136 - REF_getStatic = Constants.REF_getStatic,
1.137 - REF_putField = Constants.REF_putField,
1.138 - REF_putStatic = Constants.REF_putStatic,
1.139 - REF_invokeVirtual = Constants.REF_invokeVirtual,
1.140 - REF_invokeStatic = Constants.REF_invokeStatic,
1.141 - REF_invokeSpecial = Constants.REF_invokeSpecial,
1.142 - REF_newInvokeSpecial = Constants.REF_newInvokeSpecial,
1.143 - REF_invokeInterface = Constants.REF_invokeInterface;
1.144 -
1.145 - /**
1.146 - * Returns the reference kind of the cracked method handle, which in turn
1.147 - * determines whether the method handle's underlying member was a constructor, method, or field.
1.148 - * See the <a href="MethodHandleInfo.html#refkinds">table above</a> for definitions.
1.149 - * @return the integer code for the kind of reference used to access the underlying member
1.150 - */
1.151 - public int getReferenceKind();
1.152 -
1.153 - /**
1.154 - * Returns the class in which the cracked method handle's underlying member was defined.
1.155 - * @return the declaring class of the underlying member
1.156 - */
1.157 - public Class<?> getDeclaringClass();
1.158 -
1.159 - /**
1.160 - * Returns the name of the cracked method handle's underlying member.
1.161 - * This is {@code "<init>"} if the underlying member was a constructor,
1.162 - * else it is a simple method name or field name.
1.163 - * @return the simple name of the underlying member
1.164 - */
1.165 - public String getName();
1.166 -
1.167 - /**
1.168 - * Returns the nominal type of the cracked symbolic reference, expressed as a method type.
1.169 - * If the reference is to a constructor, the return type will be {@code void}.
1.170 - * If it is to a non-static method, the method type will not mention the {@code this} parameter.
1.171 - * If it is to a field and the requested access is to read the field,
1.172 - * the method type will have no parameters and return the field type.
1.173 - * If it is to a field and the requested access is to write the field,
1.174 - * the method type will have one parameter of the field type and return {@code void}.
1.175 - * <p>
1.176 - * Note that original direct method handle may include a leading {@code this} parameter,
1.177 - * or (in the case of a constructor) will replace the {@code void} return type
1.178 - * with the constructed class.
1.179 - * The nominal type does not include any {@code this} parameter,
1.180 - * and (in the case of a constructor) will return {@code void}.
1.181 - * @return the type of the underlying member, expressed as a method type
1.182 - */
1.183 - public MethodType getMethodType();
1.184 -
1.185 - // Utility methods.
1.186 - // NOTE: class/name/type and reference kind constitute a symbolic reference
1.187 - // member and modifiers are an add-on, derived from Core Reflection (or the equivalent)
1.188 -
1.189 - /**
1.190 - * Reflects the underlying member as a method, constructor, or field object.
1.191 - * If the underlying member is public, it is reflected as if by
1.192 - * {@code getMethod}, {@code getConstructor}, or {@code getField}.
1.193 - * Otherwise, it is reflected as if by
1.194 - * {@code getDeclaredMethod}, {@code getDeclaredConstructor}, or {@code getDeclaredField}.
1.195 - * The underlying member must be accessible to the given lookup object.
1.196 - * @param <T> the desired type of the result, either {@link Member} or a subtype
1.197 - * @param expected a class object representing the desired result type {@code T}
1.198 - * @param lookup the lookup object that created this MethodHandleInfo, or one with equivalent access privileges
1.199 - * @return a reference to the method, constructor, or field object
1.200 - * @exception ClassCastException if the member is not of the expected type
1.201 - * @exception NullPointerException if either argument is {@code null}
1.202 - * @exception IllegalArgumentException if the underlying member is not accessible to the given lookup object
1.203 - */
1.204 - public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup);
1.205 -
1.206 - /**
1.207 - * Returns the access modifiers of the underlying member.
1.208 - * @return the Java language modifiers for underlying member,
1.209 - * or -1 if the member cannot be accessed
1.210 - * @see Modifier
1.211 - * @see #reflectAs
1.212 - */
1.213 - public int getModifiers();
1.214 -
1.215 - /**
1.216 - * Determines if the underlying member was a variable arity method or constructor.
1.217 - * Such members are represented by method handles that are varargs collectors.
1.218 - * @implSpec
1.219 - * This produces a result equivalent to:
1.220 - * <pre>{@code
1.221 - * getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers())
1.222 - * }</pre>
1.223 - *
1.224 - *
1.225 - * @return {@code true} if and only if the underlying member was declared with variable arity.
1.226 - */
1.227 - // spelling derived from java.lang.reflect.Executable, not MethodHandle.isVarargsCollector
1.228 - public default boolean isVarArgs() {
1.229 - // fields are never varargs:
1.230 - if (MethodHandleNatives.refKindIsField((byte) getReferenceKind()))
1.231 - return false;
1.232 - // not in the public API: Modifier.VARARGS
1.233 - final int ACC_VARARGS = 0x00000080; // from JVMS 4.6 (Table 4.20)
1.234 - assert(ACC_VARARGS == Modifier.TRANSIENT);
1.235 - return Modifier.isTransient(getModifiers());
1.236 - }
1.237 -
1.238 - /**
1.239 - * Returns the descriptive name of the given reference kind,
1.240 - * as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>.
1.241 - * The conventional prefix "REF_" is omitted.
1.242 - * @param referenceKind an integer code for a kind of reference used to access a class member
1.243 - * @return a mixed-case string such as {@code "getField"}
1.244 - * @exception IllegalArgumentException if the argument is not a valid
1.245 - * <a href="MethodHandleInfo.html#refkinds">reference kind number</a>
1.246 - */
1.247 - public static String referenceKindToString(int referenceKind) {
1.248 - if (!MethodHandleNatives.refKindIsValid(referenceKind))
1.249 - throw newIllegalArgumentException("invalid reference kind", referenceKind);
1.250 - return MethodHandleNatives.refKindName((byte)referenceKind);
1.251 - }
1.252 -
1.253 - /**
1.254 - * Returns a string representation for a {@code MethodHandleInfo},
1.255 - * given the four parts of its symbolic reference.
1.256 - * This is defined to be of the form {@code "RK C.N:MT"}, where {@code RK} is the
1.257 - * {@linkplain #referenceKindToString reference kind string} for {@code kind},
1.258 - * {@code C} is the {@linkplain java.lang.Class#getName name} of {@code defc}
1.259 - * {@code N} is the {@code name}, and
1.260 - * {@code MT} is the {@code type}.
1.261 - * These four values may be obtained from the
1.262 - * {@linkplain #getReferenceKind reference kind},
1.263 - * {@linkplain #getDeclaringClass declaring class},
1.264 - * {@linkplain #getName member name},
1.265 - * and {@linkplain #getMethodType method type}
1.266 - * of a {@code MethodHandleInfo} object.
1.267 - *
1.268 - * @implSpec
1.269 - * This produces a result equivalent to:
1.270 - * <pre>{@code
1.271 - * String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type)
1.272 - * }</pre>
1.273 - *
1.274 - * @param kind the {@linkplain #getReferenceKind reference kind} part of the symbolic reference
1.275 - * @param defc the {@linkplain #getDeclaringClass declaring class} part of the symbolic reference
1.276 - * @param name the {@linkplain #getName member name} part of the symbolic reference
1.277 - * @param type the {@linkplain #getMethodType method type} part of the symbolic reference
1.278 - * @return a string of the form {@code "RK C.N:MT"}
1.279 - * @exception IllegalArgumentException if the first argument is not a valid
1.280 - * <a href="MethodHandleInfo.html#refkinds">reference kind number</a>
1.281 - * @exception NullPointerException if any reference argument is {@code null}
1.282 - */
1.283 - public static String toString(int kind, Class<?> defc, String name, MethodType type) {
1.284 - Objects.requireNonNull(name); Objects.requireNonNull(type);
1.285 - return String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type);
1.286 - }
1.287 -}