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

The following examples show how to use com.sonar.sslr.api.AstNode#hasDescendant() . 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: AsyncMethodsCheck.java    From enforce-sonarqube-plugin with MIT License 6 votes vote down vote up
/**
 * Determines if a method is Async by looking for the "@future" amongst it's
 * annotations.
 *
 * @param astNode the node of the loop statement.
 * @param methodName the name of the method.
 * @return True if the method has the "@future" annotation.
 */
private boolean methodIsAsync(AstNode astNode, String methodName) {
    AstNode firstAncestor = astNode.getFirstAncestor(ApexGrammarRuleKey.TYPE_DECLARATION);
    List<AstNode> methods = firstAncestor.getDescendants(ApexGrammarRuleKey.METHOD_DECLARATION);
    if (!methods.isEmpty()) {
        for (AstNode method : methods) {
            String name = method.getFirstChild(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenValue();
            if (name.equals(methodName)) {
                AstNode member = method.getFirstAncestor(ApexGrammarRuleKey.CLASS_OR_INTERFACE_MEMBER);
                AstNode modifiers = member.getFirstChild(ApexGrammarRuleKey.MODIFIERS);
                if (modifiers.hasDescendant(ApexGrammarRuleKey.ANNOTATION)) {
                    List<AstNode> annotations = modifiers.getChildren(ApexGrammarRuleKey.ANNOTATION);
                    for (AstNode annotation : annotations) {
                        String annotationValue = annotation.getFirstChild(ApexGrammarRuleKey.NAME).getTokenValue();
                        if (annotationValue.equalsIgnoreCase(FUTURE)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 2
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 3
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 4
Source File: SeeAllDataTestCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the node that contains the "@isTest" annotation.
 *
 * @param astNode The class declaration node whose modifiers contain the
 * annotation.
 * @return The node of the "@isTest" annotation.
 */
private AstNode getTestAnnotation(AstNode astNode) {
    AstNode testAnnotation = null;
    AstNode modifiers = astNode.getParent().getFirstDescendant(ApexGrammarRuleKey.MODIFIERS);
    if (modifiers.hasDescendant(ApexGrammarRuleKey.ANNOTATION)) {
        List<AstNode> annotations = modifiers.getDescendants(ApexGrammarRuleKey.ANNOTATION);
        for (AstNode annotation : annotations) {
            String annotationValue = annotation.getFirstDescendant(ApexGrammarRuleKey.NAME).getTokenValue();
            if (annotationValue.equals(IS_TEST)) {
                testAnnotation = annotation;
            }
        }
    }
    return testAnnotation;
}
 
Example 5
Source File: TestClassCheck.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Determines whether or not a declarations has the @isTest annotation
 * amongst its modifiers.
 *
 * @param astNode the declaration to be checked.
 * @return true if the @isTest annotation is amongst the modifiers.
 */
public static boolean hasTestAnnotation(AstNode astNode) {
    AstNode modifiers = astNode.getParent().getFirstDescendant(ApexGrammarRuleKey.MODIFIERS);
    if (modifiers.hasDescendant(ApexGrammarRuleKey.ANNOTATION)) {
        List<AstNode> annotations = modifiers.getDescendants(ApexGrammarRuleKey.ANNOTATION);
        for (AstNode annotation : annotations) {
            String annotationValue = annotation.getFirstDescendant(ApexGrammarRuleKey.NAME).getTokenValue();
            if (annotationValue.equals(IS_TEST)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: SoqlLimitCheck.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 {
    	AstNode nodeToCheck = astNode;
        if(SoqlParser.isStringQuery(astNode)){
            AstNode parsedQuery = SoqlParser.parseQuery(astNode);
            if(parsedQuery != null) {
                nodeToCheck = parsedQuery;
            }
        }

        if (nodeToCheck.is(ApexGrammarRuleKey.QUERY_EXPRESSION) && !nodeToCheck.hasDescendant(ApexGrammarRuleKey.LIMIT_SENTENCE)) {
        	AstNode selectNode = nodeToCheck.getFirstChild(ApexGrammarRuleKey.SELECT_SENTENCE);
        	if(selectNode.hasDescendant(ApexGrammarRuleKey.AGGREGATE_EXPR)){ 
    			if(nodeToCheck.hasDescendant(ApexGrammarRuleKey.GROUP_BY_SENTENCE)){
    				getContext().createLineViolation(this, MESSAGE, astNode);
    			}
        	}else{
        		getContext().createLineViolation(this, MESSAGE, astNode);
        	}
            
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 7
Source File: SoqlInLoopCheck.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 {
		  astNode = getStatementAstNode(astNode);
		  if ((astNode.is(ApexGrammarRuleKey.STATEMENT) && astNode.hasDescendant(ApexGrammarRuleKey.QUERY_EXPRESSION))) {
              getContext().createLineViolation(this, String.format(message,
                      astNode.getFirstDescendant(ApexGrammarRuleKey.QUERY_EXPRESSION).getTokenOriginalValue()), astNode.getFirstDescendant(ApexGrammarRuleKey.QUERY_EXPRESSION));
          }
      } catch (Exception e) {
          ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
      }
}
 
Example 8
Source File: DmlStatementCheck.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.DML_OPERATIONS)) {
            getContext().createLineViolation(this, String.format(message,
                    astNode.getFirstDescendant(ApexGrammarRuleKey.DML_OPERATIONS).getTokenOriginalValue()), astNode);
        }
    } catch (Exception e) {
        ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString());
    }
}
 
Example 9
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());
    }
}