Skip to content

Commit beaaa9c

Browse files
authored
Restore the previous missing classes logging using DEBUG level (#227)
1 parent 7bd669c commit beaaa9c

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ public ClassMetadata lookupRelatedClass(String internalName, String internalName
309309
if (options.contains(Option.FAIL_ON_MISSING_CLASSES)) {
310310
throw new RelatedClassLoadingException(cnfe, origClassName);
311311
} else {
312+
logger.debug(String.format(Locale.ENGLISH,
313+
"Class '%s' cannot be loaded (while looking up details about referenced class '%s').",
314+
type.getClassName(), origClassName));
312315
missingClasses.add(type.getClassName());
313316
return null;
314317
}

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
@SuppressForbidden
2020
public final class StdIoLogger implements Logger {
2121

22-
public static final Logger INSTANCE = new StdIoLogger();
22+
public static final Logger INSTANCE = new StdIoLogger(false);
23+
public static final Logger INSTANCE_DEBUG = new StdIoLogger(true);
2324

24-
private StdIoLogger() {}
25+
private final boolean debug;
26+
27+
private StdIoLogger(boolean debug) {
28+
this.debug = debug;
29+
}
2530

2631
@Override
2732
public void error(String msg) {
@@ -40,7 +45,8 @@ public void info(String msg) {
4045

4146
@Override
4247
public void debug(String msg) {
43-
// no reporting of debug messages
48+
if (debug) {
49+
System.err.println("DEBUG: " + msg);
50+
}
4451
}
45-
4652
}

src/main/java/de/thetaphi/forbiddenapis/cli/CliMain.java

+18-11
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@
5252
*/
5353
public final class CliMain implements Constants {
5454

55+
private final Logger logger;
5556
private final Option classpathOpt, dirOpt, includesOpt, excludesOpt, signaturesfileOpt, bundledsignaturesOpt, suppressannotationsOpt,
56-
allowmissingclassesOpt, ignoresignaturesofmissingclassesOpt, allowunresolvablesignaturesOpt, versionOpt, helpOpt;
57+
allowmissingclassesOpt, ignoresignaturesofmissingclassesOpt, allowunresolvablesignaturesOpt, versionOpt, helpOpt, debugOpt;
5758
private final CommandLine cmd;
5859

59-
private static final Logger LOG = StdIoLogger.INSTANCE;
60-
6160
public static final int EXIT_SUCCESS = 0;
6261
public static final int EXIT_VIOLATION = 1;
6362
public static final int EXIT_ERR_CMDLINE = 2;
@@ -84,6 +83,10 @@ public CliMain(String... args) throws ExitException {
8483

8584
final Options options = new Options();
8685
options.addOptionGroup(required);
86+
options.addOption(debugOpt = Option.builder()
87+
.desc("enable debug logging (stderr)")
88+
.longOpt("debug")
89+
.build());
8790
options.addOption(classpathOpt = Option.builder("c")
8891
.desc("class search path of directories and zip/jar files")
8992
.longOpt("classpath")
@@ -140,6 +143,8 @@ public CliMain(String... args) throws ExitException {
140143

141144
try {
142145
this.cmd = new DefaultParser().parse(options, args);
146+
final boolean debugLogging = cmd.hasOption(debugOpt.getLongOpt());
147+
this.logger = debugLogging ? StdIoLogger.INSTANCE_DEBUG : StdIoLogger.INSTANCE;
143148
if (cmd.hasOption(helpOpt.getLongOpt())) {
144149
printHelp(options);
145150
throw new ExitException(EXIT_SUCCESS);
@@ -156,7 +161,7 @@ public CliMain(String... args) throws ExitException {
156161

157162
private void printVersion() {
158163
final Package pkg = this.getClass().getPackage();
159-
LOG.info(String.format(Locale.ENGLISH,
164+
logger.info(String.format(Locale.ENGLISH,
160165
"%s %s",
161166
pkg.getImplementationTitle(), pkg.getImplementationVersion()
162167
));
@@ -211,18 +216,20 @@ public void run() throws ExitException {
211216
} catch (MalformedURLException mfue) {
212217
throw new ExitException(EXIT_ERR_OTHER, "The given classpath is invalid: " + mfue);
213218
}
214-
// System.err.println("Classpath: " + Arrays.toString(urls));
219+
logger.debug("Classpath: " + Arrays.toString(urls));
215220

216221
try (final URLClassLoader loader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader())) {
217222
final EnumSet<Checker.Option> options = EnumSet.of(FAIL_ON_VIOLATION);
218223
if (!cmd.hasOption(allowmissingclassesOpt.getLongOpt())) options.add(FAIL_ON_MISSING_CLASSES);
219224
if (cmd.hasOption(allowunresolvablesignaturesOpt.getLongOpt())) {
220-
LOG.warn(DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES);
225+
logger.warn(DEPRECATED_WARN_FAIL_ON_UNRESOLVABLE_SIGNATURES);
221226
} else {
222227
options.add(FAIL_ON_UNRESOLVABLE_SIGNATURES);
223228
}
224-
if (cmd.hasOption(ignoresignaturesofmissingclassesOpt.getLongOpt())) options.add(IGNORE_SIGNATURES_OF_MISSING_CLASSES);
225-
final Checker checker = new Checker(LOG, loader, options);
229+
if (cmd.hasOption(ignoresignaturesofmissingclassesOpt.getLongOpt())) {
230+
options.add(IGNORE_SIGNATURES_OF_MISSING_CLASSES);
231+
}
232+
final Checker checker = new Checker(logger, loader, options);
226233

227234
if (!checker.isSupportedJDK) {
228235
throw new ExitException(EXIT_UNSUPPORTED_JDK, String.format(Locale.ENGLISH,
@@ -235,7 +242,7 @@ public void run() throws ExitException {
235242
checker.addSuppressAnnotation(a);
236243
}
237244

238-
LOG.info("Scanning for classes to check...");
245+
logger.info("Scanning for classes to check...");
239246
if (!classesDirectory.exists()) {
240247
throw new ExitException(EXIT_ERR_OTHER, "Directory with class files does not exist: " + classesDirectory);
241248
}
@@ -282,7 +289,7 @@ public void run() throws ExitException {
282289
bundledsignaturesOpt.getLongOpt(), signaturesfileOpt.getLongOpt()
283290
));
284291
} else {
285-
LOG.info("Skipping execution because no API signatures are available.");
292+
logger.info("Skipping execution because no API signatures are available.");
286293
return;
287294
}
288295
}
@@ -308,7 +315,7 @@ public static void main(String... args) {
308315
new CliMain(args).run();
309316
} catch (ExitException e) {
310317
if (e.getMessage() != null) {
311-
LOG.error(e.getMessage());
318+
StdIoLogger.INSTANCE.error(e.getMessage());
312319
}
313320
if (e.exitCode != 0) {
314321
System.exit(e.exitCode);

0 commit comments

Comments
 (0)