26
26
package com .sun .tools .javac .util ;
27
27
28
28
import java .util .EnumSet ;
29
+ import java .util .function .UnaryOperator ;
29
30
import java .util .Locale ;
30
31
import java .util .Set ;
31
32
import java .util .stream .Stream ;
@@ -243,6 +244,21 @@ public JCDiagnostic create(
243
244
return create (null , EnumSet .noneOf (DiagnosticFlag .class ), source , pos , DiagnosticInfo .of (kind , prefix , key , args ));
244
245
}
245
246
247
+ /**
248
+ * Create a new diagnostic of the given kind, which is not mandatory and which has
249
+ * no lint category.
250
+ * @param kind The diagnostic kind
251
+ * @param source The source of the compilation unit, if any, in which to report the message.
252
+ * @param pos The source position at which to report the message.
253
+ * @param key The key for the localized message.
254
+ * @param rewriter A rewriter function used if this diagnostic needs to be rewritten
255
+ * @param args Fields of the message.
256
+ */
257
+ public JCDiagnostic create (
258
+ DiagnosticType kind , DiagnosticSource source , DiagnosticPosition pos , String key , UnaryOperator <JCDiagnostic > rewriter , Object ... args ) {
259
+ return create (null , EnumSet .noneOf (DiagnosticFlag .class ), source , pos , DiagnosticInfo .of (kind , prefix , key , args ), rewriter );
260
+ }
261
+
246
262
/**
247
263
* Create a new diagnostic of the given kind, which is not mandatory and which has
248
264
* no lint category.
@@ -282,6 +298,11 @@ public JCDiagnostic create(
282
298
LintCategory lc , Set <DiagnosticFlag > flags , DiagnosticSource source , DiagnosticPosition pos , DiagnosticInfo diagnosticInfo ) {
283
299
return new JCDiagnostic (formatter , normalize (diagnosticInfo ), lc , flags , source , pos );
284
300
}
301
+
302
+ public JCDiagnostic create (
303
+ LintCategory lc , Set <DiagnosticFlag > flags , DiagnosticSource source , DiagnosticPosition pos , DiagnosticInfo diagnosticInfo , UnaryOperator <JCDiagnostic > rewriter ) {
304
+ return new JCDiagnostic (formatter , normalize (diagnosticInfo ), lc , flags , source , pos , rewriter );
305
+ }
285
306
//where
286
307
DiagnosticInfo normalize (DiagnosticInfo diagnosticInfo ) {
287
308
//replace all nested FragmentKey with full-blown JCDiagnostic objects
@@ -446,6 +467,8 @@ public enum DiagnosticFlag {
446
467
/** source line position (set lazily) */
447
468
private SourcePosition sourcePosition ;
448
469
470
+ private final UnaryOperator <JCDiagnostic > rewriter ;
471
+
449
472
/**
450
473
* This class is used to defer the line/column position fetch logic after diagnostic construction.
451
474
*/
@@ -596,6 +619,25 @@ protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
596
619
Set <DiagnosticFlag > flags ,
597
620
DiagnosticSource source ,
598
621
DiagnosticPosition pos ) {
622
+ this (formatter , diagnosticInfo , lc , flags , source , pos , null );
623
+ }
624
+
625
+ /**
626
+ * Create a diagnostic object.
627
+ * @param formatter the formatter to use for the diagnostic
628
+ * @param diagnosticInfo the diagnostic key
629
+ * @param lc the lint category for the diagnostic
630
+ * @param source the name of the source file, or null if none.
631
+ * @param pos the character offset within the source file, if given.
632
+ * @param rewriter the rewriter function used if this diagnostic needs to be rewritten
633
+ */
634
+ protected JCDiagnostic (DiagnosticFormatter <JCDiagnostic > formatter ,
635
+ DiagnosticInfo diagnosticInfo ,
636
+ LintCategory lc ,
637
+ Set <DiagnosticFlag > flags ,
638
+ DiagnosticSource source ,
639
+ DiagnosticPosition pos ,
640
+ UnaryOperator <JCDiagnostic > rewriter ) {
599
641
if (source == null && pos != null && pos .getPreferredPosition () != Position .NOPOS )
600
642
throw new IllegalArgumentException ();
601
643
@@ -605,6 +647,7 @@ protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
605
647
this .flags = flags ;
606
648
this .source = source ;
607
649
this .position = pos ;
650
+ this .rewriter = rewriter ;
608
651
}
609
652
610
653
/**
@@ -807,6 +850,14 @@ public boolean isFlagSet(DiagnosticFlag flag) {
807
850
return flags .contains (flag );
808
851
}
809
852
853
+ boolean hasRewriter () {
854
+ return rewriter != null ;
855
+ }
856
+
857
+ JCDiagnostic rewrite () {
858
+ return rewriter .apply (this );
859
+ }
860
+
810
861
public static class MultilineDiagnostic extends JCDiagnostic {
811
862
812
863
private final List <JCDiagnostic > subdiagnostics ;
0 commit comments