Skip to content

Commit

Permalink
Fix #278 - Should detect embedded class paths from manifest.
Browse files Browse the repository at this point in the history
  • Loading branch information
leibnitz27 committed Dec 9, 2021
1 parent 6ce1da7 commit ca22319
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
68 changes: 45 additions & 23 deletions src/org/benf/cfr/reader/state/ClassFileSourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.benf.cfr.reader.util.AnalysisType;
import org.benf.cfr.reader.util.ConfusedCFRException;
import org.benf.cfr.reader.util.MiscConstants;
import org.benf.cfr.reader.util.StringUtils;
import org.benf.cfr.reader.util.collections.Functional;
import org.benf.cfr.reader.util.collections.ListFactory;
import org.benf.cfr.reader.util.collections.MapFactory;
Expand Down Expand Up @@ -266,6 +265,11 @@ public JarContent addJarContent(String jarPath, AnalysisType analysisType) {
classRenamer.notifyClassFiles(jarContent.getClassFiles());
}

String jarClassPath = jarContent.getManifestEntries().get(MiscConstants.MANIFEST_CLASS_PATH);
if (jarClassPath != null) {
addToRelativeClassPath(file, jarClassPath);
}

List < String > output = ListFactory.newList();
for (String classPath : jarContent.getClassFiles()) {
if (classPath.toLowerCase().endsWith(".class")) {
Expand Down Expand Up @@ -323,36 +327,54 @@ private Map<String, JarSourceEntry> getClassPathClasses() {

String[] classPaths = classPath.split("" + File.pathSeparatorChar);
for (String path : classPaths) {
processToClassPath(dump, path);
}
if (dump) {
System.out.println(" */");
}
}
return classToPathMap;
}

private void processToClassPath(boolean dump, String path) {
if (dump) {
System.out.println(" " + path);
}
File f = new File(path);
if (f.exists()) {
if (f.isDirectory()) {
if (dump) {
System.out.println(" " + path);
System.out.println(" (Directory)");
}
File f = new File(path);
if (f.exists()) {
if (f.isDirectory()) {
if (dump) {
System.out.println(" (Directory)");
}
// Load all the jars in that directory.
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
processClassPathFile(file, file.getAbsolutePath(), classToPathMap, AnalysisType.JAR, dump);
}
}
} else {
processClassPathFile(f, path, classToPathMap, AnalysisType.JAR, dump);
}
} else {
if (dump) {
System.out.println(" (Can't access)");
// Load all the jars in that directory.
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
processClassPathFile(file, file.getAbsolutePath(), classToPathMap, AnalysisType.JAR, dump);
}
}
} else {
processClassPathFile(f, path, classToPathMap, AnalysisType.JAR, dump);
}
} else {
if (dump) {
System.out.println(" */");
System.out.println(" (Can't access)");
}
}
return classToPathMap;
}

private void addToRelativeClassPath(File file, String jarClassPath) {
String[] classPaths = jarClassPath.split(" ");
boolean dump = options.getOption(OptionsImpl.DUMP_CLASS_PATH);
String relative = null;
try {
relative = file.getParentFile().getCanonicalPath();
} catch (IOException e) {
return;
}
for (String path : classPaths) {
processToClassPath(dump, relative + File.separatorChar + path);
}
}

private void processClassPathFile(File file, String absolutePath, Map<String, JarSourceEntry> classToPathMap, AnalysisType analysisType, boolean dump) {
Expand Down
1 change: 1 addition & 0 deletions src/org/benf/cfr/reader/util/MiscConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface MiscConstants {

String MANIFEST_PATH = "META-INF/MANIFEST.MF";
String MULTI_RELEASE_KEY = "Multi-Release";
String MANIFEST_CLASS_PATH = "Class-Path";
String MULTI_RELEASE_PREFIX = "META-INF/versions/";
String WAR_PREFIX = "WEB-INF/classes/";
Pattern MULTI_RELEASE_PATH_PATTERN = Pattern.compile("^" + MULTI_RELEASE_PREFIX + "(\\d+)/(.*)$");
Expand Down

0 comments on commit ca22319

Please sign in to comment.