
org.sonar.java.testing.JavaFileScannerContextForTests Maven / Gradle / Ivy
/*
* SonarQube Java
* Copyright (C) 2012-2023 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 GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.testing;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.SonarComponents;
import org.sonar.java.model.DefaultJavaFileScannerContext;
import org.sonar.java.model.Sema;
import org.sonar.java.reporting.AnalyzerMessage;
import org.sonar.java.reporting.FluentReporting;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.caching.CacheContext;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
import org.sonar.plugins.java.api.tree.Tree;
public class JavaFileScannerContextForTests extends DefaultJavaFileScannerContext {
private final Set issues = new LinkedHashSet<>();
private final Map> quickFixes = new HashMap<>();
public JavaFileScannerContextForTests(CompilationUnitTree tree, InputFile inputFile, Sema semanticModel,
@Nullable SonarComponents sonarComponents, JavaVersion javaVersion,
boolean failedParsing, boolean inAndroidContext, @Nullable CacheContext cacheContext) {
super(tree, inputFile, semanticModel, sonarComponents, javaVersion, failedParsing, inAndroidContext, cacheContext);
}
public Set getIssues() {
return issues;
}
public Map> getQuickFixes() {
return quickFixes;
}
@Override
public void addIssueOnProject(JavaCheck javaCheck, String message) {
issues.add(new AnalyzerMessage(javaCheck, sonarComponents.project(), null, message, 0));
}
@Override
public void addIssue(int line, JavaCheck javaCheck, String message, @Nullable Integer cost) {
issues.add(new AnalyzerMessage(javaCheck, getInputFile(), line, message, cost != null ? cost.intValue() : 0));
}
@Override
public void reportIssue(JavaCheck javaCheck, Tree tree, String message) {
throwIfEndOfAnalysisCheck(javaCheck);
newIssue()
.forRule(javaCheck)
.onTree(tree)
.withMessage(message)
.report();
}
@Override
public void reportIssue(JavaCheck javaCheck, Tree syntaxNode, String message, List secondary, @Nullable Integer cost) {
throwIfEndOfAnalysisCheck(javaCheck);
newIssue()
.forRule(javaCheck)
.onTree(syntaxNode)
.withMessage(message)
.withSecondaries(secondary)
.withCost(cost == null ? 0 : cost)
.report();
}
@Override
public void reportIssue(JavaCheck javaCheck, Tree startTree, Tree endTree, String message) {
throwIfEndOfAnalysisCheck(javaCheck);
newIssue()
.forRule(javaCheck)
.onRange(startTree, endTree)
.withMessage(message)
.report();
}
@Override
public void reportIssue(JavaCheck javaCheck, Tree startTree, Tree endTree, String message, List secondary, @Nullable Integer cost) {
throwIfEndOfAnalysisCheck(javaCheck);
newIssue()
.forRule(javaCheck)
.onRange(startTree, endTree)
.withMessage(message)
.withSecondaries(secondary)
.withCost(cost == null ? 0 : cost)
.report();
}
@Override
public void reportIssueWithFlow(JavaCheck javaCheck, Tree syntaxNode, String message, Iterable> flows, @Nullable Integer cost) {
throwIfEndOfAnalysisCheck(javaCheck);
newIssue()
.forRule(javaCheck)
.onTree(syntaxNode)
.withMessage(message)
.withFlows(StreamSupport.stream(flows.spliterator(), false).collect(Collectors.toList()))
.withCost(cost == null ? 0 : cost)
.report();
}
@Override
public void reportIssue(AnalyzerMessage message) {
issues.add(message);
}
@Override
public AnalyzerMessage createAnalyzerMessage(JavaCheck javaCheck, Tree startTree, String message) {
return createAnalyzerMessage(getInputFile(), javaCheck, startTree, null, message, Collections.emptyList(), null);
}
@Override
public FluentReporting.JavaIssueBuilder newIssue() {
return new JavaIssueBuilderForTests(getInputFile(), issues, quickFixes);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy