本文整理汇总了Java中java.lang.reflect.Method.isVarArgs方法的典型用法代码示例。如果您正苦于以下问题:Java Method.isVarArgs方法的具体用法?Java Method.isVarArgs怎么用?Java Method.isVarArgs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Method
的用法示例。
在下文中一共展示了Method.isVarArgs方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: filterMethods
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Filter all methods to ensure following conditions:
*
* <ul><li>Method name is @name</li>
*
* <li>Method is <tt>public<tt></li>
*
* <li>Method is not <tt>abstract</tt></li>.
*
* <li>Method does not have variable number of arguments</li>
*
* <li>Return type of method is @returnType</li>
*
* <li>All parameter fields are of type {@link String}, {@link Integer} or {@link Double}</li>
*
* <li>All parameters are annotated with {@link Param}</li> </ul>
*
* @param methods Array of methods to be filtered.
* @param seekedName Name of the methods we are looking for.
* @param returnType Expected return type of filtered methods.
* @return Array of methods with @name.
*/
private Method[] filterMethods(Method[] methods, String seekedName, Class<?> returnType) {
List<Method> filteredMethods = new LinkedList<Method>();
for (Method testedMethod : methods) {
String testedMethodName = testedMethod.getName();
boolean methodIsPublic = (testedMethod.getModifiers() & Modifier.PUBLIC) == Modifier.PUBLIC;
boolean methodIsAbstract = (testedMethod.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT;
boolean correctReturnType = returnType.isAssignableFrom(testedMethod.getReturnType());
boolean acceptedParams = areParamsAcceptable(testedMethod, true, allowedParamClasses);
boolean annotatedParams = areParamsAnnotated(testedMethod);
if (testedMethodName.equals(seekedName)
&& methodIsPublic
&& !methodIsAbstract
&& !testedMethod.isVarArgs()
&& correctReturnType
&& acceptedParams
&& annotatedParams) {
filteredMethods.add(testedMethod);
}
}
return filteredMethods.toArray(new Method[filteredMethods.size()]);
}
示例2: getValueObject
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Returns the result of method execution.
*
* @param type the base class
* @param args the array of arguments
* @return the value of this element
* @throws Exception if calculation is failed
*/
@Override
protected ValueObject getValueObject(Class<?> type, Object[] args) throws Exception {
Object bean = getContextBean();
Class<?>[] types = getArgumentTypes(args);
Method method = (type != null)
? MethodFinder.findStaticMethod(type, this.name, types)
: MethodFinder.findMethod(bean.getClass(), this.name, types);
if (method.isVarArgs()) {
args = getArguments(args, method.getParameterTypes());
}
Object value = MethodUtil.invoke(method, bean, args);
return method.getReturnType().equals(void.class)
? ValueObjectImpl.VOID
: ValueObjectImpl.create(value);
}
示例3: visit
import java.lang.reflect.Method; //导入方法依赖的package包/类
@Override
public void visit(Node node) throws ELException {
if (node instanceof AstFunction) {
AstFunction funcNode = (AstFunction) node;
if (this.fnMapper == null) {
throw new ELException(MessageFactory.get("error.fnMapper.null"));
}
Method m = fnMapper.resolveFunction(funcNode.getPrefix(), funcNode.getLocalName());
if (m == null) {
throw new ELException(MessageFactory.get("error.fnMapper.method", funcNode.getOutputName()));
}
int methodParameterCount = m.getParameterTypes().length;
int inputParameterCount = node.jjtGetNumChildren();
if (m.isVarArgs() && inputParameterCount < methodParameterCount - 1
|| !m.isVarArgs() && inputParameterCount != methodParameterCount) {
throw new ELException(MessageFactory.get("error.fnMapper.paramcount", funcNode.getOutputName(),
"" + methodParameterCount, "" + node.jjtGetNumChildren()));
}
} else if (node instanceof AstIdentifier && this.varMapper != null) {
String variable = ((AstIdentifier) node).getImage();
// simply capture it
this.varMapper.resolveVariable(variable);
}
}
示例4: extractArgs
import java.lang.reflect.Method; //导入方法依赖的package包/类
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;
}
示例5: executeFunctionJLRMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Execute a function represented as a java.lang.reflect.Method.
*
* @param state the expression evaluation state
* @param the java method to invoke
* @return the return value of the invoked Java method
* @throws EvaluationException if there is any problem invoking the method
*/
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
Object[] functionArgs = getArguments(state);
if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
functionArgs.length, method.getParameterTypes().length);
}
// Only static methods can be called in this way
if (!Modifier.isStatic(method.getModifiers())) {
throw new SpelEvaluationException(getStartPosition(),
SpelMessage.FUNCTION_MUST_BE_STATIC,
method.getDeclaringClass().getName() + "." + method.getName(), this.name);
}
// Convert arguments if necessary and remap them for varargs if required
if (functionArgs != null) {
TypeConverter converter = state.getEvaluationContext().getTypeConverter();
ReflectionHelper.convertAllArguments(converter, functionArgs, method);
}
if (method.isVarArgs()) {
functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
method.getParameterTypes(), functionArgs);
}
try {
ReflectionUtils.makeAccessible(method);
Object result = method.invoke(method.getClass(), functionArgs);
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method,-1)).narrow(result));
}
catch (Exception ex) {
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
this.name, ex.getMessage());
}
}
示例6: ReflectiveMethodExecutor
import java.lang.reflect.Method; //导入方法依赖的package包/类
public ReflectiveMethodExecutor(Method method) {
this.method = method;
if (method.isVarArgs()) {
Class<?>[] paramTypes = method.getParameterTypes();
this.varargsPosition = paramTypes.length - 1;
}
else {
this.varargsPosition = null;
}
}
示例7: getValue
import java.lang.reflect.Method; //导入方法依赖的package包/类
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
FunctionMapper fnMapper = ctx.getFunctionMapper();
// quickly validate again for this request
if (fnMapper == null) {
throw new ELException(MessageFactory.get("error.fnMapper.null"));
}
Method m = fnMapper.resolveFunction(this.prefix, this.localName);
if (m == null) {
throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
}
Class<?>[] paramTypes = m.getParameterTypes();
Object[] params = null;
Object result = null;
int inputParameterCount = this.jjtGetNumChildren();
int methodParameterCount = paramTypes.length;
if (inputParameterCount == 0 && methodParameterCount == 1 && m.isVarArgs()) {
params = new Object[] { null };
} else if (inputParameterCount > 0) {
params = new Object[methodParameterCount];
try {
for (int i = 0; i < methodParameterCount; i++) {
if (m.isVarArgs() && i == methodParameterCount - 1) {
if (inputParameterCount < methodParameterCount) {
params[i] = new Object[] { null };
} else if (inputParameterCount == methodParameterCount && paramTypes[i].isArray()) {
params[i] = this.jjtGetChild(i).getValue(ctx);
} else {
Object[] varargs = new Object[inputParameterCount - methodParameterCount + 1];
Class<?> target = paramTypes[i].getComponentType();
for (int j = i; j < inputParameterCount; j++) {
varargs[j - i] = this.jjtGetChild(j).getValue(ctx);
varargs[j - i] = coerceToType(varargs[j - i], target);
}
params[i] = varargs;
}
} else {
params[i] = this.jjtGetChild(i).getValue(ctx);
}
params[i] = coerceToType(params[i], paramTypes[i]);
}
} catch (ELException ele) {
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), ele);
}
}
try {
result = m.invoke(null, params);
} catch (IllegalAccessException iae) {
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), iae);
} catch (InvocationTargetException ite) {
Throwable cause = ite.getCause();
if (cause instanceof ThreadDeath) {
throw (ThreadDeath) cause;
}
if (cause instanceof VirtualMachineError) {
throw (VirtualMachineError) cause;
}
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), cause);
}
return result;
}
示例8: linkMethod
import java.lang.reflect.Method; //导入方法依赖的package包/类
public Method linkMethod(String methodName, Map<? extends String, ? extends ParameterKind> params) {
Collection<Class<?>> paramTypes = new LinkedList<Class<?>>();
for (Entry<? extends String, ? extends ParameterKind> e : params.entrySet()) {
switch ((ParameterKind) e.getValue()) {
case VARIABLE:
paramTypes.add(Variable.class);
break;
case STRING_LITERAL:
paramTypes.add(String.class);
break;
case ENUM_CONSTANT:
Enum<?> constant = loadEnumConstant(e.getKey());
paramTypes.add(constant.getDeclaringClass());
break;
}
}
Method varArgMethod = null;
for (Class<?> clazz : ruleUtilities) {
OUTER: for (Method m : clazz.getDeclaredMethods()) {
if (methodName.equals(m.getName())) {
Class<?>[] p = m.getParameterTypes();
int c = 0;
Iterator<Class<?>> it = paramTypes.iterator();
for ( ; it.hasNext() && c < p.length; ) {
Class<?> paramClass = it.next();
Class<?> declaredClass = p[c++];
if (declaredClass.equals(paramClass))
continue;
if ( m.isVarArgs()
&& declaredClass.isArray()
&& declaredClass.getComponentType().equals(paramClass)
&& c == p.length) {
while (it.hasNext()) {
if (!paramClass.equals(it.next())) {
continue OUTER;
}
}
break;
}
continue OUTER;
}
if (!it.hasNext() && c == p.length) {
if (!m.isVarArgs()) {
return m;
}
if (varArgMethod == null) {
varArgMethod = m;
}
}
}
}
}
return varArgMethod;
}
示例9: testLoggerUsageCheckerCompatibilityWithLog4j2Logger
import java.lang.reflect.Method; //导入方法依赖的package包/类
public void testLoggerUsageCheckerCompatibilityWithLog4j2Logger() throws NoSuchMethodException {
for (Method method : Logger.class.getMethods()) {
if (ESLoggerUsageChecker.LOGGER_METHODS.contains(method.getName())) {
assertThat(method.getParameterTypes().length, greaterThanOrEqualTo(1));
int markerOffset = method.getParameterTypes()[0].equals(Marker.class) ? 1 : 0;
int paramLength = method.getParameterTypes().length - markerOffset;
if (method.isVarArgs()) {
assertEquals(2, paramLength);
assertEquals(String.class, method.getParameterTypes()[markerOffset]);
assertThat(method.getParameterTypes()[markerOffset + 1], Matchers.<Class<?>>isOneOf(Object[].class, Supplier[].class));
} else {
assertThat(method.getParameterTypes()[markerOffset], Matchers.<Class<?>>isOneOf(Message.class, MessageSupplier.class,
CharSequence.class, Object.class, String.class, Supplier.class));
if (paramLength == 2) {
assertThat(method.getParameterTypes()[markerOffset + 1], Matchers.<Class<?>>isOneOf(Throwable.class, Object.class));
if (method.getParameterTypes()[markerOffset + 1].equals(Object.class)) {
assertEquals(String.class, method.getParameterTypes()[markerOffset]);
}
}
if (paramLength > 2) {
assertEquals(String.class, method.getParameterTypes()[markerOffset]);
assertThat(paramLength, lessThanOrEqualTo(11));
for (int i = 1; i < paramLength; i++) {
assertEquals(Object.class, method.getParameterTypes()[markerOffset + i]);
}
}
}
}
}
for (String methodName : ESLoggerUsageChecker.LOGGER_METHODS) {
assertEquals(48, Stream.of(Logger.class.getMethods()).filter(m -> methodName.equals(m.getName())).count());
}
for (Constructor<?> constructor : ParameterizedMessage.class.getConstructors()) {
assertThat(constructor.getParameterTypes().length, greaterThanOrEqualTo(2));
assertEquals(String.class, constructor.getParameterTypes()[0]);
assertThat(constructor.getParameterTypes()[1], Matchers.<Class<?>>isOneOf(String[].class, Object[].class, Object.class));
if (constructor.getParameterTypes().length > 2) {
assertEquals(3, constructor.getParameterTypes().length);
if (constructor.getParameterTypes()[1].equals(Object.class)) {
assertEquals(Object.class, constructor.getParameterTypes()[2]);
} else {
assertEquals(Throwable.class, constructor.getParameterTypes()[2]);
}
}
}
assertEquals(5, ParameterizedMessage.class.getConstructors().length);
}
示例10: convertArgs
import java.lang.reflect.Method; //导入方法依赖的package包/类
private Object[] convertArgs(Object[] src, Method m) {
Class<?>[] types = m.getParameterTypes();
if (types.length == 0) {
// Treated as if parameters have been provided so src is ignored
return EMPTY_ARRAY;
}
int paramCount = types.length;
if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length) ||
!m.isVarArgs() && (paramCount > 0 && src == null ||
src != null && src.length != paramCount)) {
String srcCount = null;
if (src != null) {
srcCount = Integer.toString(src.length);
}
String msg;
if (m.isVarArgs()) {
msg = MessageFactory.get("error.invoke.tooFewParams",
m.getName(), srcCount, Integer.toString(paramCount));
} else {
msg = MessageFactory.get("error.invoke.wrongParams",
m.getName(), srcCount, Integer.toString(paramCount));
}
throw new IllegalArgumentException(msg);
}
if (src == null) {
// Must be a varargs method with a single parameter.
// Use a new array every time since the called code could modify the
// contents of the array
return new Object[1];
}
Object[] dest = new Object[paramCount];
for (int i = 0; i < paramCount - 1; i++) {
dest[i] = ELSupport.coerceToType(src[i], types[i]);
}
if (m.isVarArgs()) {
Object[] varArgs = (Object[]) Array.newInstance(
m.getParameterTypes()[paramCount - 1].getComponentType(),
src.length - (paramCount - 1));
for (int i = 0; i < src.length - (paramCount - 1); i ++) {
varArgs[i] = ELSupport.coerceToType(src[paramCount - 1 + i],
types[paramCount - 1].getComponentType());
}
dest[paramCount - 1] = varArgs;
} else {
dest[paramCount - 1] = ELSupport.coerceToType(
src[paramCount - 1], types[paramCount - 1]);
}
return dest;
}
示例11: getAccessModifiers
import java.lang.reflect.Method; //导入方法依赖的package包/类
private static int getAccessModifiers(final Method method) {
return ACC_PUBLIC | (method.isVarArgs() ? ACC_VARARGS : 0);
}
示例12: canBeInvokedAs
import java.lang.reflect.Method; //导入方法依赖的package包/类
/**
* Tells whether a method invocation of 'origin' can be transformed in a
* method invocation of 'target'.
* This method only look at the parameter signatures, it doesn't look at
* the name, nor does it look at the return types.
* <p>
* Example:
* <ul>
* <li>java.util.logging.Logger.log(Level, String, Object) can be invoked as<br>
java.util.logging.spi.Logger.log(Level, String, Object...) because the
last parameter in 'target' is a varargs.</li>
* <li>java.util.logging.Logger.log(Level, String) can also be invoked as<br>
java.util.logging.spi.Logger.log(Level, String, Object...) for the
same reason.</li>
* </ul>
* <p>
* The algorithm is tailored for our needs: when the last parameter in the
* target is a vararg, and when origin & target have the same number of
* parameters, then we consider that the types of the last parameter *must*
* match.
* <p>
* Similarly - we do not consider that o(X x, Y y, Y y) matches t(X x, Y... y)
* although strictly speaking, it should...
*
* @param origin The method in the original class
* @param target The correspondent candidate in the target class
* @return true if a method invocation of 'origin' can be transformed in a
* method invocation of 'target'.
*/
public boolean canBeInvokedAs(Method origin, Method target,
Map<Class<?>,Class<?>> substitutes) {
final Class<?>[] xParams = target.getParameterTypes();
final Class<?>[] mParams = Stream.of(origin.getParameterTypes())
.map((x) -> substitutes.getOrDefault(x, x))
.collect(Collectors.toList()).toArray(new Class<?>[0]);
if (Arrays.deepEquals(xParams, mParams)) return true;
if (target.isVarArgs()) {
if (xParams.length == mParams.length) {
if (xParams[xParams.length-1].isArray()) {
return mParams[mParams.length -1].equals(
xParams[xParams.length -1].getComponentType());
}
} else if (xParams.length == mParams.length + 1) {
return Arrays.deepEquals(
Arrays.copyOfRange(xParams, 0, xParams.length-1), mParams);
}
}
return false;
}
示例13: parameterLengthMatches
import java.lang.reflect.Method; //导入方法依赖的package包/类
private static boolean parameterLengthMatches(Method m, MethodCacheKey key) {
return m.isVarArgs() || m.getParameterTypes().length == key.arguments;
}
示例14: convertArgs
import java.lang.reflect.Method; //导入方法依赖的package包/类
private Object[] convertArgs(Object[] src, Method m) {
Class<?>[] types = m.getParameterTypes();
if (types.length == 0) {
// Treated as if parameters have been provided so src is ignored
return EMPTY_ARRAY;
}
int paramCount = types.length;
if (m.isVarArgs() && paramCount > 1 && (src == null || paramCount > src.length)
|| !m.isVarArgs() && (paramCount > 0 && src == null || src != null && src.length != paramCount)) {
String srcCount = null;
if (src != null) {
srcCount = Integer.toString(src.length);
}
String msg;
if (m.isVarArgs()) {
msg = MessageFactory.get("error.invoke.tooFewParams", m.getName(), srcCount,
Integer.toString(paramCount));
} else {
msg = MessageFactory.get("error.invoke.wrongParams", m.getName(), srcCount,
Integer.toString(paramCount));
}
throw new IllegalArgumentException(msg);
}
if (src == null) {
// Must be a varargs method with a single parameter.
// Use a new array every time since the called code could modify the
// contents of the array
return new Object[1];
}
Object[] dest = new Object[paramCount];
for (int i = 0; i < paramCount - 1; i++) {
dest[i] = ELSupport.coerceToType(src[i], types[i]);
}
if (m.isVarArgs()) {
Object[] varArgs = (Object[]) Array.newInstance(m.getParameterTypes()[paramCount - 1].getComponentType(),
src.length - (paramCount - 1));
for (int i = 0; i < src.length - (paramCount - 1); i++) {
varArgs[i] = ELSupport.coerceToType(src[paramCount - 1 + i], types[paramCount - 1].getComponentType());
}
dest[paramCount - 1] = varArgs;
} else {
dest[paramCount - 1] = ELSupport.coerceToType(src[paramCount - 1], types[paramCount - 1]);
}
return dest;
}