0

Not duplicate question

As I stated downwards, I already checked how to work in java with different number of params. It wraps them into an array, and go ahead with it. But I need to pass different number of params to a function, not an array, since it will be treated with javascript


I am working with Nashorn in order to execute some javascript scripts in java. I am trying to modularize as much as possible the code, in order to be easy to use.

I have the following function to call a javascript method already:

MyClass{
....
    public static Object call(ScriptObjectMirror ctx, String functionName, Object ...args ) {
        ScriptObjectMirror caller = ctxs.get(ctx); // assume this works, it brings correct data
        return caller.call(ctx, functionName, args);
    }
....
}

So, I have read different questions like Can I pass an array as arguments to a method with variable arguments in Java?

Where it states clearly that Object... is the same as Object[].

However, when I am working on javascript side, I receive different things depending on the args:

Lets say I do the following call:

MyClass.call(myCtx, "a", true, false);

It will call:

caller.call(myCtx, "a", [true, false]); //Javascript prints: [Ljava.lang.Object;@273d3dbb

But, If I do skip the method MyClass.call in order to directly invoke javascript function like this:

caller.call(myCtx, "a", true, false); //Javascript prints true, false

THEREFOR

I can understand that java internally works fine with both (Object... and Object[]) BUT since I am using Nashorn, I really need to be able to get args, and split them.

Something like:

public static Object call(ScriptObjectMirror ctx, String functionName, Object ...args ) {
    //Pseudocode of what I want to achieve
    for (arg: args) {
        arg1 = arg;
        arg2 = arg;
        argN = arg;
    }
    return caller.call(ctx, functionName, arg1, arg2, ..., argN);

}

Is there any way to achieve this? / Any workaround?

NOTE

I can not just in javascript side assume I always receive an array with different arguments, since it can be called from a lot of different places, and would mean to recode really a lot of code

Community
  • 1
  • 1
Mayday
  • 4,680
  • 5
  • 24
  • 58
  • Do you need to use vararg here ? – LazerBanana Feb 28 '17 at 08:58
  • No you can't, except if you know the number of parameters in advance as you suggested. – Jean-Baptiste Yunès Feb 28 '17 at 08:58
  • @Krystian_K yes, I do, since it can receive any number of arguments – Mayday Feb 28 '17 at 09:00
  • @Jean-BaptisteYunès Is it possible that a language like Java, does not support to make the exact same call from outside method to inner method? :O like: a(1,2,3) return b(1,2,3) for any ammount of params? – Mayday Feb 28 '17 at 09:01
  • Well answer is here http://stackoverflow.com/questions/2925153/can-i-pass-an-array-as-arguments-to-a-method-with-variable-arguments-in-java – Jean-Baptiste Yunès Feb 28 '17 at 09:04
  • Possible duplicate of [Can I pass an array as arguments to a method with variable arguments in Java?](http://stackoverflow.com/questions/2925153/can-i-pass-an-array-as-arguments-to-a-method-with-variable-arguments-in-java) – Jean-Baptiste Yunès Feb 28 '17 at 09:04
  • It is a different question, as I linked that one already. In my case, I need to make a javascript call with different number of arguments. That question only answers that you can work with that in java, but does not really give a workaround to call a method with all the params, instead of wrap them into arrays – Mayday Feb 28 '17 at 09:08

1 Answers1

1

You can't do exactly what Java could do, but you can do something similar.

Javascript function does support receive unlimited numbers of parameters. You can access the parameters by arguments keyword.

Please kindly read this.

For example:

function foo(){
    for(var i=0; i<arguments.length; i++){
       document.write(arguments[i] +'<br />');
    }
}

foo("A", "B", "C", 1, 2, 3);
Solomon Tam
  • 739
  • 3
  • 11
  • Thats a decent workaround, not exactly what I would like to achieve, but it can help if I do not find anything better. Thanks! – Mayday Feb 28 '17 at 09:27