Exemple #1
0
  /**
   * Recursive function to enumerate classes inside a classpath folder
   *
   * @param superClass
   * @param classloader
   * @param classes
   * @param packagePath
   * @param packageName
   */
  private static void enumerateDirectory(
      String prefix,
      Class superClass,
      ClassLoader classloader,
      LinkedList<Class> classes,
      File packagePath,
      String packageName) {
    File[] classFiles = packagePath.listFiles();

    for (File classFile : classFiles) {
      if (classFile.isDirectory()) {
        enumerateDirectory(
            prefix,
            superClass,
            classloader,
            classes,
            classFile,
            packageName + classFile.getName() + ".");
      } else {
        if (classFile.getName().endsWith(".class")
            && (prefix == null || classFile.getName().startsWith(prefix))) {
          String classFileName = classFile.getName();
          String className = packageName + classFileName.substring(0, classFileName.length() - 6);
          checkAndAddClass(classloader, superClass, classes, className);
        }
      }
    }
  }
Exemple #2
0
  /**
   * @param superClass
   * @param classloader
   * @param classes
   * @param packagePath
   * @throws FileNotFoundException
   * @throws IOException
   */
  private static void enumerateCompressedPackage(
      String prefix,
      Class superClass,
      ClassLoader classloader,
      LinkedList<Class> classes,
      File packagePath)
      throws FileNotFoundException, IOException {
    FileInputStream fileinputstream = new FileInputStream(packagePath);
    ZipInputStream zipinputstream = new ZipInputStream(fileinputstream);

    ZipEntry zipentry = null;

    do {
      zipentry = zipinputstream.getNextEntry();

      if (zipentry != null && zipentry.getName().endsWith(".class")) {
        String classFileName = zipentry.getName();
        String className =
            classFileName.lastIndexOf('/') > -1
                ? classFileName.substring(classFileName.lastIndexOf('/') + 1)
                : classFileName;

        if (prefix == null || className.startsWith(prefix)) {
          try {
            String fullClassName =
                classFileName.substring(0, classFileName.length() - 6).replaceAll("/", ".");
            checkAndAddClass(classloader, superClass, classes, fullClassName);
          } catch (Exception ex) {
          }
        }
      }
    } while (zipentry != null);

    fileinputstream.close();
  }