简体   繁体   中英

Getting unexpected behavior when using varargs - java

I am very new to java varargs.I have written a java method to return current session according to the below scenario.

method which return session

===========================

private HttpSession getSession(boolean... status) {
    if (status[0]) {
        log.info("this is true");
        session = servletRequest.getSession(true);
        return session;
    } else if (!status[0]) {
        log.info("this is false");
        session = servletRequest.getSession(false);
        return session;
    }
    log.info("outer part true false");
    session = servletRequest.getSession();
    return session;
}

scenario I am expecting

======================

1) we can not send any parameter when calling this method as getSession() - it will return current session if exists or else if will create a new one

2) we can send true as getSession(true) - same as getSession()

3) we can send false as getSession(false) - it will return a session if it is exist or else it should not create a new one

I have chosen varargs because the arguments that we are passing is not determinant can have no argument or can have true/false.

I have called this private method inside a public method as below.

public static void jsFunction_invalidate(Context cx, Scriptable thisObj, Object[] args, Function funObj)
        throws ScriptException {
    String functionName = "invalidate";
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    SessionHostObject sho = (SessionHostObject) thisObj;
    sho.getSession(false).invalidate();

}

So expected behavior is when we call this getSession(false) it should be go inside esle if condition and will log the message"this is false".

But actual output is it is logging both "this is true" and "this is false " but not "outer part true false".

Can anyone help me to succeed the expected outcome and give me some explanation what I did wrong and use of varargs?

You appear to want

boolean flag = status.length == 0 || status[0];
log.info("this is "  + flag);
session = servletRequest.getSession(flag);
return session;

However, it might be simpler to write it this way

private HttpSession getSession() {
    return getSession(true);
}

private HttpSession getSession(boolean status) {

While this is longer it may be clearer and less confusion.

If the method is called without any argument, then the passed in array has the size of zero. You try to access the first index in the if statement, but because the array doesn't have a size, this will throw a IndexOutOfBoundsException . You should account for this in the method by doing:

if (status.length > 0 && status[0]) {
    log.info("this is true");
    session = servletRequest.getSession(true);
    return session;
} else if (status.length > 0 && !status[0]) {
    log.info("this is false");
    session = servletRequest.getSession(false);
    return session;
}

Using var-args seems to be a bit over complicated, a better variant exists with overloading the method:

private HttpSession getSession() {
    return getSession(true);
}

private HttpSession getSession(boolean status) {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM