java.lang.reflect.Method.isVarArgs()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(144)

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

Method.isVarArgs介绍

[英]Returns true if this method was declared to take a variable number of arguments; returns falseotherwise.
[中]如果此方法被声明为采用可变数量的参数,则返回true;否则返回false。

代码示例

代码示例来源:origin: google/guava

@Override
 public final boolean isVarArgs() {
  return method.isVarArgs();
 }
}

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

/** Return true if the method takes a variable number of arguments. */
public boolean isVarArgs () {
  return method.isVarArgs();
}

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

/** Return true if the method takes a variable number of arguments. */
public boolean isVarArgs () {
  return method.isVarArgs();
}

代码示例来源:origin: prestodb/presto

@Override
 public final boolean isVarArgs() {
  return method.isVarArgs();
 }
}

代码示例来源:origin: org.mockito/mockito-core

public boolean isVarArgs() {
  return method.isVarArgs();
}

代码示例来源:origin: google/j2objc

@Override
 public final boolean isVarArgs() {
  return method.isVarArgs();
 }
}

代码示例来源:origin: org.apache.commons/commons-lang3

private Executable(final Method method) {
 parameterTypes = method.getParameterTypes();
 isVarArgs = method.isVarArgs();
}

代码示例来源:origin: org.mockito/mockito-core

private boolean wantedArgIndexIsVarargAndSameTypeAsReturnType(Method method, int argumentPosition) {
  Class<?>[] parameterTypes = method.getParameterTypes();
  return method.isVarArgs() &&
     argumentPosition == /* vararg index */ parameterTypes.length - 1 &&
     method.getReturnType().isAssignableFrom(parameterTypes[argumentPosition]);
}

代码示例来源:origin: spring-projects/spring-framework

private Object[] extractArgs(Method method, Object[] args) {
  if (!method.isVarArgs()) {
    return args;
  }
  Object[] varArgs = ObjectUtils.toObjectArray(args[args.length - 1]);
  Object[] combinedArgs = new Object[args.length - 1 + varArgs.length];
  System.arraycopy(args, 0, combinedArgs, 0, args.length - 1);
  System.arraycopy(varArgs, 0, combinedArgs, args.length - 1, varArgs.length);
  return combinedArgs;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a new executor for the given method.
 * @param method the method to invoke
 */
public ReflectiveMethodExecutor(Method method) {
  this.originalMethod = method;
  this.methodToInvoke = ClassUtils.getInterfaceMethodIfPossible(method);
  if (method.isVarArgs()) {
    Class<?>[] paramTypes = method.getParameterTypes();
    this.varargsPosition = paramTypes.length - 1;
  }
  else {
    this.varargsPosition = null;
  }
}

代码示例来源:origin: google/j2objc

public SerializableMethod(Method method) {
  declaringClass = method.getDeclaringClass();
  methodName = method.getName();
  parameterTypes = method.getParameterTypes();
  returnType = method.getReturnType();
  exceptionTypes = method.getExceptionTypes();
  isVarArgs = method.isVarArgs();
}

代码示例来源:origin: org.mockito/mockito-core

private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition) {
  if (argumentPosition < 0) {
    return false;
  }
  if (!invocation.getMethod().isVarArgs()) {
    return invocation.getArguments().length > argumentPosition;
  }
  // for all varargs accepts positive ranges
  return true;
}

代码示例来源:origin: org.mockito/mockito-core

public SerializableMethod(Method method) {
  this.method = method;
  declaringClass = method.getDeclaringClass();
  methodName = method.getName();
  parameterTypes = SuspendMethod.trimSuspendParameterTypes(method.getParameterTypes());
  returnType = method.getReturnType();
  exceptionTypes = method.getExceptionTypes();
  isVarArgs = method.isVarArgs();
  isAbstract = (method.getModifiers() & Modifier.ABSTRACT) != 0;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Convert a supplied set of arguments into the requested types. If the parameterTypes are related to
 * a varargs method then the final entry in the parameterTypes array is going to be an array itself whose
 * component type should be used as the conversion target for extraneous arguments. (For example, if the
 * parameterTypes are {Integer, String[]} and the input arguments are {Integer, boolean, float} then both
 * the boolean and float must be converted to strings). This method does *not* repackage the arguments
 * into a form suitable for the varargs invocation - a subsequent call to setupArgumentsForVarargsInvocation handles that.
 * @param converter the converter to use for type conversions
 * @param arguments the arguments to convert to the requested parameter types
 * @param method the target Method
 * @return true if some kind of conversion occurred on the argument
 * @throws SpelEvaluationException if there is a problem with conversion
 */
public static boolean convertAllArguments(TypeConverter converter, Object[] arguments, Method method)
    throws SpelEvaluationException {
  Integer varargsPosition = (method.isVarArgs() ? method.getParameterCount() - 1 : null);
  return convertArguments(converter, arguments, method, varargsPosition);
}

代码示例来源:origin: org.apache.commons/commons-lang3

private static Object[] toVarArgs(final Method method, Object[] args) {
  if (method.isVarArgs()) {
    final Class<?>[] methodParameterTypes = method.getParameterTypes();
    args = getVarArgs(args, methodParameterTypes);
  }
  return args;
}

代码示例来源:origin: org.springframework/spring-context

private Object[] extractArgs(Method method, Object[] args) {
  if (!method.isVarArgs()) {
    return args;
  }
  Object[] varArgs = ObjectUtils.toObjectArray(args[args.length - 1]);
  Object[] combinedArgs = new Object[args.length - 1 + varArgs.length];
  System.arraycopy(args, 0, combinedArgs, 0, args.length - 1);
  System.arraycopy(varArgs, 0, combinedArgs, args.length - 1, varArgs.length);
  return combinedArgs;
}

代码示例来源:origin: org.mockito/mockito-core

private static StringBuilder possibleArgumentTypesOf(InvocationOnMock invocation) {
  Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes();
  if (parameterTypes.length == 0) {
    return new StringBuilder("the method has no arguments.\n");
  }
  StringBuilder stringBuilder = new StringBuilder("the possible argument indexes for this method are :\n");
  for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) {
    stringBuilder.append("    [").append(i);
    if (invocation.getMethod().isVarArgs() && i == parameterTypesLength - 1) {
      stringBuilder.append("+] ").append(parameterTypes[i].getComponentType().getSimpleName()).append("  <- Vararg").append("\n");
    } else {
      stringBuilder.append("] ").append(parameterTypes[i].getSimpleName()).append("\n");
    }
  }
  return stringBuilder;
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private static Object doGenerate(KeyGenerator keyGenerator, CacheKeyInvocationContext<?> context) {
  List<Object> parameters = new ArrayList<>();
  for (CacheInvocationParameter param : context.getKeyParameters()) {
    Object value = param.getValue();
    if (param.getParameterPosition() == context.getAllParameters().length - 1 &&
        context.getMethod().isVarArgs()) {
      parameters.addAll((List<Object>) CollectionUtils.arrayToList(value));
    }
    else {
      parameters.add(value);
    }
  }
  return keyGenerator.generate(context.getTarget(), context.getMethod(), parameters.toArray());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
  try {
    this.argumentConversionOccurred = ReflectionHelper.convertArguments(
        context.getTypeConverter(), arguments, this.originalMethod, this.varargsPosition);
    if (this.originalMethod.isVarArgs()) {
      arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(
          this.originalMethod.getParameterTypes(), arguments);
    }
    ReflectionUtils.makeAccessible(this.methodToInvoke);
    Object value = this.methodToInvoke.invoke(target, arguments);
    return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.originalMethod, -1)).narrow(value));
  }
  catch (Exception ex) {
    throw new AccessException("Problem invoking method: " + this.methodToInvoke, ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
    throws AccessException {
  try {
    Method m = HasRoleExecutor.class.getMethod("hasRole", String[].class);
    Object[] args = arguments;
    if (args != null) {
      ReflectionHelper.convertAllArguments(tc, args, m);
    }
    if (m.isVarArgs()) {
      args = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), args);
    }
    return new TypedValue(m.invoke(null, args), new TypeDescriptor(new MethodParameter(m,-1)));
  }
  catch (Exception ex) {
    throw new AccessException("Problem invoking hasRole", ex);
  }
}

相关文章

微信公众号

最新文章

更多