Java Code Examples for com.sonar.sslr.api.AstNode#getFirstDescendant()
The following examples show how to use
com.sonar.sslr.api.AstNode#getFirstDescendant() .
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: TestClassCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
/** * 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 identifier = astNode.getFirstDescendant(ApexGrammarRuleKey.ALLOWED_KEYWORDS_AS_IDENTIFIER, ApexGrammarRuleKey.SPECIAL_KEYWORDS_AS_IDENTIFIER); if (hasTestAnnotation(astNode)) { if (astNode.is(ApexGrammarRuleKey.ENUM_DECLARATION) || astNode.getFirstDescendant(ApexGrammarRuleKey.TYPE_CLASS).hasDirectChildren(ApexKeyword.INTERFACE)) { getContext().createLineViolation(this, ANNOTATION_MESSAGE, astNode, identifier.getTokenOriginalValue()); } } else if (identifier.getTokenValue().contains(TEST)) { getContext().createLineViolation(this, NAME_MESSAGE, astNode, identifier.getTokenOriginalValue()); } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 2
Source File: DeprecatedMethodCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
/** * 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 parent = astNode.getParent(); AstNode modifiersNode = parent.getFirstDescendant(ApexGrammarRuleKey.MODIFIERS); AstNode blockNode = astNode.getFirstDescendant(ApexGrammarRuleKey.BLOCK); if (isDeprecated(modifiersNode) && !isEmptyBlock(blockNode)) { AstNode method = astNode.getFirstDescendant(ApexGrammarRuleKey.METHOD_IDENTIFIER); getContext().createLineViolation(this, String.format(MESSAGE, method.getTokenOriginalValue()), method); } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 3
Source File: AssertMessageCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 4
Source File: AssertLiteralBooleanCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 5
Source File: SeeAllDataTestCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 identifier = astNode.getFirstDescendant(ApexGrammarRuleKey.ALLOWED_KEYWORDS_AS_IDENTIFIER, ApexGrammarRuleKey.SPECIAL_KEYWORDS_AS_IDENTIFIER); if (astNode.getFirstDescendant(ApexGrammarRuleKey.TYPE_CLASS).hasDirectChildren(ApexKeyword.CLASS)) { AstNode testAnnotation = getTestAnnotation(astNode); if (testAnnotation != null && testAnnotation.hasDirectChildren(ApexGrammarRuleKey.EXPRESSION)) { AstNode expression = testAnnotation.getFirstDescendant(ApexGrammarRuleKey.EXPRESSION); List<AstNode> assignments = expression.getDescendants(ApexPunctuator.ASSIGN); for (AstNode assignment : assignments) { AstNode previousSibling = assignment.getFirstAncestor( ApexGrammarRuleKey.ASSIGNMENT_OPERATOR).getPreviousSibling(); AstNode nextSibling = assignment.getFirstAncestor( ApexGrammarRuleKey.ASSIGNMENT_OPERATOR).getNextSibling(); if (previousSibling.getTokenValue().equals(SEE_ALL_DATA) && nextSibling.getTokenValue().equals(TRUE)) { getContext().createLineViolation(this, MESSAGE, testAnnotation, identifier.getTokenOriginalValue()); } } } } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 6
Source File: StartAndStopCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 7
Source File: AsyncMethodsCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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()); } }
Example 8
Source File: TestMethodCheck.java From enforce-sonarqube-plugin with MIT License | 2 votes |
/** * Returns the method message. * * @param astNode current node. * @return the message. */ private String methodMessage(AstNode astNode) { AstNode method = astNode.getFirstDescendant(ApexGrammarRuleKey.METHOD_IDENTIFIER); return String.format(MESSAGE, method.getTokenOriginalValue()); }