2014-05-20 3 views
-2

편집 : 설명하지 않은 것에 대해 사과드립니다. goto은 현재 Java 언어의 기능적 부분이 아닙니다. 이 질문은 자바에서 goto을 사용하려고 시도하는 것이 아니라 이 아니고을 사용하려고합니다.Goto 문 축소 (예 : 자바 대체 용)

편집 : 주어진 특정 사례에 도움을 주겠지 만이 질문은 이러한 코드를 리팩토링하는 프로세스에 적용 할 수있는 결정적 방법이 있는지 여부를 조사하는 것입니다.

안녕 모두

,

이것은 this one과 유사하지만 더 일반적으로 추구하는 문제이다.

즉, 클래스 파일을 디 컴파일하고 수정하고 다시 사용하려면 다시 컴파일해야하지만 ~25 개의 오류가 발생합니다. 그 중 대부분은 goto 및 관련 명명 된 코드 블록과 관련되어 있습니다.

주위를 둘러 본 후 내 선택은 인터넷 패브릭에서 사용한 것보다 더 나은 디 컴파일러를 가져 오거나 직접 압축을 풀고 내가 만들 수있는 것이 제대로 컴파일되는지 확인하는 것 같습니다. .

제 질문은 goto 문을 더 정상/일반적으로 허용되는 제어 흐름 키워드로 변환하려고 할 때 따라야 할 일반 지침이 있습니까? 지금까지는 권장 사항이 사안별로 나온 것처럼 보입니다. 그러한 연습을 통해 적용 할 수있는 아이디어가 있습니까? 이 도움이 경우

, 나는 여기에 관련 코드를 게시합니다 :이 SO에 대한 내 첫 번째 질문은

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: 
    } 

입니다, 그래서 형식에 도움/태그 환영입니다 .

+3

goto는 Java에서 기능이 없습니다. 예약 키워드이지만 아무 것도하지 않으므로 소스에 나타나는대로 사용할 수 없습니다. – berry120

+2

if/else, for, while, do/while 또는 switch 문이 어셈블리 언어로 변환되고 사용자가 보유한 문을 파악한 다음 다시 번역하는 방법을 이해하기 만하면됩니다. 그것은 단지 일이며 마술은 아닙니다. –

+1

예를 들어, 위의 맨 마지막 실행 문은 대부분의 메서드에 걸쳐 do/while의 while 문처럼 보입니다. 그러나 당신이 가까이에서 보면 맨 위부터 L1까지의 점프가 있습니다. 그래서 그것은 정말 간단한 진술입니다. –

답변

-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 

내가 뒤섞인 후 이렇게 작성해야합니다. Java가 goto를 지원하지 않기 때문에 모든 goto 라인 참조를 대체했습니다. 대부분의 코더들이 코딩의 좋은 방법은 goto를 필요로하지 않는다는 점에 동의하기 때문에 Goto는 논쟁의 여지가없는 코딩입니다. 기본적으로 'goto'는 참조 코드로 점프하고 있습니다. 낮은 수준의 메모리에 당신은 아마 따라서는 지적 얻으려면, 그 중에 참조, 그것을 수습하는

jmp <address path to whatever reference> 

같은 것을보고하고 경우/다른 코드의 비트를 읽고.

+1

반복자를 반복하지 않습니다. –

+0

@LawrenceDol 코드에 대한 주석을 기반으로하지만 불완전한 코드 인 것처럼 보였습니다. 그리고 OP는 루프에 있지만 goto에 의문의 여지가 없습니다. – Sky

+0

@LawrenceDol 및 OP가 제공 한 정보에 루프 기반을 구현하는 방법에 대해 궁금한 점이 있습니까? – Sky