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

If FAIL_ON_MISSING_CLASSES is disabled only log a summary of classes with WARN level #210

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
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
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