Skip to content

Commit 3336c33

Browse files
authored
Add DEBUG level logging and report classpath and JDK type to allow debugging of classpath/JDK related problems (#206)
1 parent 500e8cc commit 3336c33

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
101101
method_Module_getName = method_Class_getModule
102102
.getReturnType().getMethod("getName");
103103
isSupportedJDK = true;
104+
logger.debug("Detected Java 9 or later with module system.");
104105
} catch (NoSuchMethodException e) {
105106
method_Class_getModule = method_Module_getName = null;
106107
}
@@ -116,6 +117,7 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
116117
if (objectClassURL != null && "jrt".equalsIgnoreCase(objectClassURL.getProtocol())) {
117118
// this is Java 9+ allowing direct access to .class file resources - we do not need to deal with modules!
118119
isSupportedJDK = true;
120+
logger.debug("Detected Java 9 or later with JRT file system.");
119121
} else {
120122
String javaHome = System.getProperty("java.home");
121123
if (javaHome != null) {
@@ -146,7 +148,9 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
146148
}
147149
}
148150
isSupportedJDK = !runtimePaths.isEmpty();
149-
if (!isSupportedJDK) {
151+
if (isSupportedJDK) {
152+
logger.debug("Detected classical classpath-based JDK @ " + runtimePaths);
153+
} else {
150154
logger.warn("Boot classpath appears to be empty or ${java.home} not defined; marking runtime as not suppported.");
151155
}
152156
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ public interface Logger {
2020
void error(String msg);
2121
void warn(String msg);
2222
void info(String msg);
23+
void debug(String msg);
2324
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public void info(String msg) {
3838
System.out.println(msg);
3939
}
4040

41+
@Override
42+
public void debug(String msg) {
43+
// no reporting of debug messages
44+
}
45+
4146
}

src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void warn(String msg) {
9191
public void info(String msg) {
9292
log(msg, Project.MSG_INFO);
9393
}
94+
95+
@Override
96+
public void debug(String msg) {
97+
log(msg, Project.MSG_DEBUG);
98+
}
9499
};
95100

96101
AntClassLoader antLoader = null;
@@ -100,6 +105,7 @@ public void info(String msg) {
100105
classpath.setProject(getProject());
101106
loader = antLoader = getProject().createClassLoader(ClassLoader.getSystemClassLoader(), classpath);
102107
antLoader.setParentFirst(true); // use default classloader delegation
108+
log.debug("Classpath: " + classpath.toString());
103109
} else {
104110
loader = ClassLoader.getSystemClassLoader();
105111
}

src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java

+11
Original file line numberDiff line numberDiff line change
@@ -498,21 +498,32 @@ public void warn(String msg) {
498498
public void info(String msg) {
499499
getLogger().info(msg);
500500
}
501+
502+
@Override
503+
public void debug(String msg) {
504+
getLogger().debug(msg);
505+
}
501506
};
502507

503508
final Set<File> cpElements = new LinkedHashSet<>();
504509
cpElements.addAll(classpath.getFiles());
505510
cpElements.addAll(classesDirs.getFiles());
506511
final URL[] urls = new URL[cpElements.size()];
512+
final StringBuilder humanClasspath = new StringBuilder();
507513
try {
508514
int i = 0;
509515
for (final File cpElement : cpElements) {
510516
urls[i++] = cpElement.toURI().toURL();
517+
if (humanClasspath.length() > 0) {
518+
humanClasspath.append(File.pathSeparatorChar);
519+
}
520+
humanClasspath.append(cpElement);
511521
}
512522
assert i == urls.length;
513523
} catch (MalformedURLException mfue) {
514524
throw new InvalidUserDataException("Failed to build classpath URLs.", mfue);
515525
}
526+
log.debug("Classpath: " + humanClasspath);
516527

517528
URLClassLoader urlLoader = null;
518529
final ClassLoader loader = (urls.length > 0) ?

src/main/java/de/thetaphi/forbiddenapis/maven/AbstractCheckMojo.java

+11
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ public void warn(String msg) {
304304
public void info(String msg) {
305305
getLog().info(msg);
306306
}
307+
308+
@Override
309+
public void debug(String msg) {
310+
getLog().debug(msg);
311+
}
307312
};
308313

309314
if (skip) {
@@ -323,15 +328,21 @@ public void info(String msg) {
323328

324329
final List<String> cp = getClassPathElements();
325330
final URL[] urls = new URL[cp.size()];
331+
final StringBuilder humanClasspath = new StringBuilder();
326332
try {
327333
int i = 0;
328334
for (final String cpElement : cp) {
329335
urls[i++] = new File(cpElement).toURI().toURL();
336+
if (humanClasspath.length() > 0) {
337+
humanClasspath.append(File.pathSeparatorChar);
338+
}
339+
humanClasspath.append(cpElement);
330340
}
331341
assert i == urls.length;
332342
} catch (MalformedURLException e) {
333343
throw new MojoExecutionException("Failed to build classpath.", e);
334344
}
345+
log.debug("Classpath: " + humanClasspath);
335346

336347
URLClassLoader urlLoader = null;
337348
final ClassLoader loader = (urls.length > 0) ?

0 commit comments

Comments
 (0)