Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a custom exception to the "Methods should not have too many parameters" SonarQube rule

In sonar rules, there is a S00107 rule for "Methods should not have too many parameters". This rule has exceptions for some annotations:

Exceptions

Methods annotated with Spring's @RequestMapping (and related shortcut annotations, like @GetRequest) or @JsonCreator may have a lot of parameters, encapsulation being possible. Such methods are therefore ignored.

Is it possible to add another annotations to this exceptions?

My case: I have constructors, annotated by the Lombok's @Builder, which contains a lot of parameters! So i want to ignore this rule on these constructors.

public class MyClass extends MySupperClass {
  @Builder
  public MyClass(String a, int b, ..., String z) {
  }
}
like image 935
Arya Avatar asked Oct 30 '19 08:10

Arya


People also ask

How do you avoid too many parameters in a constructor?

Using a builder can solve the issue of having to many parameters in a constructor when all the fields are not mandatory. It also takes away the problems with having multiple constructors for different purposes. Note that a builder still should have a constructor with the mandatory fields that always needs to be set.

How many parameters are allowed in a method in Java?

The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3. 3), where the limit includes one unit for this in the case of instance or interface method invocations. Section §4.3.

How do I add coding rules to SonarQube?

Create a SonarQube plugin. Put a dependency on the API of the language plugin for which you are writing coding rules. Create as many custom rules as required. Generate the SonarQube plugin (jar file). Place this jar file in the SONARQUBE_HOME/extensions/plugins directory. Restart SonarQube server.

Can a method have too many parameters?

In sonar rules, there is a S00107 rule for " Methods should not have too many parameters ". This rule has exceptions for some annotations: Methods annotated with Spring's @RequestMapping (and related shortcut annotations, like @GetRequest) or @JsonCreator may have a lot of parameters, encapsulation being possible.

Should I use SonarQube or the Java API?

The Java API will be more fully-featured than what's available for XPath, and is generally preferable. However, this comes with the overhead of maintaining a SonarQube plugin (including keeping it up-to-date as APIs change, upgrading the plugin after releasing a new version).

What are the exceptions for @requestmapping annotations?

This rule has exceptions for some annotations: Methods annotated with Spring's @RequestMapping (and related shortcut annotations, like @GetRequest) or @JsonCreator may have a lot of parameters, encapsulation being possible. Such methods are therefore ignored. Is it possible to add another annotations to this exceptions?


1 Answers

Try to add @SuppressWarnings("squid:S00107")

like image 122
Nick Avatar answered Oct 03 '22 05:10

Nick