示例#1
0
 private void handleDir(
     File packDir, boolean scanRecursively, String packName, Set<String> foundClasses) {
   if (packDir != null && packDir.exists() && packDir.canRead()) {
     for (File file : packDir.listFiles()) {
       if (file.isFile()) {
         if (Files.isUsable(file) && Files.isClass(file.getName())) {
           foundClasses.add(Files.filenameToClassname(packName + "." + file.getName()));
         }
       }
       if (file.isDirectory() && scanRecursively) {
         handleDir(file, scanRecursively, packName + "." + file.getName(), foundClasses);
       }
     }
   }
 }
示例#2
0
  private void handleJar(
      URI resourceUri, boolean scanRecursively, String packName, Set<String> foundClasses) {

    // Currently we only support jar:file
    if (resourceUri.getSchemeSpecificPart().startsWith(PROCOTOL_FILE)) {

      // Get the JAR file path, e.g. "jar:file:/home/duke/duke.jar!/com/foo/Bar" becomes
      // "/home/duke/duke.jar"
      String path = resourceUri.getSchemeSpecificPart().substring(PROTOCOL_FILE_PART.length());
      if (path.lastIndexOf(JAR_URL_SEPARATOR) > 0) {
        path = path.substring(0, path.lastIndexOf(JAR_URL_SEPARATOR));
      }

      JarFile jar = null;
      String packNamePath = packName.replace('.', '/');
      int expectedPartsLength = splitBySlash(packNamePath).length + 1;

      try {
        jar = new JarFile(new File(path));
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
          JarEntry entry = entries.nextElement();
          if (!entry.getName().endsWith(Files.CLASS_FILE_EXTENSION)) {
            continue;
          }
          if (entry.getName().startsWith(packNamePath)) {
            if (scanRecursively) {
              foundClasses.add(Files.filenameToClassname(entry.getName()));
            } else {
              String[] parts = splitBySlash(entry.getName());
              if (parts.length == expectedPartsLength) {
                foundClasses.add(Files.filenameToClassname(entry.getName()));
              }
            }
          }
        }
      } catch (IOException e) {
        CommonLogger.LOG.couldNotReadResource(resourceUri, e);
      } finally {
        if (jar != null) {
          try {
            jar.close();
          } catch (IOException ignored) {
          }
        }
      }
    }
  }