【问题标题】:JAVA: if without curly brackets, and relation to continueJAVA:如果没有大括号,关系继续
【发布时间】:2010-01-14 00:41:44
【问题描述】:

我有以下 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 如果可以的话,我会尝试删除这个问题。

【问题讨论】:

  • 顺便说一句...我不是在争论反编译代码的正确性。我很惊讶if(condition2) continue;if(!condition2); 相同这是否意味着执行会自动从正确的点继续(在这种情况下,是while 循环的开头)?
  • 只是我还是他们在逻辑上不一样。在第一组代码中,如果condition1 = truesomeInteger = 1condition2 = true// other stuff here 不会 被执行。但是在第二个代码块中具有相同条件// other stuff here会被执行。
  • @David - 我和你在一起,我没看到。
  • 我的猜测是它在 // other stuff here 部分中没有任何内容进行编译。这将使它在逻辑上是正确的 - 只有 OP 对它的解释是错误的(// other stuff here 属于条件的“then”部分)
  • @Anon: - 是的,我认为你是对的。

标签: java syntax decompiler


【解决方案1】:

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

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

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

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

编辑(看到您的评论):

实际上,反编译器很可能(这很常见)出错了。

if(!condition2); 语句基本上没有任何作用(前提是 condition2 确实是一个布尔值而不是伪代码)。

因此,无论反编译版本中的condition2 是什么,都会处理您的第一个//other stuff here

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

【讨论】:

  • OP 假设 //other stuff here 可能仍会出现在那里。请参阅@Anon 对问题的评论。
  • 我同意,无论是那个还是反编译器出错,尽管我没有使用他提到的反编译器,但根据我的经验,这很常见。
【解决方案2】:

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

【讨论】:

  • 错误 - 这是一个 if 语句,如果条件为真则执行下一行代码,如果条件为假则跳过它...
  • @Basic:一般情况下是这样,但是您将我的回答断章取义; OP 使用短语“没有花括号”来表示语句 if(!condition2);
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2015-12-18
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多