2121import java .io .InputStream ;
2222import java .nio .file .Files ;
2323import java .nio .file .Path ;
24+ import java .util .List ;
2425import java .util .jar .JarEntry ;
2526import java .util .jar .JarInputStream ;
2627import java .util .jar .Manifest ;
3435 */
3536class ModuleGleaner
3637{
37- private String gleanFromManifest ( Manifest mf )
38+ private String gleanAutomaticFromManifest ( Manifest mf )
3839 {
3940 if ( mf != null )
4041 {
@@ -47,7 +48,28 @@ private String gleanFromManifest( Manifest mf )
4748 return null ;
4849 }
4950
50- private String gleanFromModuleInfo ( InputStream inputStream )
51+ private String gleanAutomaticFromJar ( Path jarPath )
52+ {
53+ try ( JarInputStream jis = new JarInputStream ( Files .newInputStream ( jarPath ) ) )
54+ {
55+ return gleanAutomaticFromManifest ( jis .getManifest () );
56+ }
57+ catch ( IOException e )
58+ {
59+ return null ;
60+ }
61+ }
62+
63+ private String gleanAutomatic ( Path path )
64+ {
65+ if ( path .getFileName ().toString ().endsWith ( ".jar" ) )
66+ {
67+ return gleanAutomaticFromJar ( path );
68+ }
69+ return null ;
70+ }
71+
72+ private String gleanFromModuleInfoClass ( InputStream inputStream )
5173 throws IOException
5274 {
5375 String [] moduleName = new String [1 ];
@@ -64,37 +86,37 @@ public ModuleVisitor visitModule( String modName, int modAccess, String modVersi
6486 return moduleName [0 ];
6587 }
6688
67- private String gleanFromJar ( Path jarPath )
89+ private String gleanModuleInfoFromJar ( Path jarPath )
6890 {
6991 try ( JarInputStream jis = new JarInputStream ( Files .newInputStream ( jarPath ) ) )
7092 {
7193 for ( JarEntry entry ; ( entry = jis .getNextJarEntry () ) != null ; )
7294 {
7395 if ( "module-info.class" .equals ( entry .getName () ) )
7496 {
75- String moduleName = gleanFromModuleInfo ( jis );
97+ String moduleName = gleanFromModuleInfoClass ( jis );
7698 if ( moduleName != null )
7799 {
78100 return moduleName ;
79101 }
80102 }
81103 }
82- return gleanFromManifest ( jis . getManifest () ) ;
104+ return null ;
83105 }
84106 catch ( IOException e )
85107 {
86108 return null ;
87109 }
88110 }
89111
90- private String gleanFromClasses ( Path classesPath )
112+ private String gleanModuleInfoFromClasses ( Path classesPath )
91113 {
92114 Path moduleInfoPath = classesPath .resolve ( "module-info.class" );
93115 if ( Files .isRegularFile ( moduleInfoPath ) )
94116 {
95117 try ( InputStream is = Files .newInputStream ( moduleInfoPath ) )
96118 {
97- return gleanFromModuleInfo ( is );
119+ return gleanFromModuleInfoClass ( is );
98120 }
99121 catch ( IOException e )
100122 {
@@ -103,16 +125,32 @@ private String gleanFromClasses( Path classesPath )
103125 return null ;
104126 }
105127
106- public String glean ( Path path )
128+ private String gleanModuleInfo ( Path path )
107129 {
108130 if ( Files .isDirectory ( path ) )
109131 {
110- return gleanFromClasses ( path );
132+ return gleanModuleInfoFromClasses ( path );
111133 }
112134 if ( path .getFileName ().toString ().endsWith ( ".jar" ) )
113135 {
114- return gleanFromJar ( path );
136+ return gleanModuleInfoFromJar ( path );
115137 }
116138 return null ;
117139 }
140+
141+ public JavadocModule glean ( Path artifactPath , List <Path > sourcePaths , List <Path > dependencies , boolean ignoreJPMS )
142+ {
143+ String moduleName = null ;
144+ boolean isAutomatic = false ;
145+ if ( !ignoreJPMS )
146+ {
147+ moduleName = gleanModuleInfo ( artifactPath );
148+ if ( moduleName == null )
149+ {
150+ moduleName = gleanAutomatic ( artifactPath );
151+ isAutomatic = moduleName != null ;
152+ }
153+ }
154+ return new JavadocModule ( moduleName , isAutomatic , artifactPath , sourcePaths , dependencies );
155+ }
118156}
0 commit comments