1 /* 2 * Copyright (c) 2012, 2019, 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 */ 25 26 package java.lang.invoke; 27 28 import java.lang.reflect.*; 29 import java.util.*; 30 import java.lang.invoke.MethodHandleNatives.Constants; 31 import java.lang.invoke.MethodHandles.Lookup; 32 import static java.lang.invoke.MethodHandleStatics.*; 33 34 /** 35 * A symbolic reference obtained by cracking a direct method handle 36 * into its consitutent symbolic parts. 37 * To crack a direct method handle, call {@link Lookup#revealDirect Lookup.revealDirect}. 38 * <h2><a id="directmh"></a>Direct Method Handles</h2> 39 * A <em>direct method handle</em> represents a method, constructor, or field without 40 * any intervening argument bindings or other transformations. 41 * The method, constructor, or field referred to by a direct method handle is called 42 * its <em>underlying member</em>. 43 * Direct method handles may be obtained in any of these ways: 44 * <ul> 45 * <li>By executing an {@code ldc} instruction on a {@code CONSTANT_MethodHandle} constant. 46 * (See the Java Virtual Machine Specification, sections 4.4.8 and 5.4.3.) 47 * <li>By calling one of the <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a>, 48 * such as {@link Lookup#findVirtual Lookup.findVirtual}, 49 * to resolve a symbolic reference into a method handle. 50 * A symbolic reference consists of a class, name string, and type. 51 * <li>By calling the factory method {@link Lookup#unreflect Lookup.unreflect} 52 * or {@link Lookup#unreflectSpecial Lookup.unreflectSpecial} 53 * to convert a {@link Method} into a method handle. 54 * <li>By calling the factory method {@link Lookup#unreflectConstructor Lookup.unreflectConstructor} 55 * to convert a {@link Constructor} into a method handle. 56 * <li>By calling the factory method {@link Lookup#unreflectGetter Lookup.unreflectGetter} 57 * or {@link Lookup#unreflectSetter Lookup.unreflectSetter} 58 * to convert a {@link Field} into a method handle. 59 * </ul> 60 * 61 * <h2>Restrictions on Cracking</h2> 62 * Given a suitable {@code Lookup} object, it is possible to crack any direct method handle 63 * to recover a symbolic reference for the underlying method, constructor, or field. 64 * Cracking must be done via a {@code Lookup} object equivalent to that which created 65 * the target method handle, or which has enough access permissions to recreate 66 * an equivalent method handle. 67 * <p> 68 * If the underlying method is <a href="MethodHandles.Lookup.html#callsens">caller sensitive</a>, 69 * the direct method handle will have been "bound" to a particular caller class, the 70 * {@linkplain java.lang.invoke.MethodHandles.Lookup#lookupClass() lookup class} 71 * of the lookup object used to create it. 72 * Cracking this method handle with a different lookup class will fail 73 * even if the underlying method is public (like {@code Class.forName}). 74 * <p> 75 * The requirement of lookup object matching provides a "fast fail" behavior 76 * for programs which may otherwise trust erroneous revelation of a method 77 * handle with symbolic information (or caller binding) from an unexpected scope. 78 * Use {@link java.lang.invoke.MethodHandles#reflectAs} to override this limitation. 79 * 80 * <h2><a id="refkinds"></a>Reference kinds</h2> 81 * The <a href="MethodHandles.Lookup.html#lookups">Lookup Factory Methods</a> 82 * correspond to all major use cases for methods, constructors, and fields. 83 * These use cases may be distinguished using small integers as follows: 84 * <table class="striped"> 85 * <caption style="display:none">reference kinds</caption> 86 * <thead> 87 * <tr><th scope="col">reference kind</th><th scope="col">descriptive name</th><th scope="col">scope</th><th scope="col">member</th><th scope="col">behavior</th></tr> 88 * </thead> 89 * <tbody> 90 * <tr> 91 * <th scope="row">{@code 1}</th><td>{@code REF_getField}</td><td>{@code class}</td> 92 * <td>{@code FT f;}</td><td>{@code (T) this.f;}</td> 93 * </tr> 94 * <tr> 95 * <th scope="row">{@code 2}</th><td>{@code REF_getStatic}</td><td>{@code class} or {@code interface}</td> 96 * <td>{@code static}<br>{@code FT f;}</td><td>{@code (T) C.f;}</td> 97 * </tr> 98 * <tr> 99 * <th scope="row">{@code 3}</th><td>{@code REF_putField}</td><td>{@code class}</td> 100 * <td>{@code FT f;}</td><td>{@code this.f = x;}</td> 101 * </tr> 102 * <tr> 103 * <th scope="row">{@code 4}</th><td>{@code REF_putStatic}</td><td>{@code class}</td> 104 * <td>{@code static}<br>{@code FT f;}</td><td>{@code C.f = arg;}</td> 105 * </tr> 106 * <tr> 107 * <th scope="row">{@code 5}</th><td>{@code REF_invokeVirtual}</td><td>{@code class}</td> 108 * <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td> 109 * </tr> 110 * <tr> 111 * <th scope="row">{@code 6}</th><td>{@code REF_invokeStatic}</td><td>{@code class} or {@code interface}</td> 112 * <td>{@code static}<br>{@code T m(A*);}</td><td>{@code (T) C.m(arg*);}</td> 113 * </tr> 114 * <tr> 115 * <th scope="row">{@code 7}</th><td>{@code REF_invokeSpecial}</td><td>{@code class} or {@code interface}</td> 116 * <td>{@code T m(A*);}</td><td>{@code (T) super.m(arg*);}</td> 117 * </tr> 118 * <tr> 119 * <th scope="row">{@code 8}</th><td>{@code REF_newInvokeSpecial}</td><td>{@code class}</td> 120 * <td>{@code C(A*);}</td><td>{@code new C(arg*);}</td> 121 * </tr> 122 * <tr> 123 * <th scope="row">{@code 9}</th><td>{@code REF_invokeInterface}</td><td>{@code interface}</td> 124 * <td>{@code T m(A*);}</td><td>{@code (T) this.m(arg*);}</td> 125 * </tr> 126 * </tbody> 127 * </table> 128 * @since 1.8 129 */ 130 public interface MethodHandleInfo { 131 /** 132 * A direct method handle reference kind, 133 * as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>. 134 */ 135 public static final int 136 REF_getField = Constants.REF_getField, 137 REF_getStatic = Constants.REF_getStatic, 138 REF_putField = Constants.REF_putField, 139 REF_putStatic = Constants.REF_putStatic, 140 REF_invokeVirtual = Constants.REF_invokeVirtual, 141 REF_invokeStatic = Constants.REF_invokeStatic, 142 REF_invokeSpecial = Constants.REF_invokeSpecial, 143 REF_newInvokeSpecial = Constants.REF_newInvokeSpecial, 144 REF_invokeInterface = Constants.REF_invokeInterface; 145 146 /** 147 * Returns the reference kind of the cracked method handle, which in turn 148 * determines whether the method handle's underlying member was a constructor, method, or field. 149 * See the <a href="MethodHandleInfo.html#refkinds">table above</a> for definitions. 150 * @return the integer code for the kind of reference used to access the underlying member 151 */ 152 public int getReferenceKind(); 153 154 /** 155 * Returns the class in which the cracked method handle's underlying member was defined. 156 * @return the declaring class of the underlying member 157 */ 158 public Class<?> getDeclaringClass(); 159 160 /** 161 * Returns the name of the cracked method handle's underlying member. 162 * This is {@code "<init>"} if the underlying member was a constructor, 163 * else it is a simple method name or field name. 164 * @return the simple name of the underlying member 165 */ 166 public String getName(); 167 168 /** 169 * Returns the nominal type of the cracked symbolic reference, expressed as a method type. 170 * If the reference is to a constructor, the return type will be {@code void}. 171 * If it is to a non-static method, the method type will not mention the {@code this} parameter. 172 * If it is to a field and the requested access is to read the field, 173 * the method type will have no parameters and return the field type. 174 * If it is to a field and the requested access is to write the field, 175 * the method type will have one parameter of the field type and return {@code void}. 176 * <p> 177 * Note that original direct method handle may include a leading {@code this} parameter, 178 * or (in the case of a constructor) will replace the {@code void} return type 179 * with the constructed class. 180 * The nominal type does not include any {@code this} parameter, 181 * and (in the case of a constructor) will return {@code void}. 182 * @return the type of the underlying member, expressed as a method type 183 */ 184 public MethodType getMethodType(); 185 186 // Utility methods. 187 // NOTE: class/name/type and reference kind constitute a symbolic reference 188 // member and modifiers are an add-on, derived from Core Reflection (or the equivalent) 189 190 /** 191 * Reflects the underlying member as a method, constructor, or field object. 192 * If the underlying member is public, it is reflected as if by 193 * {@code getMethod}, {@code getConstructor}, or {@code getField}. 194 * Otherwise, it is reflected as if by 195 * {@code getDeclaredMethod}, {@code getDeclaredConstructor}, or {@code getDeclaredField}. 196 * The underlying member must be accessible to the given lookup object. 197 * @param <T> the desired type of the result, either {@link Member} or a subtype 198 * @param expected a class object representing the desired result type {@code T} 199 * @param lookup the lookup object that created this MethodHandleInfo, or one with equivalent access privileges 200 * @return a reference to the method, constructor, or field object 201 * @throws ClassCastException if the member is not of the expected type 202 * @throws NullPointerException if either argument is {@code null} 203 * @throws IllegalArgumentException if the underlying member is not accessible to the given lookup object 204 */ 205 public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup); 206 207 /** 208 * Returns the access modifiers of the underlying member. 209 * @return the Java language modifiers for underlying member, 210 * or -1 if the member cannot be accessed 211 * @see Modifier 212 * @see #reflectAs 213 */ 214 public int getModifiers(); 215 216 /** 217 * Determines if the underlying member was a variable arity method or constructor. 218 * Such members are represented by method handles that are varargs collectors. 219 * @implSpec 220 * This produces a result equivalent to: 221 * <pre>{@code 222 * getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers()) 223 * }</pre> 224 * 225 * 226 * @return {@code true} if and only if the underlying member was declared with variable arity. 227 */ 228 // spelling derived from java.lang.reflect.Executable, not MethodHandle.isVarargsCollector 229 public default boolean isVarArgs() { 230 // fields are never varargs: 231 if (MethodHandleNatives.refKindIsField((byte) getReferenceKind())) 232 return false; 233 // not in the public API: Modifier.VARARGS 234 final int ACC_VARARGS = 0x00000080; // from JVMS 4.6 (Table 4.20) 235 assert(ACC_VARARGS == Modifier.TRANSIENT); 236 return Modifier.isTransient(getModifiers()); 237 } 238 239 /** 240 * Returns the descriptive name of the given reference kind, 241 * as defined in the <a href="MethodHandleInfo.html#refkinds">table above</a>. 242 * The conventional prefix "REF_" is omitted. 243 * @param referenceKind an integer code for a kind of reference used to access a class member 244 * @return a mixed-case string such as {@code "getField"} 245 * @throws IllegalArgumentException if the argument is not a valid 246 * <a href="MethodHandleInfo.html#refkinds">reference kind number</a> 247 */ 248 public static String referenceKindToString(int referenceKind) { 249 if (!MethodHandleNatives.refKindIsValid(referenceKind)) 250 throw newIllegalArgumentException("invalid reference kind", referenceKind); 251 return MethodHandleNatives.refKindName((byte)referenceKind); 252 } 253 254 /** 255 * Returns a string representation for a {@code MethodHandleInfo}, 256 * given the four parts of its symbolic reference. 257 * This is defined to be of the form {@code "RK C.N:MT"}, where {@code RK} is the 258 * {@linkplain #referenceKindToString reference kind string} for {@code kind}, 259 * {@code C} is the {@linkplain java.lang.Class#getName name} of {@code defc} 260 * {@code N} is the {@code name}, and 261 * {@code MT} is the {@code type}. 262 * These four values may be obtained from the 263 * {@linkplain #getReferenceKind reference kind}, 264 * {@linkplain #getDeclaringClass declaring class}, 265 * {@linkplain #getName member name}, 266 * and {@linkplain #getMethodType method type} 267 * of a {@code MethodHandleInfo} object. 268 * 269 * @implSpec 270 * This produces a result equivalent to: 271 * <pre>{@code 272 * String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type) 273 * }</pre> 274 * 275 * @param kind the {@linkplain #getReferenceKind reference kind} part of the symbolic reference 276 * @param defc the {@linkplain #getDeclaringClass declaring class} part of the symbolic reference 277 * @param name the {@linkplain #getName member name} part of the symbolic reference 278 * @param type the {@linkplain #getMethodType method type} part of the symbolic reference 279 * @return a string of the form {@code "RK C.N:MT"} 280 * @throws IllegalArgumentException if the first argument is not a valid 281 * <a href="MethodHandleInfo.html#refkinds">reference kind number</a> 282 * @throws NullPointerException if any reference argument is {@code null} 283 */ 284 public static String toString(int kind, Class<?> defc, String name, MethodType type) { 285 Objects.requireNonNull(name); Objects.requireNonNull(type); 286 return String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type); 287 } 288 }