[sonar-dev] Best documentation for understanding how to navigate the Trees in a Java plugin?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[sonar-dev] Best documentation for understanding how to navigate the Trees in a Java plugin?

Dorfman, Erik (NIH/OD) [C]

(Sonar 5.1) I’m developing Checks for a custom Java plugin for enforcing our Enterprise coding conventions, based on the sonar-examples-master.  So my checks are individual classes declared like this:

 

class MyJavaTreeVisitor extends BaseTreeVisitor implements JavaFileScanner { … }

   

Assuming this is a sensible approach, I’m at the point where I really need to understand all the various Tree nodes and how information is stored in them. 

 

For example, in one check, I want to verify that certain classes are placed in certain packages.  So really, all I need is the ability to find the class name and the package name for all classes declared in a file.  I’ve gotten stuck on just working with the visitCompilationUnit() to get the package name.  L

 

What documentation resources are there to help me with this?  If I should look at code, where is the best place to start to get a lot of examples?

 

Thanks!


Erik

 

 

Reply | Threaded
Open this post in threaded view
|

Re: [sonar-dev] Best documentation for understanding how to navigate the Trees in a Java plugin?

Nicolas Peru
Hi, 
Thanks for your feedback ! 

Even more important than your SQ version is the java plugin version you are using. It is recommend to use a version greater or equals to 3.1 of the java plugin as several improvements have been made from this version (and sonar-example project is based on this version).

As for examples, you can have a look at the checks implemented in sonar-java project : https://github.com/SonarSource/sonar-java/tree/master/java-checks/src/main/java/org/sonar/java/checks 

As for the specific case you mention, package is either an IdentifierTree or a MemberSelectExpressionTree or null, so you should test for those and construct the name. 
See for example what is done here (I admit that this is not the simplest stuff and should be improved). As for classes, if you are only interested in classes at the top level of your compilation unit (no nested classes or innerclasses) you can iterate through the types of the compilation unit verifying the node is indeed a Class (using Tree.Kind.CLASS) then casting it to the correct type and  get the name using its simpleName node.

On a side note, If you have any feedback on this, any pain points you face during the development of your custom rules or any suggestions, please make some feedback on this mailing list as we are really interested in those. 

Moreover, if you think your rules are worth sharing with the community do not hesitate to suggest them to be implemented in java plugin. 

Cheers,


Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com


2015-05-01 0:54 GMT+02:00 Dorfman, Erik (NIH/OD) [C] <[hidden email]>:

(Sonar 5.1) I’m developing Checks for a custom Java plugin for enforcing our Enterprise coding conventions, based on the sonar-examples-master.  So my checks are individual classes declared like this:

 

class MyJavaTreeVisitor extends BaseTreeVisitor implements JavaFileScanner { … }

   

Assuming this is a sensible approach, I’m at the point where I really need to understand all the various Tree nodes and how information is stored in them. 

 

For example, in one check, I want to verify that certain classes are placed in certain packages.  So really, all I need is the ability to find the class name and the package name for all classes declared in a file.  I’ve gotten stuck on just working with the visitCompilationUnit() to get the package name.  L

 

What documentation resources are there to help me with this?  If I should look at code, where is the best place to start to get a lot of examples?

 

Thanks!


Erik

 

 


Reply | Threaded
Open this post in threaded view
|

RE: [sonar-dev] Best documentation for understanding how to navigate the Trees in a Java plugin?

Dorfman, Erik (NIH/OD) [C]

Thanks, this is perfect.  J

 

From: Nicolas Peru [mailto:[hidden email]]
Sent: Friday, May 01, 2015 2:59 AM
To: /!\ Public dev ML
Subject: Re: [sonar-dev] Best documentation for understanding how to navigate the Trees in a Java plugin?

 

Hi, 

Thanks for your feedback ! 

 

Even more important than your SQ version is the java plugin version you are using. It is recommend to use a version greater or equals to 3.1 of the java plugin as several improvements have been made from this version (and sonar-example project is based on this version).

 

As for examples, you can have a look at the checks implemented in sonar-java project : https://github.com/SonarSource/sonar-java/tree/master/java-checks/src/main/java/org/sonar/java/checks 

 

As for the specific case you mention, package is either an IdentifierTree or a MemberSelectExpressionTree or null, so you should test for those and construct the name. 

See for example what is done here (I admit that this is not the simplest stuff and should be improved). As for classes, if you are only interested in classes at the top level of your compilation unit (no nested classes or innerclasses) you can iterate through the types of the compilation unit verifying the node is indeed a Class (using Tree.Kind.CLASS) then casting it to the correct type and  get the name using its simpleName node.

 

On a side note, If you have any feedback on this, any pain points you face during the development of your custom rules or any suggestions, please make some feedback on this mailing list as we are really interested in those. 

 

Moreover, if you think your rules are worth sharing with the community do not hesitate to suggest them to be implemented in java plugin. 

 

Cheers,

 


Nicolas PERU | SonarSource

Senior Developer
http://sonarsource.com

 

 

2015-05-01 0:54 GMT+02:00 Dorfman, Erik (NIH/OD) [C] <[hidden email]>:

(Sonar 5.1) I’m developing Checks for a custom Java plugin for enforcing our Enterprise coding conventions, based on the sonar-examples-master.  So my checks are individual classes declared like this:

 

class MyJavaTreeVisitor extends BaseTreeVisitor implements JavaFileScanner { … }

   

Assuming this is a sensible approach, I’m at the point where I really need to understand all the various Tree nodes and how information is stored in them. 

 

For example, in one check, I want to verify that certain classes are placed in certain packages.  So really, all I need is the ability to find the class name and the package name for all classes declared in a file.  I’ve gotten stuck on just working with the visitCompilationUnit() to get the package name.  L

 

What documentation resources are there to help me with this?  If I should look at code, where is the best place to start to get a lot of examples?

 

Thanks!


Erik