Skip to content

Commit 1124a8e

Browse files
authored
If FAIL_ON_MISSING_CLASSES is disabled only log a summary of classes with WARN level (#210)
1 parent dbcf646 commit 1124a8e

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/main/java/de/thetaphi/forbiddenapis/AsmUtils.java

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URL;
2424
import java.nio.ByteBuffer;
2525
import java.nio.ByteOrder;
26+
import java.util.Collection;
2627
import java.util.Locale;
2728
import java.util.Objects;
2829
import java.util.regex.Pattern;
@@ -190,5 +191,23 @@ public static boolean isExceptionInAsmClassReader(RuntimeException re) {
190191
final StackTraceElement[] stack = re.getStackTrace();
191192
return stack.length > 0 && Objects.equals(ClassReader.class.getName(), stack[0].getClassName());
192193
}
194+
195+
/** Formats a list of classes, abbreviated, with 2 spaces in front (for logging) */
196+
public static String formatClassesAbbreviated(Collection<String> missingClasses) {
197+
final StringBuilder sb = new StringBuilder();
198+
int count = 0;
199+
for (String s : missingClasses) {
200+
sb.append(count == 0 ? " " : ", ").append(s);
201+
count++;
202+
if (sb.length() >= 70) {
203+
int remaining = missingClasses.size() - count;
204+
if (remaining > 0) {
205+
sb.append(",... (and ").append(remaining).append(" more).");
206+
}
207+
break;
208+
}
209+
}
210+
return sb.toString();
211+
}
193212

194213
}

src/main/java/de/thetaphi/forbiddenapis/Checker.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public static enum Option {
7474
/** Cache of loaded classes: key is the binary name (dotted) */
7575
final Map<String,ClassMetadata> classpathClassCache = new HashMap<>();
7676

77+
/** Related classes (binary name, dotted) which were not found while looking up
78+
* class metadata [referenced (super)classes, interfaces,...] */
79+
final Set<String> missingClasses = new TreeSet<>();
80+
7781
final Signatures forbiddenSignatures;
7882

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

468+
if (!missingClasses.isEmpty() ) {
469+
logger.warn("While scanning classes to check, the following referenced classes were not found on classpath (this may miss some violations):");
470+
logger.warn(AsmUtils.formatClassesAbbreviated(missingClasses));
471+
}
472+
467473
final String message = String.format(Locale.ENGLISH,
468474
"Scanned %d class file(s) for forbidden API invocations (in %.2fs), %d error(s).",
469475
classesToCheck.size(), (System.currentTimeMillis() - start) / 1000.0, errors);

src/main/java/de/thetaphi/forbiddenapis/Signatures.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,7 @@ private void reportMissingSignatureClasses(Set<String> missingClasses) {
232232
return;
233233
}
234234
logger.warn("Some signatures were ignored because the following classes were not found on classpath:");
235-
final StringBuilder sb = new StringBuilder();
236-
int count = 0;
237-
for (String s : missingClasses) {
238-
sb.append(count == 0 ? " " : ", ").append(s);
239-
count++;
240-
if (sb.length() >= 70) {
241-
int remaining = missingClasses.size() - count;
242-
if (remaining > 0) {
243-
sb.append(",... (and ").append(remaining).append(" more).");
244-
}
245-
break;
246-
}
247-
}
248-
logger.warn(sb.toString());
235+
logger.warn(AsmUtils.formatClassesAbbreviated(missingClasses));
249236
}
250237

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

0 commit comments

Comments
 (0)