Skip to content

Commit

Permalink
Add better error reporting if ASM can't load a class (catch IAE and a…
Browse files Browse the repository at this point in the history
…dd message); if a related runtime class can't be loaded by ASM, automatically fall back to Jigsaw loading
  • Loading branch information
uschindler committed Oct 3, 2017
1 parent 2c17d37 commit d05c05d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
37 changes: 28 additions & 9 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ private boolean isRuntimeClass(URLConnection conn) throws IOException {

/** Reads a class (binary name) from the given {@link ClassLoader}. If not found there, falls back to the list of classes to be checked. */
private ClassSignature getClassFromClassLoader(final String clazz) throws ClassNotFoundException,IOException {
final ClassSignature c;
if (classpathClassCache.containsKey(clazz)) {
c = classpathClassCache.get(clazz);
final ClassSignature c = classpathClassCache.get(clazz);
if (c == null) {
throw new ClassNotFoundException(clazz);
}
Expand All @@ -283,21 +282,37 @@ private ClassSignature getClassFromClassLoader(final String clazz) throws ClassN
conn.setUseCaches(false);
}
final InputStream in = conn.getInputStream();
final ClassReader cr;
try {
classpathClassCache.put(clazz, c = new ClassSignature(AsmUtils.readAndPatchClass(in), isRuntimeClass, false));
cr = AsmUtils.readAndPatchClass(in);
} catch (IllegalArgumentException iae) {
// if class is too new for this JVM, we try to load it as Class<?> via Jigsaw
// (only if it's a runtime class):
if (isRuntimeClass) {
final ClassSignature c = loadClassFromJigsaw(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
}
}
// unfortunately the ASM IAE has no message, so add good info!
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"The class file format of '%s' is too recent to be parsed by ASM.", clazz));
} finally {
in.close();
}
final ClassSignature c = new ClassSignature(cr, isRuntimeClass, false);
classpathClassCache.put(clazz, c);
return c;
} else {
final ClassSignature jigsawCl = loadClassFromJigsaw(clazz);
if (jigsawCl != null) {
classpathClassCache.put(clazz, c = jigsawCl);
final ClassSignature c = loadClassFromJigsaw(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
}
}
// try to get class from our list of classes we are checking:
c = classesToCheck.get(clazz);
final ClassSignature c = classesToCheck.get(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
Expand Down Expand Up @@ -536,10 +551,14 @@ private void parseSignaturesFile(Reader reader, boolean isBundled) throws IOExce
}

/** Parses and adds a class from the given stream to the list of classes to check. Closes the stream when parsed (on Exception, too)! Does not log anything. */
public void addClassToCheck(final InputStream in) throws IOException {
public void addClassToCheck(final InputStream in, String name) throws IOException {
final ClassReader reader;
try {
reader = AsmUtils.readAndPatchClass(in);
} catch (IllegalArgumentException iae) {
// unfortunately the ASM IAE has no message, so add good info!
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"The class file format of '%s' is too recent to be parsed by ASM.", name));
} finally {
in.close();
}
Expand All @@ -549,7 +568,7 @@ public void addClassToCheck(final InputStream in) throws IOException {

/** Parses and adds a class from the given file to the list of classes to check. Does not log anything. */
public void addClassToCheck(File f) throws IOException {
addClassToCheck(new FileInputStream(f));
addClassToCheck(new FileInputStream(f), f.toString());
}

/** Parses and adds a multiple class files. */
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void info(String msg) {
if (restrictClassFilename && name != null && !name.endsWith(".class")) {
continue;
}
checker.addClassToCheck(r.getInputStream());
checker.addClassToCheck(r.getInputStream(), r.getName());
foundClass = true;
}
if (!foundClass) {
Expand Down

0 comments on commit d05c05d

Please sign in to comment.