1616import java .util .jar .JarFile ;
1717import java .util .regex .Matcher ;
1818import java .util .regex .Pattern ;
19+ import java .util .stream .Stream ;
1920
2021public class ClassUtil {
2122
@@ -24,20 +25,37 @@ public class ClassUtil {
2425 /** Cache of class names vs list of URLs found in the pom.xml files of their contaning jar files, if any. */
2526 static private final Map <String , JarProperties > class_urls = new HashMap <>();
2627
28+ static private final Map <String , JarProperties > package_urls = new HashMap <>();
29+
30+ static private boolean ready = false ;
31+
2732 /** Cache of subURL javadoc at https://javadoc.scijava.org */
2833 static private final HashMap <String , String > scijava_javadoc_URLs = new HashMap <>();
2934
30- static private final void ensureCache () {
35+ static public final void ensureCache () {
3136 synchronized (class_urls ) {
3237 if (class_urls .isEmpty ()) {
3338 final ArrayList <String > dirs = new ArrayList <>();
3439 dirs .add (System .getProperty ("java.home" ));
3540 dirs .add (System .getProperty ("ij.dir" ));
3641 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 ;
3751 }
3852 }
3953 }
4054
55+ static public final boolean isCacheReady () {
56+ return ready ;
57+ }
58+
4159 static public final void ensureSciJavaSubURLCache () {
4260 synchronized (scijava_javadoc_URLs ) {
4361 if (!scijava_javadoc_URLs .isEmpty ()) return ;
@@ -237,4 +255,30 @@ static public final HashMap<String, JarProperties> findAllClasses(final List<Str
237255 }
238256 return class_urls ;
239257 }
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+ }
240284}
0 commit comments