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

Add DEBUG level logging and report classpath and JDK type to allow debugging of classpath/JDK related problems #206

Merged
merged 2 commits into from
Sep 25, 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
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