Skip to content

Commit

Permalink
[lang] Internal Xtext messages are logged into the Maven CLI compiler…
Browse files Browse the repository at this point in the history
…'s logger.

see #787

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jan 12, 2018
1 parent 889d665 commit 8d1fe92
Showing 1 changed file with 27 additions and 0 deletions.
Expand Up @@ -28,6 +28,8 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -99,6 +101,7 @@
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.resource.BatchLinkableResource;
import org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable;

import io.sarl.lang.SARLConfig;
import io.sarl.lang.compiler.GeneratorConfig2;
Expand Down Expand Up @@ -1039,6 +1042,7 @@ public boolean compile(CancelIndicator cancelIndicator) {
if (hasError || cancel.isCanceled()) {
return false;
}
overrideXtextInternalLoggers();
generateJavaFiles(validatedResources, cancel);
if (cancel.isCanceled()) {
return false;
Expand All @@ -1061,6 +1065,29 @@ public boolean compile(CancelIndicator cancelIndicator) {
return true;
}

/** Change the loggers that are internally used by Xtext.
*/
protected void overrideXtextInternalLoggers() {
final Logger logger = getLogger();
setStaticField(BatchLinkableResourceStorageWritable.class, "LOG", logger); //$NON-NLS-1$
setStaticField(BatchLinkableResource.class, "log", logger); //$NON-NLS-1$
}

private void setStaticField(Class<?> type, String name, Logger logger) {
try {
final Field field = type.getDeclaredField(name);
field.setAccessible(true);
if ((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
final Field modifiersField = Field.class.getDeclaredField("modifiers"); //$NON-NLS-1$
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
field.set(null, logger);
} catch (Exception exception) {
getLogger().error(exception.getLocalizedMessage(), exception);
}
}

/** Create a message for the issue.
*
* @param issue the issue.
Expand Down

0 comments on commit 8d1fe92

Please sign in to comment.