Skip to content

Commit

Permalink
If FAIL_ON_MISSING_CLASSES is disabled only log a summary of classes …
Browse files Browse the repository at this point in the history
…with WARN level (#210)
  • Loading branch information
uschindler authored Oct 4, 2022
1 parent dbcf646 commit 1124a8e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -190,5 +191,23 @@ public static boolean isExceptionInAsmClassReader(RuntimeException re) {
final StackTraceElement[] stack = re.getStackTrace();
return stack.length > 0 && Objects.equals(ClassReader.class.getName(), stack[0].getClassName());
}

/** Formats a list of classes, abbreviated, with 2 spaces in front (for logging) */
public static String formatClassesAbbreviated(Collection<String> missingClasses) {
final StringBuilder sb = new StringBuilder();
int count = 0;
for (String s : missingClasses) {
sb.append(count == 0 ? " " : ", ").append(s);
count++;
if (sb.length() >= 70) {
int remaining = missingClasses.size() - count;
if (remaining > 0) {
sb.append(",... (and ").append(remaining).append(" more).");
}
break;
}
}
return sb.toString();
}

}
14 changes: 10 additions & 4 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public static enum Option {
/** Cache of loaded classes: key is the binary name (dotted) */
final Map<String,ClassMetadata> classpathClassCache = new HashMap<>();

/** Related classes (binary name, dotted) which were not found while looking up
* class metadata [referenced (super)classes, interfaces,...] */
final Set<String> missingClasses = new TreeSet<>();

final Signatures forbiddenSignatures;

/** descriptors (not internal names) of all annotations that suppress */
Expand Down Expand Up @@ -305,10 +309,7 @@ public ClassMetadata lookupRelatedClass(String internalName, String internalName
if (options.contains(Option.FAIL_ON_MISSING_CLASSES)) {
throw new RelatedClassLoadingException(cnfe, origClassName);
} else {
logger.warn(String.format(Locale.ENGLISH,
"Class '%s' cannot be loaded (while looking up details about referenced class '%s'). Please fix the classpath!",
type.getClassName(), origClassName
));
missingClasses.add(type.getClassName());
return null;
}
} catch (IOException ioe) {
Expand Down Expand Up @@ -464,6 +465,11 @@ public void run() throws ForbiddenApiException {
errors += checkClass(c, suppressAnnotationsPattern);
}

if (!missingClasses.isEmpty() ) {
logger.warn("While scanning classes to check, the following referenced classes were not found on classpath (this may miss some violations):");
logger.warn(AsmUtils.formatClassesAbbreviated(missingClasses));
}

final String message = String.format(Locale.ENGLISH,
"Scanned %d class file(s) for forbidden API invocations (in %.2fs), %d error(s).",
classesToCheck.size(), (System.currentTimeMillis() - start) / 1000.0, errors);
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/de/thetaphi/forbiddenapis/Signatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,7 @@ private void reportMissingSignatureClasses(Set<String> missingClasses) {
return;
}
logger.warn("Some signatures were ignored because the following classes were not found on classpath:");
final StringBuilder sb = new StringBuilder();
int count = 0;
for (String s : missingClasses) {
sb.append(count == 0 ? " " : ", ").append(s);
count++;
if (sb.length() >= 70) {
int remaining = missingClasses.size() - count;
if (remaining > 0) {
sb.append(",... (and ").append(remaining).append(" more).");
}
break;
}
}
logger.warn(sb.toString());
logger.warn(AsmUtils.formatClassesAbbreviated(missingClasses));
}

private void addBundledSignatures(String name, String jdkTargetVersion, boolean logging, Set<String> missingClasses) throws IOException,ParseException {
Expand Down

0 comments on commit 1124a8e

Please sign in to comment.