How can I detect if a value passed via varargs has been autoboxed?
I'm running into an interesting and frustrating problem with auto-boxing and varargs. The problem is that calling a method with varargs, the varargs are autoboxed.
I have a utility method called invoke that looks like this:
public invoke(Object obj, String methodName, Object... args)
The invoke method uses Java reflection to find the appropriate method in obj's class and run it. The problem is when one of the arguments is a primitive. If I call:
invoke(someObject, "someMethod", 1, "SomeString");
and someObject's class contains:
public void someMethod(int i, String s)
I'll fail to find the method because the Invoke method will receive an Integer and a String instead of an int and a String.
Does anyone know how I can either:
1) call invoke in such a way that the int is not autoboxed
2) have invoke detect that an Integer is a auto-boxed int?
Any pointers are aprecciated,
Steve
[971 byte] By [
ssalimana] at [2007-11-27 2:39:53]

> 1) call invoke in such a way that the int is not autoboxed
Impossible, since args is an Object array.
2) have invoke detect that an Integer is a auto-boxed int?
Impossible, since an Integer is an Integer.
Suggestion: aren't you already dancing on the edge of the volcano,
even without considering primitives and autoboxing?
If A extends B, and there are methods:
void f(A a1, A a2)
void f(A a1, B b)
will your code handle invoke(obj, "f", new B(), new B())?
I was afraid the answer was going to be "impossible", but I was hoping to find a sneaky way around it.
To answer your question about
void f(A a1, A a2)
void f(A a1, B b)
and invoke(obj, "f", new B(), new B())
My code will handle it in the sense that it will detect 2 methods that have signatures that can be used with the arguments given, and throw an AmbiguousMethodException (an exception class created for just such an occasion).
One way I can work around the problem would be in my signatureMatches method that I'm using to fetch the appropriate method. It gets all the methods for the class (and parents) and looks for the one method whose signature matches the agruments I have. when I get an argument that belongs to one of the 8 classes that can wrap a primitive (Integer, Boolean, etc), I can call it a match if the method has the primitive. However, this leads to some problematic results.
Scenario 1)
The class has method(int)
If I call invoke(obj, "method", 1) I get the result I want
but if I call invoke(obj, "method", new Integer(1)), I don't. Ideally, the second call should result in a NoSuchMethodException. Though with autoboxing, the code will probably work OK in most cases.
Scenario 2)
The class has both method(int) and method(Integer).
In this case, I always get AmbiguousMethodException when I try to call invoke. I would like to limit the number of times I get AmbiguousMethodException, as this limits the usefulness of my invoke method.
I'm hoping someone has a clever way for me to avoid my rather clumsy workaround.