当前位置: 首页 > 知识库问答 >
问题:

无法设置不存在的属性PMD sonarqube插件

向安福
2023-03-14

我已经为sonarqube 4.5.1版创建了一个自定义插件。这个插件包含一个新的自定义规则,基于PMD。我遵循了一些例子(https://github.com/SonarSource/sonar-examples/tree/master/plugins),以正确的方式开发这个插件,但我一直在构建我必须用声纳检查的项目,我有同样的错误:java.lang.IllegalArgumentExctive:无法在规则MaximummetodsCountCheck规则上设置不存在的属性'maxAuthorisedmetodsCount'我的声纳安装也安装了声纳-pmd-plugin v2.2和声纳-java-plugin v2.4。下面的代码会产生错误

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>

规则集。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>

MaximumMethodCountCheck。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);
    }
}

波姆。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>

有人知道如何修复错误吗?请帮忙!非常感谢你!!!

共有1个答案

盛跃
2023-03-14

您需要在您的MaximummetodsCountCheck类的构造函数中调用定义属性描述符。

(我通过查看提供的一些具有属性的规则,例如NPathComplexityRule,了解到了这一点)

 类似资料:
  • 问题内容: 我在这里阅读了一些有关如何从Maven插件设置属性的问题(大多数问题涉及应用程序的版本号)。似乎没有简单的方法可以做到这一点,我发现最好的解决方案是拥有一个filter.properties文件,该文件从插件进行更新,并由主pom文件用来过滤所需的资源。 从Maven文档(Maven过滤器插件)中阅读以下内容后,我尝试了另一种解决方案: 变量可以包含在您的资源中。这些变量用$ {…}分

  • 为什么我会得到一个错误或未捕获的TypeError:不能设置null的属性'innerhtml'?我以为我了解innerHTML并且在此之前使用过它。

  • 我有一个简单的html页面,在body标记中没有代码。我想通过JavaScript在body标记中插入html。 我的javascript文件如下所示。 现在我想要这个很长的html代码在页面加载时自动插入到body标记中。但它给了我我在标题中提到的错误。为什么? 而且这也是创建基于ajax的网站的正确方法吗(不重新加载页面),这意味着如果我调用服务器端脚本来更新这个很长的html代码并将其附加到

  • 有一个错误,其中登录的用户将尝试更新他们的帐户用户名,但遇到一个错误。在我的一生中,我无法弄清楚为什么有时(可能有1/20的用户遇到这种情况)找不到当前的用户。只有登录后,用户才能访问此页面。错误有: 投掷;//未处理的“错误”事件 TypeError:无法设置null的属性“username” 错误似乎发生在这里:user.username=req.body.username;

  • 我正在开发一个Grails应用程序。我有一个带有一些属性的Groovy bean。我试图使用setter方法实例化和设置Java类中的一些属性。虽然我没有任何错误,但属性不包含任何值。我尝试在调试模式下查看对象内容:groovy对象包含1个唯一字段(r$fields),其中包含一个空的HashMap。 新潮豆 Java代码 添加作为Java代码中的最后一条语句,我得到以下NPE Stacktrac

  • 问题内容: 因此,我在回答这个问题的同时一直在使用Python,但发现这是无效的: 由于 。但是,对于从对象继承的任何类,它都是有效的: 打印将按预期显示“ hello”。为什么会这样呢?在Python语言规范中,有哪些规定不能将属性分配给香草对象? 问题答案: 为了支持任意属性分配,对象需要一个:与对象关联的字典,可以在其中存储任意属性。否则,就无处放置新属性。 的实例object并没有随身携带