private static int getNumberOfLines(Tree tree) { return new LinesOfCodeVisitor().linesOfCode(tree); } }
public int linesOfCode(Tree tree) { lines.clear(); scanTree(tree); return lines.size(); }
public int linesOfCode(CompilationUnitTree tree) { lines.clear(); visitTokens(tree); return lines.size(); }
@Override public void visitNode(Tree tree) { SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree; LinesOfCodeVisitor locVisitor = new LinesOfCodeVisitor(); switchStatementTree.cases().forEach( cgt -> { int lines = cgt.body().stream().mapToInt(locVisitor::linesOfCode).sum(); if (lines > max) { reportIssue(cgt.labels().get(cgt.labels().size() - 1), "Reduce this switch case number of lines from " + lines + " to at most " + max + ", for example by extracting code into methods."); } } ); } }
@Override public void visitNode(Tree tree) { SwitchStatementTree switchStatementTree = (SwitchStatementTree) tree; LinesOfCodeVisitor locVisitor = new LinesOfCodeVisitor(); switchStatementTree.cases().forEach( cgt -> { int lines = cgt.body().stream().mapToInt(locVisitor::linesOfCode).sum(); if (lines > max) { reportIssue(cgt.labels().get(cgt.labels().size() - 1), "Reduce this switch case number of lines from " + lines + " to at most " + max + ", for example by extracting code into methods."); } } ); } }
private static int getNumberOfLines(Tree tree) { return new LinesOfCodeVisitor().linesOfCode(tree); } }
public int linesOfCode(Tree tree) { lines.clear(); scanTree(tree); return lines.size(); }
@Override public void visitNode(Tree tree) { int lines = new LinesOfCodeVisitor().linesOfCode(tree); if (lines > maximum) { addIssueOnFile(MessageFormat.format("This file has {0} lines, which is greater than {1} authorized. Split it into smaller files.", lines, maximum)); } } }
public int linesOfCode(CompilationUnitTree tree) { lines.clear(); scanTree(tree); return lines.size(); }
@Override public void visitNode(Tree tree) { int lines = new LinesOfCodeVisitor().linesOfCode(tree); if (lines > maximum) { addIssueOnFile(MessageFormat.format("This file has {0} lines, which is greater than {1} authorized. Split it into smaller files.", lines, maximum)); } } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; BlockTree block = methodTree.block(); if (block != null) { int lines = new LinesOfCodeVisitor().linesOfCode(block); if (lines > max) { reportIssue(methodTree.simpleName(), "This method has " + lines + " lines, which is greater than the " + max + " lines authorized. Split it into smaller methods."); } } } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; BlockTree block = methodTree.block(); if (block != null) { int lines = new LinesOfCodeVisitor().linesOfCode(block); if (lines > max) { reportIssue(methodTree.simpleName(), "This method has " + lines + " lines, which is greater than the " + max + " lines authorized. Split it into smaller methods."); } } } }
@Override public void visitNode(Tree tree) { ClassTree node = (ClassTree) tree; Symbol.TypeSymbol symbol = node.symbol(); Symbol owner = symbol.owner(); Type ownerType = owner.type(); if (ownerType != null && ownerType.isClass() && owner.owner().isPackageSymbol()) { // raise only one issue for the first level of nesting when multiple nesting int lines = new LinesOfCodeVisitor().linesOfCode(node); if (lines > max) { reportIssue(ExpressionsHelper.reportOnClassTree(node), "Reduce this class from " + lines + " to the maximum allowed " + max + " or externalize it in a public class."); } } }
@Override public void visitNode(Tree tree) { ClassTree node = (ClassTree) tree; Symbol.TypeSymbol symbol = node.symbol(); Symbol owner = symbol.owner(); Type ownerType = owner.type(); if (ownerType != null && ownerType.isClass() && owner.owner().isPackageSymbol()) { // raise only one issue for the first level of nesting when multiple nesting int lines = new LinesOfCodeVisitor().linesOfCode(node); if (lines > max) { reportIssue(ExpressionsHelper.reportOnClassTree(node), "Reduce this class from " + lines + " to the maximum allowed " + max + " or externalize it in a public class."); } } }
@Override public void scanFile(JavaFileScannerContext context) { sonarFile = fs.inputFile(fs.predicates().is(context.getFile())); CommentLinesVisitor commentLinesVisitor = createCommentLineVisitorAndFindNoSonar(context); if(isSonarLintContext()) { // No need to compute metrics on SonarLint side, but the no sonar filter is still required return; } classTrees.clear(); methods = 0; complexityInMethods = 0; classes = 0; methodComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_METHODS); super.setContext(context); scanTree(context.getTree()); //leave file. int fileComplexity = context.getComplexityNodes(context.getTree()).size(); saveMetricOnFile(CoreMetrics.CLASSES, classes); saveMetricOnFile(CoreMetrics.FUNCTIONS, methods); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInMethods); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_CLASSES, fileComplexity); saveMetricOnFile(CoreMetrics.COMPLEXITY, fileComplexity); saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric()); saveMetricOnFile(CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree())); saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree())); saveMetricOnFile(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, methodComplexityDistribution.build()); RangeDistributionBuilder fileComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_FILES); saveMetricOnFile(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, fileComplexityDistribution.add(fileComplexity).build()); saveMetricOnFile(CoreMetrics.COGNITIVE_COMPLEXITY, CognitiveComplexityVisitor.compilationUnitComplexity(context.getTree())); }
@Override public void scanFile(JavaFileScannerContext context) { sonarFile = fs.inputFile(fs.predicates().is(context.getFile())); CommentLinesVisitor commentLinesVisitor = createCommentLineVisitorAndFindNoSonar(context); if(isSonarLintContext()) { // No need to compute metrics on SonarLint side, but the no sonar filter is still required return; } classTrees.clear(); methods = 0; complexityInMethods = 0; classes = 0; methodComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_METHODS); super.setContext(context); scanTree(context.getTree()); //leave file. int fileComplexity = context.getComplexityNodes(context.getTree()).size(); saveMetricOnFile(CoreMetrics.CLASSES, classes); saveMetricOnFile(CoreMetrics.FUNCTIONS, methods); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInMethods); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_CLASSES, fileComplexity); saveMetricOnFile(CoreMetrics.COMPLEXITY, fileComplexity); saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric()); saveMetricOnFile(CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree())); saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree())); saveMetricOnFile(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, methodComplexityDistribution.build()); RangeDistributionBuilder fileComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_FILES); saveMetricOnFile(CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, fileComplexityDistribution.add(fileComplexity).build()); saveMetricOnFile(CoreMetrics.COGNITIVE_COMPLEXITY, CognitiveComplexityVisitor.compilationUnitComplexity(context.getTree())); }
saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric()); saveMetricOnFile(CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree())); saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree()));
saveMetricOnFile(CoreMetrics.COMMENT_LINES, new CommentLinesVisitor().commentLines(context.getTree())); saveMetricOnFile(CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree())); saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree()));