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 /samples/checkstyle-extensions-plugin | |
parent | 7a957c2d1b8736b0c5e14396029bde45524688b3 (diff) | |
download | sonarqube-a2c6e3b52e0dca691632b7ab6f9dba06026a279d.tar.gz sonarqube-a2c6e3b52e0dca691632b7ab6f9dba06026a279d.zip |
SONAR-2357 add a sample for Checkstyle extensions
Diffstat (limited to 'samples/checkstyle-extensions-plugin')
5 files changed, 161 insertions, 0 deletions
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 |