1 /* 2 * Copyright (c) 2008, 2012, 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.security.AccessController; 30 import java.security.PrivilegedAction; 31 import sun.invoke.WrapperInstance; 32 import java.util.ArrayList; 33 import sun.reflect.CallerSensitive; 34 import sun.reflect.Reflection; 35 import sun.reflect.misc.ReflectUtil; 36 37 /** 38 * This class consists exclusively of static methods that help adapt 39 * method handles to other JVM types, such as interfaces. 40 */ 41 public class MethodHandleProxies { 42 43 private MethodHandleProxies() { } // do not instantiate 44 45 /** 46 * Produces an instance of the given single-method interface which redirects 47 * its calls to the given method handle. 48 * <p> 49 * A single-method interface is an interface which declares a uniquely named method. 50 * When determining the uniquely named method of a single-method interface, 51 * the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode}) 52 * are disregarded. For example, {@link java.util.Comparator} is a single-method interface, 53 * even though it re-declares the {@code Object.equals} method. 54 * <p> 55 * The interface must be public. No additional access checks are performed. 56 * <p> 57 * The resulting instance of the required type will respond to 58 * invocation of the type's uniquely named method by calling 59 * the given target on the incoming arguments, 60 * and returning or throwing whatever the target 61 * returns or throws. The invocation will be as if by 62 * {@code target.invoke}. 63 * The target's type will be checked before the 64 * instance is created, as if by a call to {@code asType}, 65 * which may result in a {@code WrongMethodTypeException}. 66 * <p> 67 * The uniquely named method is allowed to be multiply declared, 68 * with distinct type descriptors. (E.g., it can be overloaded, 69 * or can possess bridge methods.) All such declarations are 70 * connected directly to the target method handle. 71 * Argument and return types are adjusted by {@code asType} 72 * for each individual declaration. 73 * <p> 74 * The wrapper instance will implement the requested interface 75 * and its super-types, but no other single-method interfaces. 76 * This means that the instance will not unexpectedly 77 * pass an {@code instanceof} test for any unrequested type. 78 * <p style="font-size:smaller;"> 79 * <em>Implementation Note:</em> 80 * Therefore, each instance must implement a unique single-method interface. 81 * Implementations may not bundle together 82 * multiple single-method interfaces onto single implementation classes 83 * in the style of {@link java.awt.AWTEventMulticaster}. 84 * <p> 85 * The method handle may throw an <em>undeclared exception</em>, 86 * which means any checked exception (or other checked throwable) 87 * not declared by the requested type's single abstract method. 88 * If this happens, the throwable will be wrapped in an instance of 89 * {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException} 90 * and thrown in that wrapped form. 91 * <p> 92 * Like {@link java.lang.Integer#valueOf Integer.valueOf}, 93 * {@code asInterfaceInstance} is a factory method whose results are defined 94 * by their behavior. 95 * It is not guaranteed to return a new instance for every call. 96 * <p> 97 * Because of the possibility of {@linkplain java.lang.reflect.Method#isBridge bridge methods} 98 * and other corner cases, the interface may also have several abstract methods 99 * with the same name but having distinct descriptors (types of returns and parameters). 100 * In this case, all the methods are bound in common to the one given target. 101 * The type check and effective {@code asType} conversion is applied to each 102 * method type descriptor, and all abstract methods are bound to the target in common. 103 * Beyond this type check, no further checks are made to determine that the 104 * abstract methods are related in any way. 105 * <p> 106 * Future versions of this API may accept additional types, 107 * such as abstract classes with single abstract methods. 108 * Future versions of this API may also equip wrapper instances 109 * with one or more additional public "marker" interfaces. 110 * 111 * @param target the method handle to invoke from the wrapper 112 * @param intfc the desired type of the wrapper, a single-method interface 113 * @return a correctly-typed wrapper for the given target 114 * @throws NullPointerException if either argument is null 115 * @throws IllegalArgumentException if the {@code intfc} is not a 116 * valid argument to this method 117 * @throws WrongMethodTypeException if the target cannot 118 * be converted to the type required by the requested interface 119 */ 120 // Other notes to implementors: 121 // <p> 122 // No stable mapping is promised between the single-method interface and 123 // the implementation class C. Over time, several implementation 124 // classes might be used for the same type. 125 // <p> 126 // If the implementation is able 127 // to prove that a wrapper of the required type 128 // has already been created for a given 129 // method handle, or for another method handle with the 130 // same behavior, the implementation may return that wrapper in place of 131 // a new wrapper. 132 // <p> 133 // This method is designed to apply to common use cases 134 // where a single method handle must interoperate with 135 // an interface that implements a function-like 136 // API. Additional variations, such as single-abstract-method classes with 137 // private constructors, or interfaces with multiple but related 138 // entry points, must be covered by hand-written or automatically 139 // generated adapter classes. 140 // 141 @CallerSensitive 142 public static 143 <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) { 144 if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers())) 145 throw new IllegalArgumentException("not a public interface: "+intfc.getName()); 146 final MethodHandle mh; 147 if (System.getSecurityManager() != null) { 148 final Class<?> caller = Reflection.getCallerClass(); 149 final ClassLoader ccl = caller != null ? caller.getClassLoader() : null; 150 ReflectUtil.checkProxyPackageAccess(ccl, intfc); 151 mh = ccl != null ? bindCaller(target, caller) : target; 152 } else { 153 mh = target; 154 } 155 ClassLoader proxyLoader = intfc.getClassLoader(); 156 if (proxyLoader == null) { 157 ClassLoader cl = Thread.currentThread().getContextClassLoader(); // avoid use of BCP 158 proxyLoader = cl != null ? cl : ClassLoader.getSystemClassLoader(); 159 } 160 final Method[] methods = getSingleNameMethods(intfc); 161 if (methods == null) 162 throw new IllegalArgumentException("not a single-method interface: "+intfc.getName()); 163 final MethodHandle[] vaTargets = new MethodHandle[methods.length]; 164 for (int i = 0; i < methods.length; i++) { 165 Method sm = methods[i]; 166 MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes()); 167 MethodHandle checkTarget = mh.asType(smMT); // make throw WMT 168 checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class)); 169 vaTargets[i] = checkTarget.asSpreader(Object[].class, smMT.parameterCount()); 170 } 171 final InvocationHandler ih = new InvocationHandler() { 172 private Object getArg(String name) { 173 if ((Object)name == "getWrapperInstanceTarget") return target; 174 if ((Object)name == "getWrapperInstanceType") return intfc; 175 throw new AssertionError(); 176 } 177 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 178 for (int i = 0; i < methods.length; i++) { 179 if (method.equals(methods[i])) 180 return vaTargets[i].invokeExact(args); 181 } 182 if (method.getDeclaringClass() == WrapperInstance.class) 183 return getArg(method.getName()); 184 if (isObjectMethod(method)) 185 return callObjectMethod(proxy, method, args); 186 throw new InternalError("bad proxy method: "+method); 187 } 188 }; 189 190 final Object proxy; 191 if (System.getSecurityManager() != null) { 192 // sun.invoke.WrapperInstance is a restricted interface not accessible 193 // by any non-null class loader. 194 final ClassLoader loader = proxyLoader; 195 proxy = AccessController.doPrivileged(new PrivilegedAction<Object>() { 196 public Object run() { 197 return Proxy.newProxyInstance( 198 loader, 199 new Class<?>[]{ intfc, WrapperInstance.class }, 200 ih); 201 } 202 }); 203 } else { 204 proxy = Proxy.newProxyInstance(proxyLoader, 205 new Class<?>[]{ intfc, WrapperInstance.class }, 206 ih); 207 } 208 return intfc.cast(proxy); 209 } 210 211 private static MethodHandle bindCaller(MethodHandle target, Class<?> hostClass) { 212 MethodHandle cbmh = MethodHandleImpl.bindCaller(target, hostClass); 213 if (target.isVarargsCollector()) { 214 MethodType type = cbmh.type(); 215 int arity = type.parameterCount(); 216 return cbmh.asVarargsCollector(type.parameterType(arity-1)); 217 } 218 return cbmh; 219 } 220 221 /** 222 * Determines if the given object was produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. 223 * @param x any reference 224 * @return true if the reference is not null and points to an object produced by {@code asInterfaceInstance} 225 */ 226 public static 227 boolean isWrapperInstance(Object x) { 228 return x instanceof WrapperInstance; 229 } 230 231 private static WrapperInstance asWrapperInstance(Object x) { 232 try { 233 if (x != null) 234 return (WrapperInstance) x; 235 } catch (ClassCastException ex) { 236 } 237 throw new IllegalArgumentException("not a wrapper instance"); 238 } 239 240 /** 241 * Produces or recovers a target method handle which is behaviorally 242 * equivalent to the unique method of this wrapper instance. 243 * The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. 244 * This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. 245 * @param x any reference 246 * @return a method handle implementing the unique method 247 * @throws IllegalArgumentException if the reference x is not to a wrapper instance 248 */ 249 public static 250 MethodHandle wrapperInstanceTarget(Object x) { 251 return asWrapperInstance(x).getWrapperInstanceTarget(); 252 } 253 254 /** 255 * Recovers the unique single-method interface type for which this wrapper instance was created. 256 * The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. 257 * This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. 258 * @param x any reference 259 * @return the single-method interface type for which the wrapper was created 260 * @throws IllegalArgumentException if the reference x is not to a wrapper instance 261 */ 262 public static 263 Class<?> wrapperInstanceType(Object x) { 264 return asWrapperInstance(x).getWrapperInstanceType(); 265 } 266 267 private static 268 boolean isObjectMethod(Method m) { 269 switch (m.getName()) { 270 case "toString": 271 return (m.getReturnType() == String.class 272 && m.getParameterTypes().length == 0); 273 case "hashCode": 274 return (m.getReturnType() == int.class 275 && m.getParameterTypes().length == 0); 276 case "equals": 277 return (m.getReturnType() == boolean.class 278 && m.getParameterTypes().length == 1 279 && m.getParameterTypes()[0] == Object.class); 280 } 281 return false; 282 } 283 284 private static 285 Object callObjectMethod(Object self, Method m, Object[] args) { 286 assert(isObjectMethod(m)) : m; 287 switch (m.getName()) { 288 case "toString": 289 return self.getClass().getName() + "@" + Integer.toHexString(self.hashCode()); 290 case "hashCode": 291 return System.identityHashCode(self); 292 case "equals": 293 return (self == args[0]); 294 } 295 return null; 296 } 297 298 private static 299 Method[] getSingleNameMethods(Class<?> intfc) { 300 ArrayList<Method> methods = new ArrayList<Method>(); 301 String uniqueName = null; 302 for (Method m : intfc.getMethods()) { 303 if (isObjectMethod(m)) continue; 304 if (!Modifier.isAbstract(m.getModifiers())) continue; 305 String mname = m.getName(); 306 if (uniqueName == null) 307 uniqueName = mname; 308 else if (!uniqueName.equals(mname)) 309 return null; // too many abstract methods 310 methods.add(m); 311 } 312 if (uniqueName == null) return null; 313 return methods.toArray(new Method[methods.size()]); 314 } 315 }