Source:
<code>public void scanFile(JavaFileScannerContext context) { sonarFile = fs.inputFile(fs.predicates().is(context.getFile())); CommentLinesVisitor commentLinesVisitor = createCommentLineVisitorAndFindNoSonar(context); if(isSonarLintContext()) { return; } classTrees.clear(); methods = 0; complexityInMethods = 0; classes = 0; methodComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_METHODS); super.scanFile(context); 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()); if (isSonarQubeGreaterThanOrEqualTo63()) { <START> saveMetricOnFile(CoreMetrics.COGNITIVE_COMPLEXITY, context.compilationUnitCognitiveComplexity()); <END> } }</code><technical_language>context.getTree() provides access CompilationUnitTree, CognitiveComplexityVisitor.cognitiveComplexity directly, avoiding need for new method in JavaScannerContext interface</technical_language>

Target:
public void scanFile(JavaFileScannerContext context) { sonarFile = fs.inputFile(fs.predicates().is(context.getFile())); CommentLinesVisitor commentLinesVisitor = createCommentLineVisitorAndFindNoSonar(context); if(isSonarLintContext()) { return; } classTrees.clear(); methods = 0; complexityInMethods = 0; classes = 0; methodComplexityDistribution = new RangeDistributionBuilder(LIMITS_COMPLEXITY_METHODS); super.scanFile(context); 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()); if (isSonarQubeGreaterThanOrEqualTo63()) { saveMetricOnFile(CoreMetrics.COGNITIVE_COMPLEXITY, CognitiveComplexityVisitor.compilationUnitComplexity(context.getTree())); } }