Skip to content

Commit

Permalink
Restore AbstractCompiler#getLogger() method
Browse files Browse the repository at this point in the history
We need preserve a compatibility for some time.
  • Loading branch information
slawekjaranowski committed Dec 13, 2023
1 parent 49a464d commit 3ab39cd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 22 deletions.
5 changes: 5 additions & 0 deletions plexus-compiler-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
* @author <a href="mailto:[email protected]">Trygve Laugst&oslash;l</a>
*/
public abstract class AbstractCompiler implements Compiler {
protected Logger log = LoggerFactory.getLogger(getClass());
private final Logger log = LoggerFactory.getLogger(getClass());

private final org.codehaus.plexus.logging.Logger plexusLogger;

protected static final String EOL = System.lineSeparator();

protected static final String PS = System.getProperty("path.separator");
Expand Down Expand Up @@ -68,6 +71,25 @@ protected AbstractCompiler(
this.outputFileEnding = outputFileEnding;

this.outputFile = outputFile;

this.plexusLogger = new PlexusLoggerWrapper(log);
}

/**
*
* @return a Logger
*/
protected Logger getLog() {
return log;
}

/**
* @return a plexus Logger
* @deprecated please use {@link #getLog()}
*/
@Deprecated
protected org.codehaus.plexus.logging.Logger getLogger() {
return plexusLogger;
}

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.codehaus.plexus.compiler;

import org.slf4j.Logger;

class PlexusLoggerWrapper implements org.codehaus.plexus.logging.Logger {

private final Logger log;

PlexusLoggerWrapper(Logger log) {
this.log = log;
}

@Override
public void debug(String message) {
log.debug(message);
}

@Override
public void debug(String message, Throwable throwable) {
log.debug(message, throwable);
}

@Override
public boolean isDebugEnabled() {
return log.isDebugEnabled();
}

@Override
public void info(String message) {
log.info(message);
}

@Override
public void info(String message, Throwable throwable) {
log.info(message, throwable);
}

@Override
public boolean isInfoEnabled() {
return log.isInfoEnabled();
}

@Override
public void warn(String message) {
log.warn(message);
}

@Override
public void warn(String message, Throwable throwable) {
log.warn(message, throwable);
}

@Override
public boolean isWarnEnabled() {
return log.isWarnEnabled();
}

@Override
public void error(String message) {
log.error(message);
}

@Override
public void error(String message, Throwable throwable) {
log.error(message, throwable);
}

@Override
public boolean isErrorEnabled() {
return log.isErrorEnabled();
}

@Override
public void fatalError(String message) {
log.error(message);
}

@Override
public void fatalError(String message, Throwable throwable) {
log.error(message, throwable);
}

@Override
public boolean isFatalErrorEnabled() {
return log.isErrorEnabled();
}

@Override
public int getThreshold() {
return 0;
}

@Override
public void setThreshold(int threshold) {
// not implemented
}

@Override
public org.codehaus.plexus.logging.Logger getChildLogger(String name) {
return null;
}

@Override
public String getName() {
return log.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ public CompilerResult performCompile(CompilerConfiguration config) throws Compil
JavaCompiler compiler = getEcj();
boolean success = false;
if (compiler != null) {
log.debug("Using JSR-199 EclipseCompiler");
getLog().debug("Using JSR-199 EclipseCompiler");
// ECJ JSR-199 compiles against the latest Java version it supports if no source
// version is given explicitly. BatchCompiler uses 1.3 as default. So check
// whether a source version is specified, and if not supply 1.3 explicitly.
if (!haveSourceOrReleaseArgument(args)) {
log.debug("ecj: no source level nor release specified, defaulting to Java 1.3");
getLog().debug("ecj: no source level nor release specified, defaulting to Java 1.3");
args.add("-source");
args.add("1.3");
}
Expand Down Expand Up @@ -286,17 +286,17 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
try {
charset = Charset.forName(encoding);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
log.warn("ecj: invalid or unsupported character set '" + encoding + "', using default");
getLog().warn("ecj: invalid or unsupported character set '" + encoding + "', using default");
// charset remains null
}
}
if (charset == null) {
charset = Charset.defaultCharset();
}
if (log.isDebugEnabled()) {
log.debug("ecj: using character set " + charset.displayName());
log.debug("ecj command line: " + args);
log.debug("ecj input source files: " + allSources);
if (getLog().isDebugEnabled()) {
getLog().debug("ecj: using character set " + charset.displayName());
getLog().debug("ecj command line: " + args);
getLog().debug("ecj input source files: " + allSources);
}

try (StandardJavaFileManager manager =
Expand All @@ -308,19 +308,19 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
} catch (RuntimeException e) {
throw new EcjFailureException(e.getLocalizedMessage());
}
log.debug(sw.toString());
getLog().debug(sw.toString());
} else {
// Use the BatchCompiler and send all errors to xml temp file.
File errorF = null;
try {
errorF = File.createTempFile("ecjerr-", ".xml");
log.debug("Using legacy BatchCompiler; error file " + errorF);
getLog().debug("Using legacy BatchCompiler; error file " + errorF);

args.add("-log");
args.add(errorF.toString());
args.addAll(allSources);

log.debug("ecj command line: " + args);
getLog().debug("ecj command line: " + args);

success = BatchCompiler.compile(
args.toArray(new String[args.size()]), devNull, devNull, new CompilationProgress() {
Expand All @@ -341,7 +341,7 @@ public void setTaskName(String s) {}
@Override
public void worked(int i, int i1) {}
});
log.debug(sw.toString());
getLog().debug(sw.toString());

if (errorF.length() < 80) {
throw new EcjFailureException(sw.toString());
Expand Down Expand Up @@ -524,7 +524,7 @@ private JavaCompiler getEcj() {
}
}
}
log.debug("Cannot find org.eclipse.jdt.internal.compiler.tool.EclipseCompiler");
getLog().debug("Cannot find org.eclipse.jdt.internal.compiler.tool.EclipseCompiler");
return null;
}

Expand Down Expand Up @@ -618,7 +618,7 @@ private String decodeVersion(String versionSpec) {
}

if (versionSpec.equals("1.9")) {
log.warn("Version 9 should be specified as 9, not 1.9");
getLog().warn("Version 9 should be specified as 9, not 1.9");
return "9";
}
return versionSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public CompilerResult performCompile(CompilerConfiguration config) throws Compil
try {
executable = getJavacExecutable();
} catch (IOException e) {
if (log.isWarnEnabled()) {
log.warn("Unable to autodetect 'javac' path, using 'javac' from the environment.");
if (getLog().isWarnEnabled()) {
getLog().warn("Unable to autodetect 'javac' path, using 'javac' from the environment.");
}
executable = "javac";
}
Expand Down Expand Up @@ -541,7 +541,7 @@ protected CompilerResult compileOutOfProcess(CompilerConfiguration config, Strin

List<CompilerMessage> messages;

if (log.isDebugEnabled()) {
if (getLog().isDebugEnabled()) {
String debugFileName = StringUtils.isEmpty(config.getDebugFileName()) ? "javac" : config.getDebugFileName();

File commandLineFile = new File(
Expand All @@ -555,8 +555,8 @@ protected CompilerResult compileOutOfProcess(CompilerConfiguration config, Strin
Runtime.getRuntime().exec(new String[] {"chmod", "a+x", commandLineFile.getAbsolutePath()});
}
} catch (IOException e) {
if (log.isWarnEnabled()) {
log.warn("Unable to write '" + commandLineFile.getName() + "' debug script file", e);
if (getLog().isWarnEnabled()) {
getLog().warn("Unable to write '" + commandLineFile.getName() + "' debug script file", e);
}
}
}
Expand Down Expand Up @@ -587,8 +587,8 @@ CompilerResult compileInProcess(String[] args, CompilerConfiguration config) thr
final Thread thread = Thread.currentThread();
final ClassLoader contextClassLoader = thread.getContextClassLoader();
thread.setContextClassLoader(javacClass.getClassLoader());
if (log.isDebugEnabled()) {
log.debug("ttcl changed run compileInProcessWithProperClassloader");
if (getLog().isDebugEnabled()) {
getLog().debug("ttcl changed run compileInProcessWithProperClassloader");
}
try {
return compileInProcessWithProperClassloader(javacClass, args);
Expand Down Expand Up @@ -880,7 +880,7 @@ private File createFileWithArguments(String[] args, String outputDirectory) thro
PrintWriter writer = null;
try {
File tempFile;
if (log.isDebugEnabled()) {
if (getLog().isDebugEnabled()) {
tempFile = File.createTempFile(JavacCompiler.class.getName(), "arguments", new File(outputDirectory));
} else {
tempFile = File.createTempFile(JavacCompiler.class.getName(), "arguments");
Expand Down

0 comments on commit 3ab39cd

Please sign in to comment.