28 import java.lang.reflect.*; |
28 import java.lang.reflect.*; |
29 import java.security.AccessController; |
29 import java.security.AccessController; |
30 import java.security.PrivilegedAction; |
30 import java.security.PrivilegedAction; |
31 import sun.invoke.WrapperInstance; |
31 import sun.invoke.WrapperInstance; |
32 import java.util.ArrayList; |
32 import java.util.ArrayList; |
|
33 import sun.reflect.CallerSensitive; |
33 import sun.reflect.Reflection; |
34 import sun.reflect.Reflection; |
34 import sun.reflect.misc.ReflectUtil; |
35 import sun.reflect.misc.ReflectUtil; |
35 |
36 |
36 /** |
37 /** |
37 * This class consists exclusively of static methods that help adapt |
38 * This class consists exclusively of static methods that help adapt |
135 // API. Additional variations, such as single-abstract-method classes with |
136 // API. Additional variations, such as single-abstract-method classes with |
136 // private constructors, or interfaces with multiple but related |
137 // private constructors, or interfaces with multiple but related |
137 // entry points, must be covered by hand-written or automatically |
138 // entry points, must be covered by hand-written or automatically |
138 // generated adapter classes. |
139 // generated adapter classes. |
139 // |
140 // |
|
141 @CallerSensitive |
140 public static |
142 public static |
141 <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) { |
143 <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) { |
142 if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers())) |
144 if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers())) |
143 throw new IllegalArgumentException("not a public interface: "+intfc.getName()); |
145 throw new IllegalArgumentException("not a public interface: "+intfc.getName()); |
144 final MethodHandle mh; |
146 final MethodHandle mh; |
145 if (System.getSecurityManager() != null) { |
147 if (System.getSecurityManager() != null) { |
146 final int CALLER_FRAME = 2; // 0: Reflection, 1: asInterfaceInstance, 2: caller |
148 final Class<?> caller = Reflection.getCallerClass(); |
147 final Class<?> caller = Reflection.getCallerClass(CALLER_FRAME); |
|
148 final ClassLoader ccl = caller != null ? caller.getClassLoader() : null; |
149 final ClassLoader ccl = caller != null ? caller.getClassLoader() : null; |
149 ReflectUtil.checkProxyPackageAccess(ccl, intfc); |
150 ReflectUtil.checkProxyPackageAccess(ccl, intfc); |
150 mh = ccl != null ? bindCaller(target, caller) : target; |
151 mh = ccl != null ? bindCaller(target, caller) : target; |
151 } else { |
152 } else { |
152 mh = target; |
153 mh = target; |