< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java

Print this page




  34 
  35 import sun.invoke.empty.Empty;
  36 import sun.invoke.util.ValueConversions;
  37 import sun.invoke.util.VerifyType;
  38 import sun.invoke.util.Wrapper;
  39 import sun.reflect.CallerSensitive;
  40 import sun.reflect.Reflection;
  41 import static java.lang.invoke.LambdaForm.*;
  42 import static java.lang.invoke.MethodHandleStatics.*;
  43 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  44 
  45 /**
  46  * Trusted implementation code for MethodHandle.
  47  * @author jrose
  48  */
  49 /*non-public*/ abstract class MethodHandleImpl {
  50     // Do not adjust this except for special platforms:
  51     private static final int MAX_ARITY;
  52     static {
  53         final Object[] values = { 255 };
  54         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  55             @Override
  56             public Void run() {
  57                 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255);
  58                 return null;
  59             }
  60         });
  61         MAX_ARITY = (Integer) values[0];
  62     }
  63 
  64     /// Factory methods to create method handles:
  65 
  66     static void initStatics() {
  67         // Trigger selected static initializations.
  68         MemberName.Factory.INSTANCE.getClass();
  69     }
  70 
  71     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
  72         if (arrayClass == Object[].class)
  73             return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER);
  74         if (!arrayClass.isArray())


1217                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
1218             } catch (Throwable ex) {
1219                 throw new InternalError(ex);
1220             }
1221         }
1222 
1223         @CallerSensitive
1224         private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
1225             // This method is called via MH_checkCallerClass and so it's
1226             // correct to ask for the immediate caller here.
1227             Class<?> actual = Reflection.getCallerClass();
1228             if (actual != expected && actual != expected2)
1229                 throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
1230                                         +(expected == expected2 ? "" : ", or else "+expected2.getName()));
1231             return true;
1232         }
1233 
1234         private static final byte[] T_BYTES;
1235         static {
1236             final Object[] values = {null};
1237             AccessController.doPrivileged(new PrivilegedAction<Void>() {
1238                     public Void run() {
1239                         try {
1240                             Class<T> tClass = T.class;
1241                             String tName = tClass.getName();
1242                             String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
1243                             java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
1244                             int len = uconn.getContentLength();
1245                             byte[] bytes = new byte[len];
1246                             try (java.io.InputStream str = uconn.getInputStream()) {
1247                                 int nr = str.read(bytes);
1248                                 if (nr != len)  throw new java.io.IOException(tResource);
1249                             }
1250                             values[0] = bytes;
1251                         } catch (java.io.IOException ex) {
1252                             throw new InternalError(ex);
1253                         }
1254                         return null;
1255                     }
1256                 });
1257             T_BYTES = (byte[]) values[0];




  34 
  35 import sun.invoke.empty.Empty;
  36 import sun.invoke.util.ValueConversions;
  37 import sun.invoke.util.VerifyType;
  38 import sun.invoke.util.Wrapper;
  39 import sun.reflect.CallerSensitive;
  40 import sun.reflect.Reflection;
  41 import static java.lang.invoke.LambdaForm.*;
  42 import static java.lang.invoke.MethodHandleStatics.*;
  43 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
  44 
  45 /**
  46  * Trusted implementation code for MethodHandle.
  47  * @author jrose
  48  */
  49 /*non-public*/ abstract class MethodHandleImpl {
  50     // Do not adjust this except for special platforms:
  51     private static final int MAX_ARITY;
  52     static {
  53         final Object[] values = { 255 };
  54         AccessController.doPrivileged(new PrivilegedAction<>() {
  55             @Override
  56             public Void run() {
  57                 values[0] = Integer.getInteger(MethodHandleImpl.class.getName()+".MAX_ARITY", 255);
  58                 return null;
  59             }
  60         });
  61         MAX_ARITY = (Integer) values[0];
  62     }
  63 
  64     /// Factory methods to create method handles:
  65 
  66     static void initStatics() {
  67         // Trigger selected static initializations.
  68         MemberName.Factory.INSTANCE.getClass();
  69     }
  70 
  71     static MethodHandle makeArrayElementAccessor(Class<?> arrayClass, boolean isSetter) {
  72         if (arrayClass == Object[].class)
  73             return (isSetter ? ArrayAccessor.OBJECT_ARRAY_SETTER : ArrayAccessor.OBJECT_ARRAY_GETTER);
  74         if (!arrayClass.isArray())


1217                 assert((boolean) MH_checkCallerClass.invokeExact(THIS_CLASS, THIS_CLASS));
1218             } catch (Throwable ex) {
1219                 throw new InternalError(ex);
1220             }
1221         }
1222 
1223         @CallerSensitive
1224         private static boolean checkCallerClass(Class<?> expected, Class<?> expected2) {
1225             // This method is called via MH_checkCallerClass and so it's
1226             // correct to ask for the immediate caller here.
1227             Class<?> actual = Reflection.getCallerClass();
1228             if (actual != expected && actual != expected2)
1229                 throw new InternalError("found "+actual.getName()+", expected "+expected.getName()
1230                                         +(expected == expected2 ? "" : ", or else "+expected2.getName()));
1231             return true;
1232         }
1233 
1234         private static final byte[] T_BYTES;
1235         static {
1236             final Object[] values = {null};
1237             AccessController.doPrivileged(new PrivilegedAction<>() {
1238                     public Void run() {
1239                         try {
1240                             Class<T> tClass = T.class;
1241                             String tName = tClass.getName();
1242                             String tResource = tName.substring(tName.lastIndexOf('.')+1)+".class";
1243                             java.net.URLConnection uconn = tClass.getResource(tResource).openConnection();
1244                             int len = uconn.getContentLength();
1245                             byte[] bytes = new byte[len];
1246                             try (java.io.InputStream str = uconn.getInputStream()) {
1247                                 int nr = str.read(bytes);
1248                                 if (nr != len)  throw new java.io.IOException(tResource);
1249                             }
1250                             values[0] = bytes;
1251                         } catch (java.io.IOException ex) {
1252                             throw new InternalError(ex);
1253                         }
1254                         return null;
1255                     }
1256                 });
1257             T_BYTES = (byte[]) values[0];


< prev index next >