# Custom Java rule

**URL:** <https://community.sonarsource.com/t/custom-java-rule/94477>\
**Category:** SonarQube Server / Community Build\
**Tags:** custom\_rules, rules, java\
**Created:** [June 22, 2023, 9:37am UTC](https://community.sonarsource.com/t/custom-java-rule/94477 "2023-06-22T09:37:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabzouzer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gabzouzer/32/45104_2.png) [@Gabzouzer](https://community.sonarsource.com/u/Gabzouzer)\
**Post date:** [June 22, 2023, 9:37am UTC](https://community.sonarsource.com/t/custom-java-rule/94477/1 "2023-06-22T09:37:59Z")

</div>

Dear all

I am trying to create a custom java rule. In my project, I need every java file to start with a header. I choose to create a rule that signal the error if the file does not start with a comment. (the next step would be to check inside the comment to see if there are the required informations)  
Helped by the internet, I wrote this code.

```auto
import java.util.List;
import java.util.Collections;
import org.sonar.check.Rule;
import org.sonar.check.Priority;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.Tree;

@Rule(key = "IncludeHeader", description = "The file need a header",
    priority = Priority.MINOR)

public class IncludeHeaderRule extends IssuableSubscriptionVisitor{

  @Override
  public List<Tree.Kind> nodesToVisit(){
	  return Collections.singletonList(Tree.Kind.ANNOTATION_TYPE);  
  }
  
  @Override
  public void visitNode(Tree tree) {
      List<String> lines = context.getFileLines();
      if (!(lines.get(0).contains("/*"))||(lines.get(0).contains("//"))) {
    	  reportIssue(tree, "The file should start with a header.");
      }
      
  }
```

The code looks pretty easy. I create a list of strings where I include the lines. I check if there is a comment that starts with “//” or “/\*” for the first item of the list that represent line 1(lines.get(0) and if there is no comment I report the issue.  
However, when I try to check if the rule works I see that nothing happen. In one case it is normal but in the other one, somthing should happen.

![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/5/65430d364940978e28e459f26deab80d8b185800.png)  
 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/c/6/c68907234b5c44468959994fce2eedfbc56cfe20.png)

I am using - sonarqube-10.0.0.68432  
- java 17  
- Apache Maven 3.9.1  
Of course, I did insert the jar file in sonar/extensions/plugin then restarted SonarQube. I also created a quality profile and activated that rule.

If someone knows what is wrong with my code, knows what did I missed or has tips to give me, I will be more than thankfull  
Thank you very much  
Gab

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://community.sonarsource.com/u/oliver)\
**Post date:** [June 22, 2023, 10:46am UTC](https://community.sonarsource.com/t/custom-java-rule/94477/2 "2023-06-22T10:46:52Z")

</div>

Shouldn’t you be visiting something like `Kind.COMPILATION_UNIT` or at least `Kind.CLASS`? `Kind.ANNOTATION_TYPE` is likely an annotation definition:

```auto
public @Interface MyAnnotation {

}

```

Since your second test class is not an annotation it would not get visited.

Additionally, you can write unit tests for your rules, no need to deploy to a SonarQube instance.

Regards,  
Oliver

---

<div class="post-metadata">

**Author:** ![Gabzouzer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gabzouzer/32/45104_2.png) [@Gabzouzer](https://community.sonarsource.com/u/Gabzouzer)\
**Post date:** [June 22, 2023, 11:59am UTC](https://community.sonarsource.com/t/custom-java-rule/94477/3 "2023-06-22T11:59:04Z")

</div>

> [@oliver](#):
>
> Kind.COMPILATION\_UNIT

Indeed it works if I write `Kind.COMPILATION_UNIT` instead of `Kind.ANNOTATION\_TYPE’ but I am not sure to understand why. Can you explain in few words what is the difference between these two lines ?

Thanks you very much for your answer  
Best regards  
Gab

---

<div class="post-metadata">

**Author:** ![oliver](https://avatars.discourse-cdn.com/v4/letter/o/85f322/32.png) [@oliver](https://community.sonarsource.com/u/oliver)\
**Post date:** [June 22, 2023, 12:24pm UTC](https://community.sonarsource.com/t/custom-java-rule/94477/4 "2023-06-22T12:24:21Z")

</div>

@Gabzouzer Please have a look at the basic info on writing custom rules. You can find it here: [sonar-java/docs/CUSTOM\_RULES\_101.md at master · SonarSource/sonar-java · GitHub](https://github.com/SonarSource/sonar-java/blob/master/docs/CUSTOM_RULES_101.md)

Adressing your question: with `nodesToVisit()` you state which parts of a files syntax tree you want to analyse. You could initially request the full tree, but usually a rule is interested only in specific patterns. For example, to check that a method matches a naming convention a rule does not inspect, say, package names.  
With your `Collections.singletonList(Tree.Kind.ANNOTATION_TYPE)` you specify, that your rule is interested in definitions of new annotations, like

```auto
public @interface MyAnnotation {
  // here come the params
}

```

If your file does not contain a annotation definition, as in the case of both of your files, your `visitNode()` isn’t called.  
Using `Kind.COMPILATION_UNIT` you state that you are interested in basically in the full syntax tree of the file. This has the advantage that you report issues only once per file. If you would use anything else that is usually contained in a file, like `Kind.CLASS`, you might report missing comments multiple times if there are multiple classes defined in the file.

Regards,  
Oliver

---

<div class="post-metadata">

**Author:** ![Gabzouzer](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gabzouzer/32/45104_2.png) [@Gabzouzer](https://community.sonarsource.com/u/Gabzouzer)\
**Post date:** [June 22, 2023, 12:27pm UTC](https://community.sonarsource.com/t/custom-java-rule/94477/5 "2023-06-22T12:27:29Z")

</div>

Thank you very much. It helps me a lot.  
Have a nice day  
Best regards  
Gab

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [June 29, 2023, 12:27pm UTC](https://community.sonarsource.com/t/custom-java-rule/94477/6 "2023-06-29T12:27:55Z")

</div>

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