Java Code Examples for com.sonar.sslr.api.AstNode#getLastChild()

The following examples show how to use com.sonar.sslr.api.AstNode#getLastChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssertMessageCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
        if (astNode.hasDescendant(ApexGrammarRuleKey.PRIMARY_SUFFIX)) {
            List<AstNode> suffixes = astNode.getDescendants(ApexGrammarRuleKey.PRIMARY_SUFFIX);
            for (AstNode suffix : suffixes) {
                AstNode primaryExpression = suffix.getParent();
                AstNode name = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.NAME);
                if (name != null) {
                    AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    if (first != null && last != null
                            && first.getTokenValue().equals(SYSTEM)
                            && last.getTokenValue().matches(ASSERT_REGEX)) {
                        List<AstNode> arguments = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.ARGUMENTS_LIST).getChildren();
                        if (arguments.size() <= VALUE_OF_ONE || !hasMessage(arguments)) {
                            getContext().createLineViolation(this,
                                    MESSAGE, primaryExpression, astNode.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 2
Source File: AssertLiteralBooleanCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
        if (astNode.hasDescendant(ApexGrammarRuleKey.PRIMARY_SUFFIX)) {
            List<AstNode> suffixes = astNode.getDescendants(ApexGrammarRuleKey.PRIMARY_SUFFIX);
            for (AstNode suffix : suffixes) {
                AstNode primaryExpression = suffix.getParent();
                AstNode name = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.NAME);
                if (name != null) {
                    AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
                    if (first != null && last != null
                            && first.getTokenValue().equals(SYSTEM)
                            && last.getTokenValue().equals(ASSERT)) {
                        List<AstNode> arguments = primaryExpression.getFirstDescendant(ApexGrammarRuleKey.ARGUMENTS_LIST).getChildren();
                        for (AstNode argument : arguments) {
                            if (argument.getTokenValue().equals(TRUE_LITERAL)
                                    || argument.getTokenValue().equals(FALSE_LITERAL)) {
                                getContext().createLineViolation(this,
                                        MESSAGE, suffix, astNode.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue());
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 3
Source File: StartAndStopCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies if the expression is a sequence of test.startTest or
 * test.stopTest
 *
 * @param expression
 * @param keyword
 * @return true if the expression is the right sequence
 */
private boolean isTestMethodCall(AstNode expression, String keyword) {
    AstNode name = expression.getFirstDescendant(ApexGrammarRuleKey.NAME);
    if (name != null) {
        AstNode first = name.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
        AstNode last = name.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER);
        return first != null && last != null
                && first.getTokenValue().equals(TEST)
                && last.getTokenValue().matches(keyword);
    }
    return false;
}
 
Example 4
Source File: SoqlInLoopCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
private AstNode getStatementAstNode(AstNode astNode){
		AstNode statementAstNode = astNode.getFirstChild(ApexGrammarRuleKey.STATEMENT);
 	if(statementAstNode == null){
 		statementAstNode = astNode.getLastChild(); // For this Rule, Last Child will be FOR_LOOP or FOR_EACH_LOOP
 		statementAstNode = getStatementAstNode(statementAstNode);
 	}
 	return statementAstNode;
}
 
Example 5
Source File: AsyncMethodsCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * It is responsible for verifying whether the rule is met in the rule base.
 * In the event that the rule is not correct, create message error.
 *
 * @param astNode It is the node that stores all the rules.
 */
@Override
public void visitNode(AstNode astNode) {
    try {
        if (astNode.hasDescendant(ApexGrammarRuleKey.STATEMENT_EXPRESSION)) {
            List<AstNode> expressions = astNode.getDescendants(ApexGrammarRuleKey.STATEMENT_EXPRESSION);
            for (AstNode expression : expressions) {
                if (expression.hasDescendant(ApexGrammarRuleKey.PRIMARY_SUFFIX)
                        && expression.hasDescendant(ApexGrammarRuleKey.ARGUMENTS)) {
                    List<AstNode> arguments = expression.getDescendants(ApexGrammarRuleKey.ARGUMENTS);
                    for (AstNode argument : arguments) {
                        AstNode prefix = argument.getPreviousAstNode();
                        AstNode method = prefix.getFirstDescendant(ApexGrammarRuleKey.NAME,
                                ApexGrammarRuleKey.ALLOWED_KEYWORDS_AS_IDENTIFIER,
                                ApexGrammarRuleKey.ALLOWED_KEYWORDS_AS_IDENTIFIER_FOR_METHODS);
                        if (method != null) {
                            AstNode methodIdentifier = method.hasDescendant(ApexGrammarRuleKey.METHOD_IDENTIFIER)
                                    ? method.getLastChild(ApexGrammarRuleKey.METHOD_IDENTIFIER) : method;
                            String methodName = methodIdentifier.getTokenValue();
                            if (methodIsAsync(astNode, methodName)) {
                                String message = ChecksBundle.getStringFromBundle("AsyncMethodsCheckMessage");
                                getContext().createLineViolation(this,
                                        message, method,
                                        methodIdentifier.getTokenOriginalValue());
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}