1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*
* SonarQube
* Copyright (C) 2009-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 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.scanner.mediumtest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.CheckForNull;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextPointer;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputComponent;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.scanner.fs.InputProject;
import org.sonar.core.util.CloseableIterator;
import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Component;
import org.sonar.scanner.protocol.output.ScannerReport.Symbol;
import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.report.ScannerReportUtils;
import org.sonar.scanner.scan.SpringProjectScanContainer;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
public class AnalysisResult implements AnalysisObserver {
private static final Logger LOG = LoggerFactory.getLogger(AnalysisResult.class);
private final Map<String, InputFile> inputFilesByKeys = new HashMap<>();
private InputProject project;
private ScannerReportReader reader;
@Override
public void analysisCompleted(SpringProjectScanContainer container) {
LOG.info("Store analysis results in memory for later assertions in medium test");
FileStructure fileStructure = container.getComponentByType(FileStructure.class);
reader = new ScannerReportReader(fileStructure);
project = container.getComponentByType(InputProject.class);
storeFs(container);
}
public ScannerReportReader getReportReader() {
return reader;
}
private void storeFs(SpringProjectScanContainer container) {
InputComponentStore inputFileCache = container.getComponentByType(InputComponentStore.class);
for (InputFile inputPath : inputFileCache.inputFiles()) {
inputFilesByKeys.put(((DefaultInputFile) inputPath).getProjectRelativePath(), inputPath);
}
}
public Component getReportComponent(InputComponent inputComponent) {
return getReportReader().readComponent(((DefaultInputComponent) inputComponent).scannerId());
}
public Component getReportComponent(int scannerId) {
return getReportReader().readComponent(scannerId);
}
public List<ScannerReport.Issue> issuesFor(InputComponent inputComponent) {
return readFromReport(inputComponent, ScannerReportReader::readComponentIssues);
}
public List<ScannerReport.ExternalIssue> externalIssuesFor(InputComponent inputComponent) {
return readFromReport(inputComponent, ScannerReportReader::readComponentExternalIssues);
}
public List<ScannerReport.Issue> issuesFor(Component reportComponent) {
return readFromReport(reportComponent, ScannerReportReader::readComponentIssues);
}
public InputProject project() {
return project;
}
public Collection<InputFile> inputFiles() {
return inputFilesByKeys.values();
}
@CheckForNull
public InputFile inputFile(String relativePath) {
return inputFilesByKeys.get(relativePath);
}
public Map<String, List<ScannerReport.Measure>> allMeasures() {
Map<String, List<ScannerReport.Measure>> result = new HashMap<>();
result.put(project.key(), readFromReport(project, ScannerReportReader::readComponentMeasures));
for (InputFile inputFile : inputFilesByKeys.values()) {
result.put(inputFile.key(), readFromReport(inputFile, ScannerReportReader::readComponentMeasures));
}
return result;
}
/**
* Get highlighting types at a given position in an inputfile
*
* @param lineOffset 0-based offset in file
*/
public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) {
int ref = ((DefaultInputComponent) file).scannerId();
if (!reader.hasSyntaxHighlighting(ref)) {
return Collections.emptyList();
}
TextPointer pointer = file.newPointer(line, lineOffset);
List<TypeOfText> result = new ArrayList<>();
try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) {
while (it.hasNext()) {
ScannerReport.SyntaxHighlightingRule rule = it.next();
TextRange ruleRange = toRange(file, rule.getRange());
if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) {
result.add(ScannerReportUtils.toBatchType(rule.getType()));
}
}
} catch (Exception e) {
throw new IllegalStateException("Can't read syntax highlighting for " + file, e);
}
return result;
}
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) {
return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset()));
}
/**
* Get list of all start positions of a symbol in an inputfile
*
* @param symbolStartLine 0-based start offset for the symbol in file
* @param symbolStartLineOffset 0-based end offset for the symbol in file
*/
@CheckForNull
public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) {
int ref = ((DefaultInputComponent) file).scannerId();
try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) {
while (symbols.hasNext()) {
Symbol symbol = symbols.next();
if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) {
return symbol.getReferenceList();
}
}
}
return Collections.emptyList();
}
public List<ScannerReport.Duplication> duplicationsFor(InputFile file) {
return readFromReport(file, ScannerReportReader::readComponentDuplications);
}
public List<ScannerReport.CpdTextBlock> duplicationBlocksFor(InputFile file) {
return readFromReport(file, ScannerReportReader::readCpdTextBlocks);
}
@CheckForNull
public ScannerReport.LineCoverage coverageFor(InputFile file, int line) {
int ref = ((DefaultInputComponent) file).scannerId();
try (CloseableIterator<ScannerReport.LineCoverage> it = getReportReader().readComponentCoverage(ref)) {
while (it.hasNext()) {
ScannerReport.LineCoverage coverage = it.next();
if (coverage.getLine() == line) {
return coverage;
}
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
return null;
}
public List<ScannerReport.AdHocRule> adHocRules() {
return readFromReport(ScannerReportReader::readAdHocRules);
}
public List<ScannerReport.Dependency> dependencies() {
return readFromReport(ScannerReportReader::readDependencies);
}
@NotNull
private <G> List<G> readFromReport(InputComponent component, BiFunction<ScannerReportReader, Integer, CloseableIterator<G>> readerMethod) {
int ref = ((DefaultInputComponent) component).scannerId();
return readFromReport(r -> readerMethod.apply(r, ref));
}
@NotNull
private <G> List<G> readFromReport(Component component, BiFunction<ScannerReportReader, Integer, CloseableIterator<G>> readerMethod) {
int ref = component.getRef();
return readFromReport(r -> readerMethod.apply(r, ref));
}
@NotNull
private <G> List<G> readFromReport(Function<ScannerReportReader, CloseableIterator<G>> readerMethod) {
List<G> result = new ArrayList<>();
try (CloseableIterator<G> it = readerMethod.apply(getReportReader())) {
while (it.hasNext()) {
result.add(it.next());
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
return result;
}
}
|