Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a routine to search for .class files in directories in the cl… #380

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/main/java/jep/ClassList.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private ClassList() throws JepException {
}

/**
* load jar files from class path
*
* load .jar files and .class files from class path
*/
private void loadClassPath() {
StringTokenizer tok = new StringTokenizer(
Expand All @@ -95,16 +94,23 @@ private void loadClassPath() {
while (!queue.isEmpty()) {
String el = queue.remove();

if (!el.toLowerCase().endsWith(".jar")) {
// ignore filesystem classpath
// make sure it exists
File file = new File(el);

// A directory is a special case.
if (file.isDirectory()) {
// search for all .class files recursively starting with no prefix.
addClassFilesInTree(file, "");
continue;
}

// make sure it exists
File file = new File(el);
if (!file.exists() || !file.canRead())
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check for file existence and readability should continue to be right after the File object is newed, and therefore before the file.isDirectory() check.


// The .jar file is the normal case.
if (!el.toLowerCase().endsWith(".jar"))
continue;

try (JarFile jfile = new JarFile(el, false)) {

// add entries from manifest to check later
Expand Down Expand Up @@ -159,6 +165,31 @@ private void loadClassPath() {
}
}

/**
* Recursively go through a folder and all subdirectories looking for .class
* files. Add them all.
*/
private void addClassFilesInTree(File folder, String prefix) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add javadoc @param to the javadoc on this method? I realize ClassList.java is not very consistent with javadoc, but I'd like to improve the readabiity. It took me a bit to figure out that prefix is going to be building a string of the package name as you walk down the directory tree, so maybe add a bit of explanation on that too?

if (!folder.isDirectory()) {
throw new IllegalArgumentException("folder is not a Directory");
}
for (File file : folder.listFiles()) {
String entry = file.getName();
if (file.isDirectory()) {
if (prefix!=null && !prefix.isEmpty()) {
// Include a . between directories only if there was a prefix.
addClassFilesInTree(file, prefix+"."+entry);
} else {
// Don't include a prefix--we only care about subdirectories.
addClassFilesInTree(file, entry);
}
} else if (file.exists() && file.canRead() && entry.toLowerCase().endsWith(".class")) {
// We've found a .class file on the file system. Add it.
addClass(prefix, entry.replaceAll(".class$", ""));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the $ here? I don't understand why that's there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the regex end of line character. I don't think there is a legal java filename with .class in the middle but it is pretty standard for ensuring you only modify the extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

}
}
}

/*
* the jre will tell us about what jar files it has open. use that facility
* to get a list of packages. then read the files ourselves since java won't
Expand Down