View Javadoc

1   /*
2    * Created on May 26, 2005
3    *
4    * Used by DefaultFinder to inspect inside JARs to find potential targets.
5    */
6   package net.sf.tlc.core.impl;
7   
8   import java.io.File;
9   import java.io.IOException;
10  import java.util.ArrayList;
11  import java.util.Enumeration;
12  import java.util.zip.ZipEntry;
13  import java.util.zip.ZipFile;
14  
15  import net.sf.tlc.model.Target;
16  import net.sf.tlc.util.ClassPathUtils;
17  
18  /***
19   * Used by DefaultFinder to inspect inside JARs to find potential targets.
20   * 
21   * @author aisrael
22   */
23  public final class JarInspector {
24      
25      /***
26       * JarInspector instances should NOT be constructed in standard programming.
27       */
28      private JarInspector() {
29          // noop
30      }
31  
32      /***
33       * @param jarFile
34       *            File
35       * @return Target[]
36       * @throws IOException on IOException
37       */
38      public static Target[] listTargets(final File jarFile) throws IOException {
39          final ArrayList targets = new ArrayList();
40          final ZipFile zip = new ZipFile(jarFile);
41          final Enumeration entries = zip.entries();
42          while (entries.hasMoreElements()) {
43              final ZipEntry entry = (ZipEntry) entries.nextElement();
44              if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
45                  final String className = ClassPathUtils.toClassName(entry.getName());
46                  targets.add(new Target(Target.TARGET_IS_JAR, jarFile.getAbsolutePath(), className));
47              }
48          }
49          return (Target[]) targets.toArray(new Target[0]);
50      }
51  
52  }