16
16
import java .util .jar .JarFile ;
17
17
import java .util .regex .Matcher ;
18
18
import java .util .regex .Pattern ;
19
+ import java .util .stream .Stream ;
19
20
20
21
public class ClassUtil {
21
22
@@ -24,20 +25,37 @@ public class ClassUtil {
24
25
/** Cache of class names vs list of URLs found in the pom.xml files of their contaning jar files, if any. */
25
26
static private final Map <String , JarProperties > class_urls = new HashMap <>();
26
27
28
+ static private final Map <String , JarProperties > package_urls = new HashMap <>();
29
+
30
+ static private boolean ready = false ;
31
+
27
32
/** Cache of subURL javadoc at https://javadoc.scijava.org */
28
33
static private final HashMap <String , String > scijava_javadoc_URLs = new HashMap <>();
29
34
30
- static private final void ensureCache () {
35
+ static public final void ensureCache () {
31
36
synchronized (class_urls ) {
32
37
if (class_urls .isEmpty ()) {
33
38
final ArrayList <String > dirs = new ArrayList <>();
34
39
dirs .add (System .getProperty ("java.home" ));
35
40
dirs .add (System .getProperty ("ij.dir" ));
36
41
class_urls .putAll (findAllClasses (dirs ));
42
+ // Soft attempt at getting all packages (will get them wrong if multiple jars have the same packages)
43
+ for (final Map .Entry <String , JarProperties > entry : class_urls .entrySet ()) {
44
+ final int idot = entry .getKey ().lastIndexOf ('.' );
45
+ if (-1 == idot ) continue ; // no package
46
+ final String package_name = entry .getKey ().substring (0 , idot );
47
+ if (package_urls .containsKey (package_name )) continue ;
48
+ package_urls .put (package_name , entry .getValue ());
49
+ }
50
+ ready = true ;
37
51
}
38
52
}
39
53
}
40
54
55
+ static public final boolean isCacheReady () {
56
+ return ready ;
57
+ }
58
+
41
59
static public final void ensureSciJavaSubURLCache () {
42
60
synchronized (scijava_javadoc_URLs ) {
43
61
if (!scijava_javadoc_URLs .isEmpty ()) return ;
@@ -237,4 +255,30 @@ static public final HashMap<String, JarProperties> findAllClasses(final List<Str
237
255
}
238
256
return class_urls ;
239
257
}
258
+
259
+ static public final Stream <String > findPackageNamesStartingWith (final String text ) {
260
+ ensureCache ();
261
+ return package_urls .keySet ().stream ().filter (s -> s .startsWith (text ));
262
+ }
263
+
264
+ static public final Stream <String > findClassNamesForPackage (final String packageName ) {
265
+ ensureCache ();
266
+ return class_urls .keySet ().stream ().filter (s -> s .startsWith (packageName ) && -1 == s .indexOf ('.' , packageName .length () + 2 ));
267
+ }
268
+
269
+ static public final Stream <String > findClassNamesStartingWith (final String text ) {
270
+ ensureCache ();
271
+ return class_urls .keySet ().stream ().filter (s -> s .startsWith (text ));
272
+ }
273
+
274
+ static public final ArrayList <String > findSimpleClassNamesStartingWith (final String text ) {
275
+ ensureCache ();
276
+ final ArrayList <String > matches = new ArrayList <>();
277
+ for (final String classname : class_urls .keySet ()) {
278
+ final int idot = classname .lastIndexOf ('.' );
279
+ final String simplename = -1 == idot ? classname : classname .substring (idot + 1 );
280
+ if (simplename .startsWith (text )) matches .add (simplename );
281
+ }
282
+ return matches ;
283
+ }
240
284
}
0 commit comments