Skip to content

Apex Checks

kevin titichoca veizaga edited this page Jun 15, 2016 · 12 revisions

Introduction

The apex-checks module represent the rules which are executed on source code and which generate issues in the sonarqube platform as seen in this picture. Apex rules

Rules

The rules are visitors to navigate through an AST looking for errors.

Rules Details

Each rule has a number of details that help the user to resolve the error, for example: Rules details

How to create a custom rule

Create unit test

To test checks SourceFile instance is needed. The ApexAstScanner.scanFile method used to create a SourceFile from a File and a Check.

@Test
public void testCustomCheck() {
    File file = new File("src/test/resources/checks/Book.cls");
    CustomCheck customCheck = new CustomCheck();
    SourceFile sourceFile = ApexAstScanner.scanFile(file, customCheck);
    ...
}

Now it verified that there is a violation in a particular line by CheckMessagesVerifier.verify method.

@Test
public void testCustomCheck() {
    ...
    CheckMessagesVerifier.verify(sourceFile.getCheckMessages())
                .next().atLine(2).withMessage(ERROR_MESSAGE)
                .next().atLine(5).withMessage(ERROR_MESSAGE)
                .noMore();
}

ERROR_MESSAGE must be equal to that of violation message issued by custom check

noMore() method is used when there is no more checks at source.

Extends of SquidChecks<Grammar>

To create a custom rule is necessary extends a class of SquidCheck<Grammar> and override the following methods:

init This method defined in which AST node the analysis will be performed by suscribeTo method.

@Override
public void init() {
    subscribeTo(RuleKey.METHOD_DECLARATION);
}

visitNode This method is executed when found the node declared at the beginning. It also allows to declare an issue when it's not completed the condition of the rule by createLineViolation method.

@Override
public void visitNode(AstNode astNode) {
    if (isDeprecated(astNode)) {
        getContext().createLineViolation(this, ERROR_MESSAGE, astNode);
    }
}

Defines rules for checks

To define rules for checks it is necessary to create a json file with rule key as name with .json extension in the same directory were .hmtl rule descriptions are beeing created.

{
  "title": "\"assert\" should only be used with boolean variables",
  "status": "ready",
  "remediation": {
    "func": "Constant\/Issue",
    "constantCost": "5min"
  },
  "sqaleSubCharac": "INSTRUCTION_RELIABILITY",
  "tags": [
    "suspicious",
    "cert"
  ],
  "defaultSeverity": "Major"
}
@Rule(key = MethodNameCheck.CHECK_KEY)
public class MethodNameCheck extends SquidCheck<Grammar> {

@Rule Specifies the rule data such as: key, priority and tags.

@SqaleSubCharacteristic Specifies the type of characteristic that Squale method used.

@SqaleConstantRemediation Specifies the solution's time of the error.

@ActivatedByDefault Specifies if the rules is activated when running sonarqube.

Defines html description page

The html description should be named the same as the key of the rule.

Html descripcion page

This file contains a more detailed description of the rule, because of the problem and how to fix it. Html example

Load Checks

Now, you must add ClassType of your check in ChecksList class.

public static List<Class> getChecks() {
    return ImmutableList.<Class>of(
            AssertMethodCheck.class,
            ClassNameCheck.class,
            ...

            YourCustomCheck.class

            ...
            TestMethodCheck.class);
Clone this wiki locally