0

In short, I have a function that accepts a variable number of params and another function that also accepts a variable number of paramaters, I need to forward all the variable paramaters in the method plus some extra to the second method that accepts variable paramaters ...

action1(new Filter1());
action2(new Filter1(), new Filter2());

public void action1(Object ... params){
    actionGeneric(new Action1(), params);
}

public void action2(Object ... params){
    actionGeneric(new Action2(), params);    
}

public void actionGeneric(Object ... params){
    for (Object param : params){
        if (param instanceof Action1){
            // works fine
        } else if (param instanceof Action2){
            // works fine
        } else if (param instanceof Filter1){
            // never gets here
        } else if (param instanceof Filter2){
            // never gets here
        }
    }
}

So my question is, how should I be forwarding those params in action1 / action2 in order for actionGeneric to see them as Filter1 / Filter2 instances and not (I'm guessing) array type?

Jan Vladimir Mostert
  • 12,380
  • 15
  • 80
  • 137
  • A solution would be to change your design. Make your `Action1` and `Action2` implement a common interface to avoid usage of `instanceof`. Similar for your `FilterX` and pass something different than `Object ... params`. – Luiggi Mendoza Sep 01 '14 at 06:19
  • Something looks wrong - this code should reach the "never gets here" lines. Is it possible to debug by printing out `param.getClass().getName()` inside the for loop? – ash Sep 01 '14 at 06:20
  • `java` functions don't start with `function`. Please post real code. Or is this `javascript`? – Elliott Frisch Sep 01 '14 at 06:21
  • Sorry, it's java code, working in multiple languages at the moment, so mixed some stuff in there that should not be there. – Jan Vladimir Mostert Sep 01 '14 at 06:31
  • 1
    possible duplicate of [How to add arguments to varargs?](http://stackoverflow.com/questions/18475232/how-to-add-arguments-to-varargs) – Raedwald Sep 01 '14 at 06:57

2 Answers2

4

In the action2() method, you call the actionGeneric() method with two arguments: new Action2() and params (the latter of which is an Object array). Thus, from the point of view of the actionGeneric() method, some arguments are of type Action2 but not Filter1. The solution, I think, would be to create a new array before passing it to the actionGeneric() method, along the lines of...

Object[] newParams = new Object[params.length + 1];
newParams[0] = new Action1();
System.arraycopy(params, 0, newParams, 1, params.length);
actionGeneric(newParams);
Jae Heon Lee
  • 1,101
  • 6
  • 10
1

If a single array is passed to a variable-argument method, it will be treated as variable arguments. So, create a new array like this:

  Object[] newArray = new Object[params.length + 1];
  newArray[0] = new Action1();
  System.arraycopy(params, 0, newArray, 1, params.length);

  actionGeneric(newArray);
ash
  • 4,867
  • 1
  • 23
  • 33