Skip to content

Commit

Permalink
JrtJavaBasePackages: before class search check that package exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 11, 2021
1 parent 4da3433 commit 273ac1e
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions groovy/src/main/java/org/jline/script/JrtJavaBasePackages.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
*/
public class JrtJavaBasePackages {
public static List<Object> getClassesForPackage(String pckgname) {
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
List<String> dirs = new ArrayList<>();
dirs.add("java.base");
List<Object> out = new ArrayList<>();
boolean nestedClasses = true;
boolean onlyCurrent = false;
if (pckgname.endsWith(".*")) {
Expand All @@ -40,21 +38,28 @@ public static List<Object> getClassesForPackage(String pckgname) {
onlyCurrent = true;
pckgname = pckgname.substring(0, pckgname.length() - 3);
}
dirs.addAll(Arrays.asList(pckgname.split("\\.")));
Path path = fs.getPath("modules", dirs.toArray(new String[0]));
FileVisitor fv = new FileVisitor(pckgname, nestedClasses);
try {
if (onlyCurrent) {
Files.walkFileTree(path, new HashSet<>(),1, fv);
} else {
Files.walkFileTree(path, fv);
}
} catch (IOException e) {
if (Log.isDebugEnabled()) {
e.printStackTrace();
final String packageName = pckgname;
if (Arrays.stream(Package.getPackages()).anyMatch(p->p.getName().startsWith(packageName))) {
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
List<String> dirs = new ArrayList<>();
dirs.add("java.base");
dirs.addAll(Arrays.asList(pckgname.split("\\.")));
Path path = fs.getPath("modules", dirs.toArray(new String[0]));
FileVisitor fv = new FileVisitor(packageName, nestedClasses);
try {
if (onlyCurrent) {
Files.walkFileTree(path, new HashSet<>(), 1, fv);
} else {
Files.walkFileTree(path, fv);
}
} catch (IOException e) {
if (Log.isDebugEnabled()) {
e.printStackTrace();
}
}
out = fv.getClasses();
}
return fv.getClasses();
return out;
}

private static class FileVisitor extends SimpleFileVisitor<Path> {
Expand Down

0 comments on commit 273ac1e

Please sign in to comment.