The problem is that something strange is going on inside my code; Let's go to my example (i cleaned it a bit):
public int foo() throws IOException {
if(redirect_url.indexOf("statement_1") != -1){
if(check() == true){
//do something
}else{
//do something
foo(); //yep, recursive call
}
}else if(redirect_url.indexOf("statement_2") != -1) {
map.clear();
bar();
map.put("val", 1);
map.put("val2", "2");
foo(); //yep, recursive call; Remember this line!!
}else if(redirect_url.indexOf("statement3") != -1) {
return AUTH_SUCCESS;
}else if(redirect_url.indexOf("statement4") != -1){
return AUTH_SUCCESS;
}else {
return AUTH_FAILED;
}
}catch (Exception e){
return AUTH_FAILED;
}
return AUTH_FAILED;
}
There is a little function that is called by another function, let's call it buzz
public void buzz(){
try {
switch (signInAttempt()){
case AUTH_SUCCESS:
//do smth
return true;
case AUTH_FAILED:
//do smth
return false;
case ACCESS_REQUEST:
//do smth
default:
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
I've unpacked my awesome debugger when noticed that there were logical errors in my code and found out funny thing.
Let's say that redirect_url string has "statement4" substring, so that the fourth elseif clause (don't count inner elseif clause) will go inside and return AUTH_SUCCESS. I thought so.
And here is the issues, when the return AUTH_FAILED is triggered, the next instruction is calling foo() function inside second else if statement. I do not have any idea why this could happened. So strange. And ideas?
UPD 1 : Constants defined inside the class: Example
private static final int AUTH_SUCCESS = 4;
UPD 2 Some more code:
Meet the calling function
public boolean rollIn(){
try {
switch (signInAttempt()){
case AUTH_SUCCESS:
//do smth
case AUTH_FAILED:
return false;
case ACCESS_REQUEST:
return true;
default:
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
And finally meet the patient:
public int signInAttempt() throws IOException {
try {
/*connection*/
this.doc = connection.parse();
System.out.println(redirect_url);
if(redirect_url.indexOf("authorize") != -1){
if(findCaptcha() == true){
signInAttempt();
}else{
authData.clear();
signInAttempt();
}
}else if(redirect_url.indexOf("authcheck") != -1) {
authData.clear();
authData.put("v1", 1);
authData.put("v2", 2);
System.out.println(action_url);
signInAttempt();
}else if(redirect_url.indexOf("__q_hash") != -1) {
System.out.println("AUTH SUCCESS");
return AUTH_SUCCESS;
}else if(redirect_url.indexOf("access_token") != -1){
return AUTH_SUCCESS;
}else {
return AUTH_FAILED;
}
}catch (Exception e){
return AUTH_FAILED;
}
return AUTH_FAILED;
}
Something like this
In places where you do recursive calls, you should have return foo();
.
A short explanation how it works now - without return
:
return AUTH_FAILED;
So even, if recursive call returned AUTH_SUCCESS
, the first call will ignore it and return AUTH_FAILED
anyway.
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.