com.sonar.sslr.api.AstNode Java Examples
The following examples show how to use
com.sonar.sslr.api.AstNode.
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: LocalFunctionComplexityCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void leaveNode(AstNode node) { SourceFunction function = (SourceFunction) getContext() .peekSourceCode(); int functionComplexity = ChecksHelper.getRecursiveMeasureInt(function, LuaMetric.COMPLEXITY); if (functionComplexity > maximumLocalFunctionComplexityThreshold) { String message = String .format("LocalFunction has a complexity of %s which is greater than %s authorized.", functionComplexity, maximumLocalFunctionComplexityThreshold); createIssueWithCost(message, node, (double) functionComplexity - maximumLocalFunctionComplexityThreshold); } }
Example #2
Source File: FunctionComplexityCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void leaveNode(AstNode node) { SourceFunction function = (SourceFunction) getContext() .peekSourceCode(); int functionComplexity = ChecksHelper.getRecursiveMeasureInt(function, LuaMetric.COMPLEXITY); if (functionComplexity > maximumFunctionComplexityThreshold) { String message = String .format("Function has a complexity of %s which is greater than %s authorized.", functionComplexity, maximumFunctionComplexityThreshold); createIssueWithCost(message, node, (double) functionComplexity - maximumFunctionComplexityThreshold); } }
Example #3
Source File: ExitCheck.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 6 votes |
private Boolean hasSignalSetToFailure(AstNode exitNode) { if (exitNode != null) { AstNode signalAtt = exitNode.getFirstChild(FlowAttTypes.SIGNAL); if (signalAtt != null) { logger.debug("++ Signal found ++"); String signalValue = signalAtt.getToken().getOriginalValue(); if (signalValue != null) { if (signalValue.equalsIgnoreCase("FAILURE")) { logger.debug("++ Signal is set to FAILURE! ++"); return true; } } } } return false; }
Example #4
Source File: LineLengthCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void visitFile(@Nullable AstNode astNode) { List<String> lines = Collections.emptyList(); try { lines = Files.readLines(getContext().getFile(), charset); } catch (IOException e) { LOG.error("Unable to execute rule \"LineLength\" for file {} because of error: {}", getContext().getFile().getName(), e); } for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (line.length() > maximumLineLength) { getContext().createLineViolation(this, "Split this {0} characters long line (which is greater than {1} authorized).", i + 1, line.length(), maximumLineLength); } } }
Example #5
Source File: InterfaceCommentsCheck.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void visitNode(AstNode astNode) { for (AstNode record : astNode.getChildren(NodeGrammar.RECORD)) { for (AstNode value : record.getChildren(NodeGrammar.VALUE)) { for (AstNode attr : value.getChildren(NodeGrammar.ATTRIBUTES)) { for (AstNode name : attr.getChildren(FlowAttTypes.NAME)) { if (name.getTokenValue().equals("NODE_COMMENT")) { if (attr.getParent().getChildren(FlowTypes.ELEMENT_VALUE).size() <= 0) { logger.debug("++ Comment VIOLATION found: " + value.getTokenLine() + " ++"); getContext().createLineViolation(this, "Add comment", value); } } } } } } }
Example #6
Source File: MethodComplexityCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void leaveNode(AstNode node) { SourceFunction function = (SourceFunction) getContext() .peekSourceCode(); int functionComplexity = ChecksHelper.getRecursiveMeasureInt(function, LuaMetric.COMPLEXITY); if (functionComplexity > maximumMethodComplexityThreshold) { String message = String .format("Method has a complexity of %s which is greater than %s authorized.", functionComplexity, maximumMethodComplexityThreshold); createIssueWithCost(message, node, (double) functionComplexity - maximumMethodComplexityThreshold); } }
Example #7
Source File: MethodChecksUtilsTest.java From enforce-sonarqube-plugin with MIT License | 6 votes |
@Test public void testHasTestMethodKeywordMatchesTestMethodPattern() { AstNode astNode = mock(AstNode.class); AstNode modifier = mock(AstNode.class); AstNode notTestMethodNode = mock(AstNode.class); List<AstNode> modifiers = new LinkedList<>(); List<AstNode> modifierChildren = new LinkedList<>(); when(notTestMethodNode.getTokenOriginalValue()).thenReturn("testMethod"); modifierChildren.add(notTestMethodNode); when(modifier.getChildren()).thenReturn(modifierChildren); modifiers.add(modifier); when(astNode.getChildren(MODIFIERS)).thenReturn(modifiers); assertTrue(MethodChecksUtils.hasTestMethodKeyword(astNode)); }
Example #8
Source File: MethodChecksUtilsTest.java From enforce-sonarqube-plugin with MIT License | 6 votes |
@Test public void testHasTestMethodKeywordNotMatchesTestMethodPattern() { AstNode astNode = mock(AstNode.class); AstNode modifier = mock(AstNode.class); AstNode notTestMethodNode = mock(AstNode.class); List<AstNode> modifiers = new LinkedList<>(); List<AstNode> modifierChildren = new LinkedList<>(); when(notTestMethodNode.getTokenOriginalValue()).thenReturn("somethingElse"); modifierChildren.add(notTestMethodNode); when(modifier.getChildren()).thenReturn(modifierChildren); modifiers.add(modifier); when(astNode.getChildren(MODIFIERS)).thenReturn(modifiers); assertFalse(MethodChecksUtils.hasTestMethodKeyword(astNode)); }
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: DeeplyNestedIfStmtsCheck.java From enforce-sonarqube-plugin with MIT License | 6 votes |
/** * Start at the top of each method declaration. * Inspect each top level if-then statement. * * @param node starts us at the very top of the method declaration. */ @Override public void visitNode(AstNode node) { try { List<AstNode> blockChildren = node.getChildren(ApexGrammarRuleKey.BLOCK); List<AstNode> blockStatementChildren; List<AstNode> statementChildren; for(AstNode bChild : blockChildren){ blockStatementChildren = bChild.getChildren(ApexGrammarRuleKey.BLOCK_STATEMENT); for(AstNode bsChild : blockStatementChildren){ statementChildren = bsChild.getChildren(ApexGrammarRuleKey.STATEMENT); for(AstNode sChild : statementChildren){ counter = 0; recursiveStatementMethod(sChild); } } } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example #15
Source File: QualifiedNameCheck.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { String service = FlowUtils.getQualifiedName(this.getContext().getFile()); logger.debug("Service found: " + service); if (!pattern.matcher(service).matches()) { getContext().createLineViolation(this, "Flow name " + service + " does not conform to the naming convention \"" + namingConvention + "\"", astNode); } }
Example #16
Source File: MethodChecksUtilsTest.java From enforce-sonarqube-plugin with MIT License | 5 votes |
@Test public void testHasTestMethodKeywordEmptyChildrenListOfModifiers() { AstNode parentNode = mock(AstNode.class); List<AstNode> modifiers = new LinkedList<>(); AstNode modifier = mock(AstNode.class); when(modifier.getChildren()).thenReturn(new LinkedList<>()); modifiers.add(modifier); when(parentNode.getChildren()).thenReturn(modifiers); assertFalse(MethodChecksUtils.hasTestMethodKeyword(parentNode)); }
Example #17
Source File: NestedTablesDepthCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { nestingLevel++; if (nestingLevel == getMax() + 1 ) { getContext().createLineViolation(this, "Refactor this code to not nest more than {0} tables.", astNode, getMax()); } }
Example #18
Source File: ExitCheck.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 5 votes |
private String getExitFrom(AstNode exitNode) { if (exitNode != null) { AstNode fromAtt = exitNode.getFirstChild(FlowAttTypes.FROM); if (fromAtt != null) { String fromType = fromAtt.getToken().getOriginalValue(); logger.debug("++ From field found! ++"); if (fromType != null) { return fromType; } } } return null; }
Example #19
Source File: TableWithTooManyFieldsCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { int nbParameters = astNode.getChildren(LuaGrammar.FIELD).size(); if (nbParameters > DEFAULT) { getContext() .createLineViolation( this, "This table has {0,number,integer} fields, which is greater than the {1,number,integer} authorized.", astNode, nbParameters, DEFAULT); } }
Example #20
Source File: DmlStatementCheck.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.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 #21
Source File: ClassNameCheck.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 { String className = astNode.getFirstDescendant(ApexGrammarRuleKey.COMMON_IDENTIFIER).getTokenOriginalValue(); if (!pattern.matcher(className).matches()) { getContext().createLineViolation(this, MESSAGE, astNode, className, format); } } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example #22
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 #23
Source File: VariableCountCheck.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 { checkVariableCount(astNode); } catch (Exception e) { ChecksLogger.logCheckError(this.toString(), "visitNode", e.toString()); } }
Example #24
Source File: DisabledCheck.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { AstNode disabled = astNode.getFirstChild(FlowGrammar.ATTRIBUTES) .getFirstChild(FlowAttTypes.DISABLED); if (disabled != null) { String isDisabled = disabled.getToken().getOriginalValue(); if (Boolean.valueOf(isDisabled)) { getContext().createLineViolation(this, "Remove disabled code", astNode); } } }
Example #25
Source File: AnnotationMethodCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * Analyzes if a node is annotation class or method. * * @param astNode to be analyzed. * @param keyword to be found. * @return the analysis result. */ protected boolean isAnnotation(AstNode astNode, String keyword) { boolean result = Boolean.FALSE; if (astNode.hasDirectChildren(ApexGrammarRuleKey.ANNOTATION)) { AstNode annotation = astNode.getFirstChild(ApexGrammarRuleKey.ANNOTATION); result = annotation.getFirstChild(ApexGrammarRuleKey.NAME).getTokenOriginalValue().equals(keyword); } return result; }
Example #26
Source File: FlowDependencyVisitor.java From sonar-flow-plugin with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { String service = astNode.getFirstChild(FlowGrammar.ATTRIBUTES) .getFirstChild(FlowAttTypes.SERVICE).getTokenOriginalValue(); @SuppressWarnings("unchecked") ArrayList<String> dependencies = (ArrayList<String>) getContext().peekSourceCode() .getData(FlowMetric.DEPENDENCIES); if (dependencies == null) { dependencies = new ArrayList<String>(); } dependencies.add(service); getContext().peekSourceCode().addData(FlowMetric.DEPENDENCIES, dependencies); logger.debug("**** Dependency found: " + service); }
Example #27
Source File: FunctionWithTooManyParametersCheck.java From sonar-lua with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void visitNode(AstNode astNode) { int nbParameters = astNode.getChildren(LuaGrammar.NAME, LuaGrammar.Punctuator.ELLIPSIS).size(); if (nbParameters > max ) { getContext().createLineViolation(this, "This function has {0,number,integer} parameters, which is greater than the {1,number,integer} authorized.", astNode, nbParameters, max); } }
Example #28
Source File: ApexAstScanner.java From enforce-sonarqube-plugin with MIT License | 5 votes |
/** * Builds and returns a source code builder callback. * * @param nodeName node type to identify a name. * @param isClass define a SourceClass or SourceFunction. * @return the builder callback. */ private static SourceCodeBuilderCallback buildCallback(AstNodeType nodeName, boolean isClass) { return (SourceCode sourceCode, AstNode astNode) -> { String key = generateKey(astNode, nodeName); sourceCode = isClass ? new SourceClass(key) : new SourceFunction(key); sourceCode.setStartAtLine(astNode.getTokenLine()); return sourceCode; }; }
Example #29
Source File: DeeplyNestedIfStmtsCheck.java From enforce-sonarqube-plugin with MIT License | 5 votes |
private void traverseBlockStatement(AstNode blockNode){ for(AstNode blockStatementNode : blockNode.getChildren(ApexGrammarRuleKey.BLOCK_STATEMENT)){ for(AstNode statementNode : blockStatementNode.getChildren(ApexGrammarRuleKey.STATEMENT)){ recursiveStatementMethod(statementNode); } } }
Example #30
Source File: FileLinesVisitor.java From sonar-lua with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void leaveFile(AstNode astNode) { FileLinesContext fileLinesContext = fileLinesContextFactory.createFor( fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(getContext().getFile().getAbsolutePath()))); int fileLength = getContext().peekSourceCode().getInt(LuaMetric.LINES); for (int line = 1; line <= fileLength; line++) { fileLinesContext.setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, linesOfCode.contains(line) ? 1 : 0); fileLinesContext.setIntValue(CoreMetrics.COMMENT_LINES_DATA_KEY, line, linesOfComments.contains(line) ? 1 : 0); } fileLinesContext.save(); linesOfCode.clear(); linesOfComments.clear(); }