【问题标题】:How to replace this code with a non obsolete code in a SonarQube (5.1.x) language plugin?如何用 SonarQube (5.1.x) 语言插件中的非过时代码替换此代码?
【发布时间】:2016-02-14 08:59:19
【问题描述】:

[注意事项:名称混乱;这是封闭源代码,但我尽可能多地显示代码以突出显示逻辑。很抱歉。]

我在 SonarQube 开发邮件列表上提出了这个问题,但到目前为止无济于事......

这是代码(名称有些混乱):

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

此代码注册为 SonarQube 插件的一个组件(使用 SonarPlugin#getExtensions());它的作用是为PluginConstants.LANGUAGE_KEY 标识的语言创建一个质量配置文件,名称为PluginConstants.DEFAULT_QUALITY_PROFILE,用于PluginConstants.REPOSITORY_KEY 中定义的所有规则。

这就是麻烦的开始。

首先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);
    }
}

到目前为止,与任何PluginConstants.* 都没有关系,对吧?好吧,它来了……

我也有这个:

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();
    }
}

我们在这里找到了REPOSITORY_KEYLANGUAGE_KEY

如果仅此而已,那将没有乐趣,对吗?好吧,让我们继续吧!

现在我们来到语言传感器:

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
}

在这里,我们再次找到REPOSITORY_KEY。请注意对MyChecks.noArgChecks().argChecks() 方法的调用。


好吧,那是很多代码,但我的问题很简单,虽然答案可能不是:RuleFinder 已弃用;如何转换此代码以便我不使用它?我需要更改我的ProfileDefinition 实现吗?如果是,用什么?如果不是,我需要更改什么以及如何更改?

【问题讨论】:

标签: java sonarqube sonarqube5.1


【解决方案1】:

为了任何通过 google 到达这里的人的利益 - 根据这篇发给开发者邮件列表的帖子(回复 @fge)RuleFinder 目前不推荐用于服务器端组件,只是批处理端组件。

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

所以使用RuleFinder 的代码并没有过时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2011-05-17
    • 2021-11-14
    • 2022-11-03
    • 2021-09-09
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多