# Java custom rule writing without exploring the Syntax Tree

**URL:** <https://community.sonarsource.com/t/java-custom-rule-writing-without-exploring-the-syntax-tree/550>\
**Category:** Writing rules\
**Tags:** custom\_rules, javascript, java\
**Created:** [June 27, 2018, 5:31am UTC](https://community.sonarsource.com/t/java-custom-rule-writing-without-exploring-the-syntax-tree/550 "2018-06-27T05:31:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Michael](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/michael/32/43_2.png) [@Michael](https://community.sonarsource.com/u/Michael)\
**Post date:** [June 27, 2018, 8:55am UTC](https://community.sonarsource.com/t/java-custom-rule-writing-without-exploring-the-syntax-tree/550/2 "2018-06-27T08:55:49Z")

</div>

Hello,

When implementing a Java custom rule, nothing forces you to use a `BaseTreeVisitor` or `IssuableSubscriptionVisitor`, you can perfectly only implement the `JavaFileScanner` interface, which will give you access to the content of the file:

```java
package org.sonar.samples.java.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;

@Rule(key = "MyCheck")
public class MyCheck implements JavaFileScanner {

  @Override
  public void scanFile(JavaFileScannerContext context) {
    context.getFileContent(); // to retrieve the full content of the file as a String
    context.getFileLines(); // to retrieve the content of each lines of the file, as a String
  }
}

```

Now, depending of what you need to do (especially if you try to be precise in the rule issue locations), not using the Tree is most probably only going to be a pain on the long run.  
Identifying a variable in a whole file without information from syntax tree or semantic, but only relying on a regex, may also become quite tricky for big files.

Cheers,  
Michael

_Note_: Regarding format of your message, please note that giving context (versions of analyzers, SQ?) and clearly expressing questions is usually quite appreciated, like usual introductions and conclusions are (greetings? thanks?)

---

_[View the full topic](https://community.sonarsource.com/t/java-custom-rule-writing-without-exploring-the-syntax-tree/550)._
