Skip to content

Commit

Permalink
JarScan can now accept class tree folders as well as jars
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhocodes committed Aug 22, 2017
1 parent 1365bf5 commit 60a7815
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions core/src/main/java/org/adoptopenjdk/jitwatch/jarscan/JarScan.java
Expand Up @@ -91,6 +91,35 @@ public void iterateJar(File jarFile) throws IOException
}
}

public void iterateFolder(File root, File folder) throws IOException
{
int rootLength = root.getAbsolutePath().length() + 1;

List<String> classLocations = new ArrayList<>();

classLocations.add(root.getPath());

File[] children = folder.listFiles();

for (File child : children)
{
String fileName = child.getAbsolutePath();

fileName = fileName.substring(rootLength, fileName.length());

if (child.isFile() && fileName.endsWith(S_DOT_CLASS))
{
String fqName = fileName.replace(S_SLASH, S_DOT).substring(0, fileName.length() - S_DOT_CLASS.length());

process(classLocations, fqName);
}
else if (child.isDirectory())
{
iterateFolder(root, child);
}
}
}

public void addAllowedPackagePrefix(String prefix)
{
allowedPackagePrefixes.add(prefix);
Expand Down Expand Up @@ -161,7 +190,7 @@ private static void showUsage()

String SEPARATOR = "---------------------------------------------------------------------------------------------------";

builder.append("JarScan --mode=<mode> [options] [params] <jars>").append(S_NEWLINE);
builder.append("JarScan --mode=<mode> [options] [params] <jars and class folders>").append(S_NEWLINE);
builder.append(SEPARATOR).append(S_NEWLINE);
builder.append("Options:").append(S_NEWLINE);
builder.append(" --packages=a,b,c Only include methods from named packages. E.g. --packages=java.util.*")
Expand Down Expand Up @@ -391,15 +420,22 @@ public static void main(String[] args) throws IOException
continue;
}

File jarFile = new File(arg);
File fileArg = new File(arg);

if (jarFile.exists() && jarFile.isFile())
if (fileArg.exists())
{
scanner.iterateJar(jarFile);
if (fileArg.isFile())
{
scanner.iterateJar(fileArg);
}
else if (fileArg.isDirectory())
{
scanner.iterateFolder(fileArg, fileArg);
}
}
else
{
System.err.println("Could not scan jar " + jarFile.toString());
System.err.println("Could not scan " + fileArg.toString());
}
}

Expand Down

0 comments on commit 60a7815

Please sign in to comment.