本文整理匯總了Java中java.lang.invoke.MethodHandles.arrayElementGetter方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.arrayElementGetter方法的具體用法?Java MethodHandles.arrayElementGetter怎麽用?Java MethodHandles.arrayElementGetter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.arrayElementGetter方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: lookupArrayLoad
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
/**
* Returns a method handle to do an array load.
* @param receiverClass Class of the array to load the value from
* @return a MethodHandle that accepts the receiver as first argument, the index as second argument.
* It returns the loaded value.
*/
static MethodHandle lookupArrayLoad(Class<?> receiverClass) {
if (receiverClass.isArray()) {
return MethodHandles.arrayElementGetter(receiverClass);
} else if (Map.class.isAssignableFrom(receiverClass)) {
// maps allow access like mymap[key]
return MAP_GET;
} else if (List.class.isAssignableFrom(receiverClass)) {
return LIST_GET;
}
throw new IllegalArgumentException("Attempting to address a non-array type " +
"[" + receiverClass.getCanonicalName() + "] as an array.");
}
示例2: arrayElementGetter
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle arrayElementGetter(final Class<?> type) {
final MethodHandle mh = MethodHandles.arrayElementGetter(type);
return debug(mh, "arrayElementGetter", type);
}
示例3: getElementGetter
import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private GuardedInvocationComponent getElementGetter(final CallSiteDescriptor callSiteDescriptor,
final LinkerServices linkerServices, final List<String> operations) throws Exception {
final MethodType callSiteType = callSiteDescriptor.getMethodType();
final Class<?> declaredType = callSiteType.parameterType(0);
final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
linkerServices, operations);
// If declared type of receiver at the call site is already an array, a list or map, bind without guard. Thing
// is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance they're
// dealing with an array, or a list or map, but hey...
// Note that for arrays and lists, using LinkerServices.asType() will ensure that any language specific linkers
// in use will get a chance to perform any (if there's any) implicit conversion to integer for the indices.
final GuardedInvocationComponent gic;
final boolean isMap;
if(declaredType.isArray()) {
gic = new GuardedInvocationComponent(MethodHandles.arrayElementGetter(declaredType));
isMap = false;
} else if(List.class.isAssignableFrom(declaredType)) {
gic = new GuardedInvocationComponent(GET_LIST_ELEMENT);
isMap = false;
} else if(Map.class.isAssignableFrom(declaredType)) {
gic = new GuardedInvocationComponent(GET_MAP_ELEMENT);
isMap = true;
} else if(clazz.isArray()) {
gic = getClassGuardedInvocationComponent(MethodHandles.arrayElementGetter(clazz), callSiteType);
isMap = false;
} else if(List.class.isAssignableFrom(clazz)) {
gic = new GuardedInvocationComponent(GET_LIST_ELEMENT, Guards.asType(LIST_GUARD, callSiteType), List.class,
ValidationType.INSTANCE_OF);
isMap = false;
} else if(Map.class.isAssignableFrom(clazz)) {
gic = new GuardedInvocationComponent(GET_MAP_ELEMENT, Guards.asType(MAP_GUARD, callSiteType), Map.class,
ValidationType.INSTANCE_OF);
isMap = true;
} else {
// Can't retrieve elements for objects that are neither arrays, nor list, nor maps.
return nextComponent;
}
// We can have "dyn:getElem:foo", especially in composites, i.e. "dyn:getElem|getProp|getMethod:foo"
final String fixedKey = getFixedKey(callSiteDescriptor);
// Convert the key to a number if we're working with a list or array
final Object typedFixedKey;
if(!isMap && fixedKey != null) {
typedFixedKey = convertKeyToInteger(fixedKey, linkerServices);
if(typedFixedKey == null) {
// key is not numeric, it can never succeed
return nextComponent;
}
} else {
typedFixedKey = fixedKey;
}
final GuardedInvocation gi = gic.getGuardedInvocation();
final Binder binder = new Binder(linkerServices, callSiteType, typedFixedKey);
final MethodHandle invocation = gi.getInvocation();
if(nextComponent == null) {
return gic.replaceInvocation(binder.bind(invocation));
}
final MethodHandle checkGuard;
if(invocation == GET_LIST_ELEMENT) {
checkGuard = convertArgToInt(RANGE_CHECK_LIST, linkerServices, callSiteDescriptor);
} else if(invocation == GET_MAP_ELEMENT) {
// TODO: A more complex solution could be devised for maps, one where we do a get() first, and fold it
// into a GWT that tests if it returned null, and if it did, do another GWT with containsKey()
// that returns constant null (on true), or falls back to next component (on false)
checkGuard = CONTAINS_MAP;
} else {
checkGuard = convertArgToInt(RANGE_CHECK_ARRAY, linkerServices, callSiteDescriptor);
}
final MethodPair matchedInvocations = matchReturnTypes(binder.bind(invocation),
nextComponent.getGuardedInvocation().getInvocation());
return nextComponent.compose(matchedInvocations.guardWithTest(binder.bindTest(checkGuard)), gi.getGuard(),
gic.getValidatorClass(), gic.getValidationType());
}