java.lang.invoke.MethodHandle.asFixedArity()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(116)

本文整理了Java中java.lang.invoke.MethodHandle.asFixedArity()方法的一些代码示例,展示了MethodHandle.asFixedArity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MethodHandle.asFixedArity()方法的具体详情如下:
包路径:java.lang.invoke.MethodHandle
类名称:MethodHandle
方法名:asFixedArity

MethodHandle.asFixedArity介绍

[英]Makes a fixed arity method handle which is otherwise equivalent to the current method handle.

If the current method handle is not of #asVarargsCollector, the current method handle is returned. This is true even if the current method handle could not be a valid input to asVarargsCollector.

Otherwise, the resulting fixed-arity method handle has the same type and behavior of the current method handle, except that #isVarargsCollectorwill be false. The fixed-arity method handle may (or may not) be the a previous argument to asVarargsCollector.

Here is an example, of a list-making variable arity method handle:

MethodHandle asListVar = publicLookup()try { asListFix.invoke((Object)1); } 
catch (Exception ex) { caught = ex; } 
assert(caught instanceof ClassCastException); 
assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); 
try { asListFix.invoke("two", "too"); } 
catch (Exception ex) { caught = ex; } 
assert(caught instanceof WrongMethodTypeException); 
Object[] argv = { "three", "thee", "tee" }; 
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); 
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); 
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); 
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); 
}

[中]生成一个固定算术方法句柄,该句柄在其他方面等同于当前方法句柄。
如果当前方法句柄不是#asVarargsCollector,则返回当前方法句柄。即使当前方法句柄不是asVarargsCollector的有效输入,也是如此。
否则,生成的固定arity方法句柄的类型和行为与当前方法句柄相同,只是#isvarargscollector将为false。fixed arity方法句柄可能是(也可能不是)asVarargsCollector的前一个参数。
下面是一个列表生成变量arity方法句柄的示例:

MethodHandle asListVar = publicLookup()try { asListFix.invoke((Object)1); } 
catch (Exception ex) { caught = ex; } 
assert(caught instanceof ClassCastException); 
assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); 
try { asListFix.invoke("two", "too"); } 
catch (Exception ex) { caught = ex; } 
assert(caught instanceof WrongMethodTypeException); 
Object[] argv = { "three", "thee", "tee" }; 
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); 
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); 
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); 
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); 
}

代码示例

代码示例来源:origin: eclipse/golo-lang

public FunctionReference asFixedArity() {
 return new FunctionReference(handle.asFixedArity(), this.parameterNames);
}

代码示例来源:origin: beanshell/beanshell

/** Appends varargs collector as appropriate to the MethodHandles lookup.
 * The return pushes the cascade chaining result to the parent.
 * {@inheritDoc} */
@Override
protected MethodHandle lookup(MethodHandle m) {
  if (isVarArgs() && null != m)
    return m.asFixedArity().asVarargsCollector(getVarArgsType());
  return m;
}

代码示例来源:origin: eclipse/golo-lang

public MethodHandle coerce(MethodHandle target) {
 if (target.isVarargsCollector() && isLastArgumentAnArray()) {
  return target.asFixedArity().asType(type);
 }
 return target.asType(type);
}

代码示例来源:origin: org.dynalang/dynalink

OverloadedMethod(List<MethodHandle> methodHandles, OverloadedDynamicMethod parent, MethodType callSiteType,
    LinkerServices linkerServices) {
  this.parent = parent;
  this.callSiteType = callSiteType;
  this.linkerServices = linkerServices;
  fixArgMethods = new ArrayList<>(methodHandles.size());
  varArgMethods = new ArrayList<>(methodHandles.size());
  final int argNum = callSiteType.parameterCount();
  for(MethodHandle mh: methodHandles) {
    if(mh.isVarargsCollector()) {
      final MethodHandle asFixed = mh.asFixedArity();
      if(argNum == asFixed.type().parameterCount()) {
        fixArgMethods.add(asFixed);
      }
      varArgMethods.add(mh);
    } else {
      fixArgMethods.add(mh);
    }
  }
  fixArgMethods.trimToSize();
  varArgMethods.trimToSize();
  final MethodHandle bound = SELECT_METHOD.bindTo(this);
  final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType(
      callSiteType.changeReturnType(MethodHandle.class));
  invoker = MethodHandles.foldArguments(MethodHandles.exactInvoker(callSiteType), collecting);
}

代码示例来源:origin: szegedi/dynalink

OverloadedMethod(final List<MethodHandle> methodHandles, final OverloadedDynamicMethod parent, final MethodType callSiteType,
    final LinkerServices linkerServices) {
  this.parent = parent;
  final Class<?> commonRetType = getCommonReturnType(methodHandles);
  this.callSiteType = callSiteType.changeReturnType(commonRetType);
  this.linkerServices = linkerServices;
  fixArgMethods = new ArrayList<>(methodHandles.size());
  varArgMethods = new ArrayList<>(methodHandles.size());
  final int argNum = callSiteType.parameterCount();
  for(MethodHandle mh: methodHandles) {
    if(mh.isVarargsCollector()) {
      final MethodHandle asFixed = mh.asFixedArity();
      if(argNum == asFixed.type().parameterCount()) {
        fixArgMethods.add(asFixed);
      }
      varArgMethods.add(mh);
    } else {
      fixArgMethods.add(mh);
    }
  }
  fixArgMethods.trimToSize();
  varArgMethods.trimToSize();
  final MethodHandle bound = SELECT_METHOD.bindTo(this);
  final MethodHandle collecting = SingleDynamicMethod.collectArguments(bound, argNum).asType(
      callSiteType.changeReturnType(MethodHandle.class));
  invoker = linkerServices.asTypeLosslessReturn(MethodHandles.foldArguments(
      MethodHandles.exactInvoker(this.callSiteType), collecting), callSiteType);
}

代码示例来源:origin: anba/es6draft

handle = handle.asFixedArity();

代码示例来源:origin: baratine/baratine

type = type.changeReturnType(void.class);
mh = mh.asFixedArity();
mh = mh.asType(type);

代码示例来源:origin: eclipse/golo-lang

/**
 * Spread arguments over this function parameters.
 *
 * @param arguments arguments as an array.
 * @return a return value.
 * @throws Throwable ...because an exception can be thrown.
 */
public Object spread(Object... arguments) throws Throwable {
 int arity = arity();
 if (this.handle.isVarargsCollector() && (arity > 0) && (arguments[arity - 1] instanceof Object[])) {
  return this.handle
    .asFixedArity()
    .asSpreader(Object[].class, arguments.length)
    .invoke(arguments);
 }
 return this.handle
   .asSpreader(Object[].class, arguments.length)
   .invoke(arguments);
}

代码示例来源:origin: eclipse/golo-lang

handle = handle.asFixedArity().asType(type);
 } else {
  handle = handle.asType(type);
types = constructor.getParameterTypes();
if (constructor.isVarArgs() && TypeMatching.isLastArgumentAnArray(types.length, args)) {
 handle = caller.unreflectConstructor(constructor).asFixedArity().asType(type);
} else {
 handle = caller.unreflectConstructor(constructor).asType(type);

代码示例来源:origin: org.dynalang/dynalink

final int paramsLen = methodType.parameterCount();
final boolean varArgs = target.isVarargsCollector();
final MethodHandle fixTarget = varArgs ? target.asFixedArity() : target;
final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen;
final int argsLen = callSiteType.parameterCount();

代码示例来源:origin: szegedi/dynalink

final int paramsLen = methodType.parameterCount();
final boolean varArgs = target.isVarargsCollector();
final MethodHandle fixTarget = varArgs ? filteredTarget.asFixedArity() : filteredTarget;
final int fixParamsLen = varArgs ? paramsLen - 1 : paramsLen;
final int argsLen = callSiteType.parameterCount();

代码示例来源:origin: eclipse/golo-lang

invoker = invoker.asFixedArity().asType(callSite.type());
} else {
 invoker = invoker.asCollector(

相关文章