--- /dev/null
+=== 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
<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>
--- /dev/null
+<?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
--- /dev/null
+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);
+ }
+ }
+
+}
--- /dev/null
+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);
+ }
+
+}
--- /dev/null
+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" );
+ }
+ }
+}
--- /dev/null
+<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
<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>
<modules>
<module>standard-plugin</module>
<module>gwt-plugin</module>
+ <module>checkstyle-extensions-plugin</module>
</modules>
<build>
<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>