解开Goto语句(例如,用Java替换)

时间:2014-05-20 00:44:11

标签: java goto

编辑:我为没有解释这个而道歉。我很清楚goto目前不是Java语言的功能部分。这个问题不是试图在Java中使用goto,而是试图使用它。

编辑:而且,虽然我非常感谢在给定的具体案例中提供帮助,但这个问题更多的是关注是否存在适用于重构此类代码的过程的确定性方法。

大家好,

这是一个与this one类似的问题,但我们希望更加通用。

简而言之,我正在反编译和修改一个类文件,我想再次重新编译它以供使用,但是我得到了~25个错误,其中大部分都与使用goto和相关的命名块有关。代码。

经过一段时间的环顾,似乎我的选择是要么提供一个比我用于互联网结构的更好的反编译器,要么自己解开它,看看我能做什么就能正确编译。

我的问题是,在尝试将goto语句转换为更健全/普遍接受的控制流关键字时,是否有任何一般指导方针可以遵循?到目前为止,建议似乎是逐案提出的;是否有可以在这些练习中应用的想法?

如果有帮助,我会在这里发布相关代码:

 private void unpackNatives(CompleteVersion version, File targetDir)
        throws IOException
    {
        OperatingSystem os;
        Iterator iterator;
        os = OperatingSystem.getCurrentPlatform();
        Collection libraries = version.getRelevantLibraries();
        iterator = libraries.iterator();
          goto _L1
_L7:
        ZipFile zip;
        ExtractRules extractRules;
        Library library = (Library)iterator.next();
        Map nativesPerOs = library.getNatives();
        if(nativesPerOs == null || nativesPerOs.get(os) == null)
            continue; /* Loop/switch isn't completed */
        File file = new File(Launcher.getInstance().getBaseDirectory(), (new StringBuilder("libraries/")).append(library.getArtifactPath((String)nativesPerOs.get(os))).toString());
        zip = new ZipFile(file);
        extractRules = library.getExtractRules();
        Enumeration entries = zip.entries();
          goto _L2
_L5:
        BufferedInputStream inputStream;
        byte buffer[];
        FileOutputStream outputStream;
        BufferedOutputStream bufferedOutputStream;
        ZipEntry entry = (ZipEntry)entries.nextElement();
        if(extractRules != null && !extractRules.shouldExtract(entry.getName()))
            continue; /* Loop/switch isn't completed */
        File targetFile = new File(targetDir, entry.getName());
        if(targetFile.getParentFile() != null)
            targetFile.getParentFile().mkdirs();
        if(entry.isDirectory())
            continue; /* Loop/switch isn't completed */
        inputStream = new BufferedInputStream(zip.getInputStream(entry));
        buffer = new byte[2048];
        outputStream = new FileOutputStream(targetFile);
        bufferedOutputStream = new BufferedOutputStream(outputStream);
        int length;
        while((length = inputStream.read(buffer, 0, buffer.length)) != -1) 
            bufferedOutputStream.write(buffer, 0, length);
          goto _L3
        Exception exception;
        exception;
        Downloadable.closeSilently(bufferedOutputStream);
        Downloadable.closeSilently(outputStream);
        Downloadable.closeSilently(inputStream);
        throw exception;
_L3:
        Downloadable.closeSilently(bufferedOutputStream);
        Downloadable.closeSilently(outputStream);
        Downloadable.closeSilently(inputStream);
_L2:
        if(entries.hasMoreElements()) goto _L5; else goto _L4
_L4:
        break MISSING_BLOCK_LABEL_339;
        Exception exception1;
        exception1;
        zip.close();
        throw exception1;
        zip.close();
_L1:
        if(iterator.hasNext()) goto _L7; else goto _L6
_L6:
    }

这是我关于SO的第一个问题,因此非常欢迎格式/标签的帮助。

1 个答案:

答案 0 :(得分:-1)

private void unpackNatives(CompleteVersion version, File targetDir)
        throws IOException {
        OperatingSystem os;
        Iterator iterator;
        os = OperatingSystem.getCurrentPlatform();
        Collection libraries = version.getRelevantLibraries();
        iterator = libraries.iterator();
        if(iterator.hasNext()){ // L1
            ZipFile zip; // L7
            ExtractRules extractRules;
            Library library = (Library)iterator.next();
            Map nativesPerOs = library.getNatives();
            if(nativesPerOs == null || nativesPerOs.get(os) == null)
                continue; /* Loop/switch isn't completed */
            File file = new File(Launcher.getInstance().getBaseDirectory(), (new StringBuilder("libraries/")).append(library.getArtifactPath((String)nativesPerOs.get(os))).toString());
            zip = new ZipFile(file);
            extractRules = library.getExtractRules();
            Enumeration entries = zip.entries(); 
            if(entries.hasMoreElements()){ // L2
                     BufferedInputStream inputStream; // L5
                     byte buffer[];
                     FileOutputStream outputStream;
                     BufferedOutputStream bufferedOutputStream;
                     ZipEntry entry = (ZipEntry)entries.nextElement();
                     if(extractRules != null && !extractRules.shouldExtract(entry.getName()))
                         continue; /* Loop/switch isn't completed */
                     File targetFile = new File(targetDir, entry.getName());
                     if(targetFile.getParentFile() != null)
                         targetFile.getParentFile().mkdirs();
                     if(entry.isDirectory())
                         continue; /* Loop/switch isn't completed */
                     inputStream = new BufferedInputStream(zip.getInputStream(entry));
                     buffer = new byte[2048];
                     outputStream = new FileOutputStream(targetFile);
                     bufferedOutputStream = new BufferedOutputStream(outputStream);
                     int length;
                     while((length = inputStream.read(buffer, 0, buffer.length)) != -1) 
                        bufferedOutputStream.write(buffer, 0, length);
                     Downloadable.closeSilently(bufferedOutputStream); // L3
                     Downloadable.closeSilently(outputStream);
                     Downloadable.closeSilently(inputStream);
                     Exception exception;
                     exception;
                     Downloadable.closeSilently(bufferedOutputStream);
                     Downloadable.closeSilently(outputStream);
                     Downloadable.closeSilently(inputStream);
                     throw exception;
                } else { // L4
                    break MISSING_BLOCK_LABEL_339; 
                    Exception exception1;
                    exception1;
                    zip.close();
                    throw exception1;
                    zip.close();
                }
            }
        } // L6

在我看来,在解开之后应该写出这样的东西。我替换了所有goto行引用,因为Java不支持goto。 Goto可以说是糟糕的编码,因为大多数编码人员都同意编码的好方法并不需要goto。基本上'goto'只是跳转到参考代码。在低级别的内存中你可能会看到类似

的内容
jmp <address path to whatever reference>

因此要解开它,只需引用它指向的任何一个并读取一些if / else代码。