Help with Custom Kotlin Plugin

Hi All,

I’m developing a custom kotlin plugin using sonar-kotlin-plugin.

I am able to see the plugin registered successfully with the rules within it displayed on SonarQube. But I failed to get the rule picked up when scanning from scanner side.

Would appreciate if anyone could kindly help? Thank you very much in advance!

I have added this in my pom file:

        <!-- https://mvnrepository.com/artifact/org.sonarsource.slang/sonar-kotlin-plugin -->
        <dependency>
            <groupId>org.sonarsource.slang</groupId>
            <artifactId>sonar-kotlin-plugin</artifactId>
            <version>1.7.0.883</version>
        </dependency>

My plugin class:

public class MyKotlinPlugin extends KotlinPlugin {
    public void define(Context context) {
        context.addExtensions(
                MyKotlinRulesDefinition.class
        );

    }
}

My RuleDefinition class:

package custom.rule;
import java.util.ArrayList;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;
import org.sonarsource.kotlin.plugin.KotlinCheckList;
import org.sonarsource.kotlin.plugin.KotlinPlugin;
import org.sonarsource.kotlin.plugin.KotlinProfileDefinition;
import org.sonarsource.slang.checks.CommentedCodeCheck;
import org.sonarsource.slang.checks.utils.Language;
import org.sonarsource.slang.plugin.RulesDefinitionUtils;

public class MyKotlinRulesDefinition implements RulesDefinition {

  private static final String RESOURCE_FOLDER = "org/sonar/l10n/kotlin/rules/kotlin";
  private final SonarRuntime sonarRuntime;

  public MyKotlinRulesDefinition (SonarRuntime sonarRuntime) {
    this.sonarRuntime = sonarRuntime;
  }

  @Override
  public void define(Context context) {
    NewRepository repository = context
      .createRepository("my-key", "kotlin")
      .setName("my-repo-name");
    RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_FOLDER);

    ArrayList<Class> checks = new ArrayList<>(MyKotlinCheckList.checks());
    ruleMetadataLoader.addRulesByAnnotatedClass(repository, checks);

    RulesDefinitionUtils.setDefaultValuesForParameters(repository, checks, Language.KOTLIN);

    repository.done();
  }

}

My custom rule:

@Rule(key = "MyKotlin")
public class MyKotlinRule implements SlangCheck {
    @Override
    public void initialize(InitContext initContext) {
        initContext.register(StringLiteralTree.class, (ctx, tree) -> {
            String content = tree.content();
            System.out.println("tree content:" + content);
            ctx.reportIssue(tree, content);
        });
    }
}

MyKotlinCheckList.class


public class MyKotlinCheckList{
        private static final Class[] CHECKS= new Class[]{MyKotlinRule.class};

        private MyKotlinCheckList() {
        }

        public static List<Class> checks() {
            return Arrays.asList(CHECKS);
        }
    }

up, anyone would like to help please? thanks a lot!

SonarSource Kotlin analyzer does not support custom rules.
As it’s open source, you can check out the code and modify it.
An alternative is to create a separate plugin with a different plugin key which can run on the same SonarQube server as sonar-kotlin-plugin.

Hi @pynicolas ,

Thank you very much for the information, yes we can try that, a different plugin key; A quick question is, can the new plugin co-exist with the builtin sonar-kotlin-plugin? The reason to ask is, we do need to keep the builtin one.

Yes, it is possible to have 2 plugins on the same SonarQube server which raise issues on the same files.
If you want to go that way:

  • your plugin must not use the same plugin key (‘kotlin’)
  • your plugin must not define a language with the same language key (‘kotlin’), and your plugin probably doesn’t even need to define a subclass of org.sonar.api.resources.AbstractLanguage.
  • your plugin must not try to save metrics, CPD tokens, syntax highlighting on Kotlin files.

If one of the above conditions is not met, you will get a failure when analyzing Kotlin files.

2 Likes

@pynicolas, Great, this is very helpful, thanks very much for the details!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.