diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-04 22:57:28 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-04 22:57:40 +0200 |
commit | a2c6e3b52e0dca691632b7ab6f9dba06026a279d (patch) | |
tree | af905da38361ce9f8fcac11dc1b6b9882585db8f | |
parent | 7a957c2d1b8736b0c5e14396029bde45524688b3 (diff) | |
download | sonarqube-a2c6e3b52e0dca691632b7ab6f9dba06026a279d.tar.gz sonarqube-a2c6e3b52e0dca691632b7ab6f9dba06026a279d.zip |
SONAR-2357 add a sample for Checkstyle extensions
10 files changed, 189 insertions, 2 deletions
diff --git a/samples/README.txt b/samples/README.txt new file mode 100644 index 00000000000..97e8bd3910f --- /dev/null +++ b/samples/README.txt @@ -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 diff --git a/samples/assembly.xml b/samples/assembly.xml index 014a7dc4036..252261f7b4c 100644 --- a/samples/assembly.xml +++ b/samples/assembly.xml @@ -23,5 +23,20 @@ <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 index 00000000000..d2c7b66dd16 --- /dev/null +++ b/samples/checkstyle-extensions-plugin/pom.xml @@ -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 index 00000000000..51329595709 --- /dev/null +++ b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionRepository.java @@ -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 index 00000000000..44413883245 --- /dev/null +++ b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionsPlugin.java @@ -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 index 00000000000..dcfa1244385 --- /dev/null +++ b/samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/MethodsCountCheck.java @@ -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 index 00000000000..c5d0a707071 --- /dev/null +++ b/samples/checkstyle-extensions-plugin/src/main/resources/com/mycompany/sonar/checkstyle/extensions.xml @@ -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 diff --git a/samples/gwt-plugin/pom.xml b/samples/gwt-plugin/pom.xml index 975327bca1e..aa6ba12e36c 100644 --- a/samples/gwt-plugin/pom.xml +++ b/samples/gwt-plugin/pom.xml @@ -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> diff --git a/samples/pom.xml b/samples/pom.xml index a6069f570c9..c41bac7b16c 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -16,6 +16,7 @@ <modules> <module>standard-plugin</module> <module>gwt-plugin</module> + <module>checkstyle-extensions-plugin</module> </modules> <build> diff --git a/samples/standard-plugin/pom.xml b/samples/standard-plugin/pom.xml index a075f687271..8b9c9b6f0a8 100644 --- a/samples/standard-plugin/pom.xml +++ b/samples/standard-plugin/pom.xml @@ -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> |