简体   繁体   English

使用varargs时出现意外行为-Java

[英]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. 我对java varargs非常陌生,我根据以下情况编写了java方法来返回当前会话。

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 1)当调用此方法作为getSession()时,我们无法发送任何参数-如果存在,它将返回当前会话,否则将创建一个新会话

2) we can send true as getSession(true) - same as getSession() 2)我们可以将true作为getSession(true)发送-与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 3)我们可以将false作为getSession(false)发送-如果存在会话,它将返回一个会话,否则它将不创建一个新会话

I have chosen varargs because the arguments that we are passing is not determinant can have no argument or can have true/false. 之所以选择varargs,是因为我们传递的参数不是行列式,可以没有参数,也可以有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". 因此,预期的行为是当我们调用此getSession(false)时,如果有条件,则应进入esle内,并将记录消息“ this is false”。

But actual output is it is logging both "this is true" and "this is false " but not "outer part true false". 但是实际输出是它记录了“ this is true”和“ this is false”两者,而不是“外部true false”。

Can anyone help me to succeed the expected outcome and give me some explanation what I did wrong and use of varargs? 谁能帮助我成功达到预期的结果,并给我一些解释我做错了什么以及使用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 . 您尝试访问if语句中的第一个索引,但是由于数组没有大小,因此将抛出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: 使用var-args似乎有点复杂,存在一个更好的变量,该方法重载了:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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