You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2014/07/29 00:01:37 UTC

svn commit: r1614200 - in /maven/plugins/trunk/maven-checkstyle-plugin/src: it/check-pass/ it/check-pass/src/main/java/org/ main/java/org/apache/maven/plugin/checkstyle/ test/java/org/apache/maven/plugin/checkstyle/

Author: hboutemy
Date: Mon Jul 28 22:01:36 2014
New Revision: 1614200

URL: http://svn.apache.org/r1614200
Log:
[MCHECKSTYLE-242] added fine grained igore of violations

Added:
    maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java   (with props)
Modified:
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/pom.xml
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/src/main/java/org/MyClass.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/pom.xml?rev=1614200&r1=1614199&r2=1614200&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/pom.xml (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/pom.xml Mon Jul 28 22:01:36 2014
@@ -20,7 +20,7 @@
 
 <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
   <groupId>org.apache.maven.plugins.checkstyle</groupId>
@@ -32,6 +32,7 @@
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <checkstyle.violation.ignore>javadoc,misc,HideUtilityClassConstructor</checkstyle.violation.ignore>
   </properties>
 
   <build>

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/src/main/java/org/MyClass.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/src/main/java/org/MyClass.java?rev=1614200&r1=1614199&r2=1614200&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/src/main/java/org/MyClass.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-pass/src/main/java/org/MyClass.java Mon Jul 28 22:01:36 2014
@@ -23,4 +23,7 @@ package org;
  * Yada yada yada.
  */
 public class MyClass {
+    public static void main(String[] args) {
+        // no op
+    }
 }

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?rev=1614200&r1=1614199&r2=1614200&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Mon Jul 28 22:01:36 2014
@@ -127,6 +127,15 @@ public class CheckstyleViolationCheckMoj
     private String violationSeverity = "error";
 
     /**
+     * Violations to ignore. This is a comma-separated list, each value being either
+     * a rule name, a rule category or a java package name of rule class.
+     *
+     * @since 2.13
+     */
+    @Parameter( property = "checkstyle.violation.ignore" )
+    private String violationIgnore;
+
+    /**
      * Skip entire check.
      *
      * @since 2.2
@@ -575,6 +584,8 @@ public class CheckstyleViolationCheckMoj
         throws XmlPullParserException, IOException
     {
         int count = 0;
+        RuleUtil.Matcher[] ignores =
+            ( violationIgnore == null ) ? null : RuleUtil.parseMatchers( violationIgnore.split( "," ) );
 
         int eventType = xpp.getEventType();
         String file = "";
@@ -590,7 +601,9 @@ public class CheckstyleViolationCheckMoj
                 else if ( "error".equals( xpp.getName() ) )
                 {
                     String severity = xpp.getAttributeValue( "", "severity" );
-                    if ( isViolation( severity ) )
+                    String source = xpp.getAttributeValue( "", "source" );
+
+                    if ( isViolation( severity ) && !ignore( ignores, source ) )
                     {
                         count++;
 
@@ -599,7 +612,6 @@ public class CheckstyleViolationCheckMoj
                             String line = xpp.getAttributeValue( "", "line" );
                             String column = xpp.getAttributeValue( "", "column" );
                             String message = xpp.getAttributeValue( "", "message" );
-                            String source = xpp.getAttributeValue( "", "source" );
                             String rule = RuleUtil.getName( source );
                             String category = RuleUtil.getCategory( source );
 
@@ -654,6 +666,23 @@ public class CheckstyleViolationCheckMoj
             return false;
         }
     }
+
+    private boolean ignore( RuleUtil.Matcher[] ignores, String source )
+    {
+        if ( ignores != null )
+        {
+            for ( RuleUtil.Matcher ignore : ignores )
+            {
+                if ( ignore.match( source ) )
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     private DefaultLogger getConsoleListener()
         throws MojoExecutionException
     {

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java?rev=1614200&r1=1614199&r2=1614200&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java Mon Jul 28 22:01:36 2014
@@ -29,6 +29,8 @@ import com.puppycrawl.tools.checkstyle.a
  */
 public class RuleUtil
 {
+    private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";
+
     /**
      * Get the rule name from an audit event.
      *
@@ -87,15 +89,110 @@ public class RuleUtil
         int end = eventSrcName.lastIndexOf( '.' );
         eventSrcName = eventSrcName.substring( 0,  end );
 
-        if ( "com.puppycrawl.tools.checkstyle.checks".equals( eventSrcName ) )
+        if ( CHECKSTYLE_PACKAGE.equals( eventSrcName ) )
         {
             return "misc";
         }
-        else if ( !eventSrcName.startsWith( "com.puppycrawl.tools.checkstyle.checks" ) )
+        else if ( !eventSrcName.startsWith( CHECKSTYLE_PACKAGE ) )
         {
             return "extension";
         }
 
         return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 );
     }
+
+    public static Matcher[] parseMatchers( String[] specs )
+    {
+        Matcher[] matchers = new Matcher[specs.length];
+        int i = 0;
+        for ( String spec: specs )
+        {
+            spec = spec.trim();
+            Matcher matcher;
+            if ( Character.isUpperCase( spec.charAt( 0 ) ) )
+            {
+                // spec starting with uppercase is a rule name
+                matcher = new RuleMatcher( spec );
+            }
+            else if ( "misc".equals( spec ) )
+            {
+                // "misc" is a special case
+                matcher = new PackageMatcher( CHECKSTYLE_PACKAGE );
+            }
+            else if ( "extension".equals( spec ) )
+            {
+                // "extension" is a special case
+                matcher = new ExtensionMatcher();
+            }
+            else if ( !spec.contains( "." ) )
+            {
+                matcher = new PackageMatcher( CHECKSTYLE_PACKAGE + '.' + spec );
+            }
+            else
+            {
+                // by default, spec is a package name
+                matcher = new PackageMatcher( spec );
+            }
+            matchers[i++] = matcher;
+        }
+        return matchers;
+    }
+
+    /**
+     * Audit event source name matcher.
+     */
+    public static interface Matcher
+    {
+        /**
+         * Does the event source name match?
+         * @param eventSrcName the event source name
+         * @return boolean
+         */
+        boolean match( String eventSrcName );
+    }
+
+    private static class RuleMatcher
+        implements Matcher
+    {
+        private final String rule;
+
+        public RuleMatcher( String rule )
+        {
+            this.rule = rule;
+        }
+
+        public boolean match( String eventSrcName )
+        {
+            return rule.equals( getName( eventSrcName ) );
+        }
+    }
+
+    private static class PackageMatcher
+        implements Matcher
+    {
+        private final String packageName;
+
+        public PackageMatcher( String packageName )
+        {
+            this.packageName = packageName;
+        }
+
+        public boolean match( String eventSrcName )
+        {
+            return eventSrcName.startsWith( packageName )
+                && !eventSrcName.substring( packageName.length() + 1 ).contains( "." );
+        }
+    }
+
+    /**
+     * An extension does not start with Checkstyle package.
+     */
+    private static class ExtensionMatcher
+        implements Matcher
+    {
+        public boolean match( String eventSrcName )
+        {
+            return !eventSrcName.startsWith( CHECKSTYLE_PACKAGE );
+        }
+    }
 }

Added: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java?rev=1614200&view=auto
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java (added)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java Mon Jul 28 22:01:36 2014
@@ -0,0 +1,66 @@
+package org.apache.maven.plugin.checkstyle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+
+public class RuleUtilTest
+    extends TestCase
+{
+    private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";
+
+    public void testGetName()
+    {
+        assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParameters" ) );
+        assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
+        assertNull( RuleUtil.getName( (String) null ) );
+    }
+
+    public void testGetCategory()
+    {
+        assertEquals( "misc", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
+        assertEquals( "test", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck" ) );
+        assertEquals( "extension", RuleUtil.getCategory( "test.FinalParametersCheck" ) );
+        assertNull( RuleUtil.getCategory( (String) null ) );
+    }
+
+    public void testMatcher()
+    {
+        String[] specs = ( "misc, test, extension, Header, " + CHECKSTYLE_PACKAGE + ".test2" ).split( "," );
+        String[] eventSrcNames =
+            new String[] { CHECKSTYLE_PACKAGE + ".FinalParametersCheck",
+                CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck", "test.FinalParametersCheck",
+                CHECKSTYLE_PACKAGE + ".whitespace.HeaderCheck", CHECKSTYLE_PACKAGE + ".test2.FinalParametersCheck" };
+
+        RuleUtil.Matcher[] matchers = RuleUtil.parseMatchers( specs );
+
+        for ( int i = 0; i < matchers.length; i++ )
+        {
+            String spec = specs[i];
+            RuleUtil.Matcher matcher = matchers[i];
+            for ( int j = 0; j < matchers.length; j++ )
+            {
+                String eventSrcName = eventSrcNames[j];
+                assertEquals( spec + " should" + ( ( i == j ) ? " " : " not " ) + "match " + eventSrcName, i == j,
+                              matcher.match( eventSrcName ) );
+            }
+        }
+    }
+}

Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/RuleUtilTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain