JAVA:如果没有大括号,则关系继续

发布于 2024-08-17 23:35:02 字数 2216 浏览 2 评论 0原文

我有以下 java 代码片段

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

一切都很好。当我生成一个类文件,然后使用免费工具(JD-gui)反编译它时,我得到以下代码。

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

因此它将 if(condition2) continue; 更改为 if(!condition2); 我找不到有关其他 if 语句(不带大括号)的任何信息。 谁能解释一下这里的逻辑吗?提前致谢。

编辑:我做了一些更多的测试,反编译器无法正常工作。

这是之前的代码:

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);
    }
}

这是反编译的代码

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);
}
}

抱歉打扰大家。 :P 如果可以的话我会尝试删除这个问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角迷惘 2024-08-24 23:35:02

反编译器几乎不可能重建原始语法,因为它们正在处理编译器对代码的解释。

你编写java代码,java编译器将其编译为字节码。

然后,反编译器尝试从字节码创建 Java 代码。

由于两个代码片段在逻辑上是相同的,因此反编译器已经完成了它的工作。

编辑(看到你的评论):

实际上,反编译器很可能(而且这很常见)犯了错误。

语句 if(!condition2); 本质上没有任何效果(前提是 condition2 确实是布尔值而不是伪代码)。

因此,无论反编译版本中的condition2如何,您的第一个//other stuff here都会被处理。

你确定反编译后的代码可以正常工作吗?

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?

倚栏听风 2024-08-24 23:35:02

没有主体的 if 语句(“没有大括号”)只是一个不执行任何代码的空 if 语句。

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

情域 2024-08-24 23:35:02

它是相同逻辑的替代/规范表示。反编译器不保留代码。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文