[英]Add my own rules in SonarQube with RPG
I want to create my own SonarQube Plugin for the RPG language. 我想为RPG语言创建自己的SonarQube插件。 I have the following problem.
我有以下问题。
I start by created the RpgLanguage class that extends to AbstractLanguage. 我首先创建了RpgLanguage类,该类扩展到AbstractLanguage。 In this class, I defined my new language "Rpg".
在这一节课中,我定义了新语言“ Rpg”。 You can see my class in the following code :
您可以在以下代码中看到我的课程:
public class RpgLanguage extends AbstractLanguage{
public static final String KEY = "rpg";
private Settings settings;
public RpgLanguage(Settings settings) {
super(KEY, "Rpg");
this.settings = settings;
}
public String[] getFileSuffixes() {
String[] suffixes = settings.getStringArray("");
if (suffixes == null || suffixes.length == 0) {
suffixes = StringUtils.split(".RPG", ",");
}
return suffixes;
}
}
After, I have created my RpgRulesDefinition class that implements RulesDefinition. 之后,我创建了实现RulesDefinition的RpgRulesDefinition类。 In this class, I create a new repository for the language RPG and I want to add a rule in this repository (empty rules).
在此类中,我为语言RPG创建了一个新的存储库,并希望在该存储库中添加一个规则(空规则)。 The code is like below :
代码如下:
public static final String REPOSITORY_KEY = "rpg_repository_mkoza";
public void define(Context context) {
NewRepository repo = context.createRepository(REPOSITORY_KEY, "rpg");
repo.setName("Mkoza Analyser rules RPG");
// We could use a XML or JSON file to load all rule metadata, but
// we prefer use annotations in order to have all information in a single place
RulesDefinitionAnnotationLoader annotationLoader = new RulesDefinitionAnnotationLoader();
annotationLoader.load(repo, RpgFileCheckRegistrar.checkClasses());
repo.done();
}
My class RpgFileCheckRegistrar that call my Rules : 我的类RpgFileCheckRegistrar调用了我的规则:
/**
* Register the classes that will be used to instantiate checks during analysis.
*/
public void register(RegistrarContext registrarContext) {
// Call to registerClassesForRepository to associate the classes with the correct repository key
registrarContext.registerClassesForRepository(RpgRulesDefinition.REPOSITORY_KEY, Arrays.asList(checkClasses()), Arrays.asList(testCheckClasses()));
}
/**
* Lists all the checks provided by the plugin
*/
public static Class<? extends JavaCheck>[] checkClasses() {
return new Class[] {
RulesExampleCheck.class
};
}
/**
* Lists all the test checks provided by the plugin
*/
public static Class<? extends JavaCheck>[] testCheckClasses() {
return new Class[] {};
}
My Rule class (still empty): 我的规则类(仍然为空):
@Rule(
key = "Rule1",
name = "Rule that make nothing",
priority = Priority.MAJOR,
tags = {"example"}
)
public class RulesExampleCheck extends BaseTreeVisitor{
/**
* Right in java code your rule
*/
}
And the class SonarPlugin that call all these extensions : 还有调用所有这些扩展的SonarPlugin类:
public final class RpgSonarPlugin extends SonarPlugin
{
// This is where you're going to declare all your Sonar extensions
public List getExtensions() {
return Arrays.asList(
RpgLanguage.class,
RpgRulesDefinition.class,
RpgFileCheckRegistrar.class
);
}
}
The problem when I want to start the server sonar, I obtain this error stack : 当我想启动服务器声纳时的问题,我得到了这个错误堆栈:
Exception sending context initialized event to listener instance of class org.sonar.server.platform.PlatformServletContextListener
java.lang.IllegalStateException: One of HTML description or Markdown description must be defined for rule [repository=rpg_repository_mkoza, key=Rule1]
I try different things but I don't understand why there are these error. 我尝试了不同的方法,但我不明白为什么会有这些错误。 Of course I want that my repository "rpg_repository_mkoza" is display in the RPG's repository in SonarQube with the Rules : RulesExampleCheck.
当然,我希望我的存储库“ rpg_repository_mkoza”通过“ Rules:RulesExampleCheck”显示在SonarQube的RPG存储库中。
My sonar-plugin-version is the 3.7.1 我的声纳插件版本是3.7.1
I find my problem. 我发现我的问题。 There are need to add the field 'description' in @Rule.
需要在@Rule中添加字段“说明” 。
For example : 例如 :
@Rule(
key = "Rule1",
name = "RuleExampleCheck",
description = "This rule do nothing",
priority = Priority.INFO,
tags = {"try"}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.