@@ -156,10 +156,10 @@ public class CheckstyleViolationCheckMojo
156
156
*/
157
157
@ Parameter ( property = "checkstyle.console" , defaultValue = "true" )
158
158
private boolean logViolationsToConsole ;
159
-
159
+
160
160
/**
161
161
* Output the detected violation count to the console.
162
- *
162
+ *
163
163
* @since 3.0.1
164
164
*/
165
165
@ Parameter ( property = "checkstyle.logViolationCount" , defaultValue = "true" )
@@ -577,11 +577,14 @@ public void execute()
577
577
XmlPullParser xpp = new MXParser ();
578
578
xpp .setInput ( reader );
579
579
580
- int violations = countViolations ( xpp );
580
+ final List <Violation > violationsList = getViolations ( xpp );
581
+ long violationCount = countViolations ( violationsList );
582
+ printViolations ( violationsList );
583
+
584
+ String msg = "You have " + violationCount + " Checkstyle violation"
585
+ + ( ( violationCount > 1 || violationCount == 0 ) ? "s" : "" ) + "." ;
581
586
582
- String msg = "You have " + violations + " Checkstyle violation"
583
- + ( ( violations > 1 || violations == 0 ) ? "s" : "" ) + "." ;
584
- if ( violations > maxAllowedViolations )
587
+ if ( violationCount > maxAllowedViolations )
585
588
{
586
589
if ( failOnViolation )
587
590
{
@@ -591,7 +594,7 @@ public void execute()
591
594
}
592
595
throw new MojoFailureException ( msg );
593
596
}
594
-
597
+
595
598
getLog ().warn ( "checkstyle:check violations detected but failOnViolation set to false" );
596
599
}
597
600
if ( logViolationCountToConsole )
@@ -621,16 +624,14 @@ private void checkDeprecatedParameterUsage( Object parameter, String name, Strin
621
624
}
622
625
}
623
626
624
- private int countViolations ( XmlPullParser xpp )
627
+ private List < Violation > getViolations ( XmlPullParser xpp )
625
628
throws XmlPullParserException , IOException
626
629
{
627
- int count = 0 ;
628
- int ignoreCount = 0 ;
629
- List <RuleUtil .Matcher > ignores = violationIgnore == null ? Collections .<RuleUtil .Matcher >emptyList ()
630
- : RuleUtil .parseMatchers ( violationIgnore .split ( "," ) );
630
+ List <Violation > violations = new ArrayList <>();
631
631
632
632
String basedir = project .getBasedir ().getAbsolutePath ();
633
633
String file = "" ;
634
+
634
635
for ( int eventType = xpp .getEventType (); eventType != XmlPullParser .END_DOCUMENT ; eventType = xpp .next () )
635
636
{
636
637
if ( eventType != XmlPullParser .START_TAG )
@@ -640,49 +641,99 @@ private int countViolations( XmlPullParser xpp )
640
641
else if ( "file" .equals ( xpp .getName () ) )
641
642
{
642
643
file = PathTool .getRelativeFilePath ( basedir , xpp .getAttributeValue ( "" , "name" ) );
643
- //file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 ) ;
644
+ continue ;
644
645
}
645
- else if ( "error" .equals ( xpp .getName () ) )
646
+ else if ( ! "error" .equals ( xpp .getName () ) )
646
647
{
647
- String severity = xpp .getAttributeValue ( "" , "severity" );
648
+ continue ;
649
+ }
648
650
649
- if ( !isViolation ( severity ) )
650
- {
651
- continue ;
652
- }
651
+ String severity = xpp .getAttributeValue ( "" , "severity" );
652
+ String source = xpp .getAttributeValue ( "" , "source" );
653
+ String line = xpp .getAttributeValue ( "" , "line" );
654
+ /* Nullable */
655
+ String column = xpp .getAttributeValue ( "" , "column" );
656
+ String message = xpp .getAttributeValue ( "" , "message" );
657
+ String rule = RuleUtil .getName ( source );
658
+ String category = RuleUtil .getCategory ( source );
659
+
660
+ Violation violation = new Violation (
661
+ source ,
662
+ file ,
663
+ line ,
664
+ severity ,
665
+ message ,
666
+ rule ,
667
+ category
668
+ );
669
+ if ( column != null )
670
+ {
671
+ violation .setColumn ( column );
672
+ }
653
673
654
- String source = xpp .getAttributeValue ( "" , "source" );
674
+ violations .add ( violation );
675
+ }
655
676
656
- if ( ignore ( ignores , source ) )
657
- {
658
- ignoreCount ++;
659
- }
660
- else
661
- {
662
- count ++;
677
+ return violations ;
678
+ }
663
679
664
- if ( logViolationsToConsole )
665
- {
666
- String line = xpp .getAttributeValue ( "" , "line" );
667
- String column = xpp .getAttributeValue ( "" , "column" );
668
- String message = xpp .getAttributeValue ( "" , "message" );
669
- String rule = RuleUtil .getName ( source );
670
- String category = RuleUtil .getCategory ( source );
671
-
672
- log ( severity , file + ":[" + line + ( ( column == null ) ? "" : ( ',' + column ) ) + "] ("
673
- + category + ") " + rule + ": " + message );
674
- }
675
- }
680
+ private int countViolations ( List <Violation > violations )
681
+ {
682
+ List <RuleUtil .Matcher > ignores = violationIgnore == null ? Collections .<RuleUtil .Matcher >emptyList ()
683
+ : RuleUtil .parseMatchers ( violationIgnore .split ( "," ) );
684
+
685
+ int ignored = 0 ;
686
+ int countedViolations = 0 ;
687
+
688
+ for ( Violation violation : violations )
689
+ {
690
+ if ( ! isViolation ( violation .getSeverity () ) )
691
+ {
692
+ continue ;
693
+ }
694
+
695
+ if ( ignore ( ignores , violation .getSource () ) )
696
+ {
697
+ ignored ++;
698
+ continue ;
676
699
}
700
+
701
+ countedViolations ++;
677
702
}
678
703
679
- if ( ignoreCount > 0 )
704
+ if ( ignored > 0 )
680
705
{
681
- getLog ().info ( "Ignored " + ignoreCount + " error" + ( ( ignoreCount > 1 ) ? "s" : "" ) + ", " + count
682
- + " violation" + ( ( count > 1 ) ? "s" : "" ) + " remaining." );
706
+ getLog ().info ( "Ignored " + ignored + " error" + ( ( ignored > 1L ) ? "s" : "" ) + ", " + countedViolations
707
+ + " violation" + ( ( countedViolations > 1 ) ? "s" : "" ) + " remaining." );
683
708
}
684
709
685
- return count ;
710
+ return countedViolations ;
711
+ }
712
+
713
+ private void printViolations ( List <Violation > violations )
714
+ {
715
+ if ( ! logViolationsToConsole )
716
+ {
717
+ return ;
718
+ }
719
+
720
+ List <RuleUtil .Matcher > ignores = violationIgnore == null ? Collections .<RuleUtil .Matcher >emptyList ()
721
+ : RuleUtil .parseMatchers ( violationIgnore .split ( "," ) );
722
+
723
+ violations .stream ()
724
+ .filter ( violation -> isViolation ( violation .getSeverity () ) )
725
+ .filter ( violation -> !ignore ( ignores , violation .getSource () ) )
726
+ .forEach ( violation ->
727
+ {
728
+ final String message = String .format ( "%s:[%s%s] (%s) %s: %s" ,
729
+ violation .getFile (),
730
+ violation .getLine (),
731
+ ( Violation .NO_COLUMN .equals ( violation .getColumn () ) ) ? "" : ( ',' + violation .getColumn () ),
732
+ violation .getCategory (),
733
+ violation .getRuleName (),
734
+ violation .getMessage () );
735
+ log ( violation .getSeverity (), message );
736
+ } );
686
737
}
687
738
688
739
private void log ( String severity , String message )
0 commit comments