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