2013-04-25 2 views
0

java 프로젝트 (zip/jar)에 포함 된 모든 패키지, 클래스를 반환해야합니다. 나는 QDox가 그것을 할 수 있다고 생각한다. 내가 그 클래스 발견 : 그것은 checkZipOrJarFile(File file)라는 메소드를 포함 http://www.jarvana.com/jarvana/view/com/ning/metrics.serialization-all/2.0.0-pre5/metrics.serialization-all-2.0.0-pre5-jar-with-dependencies-sources.jar!/com/thoughtworks/qdox/tools/QDoxTester.java?format=okzip 또는 Jar 프로젝트 구문 분석

package com.thoughtworks.qdox.tools; 

import com.thoughtworks.qdox.JavaDocBuilder; 
import com.thoughtworks.qdox.directorywalker.DirectoryScanner; 
import com.thoughtworks.qdox.directorywalker.FileVisitor; 
import com.thoughtworks.qdox.directorywalker.SuffixFilter; 
import com.thoughtworks.qdox.parser.ParseException; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.PrintStream; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile; 

/** 
* Tool for testing that QDox can parse Java source code. 
* 
* @author Joe Walnes 
*/ 
public class QDoxTester { 

    public static interface Reporter { 
     void success(String id); 

     void parseFailure(String id, int line, int column, String reason); 

     void error(String id, Throwable throwable); 
    } 

    private final Reporter reporter; 

    public QDoxTester(Reporter reporter) { 
     this.reporter = reporter; 
    } 

    public void checkZipOrJarFile(File file) throws IOException { 
     ZipFile zipFile = new ZipFile(file); 
     Enumeration entries = zipFile.entries(); 
     while (entries.hasMoreElements()) { 
      ZipEntry zipEntry = (ZipEntry) entries.nextElement(); 
      InputStream inputStream = zipFile.getInputStream(zipEntry); 
      try { 
       verify(file.getName() + "!" + zipEntry.getName(), inputStream); 
      } finally { 
       inputStream.close(); 
      } 
     } 
    } 

    public void checkDirectory(File dir) throws IOException { 
     DirectoryScanner directoryScanner = new DirectoryScanner(dir); 
     directoryScanner.addFilter(new SuffixFilter(".java")); 
     directoryScanner.scan(new FileVisitor() { 
      public void visitFile(File file) { 
       try { 
        checkJavaFile(file); 
       } catch (IOException e) { 
        // ? 
       } 
      } 
     }); 
    } 

    public void checkJavaFile(File file) throws IOException { 
     InputStream inputStream = new FileInputStream(file); 
     try { 
      verify(file.getName(), inputStream); 
     } finally { 
      inputStream.close(); 
     } 
    } 

    private void verify(String id, InputStream inputStream) { 
     try { 
      JavaDocBuilder javaDocBuilder = new JavaDocBuilder(); 
      javaDocBuilder.addSource(new BufferedReader(new InputStreamReader(inputStream))); 
      reporter.success(id); 
     } catch (ParseException parseException) { 
      reporter.parseFailure(id, parseException.getLine(), parseException.getColumn(), parseException.getMessage()); 
     } catch (Exception otherException) { 
      reporter.error(id, otherException); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     if (args.length == 0) { 
      System.err.println("Tool that verifies that QDox can parse some Java source."); 
      System.err.println(); 
      System.err.println("Usage: java " + QDoxTester.class.getName() + " src1 [src2] [src3]..."); 
      System.err.println(); 
      System.err.println("Each src can be a single .java file, or a directory/zip/jar containing multiple source files"); 
      System.exit(-1); 
     } 

     ConsoleReporter reporter = new ConsoleReporter(System.out); 
     QDoxTester qDoxTester = new QDoxTester(reporter); 
     for (int i = 0; i < args.length; i++) { 
      File file = new File(args[i]); 
      if (file.isDirectory()) { 
       qDoxTester.checkDirectory(file); 
      } else if (file.getName().endsWith(".java")) { 
       qDoxTester.checkJavaFile(file); 
      } else if (file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) { 
       qDoxTester.checkZipOrJarFile(file); 
      } else { 
       System.err.println("Unknown input <" + file.getName() + ">. Should be zip, jar, java or directory"); 
      } 
     } 
     reporter.writeSummary(); 
    } 

    private static class ConsoleReporter implements Reporter { 

     private final PrintStream out; 

     private int success; 
     private int failure; 
     private int error; 

     private int dotsWrittenThisLine; 

     public ConsoleReporter(PrintStream out) { 
      this.out = out; 
     } 

     public void success(String id) { 
      success++; 
      if (++dotsWrittenThisLine > 80) { 
       newLine(); 
      } 
      out.print('.'); 
     } 

     private void newLine() { 
      dotsWrittenThisLine = 0; 
      out.println(); 
      out.flush(); 
     } 

     public void parseFailure(String id, int line, int column, String reason) { 
      newLine(); 
      out.println("* " + id); 
      out.println(" [" + line + ":" + column + "] " + reason); 
      failure++; 
     } 

     public void error(String id, Throwable throwable) { 
      newLine(); 
      out.println("* " + id); 
      throwable.printStackTrace(out); 
      error++; 
     } 

     public void writeSummary() { 
      newLine(); 
      out.println("-- Summary --------------"); 
      out.println("Success: " + success); 
      out.println("Failure: " + failure); 
      out.println("Error : " + error); 
      out.println("Total : " + (success + failure + error)); 
      out.println("-------------------------"); 
     } 

    } 
} 

을, 어쩌면 나를 도울 수 있습니다. 하지만 그것을 사용하는 방법에 대한 예제 또는 자습서를 찾을 수 없습니다.

도움이 필요합니다.

답변

1

QDox는 불행히도 그것을 할 수 없습니다. (나는 여기에 항아리에 대한 QDox 지원을 찾고있었습니다). 위에 나열된 QDox의 소스 코드는 주어진 항아리의 클래스가 QDox에 의해 성공적으로 구문 분석 될 수 있는지 테스트하는 용도로만 사용됩니다. 그러나이 코드는 표준 Java API를 사용하여 원하는대로 처리하는 방법에 대한 단서를 제공합니다. 항아리에서 클래스를 통해 열거하십시오.

// Your jar file 
JarFile jar = new JarFile(jarFile); 
// Getting the files into the jar 
Enumeration<? extends JarEntry> enumeration = jar.entries(); 

// Iterates into the files in the jar file 
while (enumeration.hasMoreElements()) { 
    ZipEntry zipEntry = enumeration.nextElement(); 

    // Is this a class? 
    if (zipEntry.getName().endsWith(".class")) { 

    // Relative path of file into the jar. 
    String className = zipEntry.getName(); 

    // Complete class name 
    className = className.replace(".class", "").replace("/", "."); 

    // Load class definition from JVM - you may not need this, but I want to introspect the class 
    try { 
     Class<?> clazz = getClass().getClassLoader().loadClass(className); 
     // ... I then go on to do some intropsection 

을 당신이 실제로 내가 할, 당신은 getName에서 정지 할 수있는 클래스를 성찰하지 않으려면 : 여기

은 ( analyze jar file programmatically를 SO 여기에 대답 나는 서로 cribbed) 내가 사용하는 코드입니다(). 또한 특히 패키지를 찾으려면 우편 번호에 zipEntry.isDirectory()을 사용할 수 있습니다.

관련 문제