I have non-standard question.
Lets say I have class MyClass
that I execute from command line with list of arguments
[<key> <value>]
like:
MyClass -key1 arg1 -key2 arg2 -key3 arg3 ... -keyN argN.
therefore my class has followed method that passes input data to fileds:
public class MyClass{
private static String arg1;
private static String arg2;
private static String arg3;
...
private static String argN;
private static void getArgs(String[] args) {
for(int k=0; k<args.length; k=k+2){
if(args[k].equals("-key1")){
arg1 = args[k+1];
}
else if(args[k].equals("-key2")){
arg2 = args[k+1];
}
else if(args[k].equals("-key3")){
arg3 = args[k+1];
}
...
else if(args[k].equals("-keyN")){
argN = args[k+1];
}
}
}
I write automation and have about 30-40 MyClass
's where each one has 10-30 parameters.
All classes have the same fields and different ones as well (50/50).
I don't like private static void getArgs(String[] args)
method implementation, seems waste of time.
I though to write in Notepad++ some script based on Reg-ex and generates java code but before I want to ask if someone knows other technologies/techniques to take care about input parameters.
[EDIT]
I thought to use reflection, something like:
for(int k=0; k<args.length; k=k+2){
Field field = classInstance.getClass().getDeclaredField(args[k].substring(1));
field.setAccessible(true);
field.set(classInstance, args[k]);
}
Thank you,