开发者

JAVA: if without curly brackets, and relation to continue

开发者 https://www.devze.com 2022-12-17 09:46 出处:网络
I have the following java code fragment while (condition1){ switch (someinteger){ case 1: if(condition2) continue;

I have the following java code fragment

while (condition1){
    switch (someinteger){
        case 1:
            if(condition2) continue;
            // other stuff here
     开发者_如何学编程       break;
        // other cases here
    }
}

All is fine. When I generate a class file and then decompile it using a free tool (JD-gui), I get back the following code.

while (condition1){
    switch (someinteger){
        case 1:
            if(!condition2);
            // other stuff here
            break;
        // other cases here
    }
}

So it changes if(condition2) continue; to if(!condition2); I could not find any info on the other if statement (without braces). Can anyone explain the logic here? Thanks in advance.

EDIT: I did some more tests and the decompiler does not work correctly.

here is the code before:

public void strip(InputStreamReader f1, OutputStreamWriter f2) throws IOException{
    int commentON=0, quoteON=0;
    int b1;
    while ((b1 = f1.read()) != -1){
        switch ((char) b1){
            case '\\':
                    if (commentON==0){
                            quoteON = 1;
                            break;
                    }
                    continue;
            case '\n':
                    if (commentON>0){ commentON=0; continue; }
                    break;
            case '%':
                    if (commentON>0) continue;
                    if (quoteON>0) { quoteON=0; break; }
                    commentON=2;
                    continue;
            default:
                    if (commentON>0) continue;
                    if (quoteON>0) quoteON=0;
                    break;
        }
        f2.write(b1);
    }
}

here is the decompiled code

public void strip(InputStreamReader f1, OutputStreamWriter f2) throws IOException
{
int commentON = 0; int quoteON = 0;

while ((b1 = f1.read()) != -1)
{
  int b1;
  switch ((char)b1)
  {
  case '\\':
    if (commentON == 0);
    quoteON = 1;
    break;
  case '\n':
    if (commentON <= 0) break label109; commentON = 0; break;
  case '%':
    if (commentON <= 0);
    if (quoteON > 0) { quoteON = 0; break label109: }
    commentON = 2;
    break;
  }
  if (commentON <= 0);
  if (quoteON > 0) quoteON = 0;

  label109: f2.write(b1);
}
}

sorry for bothering everyone. :P I'll try to delete this question if I can.


it is near impossible for decompilers reconstruct your original syntax as they are working off the compiler's interpretation of your code.

you write java code, which gets compiled to byte code by the java compiler.

a decompiler then attempts to create java code from the byte code.

since the two code fragments are logically the same, the decompiler has done it's job.

EDIT (saw your comment):

actually, it's quite possible (and this is pretty common) that the decompiler has made an error.

the statement if(!condition2); essentially has no effect whatsoever (provided condition2 is indeed a boolean and not pseudo code).

therefore your first //other stuff here would be processed regardless of condition2 in the decompiled version.

are you sure the decompiled code works correctly?


An if statement with no body ("without curly braces") is simply an empty if statement that executes no code.


It is an alternative/canonical representation of the same logic. Decompilers don't preserve the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消