protected void loadImplementationsInJar ( String parentPackage , InputStream parentFileStream , ClassLoader loader , String parentFileName , String grandparentFileName ) throws IOException { Set < String > classFiles = classFilesByLocation . get ( parentFileName ) ; if ( classFiles == null ) { classFiles = new HashSet < String > ( ) ; classFilesByLocation . put ( parentFileName , classFiles ) ; Set < String > grandparentClassFiles = classFilesByLocation . get ( grandparentFileName ) ; if ( grandparentClassFiles == null ) { grandparentClassFiles = new HashSet < String > ( ) ; classFilesByLocation . put ( grandparentFileName , grandparentClassFiles ) ; } JarInputStream jarStream ; if ( parentFileStream instanceof JarInputStream ) { jarStream = ( JarInputStream ) parentFileStream ; } else { jarStream = new JarInputStream ( parentFileStream ) ; } JarEntry entry ; while ( ( entry = jarStream . getNextJarEntry ( ) ) != null ) { String name = entry . getName ( ) ; if ( name != null ) { if ( name . endsWith ( ".jar" ) ) { //in a nested jar log . fine ( LogType . LOG , "Found nested jar " + name ) ; // To avoid needing to unzip 'parentFile' in its entirety, as that // may take a very long time (see CORE-2115) or not even be possible // (see CORE-2595), we load the nested JAR from the classloader and // read it as a zip. // // It is safe to assume that the nested JAR is readable by the classloader // as a resource stream, because we have reached this point by scanning // through packages located from `classloader` by using `getResource`. // If loading this nested JAR as a resource fails, then certainly loading // classes from inside it with `classloader` would fail and we are safe // to exclude it form the PackageScan. InputStream nestedJarResourceStream = loader . getResourceAsStream ( name ) ; if ( nestedJarResourceStream != null ) { JarInputStream nestedJarStream = new JarInputStream ( nestedJarResourceStream ) ; try { loadImplementationsInJar ( parentPackage , nestedJarStream , loader , parentFileName + "!" + name , parentFileName ) ; } finally { nestedJarStream . close ( ) ; } } } else if ( ! entry . isDirectory ( ) && name . endsWith ( ".class" ) ) { classFiles . add ( name . trim ( ) ) ; grandparentClassFiles . add ( name . trim ( ) ) ; } } } } for ( String name : classFiles ) { if ( name . contains ( parentPackage ) ) { loadClass ( name , loader ) ; } } }
Finds matching classes within a jar files that contains a folder structure matching the package structure . If the File is not a JarFile or does not exist a warning will be logged but no error will be raised .