Using package in a custom rule

SONARQUBE version 7.7
Hi, i need to write a rule that check if exist a class that extends “myclass”.
If exist it is mandatory that this class overrides “mymethod”.
I’m used for write this rule, the sonar standard class for the rule S2160 that use package.
below my code:

import org.sonar.check.Rule;
import java.util.Collections;
import java.util.List;
import org.sonar.check.Priority;
		  key = "MySecondCustomCheck",
		  name = "MyClass class needs override MyMethod",
		  description = "MyClass class needs override MyMethod",
		  priority = Priority.CRITICAL,
		  tags = {"bug"})

public class MySecondCustomCheck extends IssuableSubscriptionVisitor {
	  private static final String MY_CLASS = "MyClass";
	  private static final MethodMatcher MyMethod = MethodMatcher.create().typeDefinition(TypeCriteria.subtypeOf(MY_CLASS)).name("MyMethod");
  public List<Tree.Kind> nodesToVisit() {
	    return Collections.singletonList(Tree.Kind.CLASS);
	  public void visitNode(Tree tree) {
	    ClassTree classTree = (ClassTree) tree;
	    Symbol.TypeSymbol classSymbol = classTree.symbol();
	    if (classSymbol != null
	      && isDirectSubtypeOfThread(classSymbol)
	      && !overridesMyMethodMethod(classSymbol)){
	      Tree report = classTree.simpleName();
	      Tree parent = classTree.parent();
	      if( {
	        NewClassTree newClassTree = (NewClassTree) parent;
	        report = newClassTree.identifier();
	      reportIssue(report, "Don't extend \"MY_CLASS\", since the \"MyMethod\" method is not overridden.");

	  private static boolean isDirectSubtypeOfThread(Symbol.TypeSymbol classSymbol) {
		    Type superClass = classSymbol.superClass();
		    return superClass != null &&;

		  private static boolean overridesMyMethodMethod(Symbol.TypeSymbol classSymbol) {
		    return classSymbol.lookupSymbols("MyMethod").stream().anyMatch(MyMethod::matches);

I have this error when i try to restart SonarQube:

2019.06.04 12:06:25 ERROR web[][o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
java.lang.NoClassDefFoundError: Lorg/sonar/java/matcher/MethodMatcher;
	at java.lang.Class.getDeclaredFields0(Native Method)
	at java.lang.Class.privateGetDeclaredFields(Unknown Source)
	at java.lang.Class.getDeclaredFields(Unknown Source)
	at org.sonar.api.utils.FieldUtils2.getFields(
	at org.sonar.api.server.rule.RulesDefinitionAnnotationLoader.loadRule(
	at org.sonar.api.server.rule.RulesDefinitionAnnotationLoader.loadRule(
	at org.sonar.api.server.rule.RulesDefinitionAnnotationLoader.load(
	at org.sonar.server.rule.RuleDefinitionsLoader.load(RuleDefinitionsLoad

Any Help?

The class is not from api package. It is in java-frontend module which is marked as provided during compile time. But only classes under api packages are provided during analyze time or during rules loading at the server side. So you have to include and shadow the java-frontend package or copy the class to your plugin.


Thank you! it work!

Hey @bruna and @Josef_Prochazka (thanks for your answer!)

As a complementary note, I would like to add that we are currently planning to add the MethodMatchers to the SonarJava API. I would consequently strongly encourage you to vote for the following ticket in our backlog: SONARJAVA-2357


As I undestand,

I should add the dependency to java-frontend to my pom.xml:


To shade the dependency I’m trying to use the plugin maven-shade-plugin, so, I added to the plugins section of my pom.xml:


SonarQube server starts succesfully. But, when I run an analysis that uses the new rule I obtain the following error (in the computer that is performing mvn sonar:sonar):

[ERROR] Failed to execute goal 
org.sonarsource.scanner.maven:sonar-maven-plugin: (default-cli) 
on project xxx: SonarQube is unable to analyze file : 'xxx/src/main/java/com/': 
class$MethodJavaSymbol cannot be cast to
($MethodJavaSymbol is in unnamed module of 
loader org.sonar.classloader.ClassRealm @6f66ab8f;

What I’m doing wrong?
Any help?

I am not sure at the moment but I think it should be like this.

1 Like

Thank you!
It works perfectly!

Can you please let me know what exactly have you added.In place of below