]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2357 add a sample for Checkstyle extensions
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 4 May 2011 20:57:28 +0000 (22:57 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 4 May 2011 20:57:40 +0000 (22:57 +0200)
samples/README.txt [new file with mode: 0644]
samples/assembly.xml
samples/checkstyle-extensions-plugin/pom.xml [new file with mode: 0644]
samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionRepository.java [new file with mode: 0644]
samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionsPlugin.java [new file with mode: 0644]
samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/MethodsCountCheck.java [new file with mode: 0644]
samples/checkstyle-extensions-plugin/src/main/resources/com/mycompany/sonar/checkstyle/extensions.xml [new file with mode: 0644]
samples/gwt-plugin/pom.xml
samples/pom.xml
samples/standard-plugin/pom.xml

diff --git a/samples/README.txt b/samples/README.txt
new file mode 100644 (file)
index 0000000..97e8bd3
--- /dev/null
@@ -0,0 +1,10 @@
+=== How to install a plugin
+
+1. Build plugin with Maven : mvn clean install
+2. Copy the JAR file generated into target/ to the directory extensions/plugins/ of the Sonar server 
+3. Restart the Sonar server
+
+
+=== How to activate Checkstyle extensions
+
+Install the plugin "checkstyle-extensions" then search for the rule "Methods count"  in the administration console of Quality profiles.
\ No newline at end of file
index 014a7dc4036d6669f04b359a302520f39a366d41..252261f7b4c38475229fa0e3da4c2650af6d8dcc 100644 (file)
         <include>pom.xml</include>
       </includes>
     </fileSet>
+    <fileSet>
+      <directory>checkstyle-extensions-plugin</directory>
+      <outputDirectory>checkstyle-extensions-plugin</outputDirectory>
+      <includes>
+        <include>src/**/*</include>
+        <include>pom.xml</include>
+      </includes>
+    </fileSet>
+    <fileSet>
+      <directory>${project.basedir}</directory>
+      <outputDirectory>/</outputDirectory>
+      <includes>
+        <include>*.txt</include>
+      </includes>
+    </fileSet>
   </fileSets>
 </assembly>
diff --git a/samples/checkstyle-extensions-plugin/pom.xml b/samples/checkstyle-extensions-plugin/pom.xml
new file mode 100644 (file)
index 0000000..d2c7b66
--- /dev/null
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.mycompany.sonar</groupId>
+  <artifactId>sonar-checkstyle-extensions-plugin</artifactId>
+  <packaging>sonar-plugin</packaging>
+  <version>2.8-SNAPSHOT</version>
+  <name>Sonar :: Samples :: Checkstyle extensions</name>
+  <description>Checkstyle extensions for Sonar</description>
+
+  <properties>
+    <!-- To be replaced with the minimum required version of Sonar -->
+    <sonar.buildVersion>${project.version}</sonar.buildVersion>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.sonar</groupId>
+      <artifactId>sonar-plugin-api</artifactId>
+      <version>${sonar.buildVersion}</version>
+    </dependency>
+    <dependency>
+      <groupId>checkstyle</groupId>
+      <artifactId>checkstyle</artifactId>
+      <version>5.1</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.sonar</groupId>
+        <artifactId>sonar-packaging-maven-plugin</artifactId>
+        <version>1.1</version>
+        <extensions>true</extensions>
+        <configuration>
+          <pluginClass>com.mycompany.sonar.checkstyle.CheckstyleExtensionsPlugin</pluginClass>
+          <basePlugin>checkstyle</basePlugin>
+        </configuration>
+      </plugin>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</version>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
\ No newline at end of file
diff --git a/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionRepository.java b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionRepository.java
new file mode 100644 (file)
index 0000000..5132959
--- /dev/null
@@ -0,0 +1,35 @@
+package com.mycompany.sonar.checkstyle;
+
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.resources.Java;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleRepository;
+import org.sonar.api.rules.XMLRuleParser;
+
+import java.io.InputStream;
+import java.util.List;
+
+public class CheckstyleExtensionRepository extends RuleRepository {
+
+  // Must be the same than the Checkstyle plugin
+  private static final String REPOSITORY_KEY = "checkstyle";
+  private XMLRuleParser ruleParser;
+
+  public CheckstyleExtensionRepository(XMLRuleParser ruleParser) {
+    super(REPOSITORY_KEY, Java.KEY);
+    this.ruleParser = ruleParser;
+  }
+
+  @Override
+  public List<Rule> createRules() {
+    // In this example, new rules are declared in a XML file
+    InputStream input = getClass().getResourceAsStream("/com/mycompany/sonar/checkstyle/extensions.xml");
+    try {
+      return ruleParser.parse(input);
+      
+    } finally {
+      IOUtils.closeQuietly(input);
+    }
+  }
+
+}
diff --git a/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionsPlugin.java b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionsPlugin.java
new file mode 100644 (file)
index 0000000..4441388
--- /dev/null
@@ -0,0 +1,14 @@
+package com.mycompany.sonar.checkstyle;
+
+import org.sonar.api.SonarPlugin;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class CheckstyleExtensionsPlugin extends SonarPlugin {
+  
+  public List getExtensions() {
+    return Arrays.asList(CheckstyleExtensionRepository.class);
+  }
+  
+}
diff --git a/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/MethodsCountCheck.java b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/MethodsCountCheck.java
new file mode 100644 (file)
index 0000000..dcfa124
--- /dev/null
@@ -0,0 +1,43 @@
+package com.mycompany.sonar.checkstyle;
+
+import com.puppycrawl.tools.checkstyle.api.Check;
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+
+public class MethodsCountCheck extends Check {
+
+  private int minMethodsCount = 1;
+  
+  private int methodsCount = 0;
+  private DetailAST classAST = null;
+  
+  public void setMinMethodsCount(int minMethodsCount) {
+    this.minMethodsCount = minMethodsCount;
+  }
+  
+  public int[] getDefaultTokens() {
+    return new int[]{TokenTypes.CLASS_DEF, TokenTypes.METHOD_DEF};
+  }
+  
+  public void beginTree(DetailAST rootAST) {
+    methodsCount = 0;
+    classAST = null;
+  }
+  
+  public void visitToken(DetailAST ast) {
+    //ensure this is an unit test.
+    if ( ast.getType() == TokenTypes.CLASS_DEF ) {
+      classAST = ast;
+      
+    } else if ( ast.getType() == TokenTypes.METHOD_DEF ) {
+      methodsCount++;
+    }
+  }
+
+  public void finishTree(DetailAST rootAST) {
+    super.finishTree(rootAST);
+    if (classAST != null && methodsCount < minMethodsCount) {
+        log(classAST.getLineNo(), classAST.getColumnNo(), "Too few methods (" + methodsCount + ") in class" );
+    }
+  }
+}
diff --git a/samples/checkstyle-extensions-plugin/src/main/resources/com/mycompany/sonar/checkstyle/extensions.xml b/samples/checkstyle-extensions-plugin/src/main/resources/com/mycompany/sonar/checkstyle/extensions.xml
new file mode 100644 (file)
index 0000000..c5d0a70
--- /dev/null
@@ -0,0 +1,13 @@
+<rules>
+  <rule>
+    <key>com.mycompany.sonar.checkstyle.MethodsCountCheck</key>
+    <name>Methods Count</name>
+    <configKey>Checker/TreeWalker/com.mycompany.sonar.checkstyle.MethodsCountCheck</configKey>
+    <description>Count methods</description>
+    <param>
+      <key>minMethodsCount</key>
+      <description>Mimimun threshold</description>
+      <defaultValue>10</defaultValue>
+    </param>
+  </rule>
+ </rules>
\ No newline at end of file
index 975327bca1e53e4fdba155bf5a6e2c6fa9edb0c2..aa6ba12e36c5b73ecbeaf54b92b58ccdd1a72e93 100644 (file)
@@ -61,7 +61,7 @@
       <plugin>
         <groupId>org.codehaus.sonar</groupId>
         <artifactId>sonar-packaging-maven-plugin</artifactId>
-        <version>1.0</version>
+        <version>1.1</version>
         <extensions>true</extensions>
         <configuration>
           <pluginClass>com.mycompany.sonar.gwt.GwtPlugin</pluginClass>
index a6069f570c9a880309b9830319654d4293b68773..c41bac7b16c19e74faace6b301775d16bf55bede 100644 (file)
@@ -16,6 +16,7 @@
   <modules>
     <module>standard-plugin</module>
     <module>gwt-plugin</module>
+    <module>checkstyle-extensions-plugin</module>
   </modules>
 
   <build>
index a075f6872719c5d8235ea0e551cf1b4784d6f95c..8b9c9b6f0a8e8ff3a2994f7f5e24ee167e018a33 100644 (file)
@@ -36,7 +36,7 @@
       <plugin>
         <groupId>org.codehaus.sonar</groupId>
         <artifactId>sonar-packaging-maven-plugin</artifactId>
-        <version>1.0</version>
+        <version>1.1</version>
         <extensions>true</extensions>
         <configuration>
           <pluginClass>com.mycompany.sonar.standard.SamplePlugin</pluginClass>