< prev index next >

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

Print this page

1441                 return memberName.getMethodType();
1442             }
1443 
1444             @Override
1445             public String getMethodDescriptor(Object mname) {
1446                 MemberName memberName = (MemberName)mname;
1447                 return memberName.getMethodDescriptor();
1448             }
1449 
1450             @Override
1451             public boolean isNative(Object mname) {
1452                 MemberName memberName = (MemberName)mname;
1453                 return memberName.isNative();
1454             }
1455 
1456             @Override
1457             public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1458                 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1459             }
1460 
1461             @Override
1462             public void ensureCustomized(MethodHandle mh) {
1463                 mh.customize();
1464             }
1465 
1466             @Override
1467             public VarHandle memoryAccessVarHandle(Class<?> carrier, boolean skipAlignmentMaskCheck, long alignmentMask,
1468                                                    ByteOrder order) {
1469                 return VarHandles.makeMemoryAddressViewHandle(carrier, skipAlignmentMaskCheck, alignmentMask, order);
1470             }
1471 
1472             @Override
1473             public MethodHandle nativeMethodHandle(NativeEntryPoint nep, MethodHandle fallback) {
1474                 return NativeMethodHandle.make(nep, fallback);
1475             }
1476 
1477             @Override
1478             public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1479                 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1480             }
1481 
1482             @Override
1483             public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1484                 return VarHandles.filterCoordinates(target, pos, filters);
1485             }
1486 
1487             @Override
1488             public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1489                 return VarHandles.dropCoordinates(target, pos, valueTypes);
1490             }
1491 
1492             @Override
1493             public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1494                 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1495             }
1496 
1497             @Override
1498             public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1499                 return VarHandles.collectCoordinates(target, pos, filter);
1500             }
1501 
1502             @Override
1503             public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1504                 return VarHandles.insertCoordinates(target, pos, values);
1505             }





1506         });
1507     }
1508 
1509     /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1510     private static MethodHandle unboxResultHandle(Class<?> returnType) {
1511         if (returnType.isPrimitive()) {
1512             if (returnType == void.class) {
1513                 return ValueConversions.ignore();
1514             } else {
1515                 Wrapper w = Wrapper.forPrimitiveType(returnType);
1516                 return ValueConversions.unboxExact(w);
1517             }
1518         } else {
1519             return MethodHandles.identity(Object.class);
1520         }
1521     }
1522 
1523     /**
1524      * Assembles a loop method handle from the given handles and type information.
1525      *

2092 
2093         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TABLE_SWITCH);
2094         LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2095         return prev != null ? prev : lform;
2096     }
2097 
2098     @Hidden
2099     static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2100         MethodHandle[] caseActions = holder.cases;
2101         MethodHandle selectedCase;
2102         if (input < 0 || input >= caseActions.length) {
2103             selectedCase = defaultCase;
2104         } else {
2105             selectedCase = caseActions[input];
2106         }
2107         return selectedCase.invokeWithArguments(args);
2108     }
2109 
2110     // Indexes into constant method handles:
2111     static final int
2112             MH_cast                  = 0,
2113             MH_selectAlternative     = 1,
2114             MH_countedLoopPred       = 2,
2115             MH_countedLoopStep       = 3,
2116             MH_initIterator          = 4,
2117             MH_iteratePred           = 5,
2118             MH_iterateNext           = 6,
2119             MH_Array_newInstance     = 7,
2120             MH_LIMIT                 = 8;

2121 
2122     static MethodHandle getConstantHandle(int idx) {
2123         MethodHandle handle = HANDLES[idx];
2124         if (handle != null) {
2125             return handle;
2126         }
2127         return setCachedHandle(idx, makeConstantHandle(idx));
2128     }
2129 
2130     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2131         // Simulate a CAS, to avoid racy duplication of results.
2132         MethodHandle prev = HANDLES[idx];
2133         if (prev != null) {
2134             return prev;
2135         }
2136         HANDLES[idx] = method;
2137         return method;
2138     }
2139 
2140     // Local constant method handles:

2150                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2151                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2152                 case MH_countedLoopPred:
2153                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2154                             MethodType.methodType(boolean.class, int.class, int.class));
2155                 case MH_countedLoopStep:
2156                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2157                             MethodType.methodType(int.class, int.class, int.class));
2158                 case MH_initIterator:
2159                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2160                             MethodType.methodType(Iterator.class, Iterable.class));
2161                 case MH_iteratePred:
2162                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2163                             MethodType.methodType(boolean.class, Iterator.class));
2164                 case MH_iterateNext:
2165                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2166                             MethodType.methodType(Object.class, Iterator.class));
2167                 case MH_Array_newInstance:
2168                     return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2169                             MethodType.methodType(Object.class, Class.class, int.class));



2170             }
2171         } catch (ReflectiveOperationException ex) {
2172             throw newInternalError(ex);
2173         }
2174         throw newInternalError("Unknown function index: " + idx);
2175     }
2176 }

1441                 return memberName.getMethodType();
1442             }
1443 
1444             @Override
1445             public String getMethodDescriptor(Object mname) {
1446                 MemberName memberName = (MemberName)mname;
1447                 return memberName.getMethodDescriptor();
1448             }
1449 
1450             @Override
1451             public boolean isNative(Object mname) {
1452                 MemberName memberName = (MemberName)mname;
1453                 return memberName.isNative();
1454             }
1455 
1456             @Override
1457             public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
1458                 return GenerateJLIClassesHelper.generateHolderClasses(traces);
1459             }
1460 





1461             @Override
1462             public VarHandle memoryAccessVarHandle(Class<?> carrier, boolean skipAlignmentMaskCheck, long alignmentMask,
1463                                                    ByteOrder order) {
1464                 return VarHandles.makeMemoryAddressViewHandle(carrier, skipAlignmentMaskCheck, alignmentMask, order);
1465             }
1466 
1467             @Override
1468             public MethodHandle nativeMethodHandle(NativeEntryPoint nep) {
1469                 return NativeMethodHandle.make(nep);
1470             }
1471 
1472             @Override
1473             public VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget) {
1474                 return VarHandles.filterValue(target, filterToTarget, filterFromTarget);
1475             }
1476 
1477             @Override
1478             public VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters) {
1479                 return VarHandles.filterCoordinates(target, pos, filters);
1480             }
1481 
1482             @Override
1483             public VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes) {
1484                 return VarHandles.dropCoordinates(target, pos, valueTypes);
1485             }
1486 
1487             @Override
1488             public VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder) {
1489                 return VarHandles.permuteCoordinates(target, newCoordinates, reorder);
1490             }
1491 
1492             @Override
1493             public VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter) {
1494                 return VarHandles.collectCoordinates(target, pos, filter);
1495             }
1496 
1497             @Override
1498             public VarHandle insertCoordinates(VarHandle target, int pos, Object... values) {
1499                 return VarHandles.insertCoordinates(target, pos, values);
1500             }
1501 
1502             @Override
1503             public Class<?>[] exceptionTypes(MethodHandle handle) {
1504                 return VarHandles.exceptionTypes(handle);
1505             }
1506         });
1507     }
1508 
1509     /** Result unboxing: ValueConversions.unbox() OR ValueConversions.identity() OR ValueConversions.ignore(). */
1510     private static MethodHandle unboxResultHandle(Class<?> returnType) {
1511         if (returnType.isPrimitive()) {
1512             if (returnType == void.class) {
1513                 return ValueConversions.ignore();
1514             } else {
1515                 Wrapper w = Wrapper.forPrimitiveType(returnType);
1516                 return ValueConversions.unboxExact(w);
1517             }
1518         } else {
1519             return MethodHandles.identity(Object.class);
1520         }
1521     }
1522 
1523     /**
1524      * Assembles a loop method handle from the given handles and type information.
1525      *

2092 
2093         lform = new LambdaForm(lambdaType.parameterCount(), names, Kind.TABLE_SWITCH);
2094         LambdaForm prev = TableSwitchCacheKey.CACHE.putIfAbsent(key, lform);
2095         return prev != null ? prev : lform;
2096     }
2097 
2098     @Hidden
2099     static Object tableSwitch(int input, MethodHandle defaultCase, CasesHolder holder, Object[] args) throws Throwable {
2100         MethodHandle[] caseActions = holder.cases;
2101         MethodHandle selectedCase;
2102         if (input < 0 || input >= caseActions.length) {
2103             selectedCase = defaultCase;
2104         } else {
2105             selectedCase = caseActions[input];
2106         }
2107         return selectedCase.invokeWithArguments(args);
2108     }
2109 
2110     // Indexes into constant method handles:
2111     static final int
2112             MH_cast                               = 0,
2113             MH_selectAlternative                  = 1,
2114             MH_countedLoopPred                    = 2,
2115             MH_countedLoopStep                    = 3,
2116             MH_initIterator                       = 4,
2117             MH_iteratePred                        = 5,
2118             MH_iterateNext                        = 6,
2119             MH_Array_newInstance                  = 7,
2120             MH_VarHandles_handleCheckedExceptions = 8,
2121             MH_LIMIT                              = 9;
2122 
2123     static MethodHandle getConstantHandle(int idx) {
2124         MethodHandle handle = HANDLES[idx];
2125         if (handle != null) {
2126             return handle;
2127         }
2128         return setCachedHandle(idx, makeConstantHandle(idx));
2129     }
2130 
2131     private static synchronized MethodHandle setCachedHandle(int idx, final MethodHandle method) {
2132         // Simulate a CAS, to avoid racy duplication of results.
2133         MethodHandle prev = HANDLES[idx];
2134         if (prev != null) {
2135             return prev;
2136         }
2137         HANDLES[idx] = method;
2138         return method;
2139     }
2140 
2141     // Local constant method handles:

2151                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "selectAlternative",
2152                             MethodType.methodType(MethodHandle.class, boolean.class, MethodHandle.class, MethodHandle.class));
2153                 case MH_countedLoopPred:
2154                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopPredicate",
2155                             MethodType.methodType(boolean.class, int.class, int.class));
2156                 case MH_countedLoopStep:
2157                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "countedLoopStep",
2158                             MethodType.methodType(int.class, int.class, int.class));
2159                 case MH_initIterator:
2160                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "initIterator",
2161                             MethodType.methodType(Iterator.class, Iterable.class));
2162                 case MH_iteratePred:
2163                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iteratePredicate",
2164                             MethodType.methodType(boolean.class, Iterator.class));
2165                 case MH_iterateNext:
2166                     return IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "iterateNext",
2167                             MethodType.methodType(Object.class, Iterator.class));
2168                 case MH_Array_newInstance:
2169                     return IMPL_LOOKUP.findStatic(Array.class, "newInstance",
2170                             MethodType.methodType(Object.class, Class.class, int.class));
2171                 case MH_VarHandles_handleCheckedExceptions:
2172                     return IMPL_LOOKUP.findStatic(VarHandles.class, "handleCheckedExceptions",
2173                             MethodType.methodType(void.class, Throwable.class));
2174             }
2175         } catch (ReflectiveOperationException ex) {
2176             throw newInternalError(ex);
2177         }
2178         throw newInternalError("Unknown function index: " + idx);
2179     }
2180 }
< prev index next >