Programmer's notes

Sunday, February 15, 2009

Calling a varargs method from another vararg method in Java 5+

I've decided to blog about this, because I ran into this kind of problem and I couldn't find the answer anywhere, until I finally figured out how it works.
Every Java developer knows that Java 5+ supports variable argument methods. You can find a plenty of tutorials and examples how to write a method with a variable number of arguments, so I'll jump right at the problem that I had.
Imagine that you have the following methods in one or two classes:

void callMethod(String name, Object... args) {...}

void sendMessage(String message, Object... params) {...}

Now imagine that the implementation of the callMethod has to call the sendMessage method. Note that both of these methods have a vararg parameter and therefore the callMethod can be called with an arbitrary number of actual parameters. The question is, how to pass the array of parameters args into the sendMessage method. The answer is easy enough if you want to pass the args as they are. Here is a sample code:


void callMethod(String name, Object... args) {
//...
sendMessage(name, args);
//...
}

This code passes all of the parameters that were used to call the callMethod to the sendMessage method and therefore params parameter will be an array containing all of those arguments.
Now the problem I was trying to solve was, that I wanted to pass into the sendMessage method all of the args and in addition to it also one or more parameters. These parameters should have been the first of the passed arguments into the params variable. Let's say that I wanted to pass the number of parameters and then the actual parameters. Here is a code that does that:

void callMethod(String name, Object... args) {
//...
Object[] newArgs = new Object[args.length];
newArgs[0] = args.length;
System.arraycopy(args, 0, newArgs, 1, args.length);
sendMessage(name, newArgs);
//...
}
This way I was able to pass a modified number of arguments into a vararg method. The array newArgs is not going to be put into the first element of the params array of objects, but it is passed "as it is" (no new array needs to be created). So for example, if you called the callMethod this way

callMethod("method1", "param1", 2, 3.0);

then the sendMessage's params vararg parameter would have the following values:

[3, "param1", 2, 3.0]

Even though this might seem easy to some people, I think that there should be a better way how to do this. I don't really like that I have to create a new array, set the elements manually and then copy the rest of the arguments. I think it would be great if Java provided a special syntax for these cases, such as for example "^" prefix for the array/vararg variables whose elements should be copied into the vararg array of the called method. For example the following code would do the same thing like the code above:

void callMethod(String name, Object... args) {
//...
sendMessage(name, args.length, ^args); // this doesn't work in Java, but it would be nice
//...
}

What do you think? Do you agree that Java should have something like this in its syntax?

0 Comments:

Post a Comment

<< Home