Java Code Examples for com.sonar.sslr.api.AstNode#getDescendants()
The following examples show how to use
com.sonar.sslr.api.AstNode#getDescendants() .
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: BooleanParamentersOnAssertCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
/** * Verifies if the given node met the rule, in case it does not it creates * the error * * @param astNode the node to be analyzed. */ @Override public void visitNode(AstNode astNode) { try { List<AstNode> statements = astNode.getDescendants(STATEMENT); for (AstNode statement : statements) { boolean hasAssertion = hasAssertion(statement.getDescendants(NAME), SYSTEM_ASSERT); if (hasAssertion && !hasBooleanVariable(astNode)) { getContext().createLineViolation(this, ChecksBundle.getStringFromBundle("AssertBooleanVariableMessage"), astNode, astNode.getFirstChild(METHOD_IDENTIFIER).getTokenOriginalValue()); } } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 2
Source File: StartAndStopCheck.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 { int startCalls = 0; int stopCalls = 0; List<AstNode> expressions = astNode.getDescendants(ApexGrammarRuleKey.PRIMARY_EXPRESSION); for (AstNode expression : expressions) { if (isTestMethodCall(expression, START)) { startCalls++; } if (isTestMethodCall(expression, STOP)) { stopCalls++; } if (startCalls > MAX_ALLOWED_INSTANCES || stopCalls > MAX_ALLOWED_INSTANCES) { getContext().createLineViolation(this, MESSAGE, astNode, astNode.getFirstDescendant(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue()); return; } } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 3
Source File: TestAssertionsAndTestMethodKeywordCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
@Override public void visitNode(AstNode astNode) { boolean hasTestMethodKeyword = hasTestMethodKeyword(astNode.getParent()); List<AstNode> nameNodes = astNode.getDescendants(NAME); boolean hasAssertion = hasAssertion(nameNodes, SYSTEM_ASSERT_PATTERN); if (hasTestMethodKeyword && !hasAssertion) { getContext().createLineViolation(this, String.format(ChecksBundle.getStringFromBundle("AssertionError"), astNode.getFirstDescendant(METHOD_IDENTIFIER).getTokenOriginalValue()), astNode); } if (!hasTestMethodKeyword && hasAssertion) { getContext().createLineViolation(this, String.format(ChecksBundle.getStringFromBundle("TestMethodKeywordError"), astNode.getFirstDescendant(METHOD_IDENTIFIER).getTokenOriginalValue()), astNode); } }
Example 4
Source File: MethodLengthCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
private void LookForMethodLength(AstNode methodDeclarationNode) { List<AstNode> astNodes = methodDeclarationNode.getDescendants(ApexPunctuator.LBRACE, ApexPunctuator.RBRACE); int lBraceLineNumber = 0; int rBraceLineNumber = 0; int astIdex = 0; if(astNodes.size() > 0){ if (astNodes.get(astIdex).getName().equals("LBRACE")){ lBraceLineNumber = astNodes.get(astIdex).getTokenLine(); } astIdex = astNodes.size() - 1; if (astNodes.get(astIdex).getName().equals("RBRACE")){ rBraceLineNumber = astNodes.get(astIdex).getTokenLine(); } } if((rBraceLineNumber - lBraceLineNumber) > max){ getContext().createLineViolation(this, MESSAGE, methodDeclarationNode, max); } }
Example 5
Source File: TestMethodCheck.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 modifierNode = null; if (astNode.hasParent(ApexGrammarRuleKey.TYPE_DECLARATION)) { modifierNode = astNode.getParent().getFirstChild(ApexGrammarRuleKey.MODIFIERS); if (!isAnnotation(modifierNode, IS_TEST)) { List<AstNode> methods = astNode.getDescendants(ApexGrammarRuleKey.METHOD_DECLARATION); methods.stream().forEach((method) -> { AstNode parent = method.getParent(); AstNode firstChild = parent.getFirstDescendant(ApexGrammarRuleKey.MODIFIERS); if (isTest(firstChild)) { getContext().createLineViolation(this, methodMessage(method), method); } }); } } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example 6
Source File: AsyncMethodsCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
/** * 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 7
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 8
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 9
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 10
Source File: SeeAllDataTestCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 11
Source File: TestClassCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * 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 12
Source File: HardcodingIdsInMethodsAndConstructorsCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * Look for an string literal string node and then it compares this token * with a regex to proof it contains a hard-coded ID. * * @param astNode initial node. * @return a list of nodes which contains hard-coded values. */ private List<AstNode> lookFor(AstNode astNode) { List<AstNode> hardCodedNodes = new LinkedList<>(); List<AstNode> foundNodes = astNode.getDescendants(STRING_LITERAL_STRING); foundNodes.stream().filter((expressionNode) -> (expressionNode.getTokenOriginalValue() .matches(HardcodingIdsCheckInVariables.ID_PATTERN))).forEach((expressionNode) -> { hardCodedNodes.add(expressionNode); } ); return hardCodedNodes; }
Example 13
Source File: MethodNameAsClassNameCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
private void checkMethodNameAsClassName(AstNode astNode){ String className = astNode.getFirstDescendant(ApexGrammarRuleKey.COMMON_IDENTIFIER).getTokenOriginalValue(); List<AstNode> lstAstNode = astNode.getDescendants(ApexGrammarRuleKey.METHOD_DECLARATION); for(AstNode astMethodNode : lstAstNode){ if(className.equals(astMethodNode.getFirstDescendant(ApexGrammarRuleKey.METHOD_IDENTIFIER).getTokenOriginalValue()) ){ getContext().createLineViolation(this, message, astMethodNode); } } }
Example 14
Source File: EmptyCatchCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
@Override public void visitNode(AstNode astNode) { List<AstNode> statements = astNode.getDescendants(ApexGrammarRuleKey.STATEMENT); //catch should have at least one statement if(statements.size() == 0){ getContext().createLineViolation(this, MESSAGE, astNode, "Extra arg 1", "Extra arg2"); } }
Example 15
Source File: VariableCountCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
private void checkVariableCount(AstNode astNode){ List<AstNode> lstAstNode = astNode.getDescendants(ApexGrammarRuleKey.PROPERTY_DECLARATION, ApexGrammarRuleKey.FIELD_DECLARATION); if(lstAstNode.size() >= max){ getContext().createLineViolation(this, message, astNode, max); } }
Example 16
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()); } }