From: simonbrandhof Date: Thu, 5 May 2011 10:25:41 +0000 (+0200) Subject: SONAR-2357 Add sample of PMD extensions X-Git-Tag: 2.8~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a88829cf39021c807b2e58e35d54662ec77ed11f;p=sonarqube.git SONAR-2357 Add sample of PMD extensions --- diff --git a/samples/assembly.xml b/samples/assembly.xml index 252261f7b4c..9508d3a84f2 100644 --- a/samples/assembly.xml +++ b/samples/assembly.xml @@ -31,6 +31,14 @@ pom.xml + + pmd-extensions-plugin + pmd-extensions-plugin + + src/**/* + pom.xml + + ${project.basedir} / diff --git a/samples/pmd-extensions-plugin/pom.xml b/samples/pmd-extensions-plugin/pom.xml new file mode 100644 index 00000000000..864ba1ee456 --- /dev/null +++ b/samples/pmd-extensions-plugin/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.mycompany.sonar + sonar-pmd-extensions-plugin + sonar-plugin + 2.8-SNAPSHOT + Sonar :: Samples :: PMD extensions + PMD extensions for Sonar + + + + ${project.version} + + + + + org.codehaus.sonar + sonar-plugin-api + ${sonar.buildVersion} + + + pmd + pmd + 4.2.5 + provided + + + + + + + org.codehaus.sonar + sonar-packaging-maven-plugin + 1.1 + true + + com.mycompany.sonar.pmd.PmdExtensionsPlugin + + + pmd + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.5 + 1.5 + + + + + \ No newline at end of file diff --git a/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/MaximumMethodsCountCheck.java b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/MaximumMethodsCountCheck.java new file mode 100644 index 00000000000..90e9b1cc041 --- /dev/null +++ b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/MaximumMethodsCountCheck.java @@ -0,0 +1,25 @@ +package com.mycompany.sonar.pmd; + +import java.util.ArrayList; +import java.util.List; +import net.sourceforge.pmd.AbstractRule; +import net.sourceforge.pmd.ast.ASTClassOrInterfaceBody; +import net.sourceforge.pmd.ast.ASTMethodDeclaration; +import net.sourceforge.pmd.properties.IntegerProperty; + +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 methods = new ArrayList(); + methods = (List)node.findChildrenOfType(ASTMethodDeclaration.class); + + if (methods.size() > getIntProperty(propertyDescriptor)) { + addViolation(data, node); + } + return super.visit(node,data); + } + +} diff --git a/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionRepository.java b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionRepository.java new file mode 100644 index 00000000000..2ad40acd593 --- /dev/null +++ b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionRepository.java @@ -0,0 +1,35 @@ +package com.mycompany.sonar.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 createRules() { + // In this example, new rules are declared in a XML file + InputStream input = getClass().getResourceAsStream("/com/mycompany/sonar/pmd/extensions.xml"); + try { + return ruleParser.parse(input); + + } finally { + IOUtils.closeQuietly(input); + } + } + +} diff --git a/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionsPlugin.java b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionsPlugin.java new file mode 100644 index 00000000000..646cd12255a --- /dev/null +++ b/samples/pmd-extensions-plugin/src/main/java/com/mycompany/sonar/pmd/PmdExtensionsPlugin.java @@ -0,0 +1,14 @@ +package com.mycompany.sonar.pmd; + +import org.sonar.api.SonarPlugin; + +import java.util.Arrays; +import java.util.List; + +public class PmdExtensionsPlugin extends SonarPlugin { + + public List getExtensions() { + return Arrays.asList(PmdExtensionRepository.class); + } + +} diff --git a/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/extensions.xml b/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/extensions.xml new file mode 100644 index 00000000000..808974b038d --- /dev/null +++ b/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/extensions.xml @@ -0,0 +1,56 @@ + + + + MaximumMethodsCountCheck + Maximum Methods Count Check + Maximum number of methods authorised + + + com/mycompany/sonar/pmd/rulesets.xml/MaximumMethodsCountCheck + + + + + + + + maxAuthorisedMethodsCount + Maximum number of methods authorised + + + 2 + + + + + + + AvoidIfWithoutBrace + Avoid if without using brace + com/mycompany/sonar/pmd/rulesets.xml/AvoidIfWithoutBrace + CRITICAL + + + + + + PreventUseOfEmptyClass + MAJOR + + + MINOR + + xpath + + //VariableDeclarator[../Type/ReferenceType/ClassOrInterfaceType[@Image='EmptyClass']] + + + message + + Prevent use of EmptyClass class + + + \ No newline at end of file diff --git a/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/rulesets.xml b/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/rulesets.xml new file mode 100644 index 00000000000..6922cd62da2 --- /dev/null +++ b/samples/pmd-extensions-plugin/src/main/resources/com/mycompany/sonar/pmd/rulesets.xml @@ -0,0 +1,53 @@ + + + + + + Avoid too many methods + + 3 + + + + + + + + + + + Avoid if without using brace + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/pom.xml b/samples/pom.xml index c41bac7b16c..723e6e8d90d 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -17,6 +17,7 @@ standard-plugin gwt-plugin checkstyle-extensions-plugin + pmd-extensions-plugin