
org.sonar.java.metrics.MetricsComputer Maven / Gradle / Ivy
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.metrics;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.ast.visitors.CognitiveComplexityVisitor;
import org.sonar.java.ast.visitors.CommentLinesVisitor;
import org.sonar.java.ast.visitors.ComplexityVisitor;
import org.sonar.java.ast.visitors.LinesOfCodeVisitor;
import org.sonar.java.ast.visitors.MethodNestingLevelVisitor;
import org.sonar.java.ast.visitors.NumberOfAccessedVariablesVisitor;
import org.sonar.java.ast.visitors.StatementVisitor;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
public class MetricsComputer {
private final Map> methodComplexityNodes = new HashMap<>();
private final Map methodComplexity = new HashMap<>();
private final Map compilationUnityComplexity = new HashMap<>();
private final Map methodNumberOfAccessedVariables = new HashMap<>();
private final Map treeLinesOfCode = new HashMap<>();
private final Map treeNumberOfStatements = new HashMap<>();
private final Map treeNumberOfCommentedLines = new HashMap<>();
private final Map> treeNoSonarLines = new HashMap<>();
private final Map methodNestingLevel = new HashMap<>();
ComplexityVisitor complexityVisitor = new ComplexityVisitor();
public List getComplexityNodes(Tree tree) {
return methodComplexityNodes.computeIfAbsent(tree.hashCode(), k -> complexityVisitor.getNodes(tree));
}
public CognitiveComplexityVisitor.Result getMethodComplexity(MethodTree tree) {
return methodComplexity.computeIfAbsent(tree.hashCode(), k -> CognitiveComplexityVisitor.methodComplexity(tree));
}
NumberOfAccessedVariablesVisitor methodBodyVisitor = new NumberOfAccessedVariablesVisitor();
public int getNumberOfAccessedVariables(MethodTree tree) {
return methodNumberOfAccessedVariables.computeIfAbsent(tree.hashCode(), k -> methodBodyVisitor.getNumberOfAccessedVariables(tree));
}
LinesOfCodeVisitor linesOfCodeVisitor = new LinesOfCodeVisitor();
public int getLinesOfCode(Tree tree) {
return treeLinesOfCode.computeIfAbsent(tree.hashCode(), k -> linesOfCodeVisitor.linesOfCode(tree));
}
StatementVisitor numberOfStatementsVisitor = new StatementVisitor();
public int getNumberOfStatements(Tree tree) {
return treeNumberOfStatements.computeIfAbsent(tree.hashCode(), k -> numberOfStatementsVisitor.numberOfStatements(tree));
}
CommentLinesVisitor commentedLineVisitor = new CommentLinesVisitor();
public Integer getNumberOfCommentedLines(CompilationUnitTree tree) {
return treeNumberOfCommentedLines.computeIfAbsent(tree.hashCode(), k -> {
commentedLineVisitor.analyzeCommentLines(tree);
return commentedLineVisitor.commentLinesMetric();
});
}
public Set getNoSonarLines(CompilationUnitTree tree) {
return treeNoSonarLines.computeIfAbsent(tree.hashCode(), k -> {
commentedLineVisitor.analyzeCommentLines(tree);
return commentedLineVisitor.noSonarLines();
});
}
public int getCompilationUnitComplexity(CompilationUnitTree tree) {
return compilationUnityComplexity.computeIfAbsent(tree.hashCode(), k -> CognitiveComplexityVisitor.compilationUnitComplexity(tree));
}
MethodNestingLevelVisitor methodNestingVisitor = new MethodNestingLevelVisitor();
public int getMethodNestingLevel(MethodTree tree) {
return methodNestingLevel.computeIfAbsent(tree.hashCode(), k -> methodNestingVisitor.getMaxNestingLevel(tree));
}
@VisibleForTesting
Map> getMethodComplexityNodes() {
return methodComplexityNodes;
}
@VisibleForTesting
Map getMethodComplexity() {
return methodComplexity;
}
@VisibleForTesting
Map getCompilationUnityComplexity() {
return compilationUnityComplexity;
}
@VisibleForTesting
Map getMethodNumberOfAccessedVariables() {
return methodNumberOfAccessedVariables;
}
@VisibleForTesting
Map getTreeLinesOfCode() {
return treeLinesOfCode;
}
@VisibleForTesting
Map getTreeNumberOfStatements() {
return treeNumberOfStatements;
}
@VisibleForTesting
Map getTreeNumberOfCommentedLines() {
return treeNumberOfCommentedLines;
}
@VisibleForTesting
Map> getTreeNoSonarLines() {
return treeNoSonarLines;
}
@VisibleForTesting
Map getMethodNestingLevel() {
return methodNestingLevel;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy