private static SortedSet<String> findApplicationPackageNames(ClassLoader cl) {
    SortedSet<String> packages = new TreeSet<>();
    while (cl != null) {
      if (cl instanceof URLClassLoader) {
        for (URL url : ((URLClassLoader)cl).getURLs()) {
          String path = url.getPath();
          if (!path.startsWith(JAVA_HOME) && !path.startsWith(MACOS_JAVA_EXTENSIONS_DIR) &&
            path.endsWith(JAR_FILE_SUFFIX)) {
            try {
              try (ZipInputStream zip = new ZipInputStream(url.openStream())) {
                for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
                  if (!entry.isDirectory() && entry.getName().endsWith(CLASS_FILE_SUFFIX)) {
                    // This ZipEntry represents a class. Now, what class does it represent?
                    String className = entry.getName().replace('/', '.'); // including ".class"
                    className = className.substring(0, className.length() - CLASS_FILE_SUFFIX.length());
                    if (className.contains(".") && !className.startsWith(STREAMSETS_PACKAGE)) {
                      // must end with a . as we don't want o.a.h matching o.a.ha
                      packages.add(className.substring(0, className.lastIndexOf('.')) + ".");
                    }
                  }
                }
              }
            } catch (IOException unlikely) {
              // since these are local URL we will likely only
              // hit this if there is a corrupt jar in the classpath
              // which we will ignore
              if (SDCClassLoader.isDebug()) {
                System.err.println("Error opening '" + url + "' : " + unlikely);
                unlikely.printStackTrace();
              }
            }
          }
        }
      }
      cl = cl.getParent();
    }
    SystemPackage systemPackage = new SystemPackage(SDCClassLoader.SYSTEM_API_CHILDREN_CLASSES);
    Iterator<String> iterator = packages.iterator();
    while (iterator.hasNext()) {
      String packageName = iterator.next();
      if (systemPackage.isSystem(packageName)) {
        iterator.remove();
      }
    }
    removeLogicalDuplicates(packages);
    return packages;
  }
private static SortedSet < String > findApplicationPackageNames ( ClassLoader cl ) { SortedSet < String > packages = new TreeSet < > ( ) ; while ( cl != null ) { if ( cl instanceof URLClassLoader ) { for ( URL url : ( ( URLClassLoader ) cl ) . getURLs ( ) ) { String path = url . getPath ( ) ; if ( ! path . startsWith ( JAVA_HOME ) && ! path . startsWith ( MACOS_JAVA_EXTENSIONS_DIR ) && path . endsWith ( JAR_FILE_SUFFIX ) ) { try { try ( ZipInputStream zip = new ZipInputStream ( url . openStream ( ) ) ) { for ( ZipEntry entry = zip . getNextEntry ( ) ; entry != null ; entry = zip . getNextEntry ( ) ) { if ( ! entry . isDirectory ( ) && entry . getName ( ) . endsWith ( CLASS_FILE_SUFFIX ) ) { String className = entry . getName ( ) . replace ( '/' , '.' ) ; className = className . substring ( 0 , className . length ( ) - CLASS_FILE_SUFFIX . length ( ) ) ; if ( className . contains ( "." ) && ! className . startsWith ( STREAMSETS_PACKAGE ) ) { packages . add ( className . substring ( 0 , className . lastIndexOf ( '.' ) ) + "." ) ; } } } } } catch ( IOException unlikely ) { if ( SDCClassLoader . isDebug ( ) ) { System . err . println ( "Error opening '" + url + "' : " + unlikely ) ; unlikely . printStackTrace ( ) ; } } } } } cl = cl . getParent ( ) ; } SystemPackage systemPackage = new SystemPackage ( SDCClassLoader . SYSTEM_API_CHILDREN_CLASSES ) ; Iterator < String > iterator = packages . iterator ( ) ; while ( iterator . hasNext ( ) ) { String packageName = iterator . next ( ) ; if ( systemPackage . isSystem ( packageName ) ) { iterator . remove ( ) ; } } removeLogicalDuplicates ( packages ) ; return packages ; }
Finds package names associated with a class loader which which are not JVM level
packages. Anything inside java home are specifically excluded as well as some
OS specific install locations (MacOS).