简体   繁体   English

如何在SonarQube(5.1.x)语言插件中用非过时的代码替换此代码?

[英]How to replace this code with a non obsolete code in a SonarQube (5.1.x) language plugin?

[word of caution: names mangled; [警告语:名字被篡改; this is closed source but I show as much code as I possibly can to highlight the logic. 这是封闭的源代码,但是我显示了尽可能多的代码以突出显示逻辑。 Sorry for that.] 抱歉。]

I asked this question on the SonarQube development mailing list but to no avail so far... 我在SonarQube开发邮件列表上问了这个问题,但到目前为止都无济于事...

Here is the code (names somewhat mangled): 这是代码(名称有些混乱):

public final class MyProfile
    extends ProfileDefinition
{
    // RuleFinder is DEPRECATED
    private final RuleFinder finder;

    public MyProfile(final RuleFinder finder)
    {
        this.finder = finder;
    }

    @Override
    public RulesProfile createProfile(final ValidationMessages validation)
    {
        @SuppressWarnings("rawtypes")
        final List<Class> checks = MyChecks.all();

        return new AnnotationBasedProfileBuilder(finder).build(
            PluginConstants.REPOSITORY_KEY,
            PluginConstants.DEFAULT_QUALITY_PROFILE,
            PluginConstants.LANGUAGE_KEY,
            checks,
            validation
        );
    }
}

This code is registered as a component of the SonarQube plugin (using SonarPlugin#getExtensions()); 此代码已注册为SonarQube插件的组件(使用SonarPlugin#getExtensions()); its effect is to create a quality profile for the language identified by PluginConstants.LANGUAGE_KEY with name PluginConstants.DEFAULT_QUALITY_PROFILE for all rules defined in PluginConstants.REPOSITORY_KEY . 它的作用是创造查明的语言质量轮廓PluginConstants.LANGUAGE_KEY与名PluginConstants.DEFAULT_QUALITY_PROFILE为中定义的所有规则PluginConstants.REPOSITORY_KEY

And this is where the trouble starts. 这就是麻烦的开始。

First of all, MyChecks.all() : MyChecks is this: 首先, MyChecks.all()MyChecks是这样的:

public final class MyChecks
{
    private static final List<Class<? extends MyCheck>> NOARG_CHECKS;
    private static final List<Class<? extends MyCheck>> ARG_CHECKS;

    static {
        List<Class<? extends MyCheck>> list;

        list = new ArrayList<>();

        // add implementations of MyCheck here

        NOARG_CHECKS = Collections.unmodifiableList(list);

        list = new ArrayList<>();

        // add other implementations of MyCheck here, but with IOC dependecies

        ARG_CHECKS = Collections.unmodifiableList(list);
    }

    private MyChecks()
    {
        throw new Error("nice try!");
    }

    @SuppressWarnings("rawtypes")
    public static List<Class> all()
    {
        final List<Class> list = new ArrayList<>();
        list.addAll(NOARG_CHECKS);
        list.addAll(ARG_CHECKS);
        return Collections.unmodifiableList(list);
    }

    @SuppressWarnings({ "ReturnOfCollectionOrArrayField", "rawtypes" })
    public static Collection noargChecks()
    {
        return NOARG_CHECKS;
    }

    @SuppressWarnings("rawtypes")
    public static Collection argChecks(final Settings settings)
    {
        final List<MyCheck> list = new ArrayList<>();

        // add here checks whose constructors depend on "settings"

        return Collections.unmodifiableList(list);
    }
}

No relationship to any PluginConstants.* so far, right? 到目前为止,与任何PluginConstants.*没有关系PluginConstants.*对不对? Well, here it comes... 好吧,来了...

I also have this: 我也有这个:

public final class MyRulesDefinition
    implements RulesDefinition
{
    @Override
    public void define(final Context context)
    {

        @SuppressWarnings("rawtypes")
        final List<Class> list = MyChecks.all();

        // SonarConstans.DEFAULT_RULE_REPOSITORY is "SonarQube";
        // it is not used in _any other place_ than here
        final NewRepository repo = context.createRepository(
            PluginConstants.REPOSITORY_KEY,
            PluginConstants.LANGUAGE_KEY
        ).setName(SonarConstants.DEFAULT_RULE_REPOSITORY);

        AnnotationBasedRulesDefinition.load(repo, PluginConstants.LANGUAGE_KEY,
            list);

        repo.done();
    }
}

We find back both REPOSITORY_KEY and LANGUAGE_KEY here. 我们在这里找到REPOSITORY_KEYLANGUAGE_KEY

If this were all there was to it, that would be no fun, right? 如果只有这些,那就太无趣了,对吧? Well let's continue! 好吧,让我们继续!

Now we arrive to the language sensor: 现在我们到达语言传感器:

public final class MySquidSensor
    implements Sensor
{
    // HERE
    private final Checks<MyCheck> checks;

    /*
     * Yes, unfortunately, Sonar has "taken over" a name used by the JDK here.
     */
    private final org.sonar.api.batch.fs.FileSystem fs;

    private final FilePredicate predicate;
    private final ResourcePerspectives perspectives;
    private final Settings settings;

    public MySquidSensor(final org.sonar.api.batch.fs.FileSystem fs,
        final CheckFactory checkFactory,
        final ResourcePerspectives perspectives,
        final Settings settings)
    {
        this.settings = settings;
        this.fs = fs;
        this.perspectives = perspectives;

        predicate = fs.predicates().hasLanguage(PluginConstants.LANGUAGE_KEY);

        // HERE
        checks = checkFactory.
            <MyCheck>create(PluginConstants.REPOSITORY_KEY)
            .addAnnotatedChecks(MyChecks.noargChecks())
            .addAnnotatedChecks(MyChecks.argChecks(settings));
    }

    // some other, irrelevant code
}

And here, we find REPOSITORY_KEY again. 在这里,我们再次找到REPOSITORY_KEY And do note the call to both the .noArgChecks() and .argChecks() methods of MyChecks . 并注意对.noArgChecks().argChecks()方法的MyChecks


OK, well, that was a lot of code, but my question is really simple, although the answer may not be: RuleFinder is deprecated; 好的,那是很多代码,但是我的问题很简单,尽管答案可能不是: RuleFinder已过时; how do I transform this code so that I do not use it? 如何转换此代码,以便不使用它? Do I need to change my ProfileDefinition implementation? 我是否需要更改ProfileDefinition实现? If yes, what with? 如果是,该怎么办? If no, what do I need to change and how? 如果没有,我需要更改什么以及如何更改?

For the benefit of anyone getting here via google - according to this post to the developer mailing list (in response to @fge) RuleFinder is not currently deprecated for server side components, just batch side ones. 为了使任何通过Google到达这里的人受益-根据开发人员邮件列表中的帖子(回复@fge), RuleFinder当前不建议用于服务器端组件,而仅用于批处理端组件。

http://sonarqube-archive.15.x6.nabble.com/sonar-dev-RulesFinder-is-deprecated-but-what-replaces-it-td5035167.html http://sonarqube-archive.15.x6.nabble.com/sonar-dev-RulesFinder-is-deprecated-but-what-replaces-it-td5035167.html

So code using RuleFinder is not obsolete. 因此,使用RuleFinder代码并不是过时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM