Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 6fe6bbfed5abf33437d6851591848d744255f598 @simonbrandhof simonbrandhof committed Sep 29, 2011
3 README.md
@@ -0,0 +1,3 @@
+# Sonar Examples
+
+TODO
63 plugins/sonar-checkstyle-extension-plugin/pom.xml
@@ -0,0 +1,63 @@
+<?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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.codehaus.sonar.examples</groupId>
+ <artifactId>sonar-checkstyle-extension-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>sonar-plugin</packaging>
+ <name>Examples :: Checkstyle Extension Plugin</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>2.10</version>
+ </dependency>
+ <dependency>
+ <groupId>checkstyle</groupId>
+ <artifactId>checkstyle</artifactId>
+ <version>5.1</version>
+ </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>org.sonar.examples.checkstyle.CheckstyleExtensionPlugin</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>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <repositories>
+ <repository>
+ <!-- for Checkstyle 5.1-->
+ <id>sonarsource</id>
+ <name>SonarSource</name>
+ <url>http://repository.sonarsource.org/content/repositories/sonar</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+</project>
12 ...tension-plugin/src/main/java/org/sonar/examples/checkstyle/CheckstyleExtensionPlugin.java
@@ -0,0 +1,12 @@
+package org.sonar.examples.checkstyle;
+
+import org.sonar.api.SonarPlugin;
+
+import java.util.Arrays;
+import java.util.List;
+
+public final class CheckstyleExtensionPlugin extends SonarPlugin {
+ public List getExtensions() {
+ return Arrays.asList(CheckstyleExtensionRepository.class);
+ }
+}
32 ...ion-plugin/src/main/java/org/sonar/examples/checkstyle/CheckstyleExtensionRepository.java
@@ -0,0 +1,32 @@
+package org.sonar.examples.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 final class CheckstyleExtensionRepository extends RuleRepository {
+
+ private XMLRuleParser xmlRuleParser;
+
+ public CheckstyleExtensionRepository(XMLRuleParser xmlRuleParser) {
+ super("checkstyle", Java.KEY);
+ setName("Checkstyle");
+ this.xmlRuleParser = xmlRuleParser;
+ }
+
+ @Override
+ public List<Rule> createRules() {
+ InputStream input = getClass().getResourceAsStream("/org/sonar/examples/checkstyle/checkstyle-extensions.xml");
+ try {
+ return xmlRuleParser.parse(input);
+
+ } finally {
+ IOUtils.closeQuietly(input);
+ }
+ }
+}
43 ...style-extension-plugin/src/main/java/org/sonar/examples/checkstyle/MethodsCountCheck.java
@@ -0,0 +1,43 @@
+package org.sonar.examples.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 maxMethodsCount = 1;
+
+ private int methodsCount = 0;
+ private DetailAST classAST = null;
+
+ public void setMaxMethodsCount(int maxMethodsCount) {
+ this.maxMethodsCount = maxMethodsCount;
+ }
+
+ 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 > maxMethodsCount) {
+ log(classAST.getLineNo(), classAST.getColumnNo(), "Too many methods (" + methodsCount + ") in class");
+ }
+ }
+}
13 ...tension-plugin/src/main/resources/org/sonar/examples/checkstyle/checkstyle-extensions.xml
@@ -0,0 +1,13 @@
+<rules>
+ <rule>
+ <key>org.sonar.examples.checkstyle.MethodsCountCheck</key>
+ <name>Methods Count Check</name>
+ <configKey>Checker/TreeWalker/org.sonar.examples.checkstyle.MethodsCountCheck</configKey>
+ <category name="Usability"/>
+ <description>Count methods</description>
+ <param>
+ <key>maxMethodsCount</key>
+ <description>Maximum number of methods. Default is 10.</description>
+ </param>
+ </rule>
+ </rules>
52 plugins/sonar-pmd-extension-plugin/pom.xml
@@ -0,0 +1,52 @@
+<?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>org.codehaus.sonar.examples</groupId>
+ <artifactId>sonar-pmd-extension-plugin</artifactId>
+ <packaging>sonar-plugin</packaging>
+ <version>0.1-SNAPSHOT</version>
+ <name>Examples :: PMD Extension Plugin</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>2.10</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>pmd</groupId>
+ <artifactId>pmd</artifactId>
+ <version>4.2.5</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>org.sonar.examples.pmd.PmdExtensionPlugin</pluginClass>
+
+ <!-- This is important. It means that this plugin extends the PMD plugin -->
+ <basePlugin>pmd</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>
23 ...r-pmd-extension-plugin/src/main/java/org/sonar/examples/pmd/MaximumMethodsCountCheck.java
@@ -0,0 +1,23 @@
+package org.sonar.examples.pmd;
+
+import net.sourceforge.pmd.AbstractRule;
+import net.sourceforge.pmd.ast.ASTClassOrInterfaceBody;
+import net.sourceforge.pmd.ast.ASTMethodDeclaration;
+import net.sourceforge.pmd.properties.IntegerProperty;
+
+import java.util.List;
+
+public class MaximumMethodsCountCheck extends AbstractRule {
+
+ private static final IntegerProperty propertyDescriptor = new IntegerProperty(
+ "maxAuthorisedMethodsCount", "Maximum number of methods authorised", 2, 1.0f);
+
+ public Object visit(ASTClassOrInterfaceBody node, Object data) {
+ List<ASTMethodDeclaration> methods = node.findChildrenOfType(ASTMethodDeclaration.class);
+ if (methods.size() > getIntProperty(propertyDescriptor)) {
+ addViolation(data, node);
+ }
+ return super.visit(node, data);
+ }
+
+}
14 ...s/sonar-pmd-extension-plugin/src/main/java/org/sonar/examples/pmd/PmdExtensionPlugin.java
@@ -0,0 +1,14 @@
+package org.sonar.examples.pmd;
+
+import org.sonar.api.SonarPlugin;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class PmdExtensionPlugin extends SonarPlugin {
+
+ public List getExtensions() {
+ return Arrays.asList(PmdExtensionRepository.class);
+ }
+
+}
35 ...nar-pmd-extension-plugin/src/main/java/org/sonar/examples/pmd/PmdExtensionRepository.java
@@ -0,0 +1,35 @@
+package org.sonar.examples.pmd;
+
+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 PmdExtensionRepository extends RuleRepository {
+
+ // Must be the same than the PMD plugin
+ private static final String REPOSITORY_KEY = "pmd";
+ private XMLRuleParser ruleParser;
+
+ public PmdExtensionRepository(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("/org/sonar/examples/pmd/extensions.xml");
+ try {
+ return ruleParser.parse(input);
+
+ } finally {
+ IOUtils.closeQuietly(input);
+ }
+ }
+
+}
56 plugins/sonar-pmd-extension-plugin/src/main/resources/org/sonar/examples/pmd/extensions.xml
@@ -0,0 +1,56 @@
+<rules>
+ <!-- Extension written in Java -->
+ <rule>
+ <key>MaximumMethodsCountCheck</key>
+ <name>Maximum Methods Count Check</name>
+ <description>Maximum number of methods authorised</description>
+
+ <!-- path to definition -->
+ <configKey>com/mycompany/sonar/pmd/rulesets.xml/MaximumMethodsCountCheck</configKey>
+
+ <!-- Default priority ("severity"). It can be changed while activating the rule in Quality profile -->
+ <!-- Possible values are: INFO, MINOR, MAJOR, CRITICAL, BLOCKER. Default value is MAJOR -->
+ <!--<priority>MAJOR</priority>-->
+
+ <!-- parameters available in administration console of Quality profiles -->
+ <param>
+ <key>maxAuthorisedMethodsCount</key>
+ <description>Maximum number of methods authorised</description>
+
+ <!-- default value is optional -->
+ <defaultValue>2</defaultValue>
+ </param>
+ </rule>
+
+
+ <!-- Extension written in XPath, first solution -->
+ <rule>
+ <key>AvoidIfWithoutBrace</key>
+ <name>Avoid if without using brace</name>
+ <configKey>com/mycompany/sonar/pmd/rulesets.xml/AvoidIfWithoutBrace</configKey>
+ <priority>CRITICAL</priority>
+ </rule>
+
+
+ <!--
+ Extension written in XPath, second solution.
+ Difference is that the XPath expression can be configured by users from administration console of Quality profiles
+ -->
+ <rule>
+ <key>PreventUseOfEmptyClass</key>
+ <priority>MAJOR</priority>
+ <name><![CDATA[Prevent use of EmptyClass]]></name>
+ <configKey><![CDATA[net.sourceforge.pmd.rules.XPathRule]]></configKey>
+ <priority>MINOR</priority>
+ <param>
+ <key>xpath</key>
+ <description><![CDATA[XPath expressions.]]></description>
+ <defaultValue>//VariableDeclarator[../Type/ReferenceType/ClassOrInterfaceType[@Image='EmptyClass']]</defaultValue>
+ </param>
+ <param>
+ <key>message</key>
+ <description><![CDATA[Message to display when a violation occurs.]]></description>
+ <defaultValue>Prevent use of EmptyClass class</defaultValue>
+ </param>
+ </rule>
+</rules>
53 plugins/sonar-pmd-extension-plugin/src/main/resources/org/sonar/examples/pmd/rulesets.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+<ruleset name="PMD extensions"
+ xmlns="http://pmd.sf.net/ruleset/1.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
+ xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
+
+ <rule name="MaximumMethodsCountCheck"
+ message="Avoid too many methods"
+ class="com.mycompany.sonar.pmd.MaximumMethodsCountCheck">
+ <description>
+ Avoid too many methods
+ </description>
+ <priority>3</priority>
+ <properties>
+ <property name="maxAuthorisedMethodsCount" description="Maximum number of methods authorised" value="3"/>
+ </properties>
+ <example>
+ <![CDATA[
+ // too many methods!
+ public void doSomething() {}
+ public void doSomething2() {}
+ public void doSomething3() {}
+ public void doSomething4() {}
+
+ ]]>
+ </example>
+ </rule>
+
+ <rule name="AvoidIfWithoutBrace"
+ message="Avoid if without using brace"
+ class="net.sourceforge.pmd.rules.XPathRule">
+ <description>
+ Avoid if without using brace
+ </description>
+ <properties>
+ <property name="xpath">
+ <value>
+ <![CDATA[
+ //IfStatement[not(Statement/Block)]
+ ]]>
+ </value>
+ </property>
+ </properties>
+ <example>
+ <![CDATA[
+ // don't do this!
+ if (true)
+ test = "test";
+ ]]>
+ </example>
+ </rule>
+</ruleset>

0 comments on commit 6fe6bbf

Please sign in to comment.
Something went wrong with that request. Please try again.