robotframework / JavalibCore

Base for implementing Java test libraries to be used with Robot Framework
Other
42 stars 25 forks source link

General Varargs #12

Closed Thomas-- closed 4 years ago

Thomas-- commented 10 years ago

At the moment, robot framework accepts only String parameters for methods with a variable parameter list. E.g. the following is not possible: @RobotKeyword("My Keyword") @ArgumentNames({ "time", "*parameters" }) public void foo(Timestamp time, String[] parameters) ...

I suggest to change the method extractArguments(...) in the class ArgumentGrouper as follows: private Object[] extractArguments(Object[] ungroupedArguments) { int nonVarargsLength = parameterTypes.length - 1; int varargsLength = ungroupedArguments.length - nonVarargsLength; Object[] groupedArguments = new Object[nonVarargsLength + 1]; for (int i = 0; i < nonVarargsLength; i++) { groupedArguments[i] = ungroupedArguments[i]; } Class<?> varargsComponentType = parameterTypes[parameterTypes.length - 1].getComponentType(); Object[] varargs = (Object[]) Array.newInstance(varargsComponentType, varargsLength); for (int i = 0; i < varargsLength; i++) { Object varargI = ungroupedArguments[nonVarargsLength + i]; if (varargI != null && !varargsComponentType.isInstance(varargI)) { throw new AssertionError("Wrong type for varargs parameter"); } varargs[i] = varargI; } groupedArguments[parameterTypes.length - 1] = varargs; return groupedArguments; }

Also delete the method asStrings(...) and invoke the new method directly, i.e.: public Object[] groupArguments(Object[] ungroupedArguments) { if (shouldGroupArguments(ungroupedArguments)) { return extractArguments(ungroupedArguments); } else { return ungroupedArguments; } }

That way, not only the keyword above works but also the framework can accept general varargs like public void foo(Timestamp[] times).