2222import javax .tools .DiagnosticListener ;
2323import javax .tools .JavaFileObject ;
2424
25+ import java .nio .file .Path ;
2526import java .util .Arrays ;
2627import java .util .LinkedHashMap ;
2728import java .util .Locale ;
@@ -53,6 +54,11 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
5354 */
5455 private final Locale locale ;
5556
57+ /**
58+ * The base directory with which to relativize the paths to source files.
59+ */
60+ private final Path directory ;
61+
5662 /**
5763 * Number of errors or warnings.
5864 */
@@ -74,14 +80,31 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
7480 * @param logger the logger where to send diagnostics
7581 * @param messageBuilderFactory the factory for creating message builders
7682 * @param locale the locale for compiler message
83+ * @param directory the base directory with which to relativize the paths to source files
7784 */
78- DiagnosticLogger (Log logger , MessageBuilderFactory messageBuilderFactory , Locale locale ) {
85+ DiagnosticLogger (Log logger , MessageBuilderFactory messageBuilderFactory , Locale locale , Path directory ) {
7986 this .logger = logger ;
8087 this .messageBuilderFactory = messageBuilderFactory ;
8188 this .locale = locale ;
89+ this .directory = directory ;
8290 codeCount = new LinkedHashMap <>();
8391 }
8492
93+ /**
94+ * Makes the given file relative to the base directory.
95+ *
96+ * @param file the path to make relative to the base directory
97+ * @return the given path, potentially relative to the base directory
98+ */
99+ private String relativize (String file ) {
100+ try {
101+ return directory .relativize (Path .of (file )).toString ();
102+ } catch (IllegalArgumentException e ) {
103+ // Ignore, keep the absolute path.
104+ return file ;
105+ }
106+ }
107+
85108 /**
86109 * Invoked when the compiler emitted a warning.
87110 *
@@ -95,6 +118,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
95118 }
96119 MessageBuilder record = messageBuilderFactory .builder ();
97120 record .a (message );
121+ JavaFileObject source = diagnostic .getSource ();
98122 Diagnostic .Kind kind = diagnostic .getKind ();
99123 String style ;
100124 switch (kind ) {
@@ -107,11 +131,13 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
107131 break ;
108132 default :
109133 style = ".info:-bold,f:blue" ;
134+ if (diagnostic .getLineNumber () == Diagnostic .NOPOS ) {
135+ source = null ; // Some messages are generic, e.g. "Recompile with -Xlint:deprecation".
136+ }
110137 break ;
111138 }
112- JavaFileObject source = diagnostic .getSource ();
113139 if (source != null ) {
114- record .newline ().a (" at " ).a (source .getName ());
140+ record .newline ().a (" at " ).a (relativize ( source .getName () ));
115141 long line = diagnostic .getLineNumber ();
116142 long column = diagnostic .getColumnNumber ();
117143 if (line != Diagnostic .NOPOS || column != Diagnostic .NOPOS ) {
0 commit comments