2
votes

I have created a custom plugin for sonarqube version 4.5.1. This plugin contains a new custom rule, based on PMD. I have followed some examples (https://github.com/SonarSource/sonar-examples/tree/master/plugins) for develop this plugin in correct way, but all time i build the project that I have to check with sonar, i have the same error: java.lang.IllegalArgumentException: Cannot set non-existant property 'maxAuthorisedMethodsCount' on Rule MaximumMethodsCountCheckRule My Sonar installation have also installed the sonar-pmd-plugin v2.2 and sonar-java-plugin v2.4. Below the code that generates error

extensions.xml

<?xml version="1.0" encoding="UTF-8"?>
<rules>
    <rule>
    <key>MaximumMethodsCount</key>
    <name>Maximum Methods Count Check</name>
    <description>Massimo numero di metodi autorizzati per classe</description>

    <!-- path to definition -->
    <configKey>rulesets.xml/MaximumMethodsCountCheckRule</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>Numero massimo di metodi autorizzati per classe</description>
       <!-- default value is optional --> 
      <defaultValue>4</defaultValue>      
    </param>

  </rule>

</rules>

rulesets.xml

<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    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">

    <description>
        sonar plugin rulesets
    </description>

    <rule name="MaximumMethodsCountCheckRule"
          message="Superato Numero massimo di metodi autorizzati per classe. Trovati {0} metodi!"
          class="alm.plugin.rules.MaximumMethodsCountCheck">

      <description>
        Numero massimo di metodi autorizzati per classe
      </description>

      <priority>3</priority>

      <properties>
        <property name="maxAuthorisedMethodsCount" description="Maximum number of methods authorised">
        </property>
      </properties>      
      <example>
        <![CDATA[
                //Troppi metodi!
                public void metodo1(){}
                public void metodo2(){}
                public void metodo3(){}
                public void metodo4(){}
                .....
                public void metodo14(){}
        ]]>
      </example>      
    </rule>  
</ruleset>

MaximumMethodsCountCheck.java

import java.util.List;

import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Property;

public class MaximumMethodsCountCheck extends AbstractJavaRule {

    Logger logger = LoggerFactory.getLogger(getClass());

    private static final IntegerProperty propertyDescriptor = new IntegerProperty("maxAuthorisedMethodsCount", "Massimo numero di metodi", 0, 2, 4, 1.0f);

    @Override
    public Object visit(ASTClassOrInterfaceBody node, Object data) {    
        logger.info("---Analisi con MaximumMethodsCountCheck---");
        List<ASTMethodDeclaration> metodi = node.findChildrenOfType(ASTMethodDeclaration.class);
        if(metodi.size() > getProperty(propertyDescriptor)){            
            addViolation(data, node, ""+metodi.size());
        }
        return super.visit(node, data);
    }
}

pom.xml

<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>ALM-soanrqube-plugin</groupId>
  <artifactId>ALM-soanrqube-plugin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>sonar-plugin</packaging>


  <name>SONARQUBE PLUGIN</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.sonar</groupId>
            <artifactId>sonar-plugin-api</artifactId>
            <version>4.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.sonar-plugins.java</groupId>
            <artifactId>sonar-java-plugin</artifactId>
            <type>sonar-plugin</type>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd</artifactId>
            <version>5.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>sonar-maven-plugin</artifactId>
          <version>2.4</version>
          <type>maven-plugin</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.sonar</groupId>
                <artifactId>sonar-packaging-maven-plugin</artifactId>
                <version>1.9</version>
                <extensions>true</extensions>
                <configuration>
                    <pluginClass>alm.plugin.PmdExtensionPlugin</pluginClass>
                    <basePlugin>pmd</basePlugin>
                    <pluginDescription>PlugIn per Sonar</pluginDescription>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- UTF-8 bundles are not supported by Java, so they must be converted during build -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native2ascii-maven-plugin</artifactId>
                <version>1.0-beta-1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>native2ascii</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>  
</project>

Anyone have any idea on how to fix the error ? Please Help! Thank you very much!!!

1

1 Answers

2
votes

You need to call definePropertyDescriptor(propertyDescriptor) in the constructor of your MaximumMethodsCountCheck class.

(I figured this out by looking at some of the provided rules which have properties, for example NPathComplexityRule)