From a2c6e3b52e0dca691632b7ab6f9dba06026a279d Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Wed, 4 May 2011 22:57:28 +0200 Subject: [PATCH] SONAR-2357 add a sample for Checkstyle extensions --- samples/README.txt | 10 ++++ samples/assembly.xml | 15 +++++ samples/checkstyle-extensions-plugin/pom.xml | 56 +++++++++++++++++++ .../CheckstyleExtensionRepository.java | 35 ++++++++++++ .../CheckstyleExtensionsPlugin.java | 14 +++++ .../sonar/checkstyle/MethodsCountCheck.java | 43 ++++++++++++++ .../mycompany/sonar/checkstyle/extensions.xml | 13 +++++ samples/gwt-plugin/pom.xml | 2 +- samples/pom.xml | 1 + samples/standard-plugin/pom.xml | 2 +- 10 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 samples/README.txt create mode 100644 samples/checkstyle-extensions-plugin/pom.xml create mode 100644 samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionRepository.java create mode 100644 samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/CheckstyleExtensionsPlugin.java create mode 100644 samples/checkstyle-extensions-plugin/src/main/java/com/mycompany/sonar/checkstyle/MethodsCountCheck.java create mode 100644 samples/checkstyle-extensions-plugin/src/main/resources/com/mycompany/sonar/checkstyle/extensions.xml 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 @@ pom.xml + + checkstyle-extensions-plugin + checkstyle-extensions-plugin + + src/**/* + pom.xml + + + + ${project.basedir} + / + + *.txt + + 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 @@ + + + 4.0.0 + + com.mycompany.sonar + sonar-checkstyle-extensions-plugin + sonar-plugin + 2.8-SNAPSHOT + Sonar :: Samples :: Checkstyle extensions + Checkstyle extensions for Sonar + + + + ${project.version} + + + + + org.codehaus.sonar + sonar-plugin-api + ${sonar.buildVersion} + + + checkstyle + checkstyle + 5.1 + provided + + + + + + + org.codehaus.sonar + sonar-packaging-maven-plugin + 1.1 + true + + com.mycompany.sonar.checkstyle.CheckstyleExtensionsPlugin + checkstyle + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.5 + 1.5 + + + + + \ 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 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 @@ + + + com.mycompany.sonar.checkstyle.MethodsCountCheck + Methods Count + Checker/TreeWalker/com.mycompany.sonar.checkstyle.MethodsCountCheck + Count methods + + minMethodsCount + Mimimun threshold + 10 + + + \ 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 @@ org.codehaus.sonar sonar-packaging-maven-plugin - 1.0 + 1.1 true com.mycompany.sonar.gwt.GwtPlugin 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 @@ standard-plugin gwt-plugin + checkstyle-extensions-plugin 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 @@ org.codehaus.sonar sonar-packaging-maven-plugin - 1.0 + 1.1 true com.mycompany.sonar.standard.SamplePlugin -- 2.39.5