/** * 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); }
/** * 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); }
@Override public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException { try { ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.ctor, this.varargsPosition); if (this.ctor.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.ctor.getParameterTypes(), arguments); } ReflectionUtils.makeAccessible(this.ctor); return new TypedValue(this.ctor.newInstance(arguments)); } catch (Exception ex) { throw new AccessException("Problem invoking constructor: " + this.ctor, ex); } }
@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); } }
@Test public void testConvertArguments() throws Exception { StandardTypeConverter tc = new StandardTypeConverter(); Method oneArg = TestInterface.class.getMethod("oneArg", String.class); Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class); // basic conversion int>String Object[] args = new Object[] {3}; ReflectionHelper.convertArguments(tc, args, oneArg, null); checkArguments(args, "3"); // varargs but nothing to convert args = new Object[] {3}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3"); // varargs with nothing needing conversion args = new Object[] {3, "abc", "abc"}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3", "abc", "abc"); // varargs with conversion required args = new Object[] {3, false ,3.0d}; ReflectionHelper.convertArguments(tc, args, twoArg, 1); checkArguments(args, "3", "false", "3.0"); }
@Override public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException { try { ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.ctor, this.varargsPosition); if (this.ctor.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.ctor.getParameterTypes(), arguments); } ReflectionUtils.makeAccessible(this.ctor); return new TypedValue(this.ctor.newInstance(arguments)); } catch (Exception ex) { throw new AccessException("Problem invoking constructor: " + this.ctor, ex); } }
@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); } }
/** * 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); }
@Override public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException { try { ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.ctor, this.varargsPosition); if (this.ctor.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.ctor.getParameterTypes(), arguments); } ReflectionUtils.makeAccessible(this.ctor); return new TypedValue(this.ctor.newInstance(arguments)); } catch (Exception ex) { throw new AccessException("Problem invoking constructor: " + this.ctor, ex); } }
@Override public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException { try { this.argumentConversionOccurred = ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.method, this.varargsPosition); if (this.method.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.method.getParameterTypes(), arguments); } ReflectionUtils.makeAccessible(this.method); Object value = this.method.invoke(target, arguments); return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.method, -1)).narrow(value)); } catch (Exception ex) { throw new AccessException("Problem invoking method: " + this.method, ex); } }