Wojciech Piotrowiak
Wojciech Piotrowiak

Reputation: 129

How find specific code occurrences in java code with sonar rule

I wish to give developers suggestion that for example: System.currentTimeMillis() is not recommended and provide some comments with alternative solutions.

AFAIK there is no such sonar rule, should I create a new one or there is an option to parametrize existing one?

I found that xpath for java is no longer supported, so am I right that writing custom rule is the only way?

Upvotes: 0

Views: 317

Answers (2)

G. Ann - SonarSource Team
G. Ann - SonarSource Team

Reputation: 22804

You're looking for template rule S2253, which allows you to flag calls to a specific method.

Upvotes: 3

Ori Marko
Ori Marko

Reputation: 58772

Here's example of setting custom method warnings using sonar:

@Rule(key = "NoObjectWaitRule",
        name = "NoObjectWaitRule",
        description = "You should not use classic wait/notify mechanism. Instead use Java Concurrency API.",
        priority = Priority.MAJOR,
        tags =
        {
            "bad-practice"
        })
public class NoObjectWaitRule extends DisallowedMethodCheck
{

   public NoObjectWaitRule()
    {
        super.setClassName("java.lang.Object");
        super.setMethodName("wait");
        super.setAllOverloads(true);
    }

}

Upvotes: 0

Related Questions