Skip to content

Commit

Permalink
#41 Get Log from JavacProcessingEnvironment on demand (for Java 8 com…
Browse files Browse the repository at this point in the history
…patibility)
  • Loading branch information
tbsfrdrch authored and gunnarmorling committed Apr 8, 2019
1 parent 2309627 commit f9bc45b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.tools.JavaFileManager;

import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import org.moditect.deptective.internal.DeptectiveTreeVisitor;
import org.moditect.deptective.internal.handler.PackageReferenceHandler;
import org.moditect.deptective.internal.log.Log;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void init(JavacTask task, String... args) {
DeptectiveOptions options = new DeptectiveOptions(args);

Log log = Log.getInstance(
context.get(com.sun.tools.javac.util.Log.logKey),
context.get(JavacProcessingEnvironment.class),
context.get(JavacMessages.messagesKey)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@

import org.moditect.deptective.internal.options.ReportingPolicy;

import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.Note;
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.Position;

public class Log {

private final com.sun.tools.javac.util.Log log;
private final JavacProcessingEnvironment processingEnvironment;

private final DeptectiveMessages messages;

Expand All @@ -39,68 +40,68 @@ public class Log {
*/
private boolean registeredResourceBundle;

private Log(com.sun.tools.javac.util.Log log, JavacMessages messages) {
this.log = log;
private Log(JavacProcessingEnvironment processingEnvironment, JavacMessages messages) {
this.processingEnvironment = processingEnvironment;
registeredResourceBundle = true;
this.messages = new DeptectiveMessages();
registerResourceBundle(messages);
}

public static Log getInstance(com.sun.tools.javac.util.Log log, JavacMessages messages) {
return new Log(log, messages);
public static Log getInstance(JavacProcessingEnvironment processingEnvironment, JavacMessages messages) {
return new Log(processingEnvironment, messages);
}

public void report(ReportingPolicy reportingPolicy, String key, Object... params) {
if (reportingPolicy == ReportingPolicy.ERROR) {
if (registeredResourceBundle) {
log.error(Position.NOPOS, key, params);
currentLog().error(Position.NOPOS, key, params);
}
else {
MessageFormat format = new MessageFormat(messages.getFormat(DeptectiveMessages.ERROR_PREFIX, key));
log.rawError(Position.NOPOS, format.format(params));
currentLog().rawError(Position.NOPOS, format.format(params));
}
}
else {
if (registeredResourceBundle) {
log.strictWarning(null, key, params);
currentLog().strictWarning(null, key, params);
}
else {
MessageFormat format = new MessageFormat(messages.getFormat(DeptectiveMessages.WARNING_PREFIX, key));
log.rawWarning(Position.NOPOS, format.format(params));
currentLog().rawWarning(Position.NOPOS, format.format(params));
}
}
}

public void report(ReportingPolicy reportingPolicy, DiagnosticPosition pos, String key, Object... params) {
if (reportingPolicy == ReportingPolicy.ERROR) {
if (registeredResourceBundle) {
log.error(pos.getPreferredPosition(), key, params);
currentLog().error(pos.getPreferredPosition(), key, params);
}
else {
MessageFormat format = new MessageFormat(messages.getFormat(DeptectiveMessages.ERROR_PREFIX, key));
log.rawError(pos.getPreferredPosition(), format.format(params));
currentLog().rawError(pos.getPreferredPosition(), format.format(params));
}
}
else {
if (registeredResourceBundle) {
log.strictWarning(pos, key, params);
currentLog().strictWarning(pos, key, params);
}
else {
MessageFormat format = new MessageFormat(messages.getFormat(DeptectiveMessages.WARNING_PREFIX, key));
log.rawWarning(pos.getPreferredPosition(), format.format(params));
currentLog().rawWarning(pos.getPreferredPosition(), format.format(params));
}
}
}

public void note(String key, Object... params) {
// no "raw" API for producing notes; so omitting them on Java 8
if (registeredResourceBundle) {
log.note(new Note("compiler", key, params));
currentLog().note(new Note("compiler", key, params));
}
}

public void useSource(JavaFileObject file) {
log.useSource(file);
currentLog().useSource(file);
}

private void registerResourceBundle(JavacMessages messages) {
Expand All @@ -116,4 +117,11 @@ private void registerResourceBundle(JavacMessages messages) {
registeredResourceBundle = false;
}
}

private com.sun.tools.javac.util.Log currentLog() {
// For the sake of Java 8 compatibility the currently relevant Log instance has to be
// retrieved on demand from the JavacProcessingEnvironment instead of using a cached
// instance from the plugin initialization phase
return processingEnvironment.getContext().get(com.sun.tools.javac.util.Log.logKey);
}
}

0 comments on commit f9bc45b

Please sign in to comment.