1

Please help me to solve these problems. Simply I want to apply an existing sonarqube rule for only certain type of classes. As far as I know, we can use class tree to get the classes and identify the target class. The thing I want t know is how can I enable or disable existing sonar rule within a custom rule?.

The next question is to identify the base class which is child class is extended. As an example class A extend class B, B extend class C and now I want to identify all the classes that is extended from the class C(The base class).

Even a small guide, link, tip will be really appreciate.

Chanaka Fernando
  • 2,176
  • 19
  • 19

1 Answers1

2
  1. I think you canot disable existing rule from default java plugin within your custom plugin. You could disable the rule in your profile and replace the default rule with custom one. (Actually there may be way with creating custom issue filter in your plugin )

  2. The rule is implemented like this.

    @Rule(key = "S1948") public class SerializableFieldInSerializableClassCheck extends IssuableSubscriptionVisitor

     public List<Tree.Kind> nodesToVisit() {
        return ImmutableList.of(Tree.Kind.CLASS);
      }
    
      public void visitNode(Tree tree) {
        if(!hasSemantic()) {
          return;
        }
       ... rule logic
      }
    

just copy (and change the rule key and etc.) the existing rule into your custom plugin and add a check for class supertype

if(!classTree.symbol().type().isSubtypeOf("your.supertype.C")) || !hasSemantic()) {
  return;
}
Josef Prochazka
  • 1,273
  • 2
  • 9
  • 28