Skip to content

Commit

Permalink
Add DEBUG level logging and report classpath and JDK type to allow de…
Browse files Browse the repository at this point in the history
…bugging of classpath/JDK related problems (#206)
  • Loading branch information
uschindler authored Sep 25, 2022
1 parent 500e8cc commit 3336c33
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
method_Module_getName = method_Class_getModule
.getReturnType().getMethod("getName");
isSupportedJDK = true;
logger.debug("Detected Java 9 or later with module system.");
} catch (NoSuchMethodException e) {
method_Class_getModule = method_Module_getName = null;
}
Expand All @@ -116,6 +117,7 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
if (objectClassURL != null && "jrt".equalsIgnoreCase(objectClassURL.getProtocol())) {
// this is Java 9+ allowing direct access to .class file resources - we do not need to deal with modules!
isSupportedJDK = true;
logger.debug("Detected Java 9 or later with JRT file system.");
} else {
String javaHome = System.getProperty("java.home");
if (javaHome != null) {
Expand Down Expand Up @@ -146,7 +148,9 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
}
}
isSupportedJDK = !runtimePaths.isEmpty();
if (!isSupportedJDK) {
if (isSupportedJDK) {
logger.debug("Detected classical classpath-based JDK @ " + runtimePaths);
} else {
logger.warn("Boot classpath appears to be empty or ${java.home} not defined; marking runtime as not suppported.");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/thetaphi/forbiddenapis/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface Logger {
void error(String msg);
void warn(String msg);
void info(String msg);
void debug(String msg);
}
5 changes: 5 additions & 0 deletions src/main/java/de/thetaphi/forbiddenapis/StdIoLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public void info(String msg) {
System.out.println(msg);
}

@Override
public void debug(String msg) {
// no reporting of debug messages
}

}
6 changes: 6 additions & 0 deletions src/main/java/de/thetaphi/forbiddenapis/ant/AntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public void warn(String msg) {
public void info(String msg) {
log(msg, Project.MSG_INFO);
}

@Override
public void debug(String msg) {
log(msg, Project.MSG_DEBUG);
}
};

AntClassLoader antLoader = null;
Expand All @@ -100,6 +105,7 @@ public void info(String msg) {
classpath.setProject(getProject());
loader = antLoader = getProject().createClassLoader(ClassLoader.getSystemClassLoader(), classpath);
antLoader.setParentFirst(true); // use default classloader delegation
log.debug("Classpath: " + classpath.toString());
} else {
loader = ClassLoader.getSystemClassLoader();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,21 +498,32 @@ public void warn(String msg) {
public void info(String msg) {
getLogger().info(msg);
}

@Override
public void debug(String msg) {
getLogger().debug(msg);
}
};

final Set<File> cpElements = new LinkedHashSet<>();
cpElements.addAll(classpath.getFiles());
cpElements.addAll(classesDirs.getFiles());
final URL[] urls = new URL[cpElements.size()];
final StringBuilder humanClasspath = new StringBuilder();
try {
int i = 0;
for (final File cpElement : cpElements) {
urls[i++] = cpElement.toURI().toURL();
if (humanClasspath.length() > 0) {
humanClasspath.append(File.pathSeparatorChar);
}
humanClasspath.append(cpElement);
}
assert i == urls.length;
} catch (MalformedURLException mfue) {
throw new InvalidUserDataException("Failed to build classpath URLs.", mfue);
}
log.debug("Classpath: " + humanClasspath);

URLClassLoader urlLoader = null;
final ClassLoader loader = (urls.length > 0) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public void warn(String msg) {
public void info(String msg) {
getLog().info(msg);
}

@Override
public void debug(String msg) {
getLog().debug(msg);
}
};

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

final List<String> cp = getClassPathElements();
final URL[] urls = new URL[cp.size()];
final StringBuilder humanClasspath = new StringBuilder();
try {
int i = 0;
for (final String cpElement : cp) {
urls[i++] = new File(cpElement).toURI().toURL();
if (humanClasspath.length() > 0) {
humanClasspath.append(File.pathSeparatorChar);
}
humanClasspath.append(cpElement);
}
assert i == urls.length;
} catch (MalformedURLException e) {
throw new MojoExecutionException("Failed to build classpath.", e);
}
log.debug("Classpath: " + humanClasspath);

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

0 comments on commit 3336c33

Please sign in to comment.