Skip to content

Commit b259e84

Browse files
committed
[MCHECKSTYLE-385] Violation should be a value class
1 parent 7688eb9 commit b259e84

File tree

2 files changed

+323
-43
lines changed

2 files changed

+323
-43
lines changed

src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ public class CheckstyleViolationCheckMojo
156156
*/
157157
@Parameter( property = "checkstyle.console", defaultValue = "true" )
158158
private boolean logViolationsToConsole;
159-
159+
160160
/**
161161
* Output the detected violation count to the console.
162-
*
162+
*
163163
* @since 3.0.1
164164
*/
165165
@Parameter( property = "checkstyle.logViolationCount", defaultValue = "true" )
@@ -577,11 +577,14 @@ public void execute()
577577
XmlPullParser xpp = new MXParser();
578578
xpp.setInput( reader );
579579

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" : "" ) + ".";
581586

582-
String msg = "You have " + violations + " Checkstyle violation"
583-
+ ( ( violations > 1 || violations == 0 ) ? "s" : "" ) + ".";
584-
if ( violations > maxAllowedViolations )
587+
if ( violationCount > maxAllowedViolations )
585588
{
586589
if ( failOnViolation )
587590
{
@@ -591,7 +594,7 @@ public void execute()
591594
}
592595
throw new MojoFailureException( msg );
593596
}
594-
597+
595598
getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
596599
}
597600
if ( logViolationCountToConsole )
@@ -621,16 +624,14 @@ private void checkDeprecatedParameterUsage( Object parameter, String name, Strin
621624
}
622625
}
623626

624-
private int countViolations( XmlPullParser xpp )
627+
private List<Violation> getViolations( XmlPullParser xpp )
625628
throws XmlPullParserException, IOException
626629
{
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<>();
631631

632632
String basedir = project.getBasedir().getAbsolutePath();
633633
String file = "";
634+
634635
for ( int eventType = xpp.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xpp.next() )
635636
{
636637
if ( eventType != XmlPullParser.START_TAG )
@@ -640,49 +641,99 @@ private int countViolations( XmlPullParser xpp )
640641
else if ( "file".equals( xpp.getName() ) )
641642
{
642643
file = PathTool.getRelativeFilePath( basedir, xpp.getAttributeValue( "", "name" ) );
643-
//file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
644+
continue;
644645
}
645-
else if ( "error".equals( xpp.getName() ) )
646+
else if ( ! "error".equals( xpp.getName() ) )
646647
{
647-
String severity = xpp.getAttributeValue( "", "severity" );
648+
continue;
649+
}
648650

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+
}
653673

654-
String source = xpp.getAttributeValue( "", "source" );
674+
violations.add( violation );
675+
}
655676

656-
if ( ignore( ignores, source ) )
657-
{
658-
ignoreCount++;
659-
}
660-
else
661-
{
662-
count++;
677+
return violations;
678+
}
663679

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;
676699
}
700+
701+
countedViolations++;
677702
}
678703

679-
if ( ignoreCount > 0 )
704+
if ( ignored > 0 )
680705
{
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." );
683708
}
684709

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+
} );
686737
}
687738

688739
private void log( String severity, String message )

0 commit comments

Comments
 (0)