Repository navigation
Expand file tree
/
Copy pathJavaFrontend.java
More file actions
361 lines (311 loc) · 14.4 KB
/
Copy pathJavaFrontend.java
File metadata and controls
361 lines (311 loc) · 14.4 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* 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;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.ast.visitors.FileLinesVisitor;
import org.sonar.java.ast.visitors.Java25FeaturesTelemetryVisitor;
import org.sonar.java.ast.visitors.SyntaxHighlighterVisitor;
import org.sonar.java.caching.CacheContextImpl;
import org.sonar.java.classpath.DependencyVersionInference;
import org.sonar.java.collections.CollectionUtils;
import org.sonar.java.exceptions.ApiMismatchException;
import org.sonar.java.filters.SonarJavaIssueFilter;
import org.sonar.java.model.JParseResult;
import org.sonar.java.model.JParser;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.java.telemetry.Telemetry;
import org.sonar.java.telemetry.TelemetryKey;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaResourceLocator;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonarsource.performance.measure.PerformanceMeasure;
import org.sonarsource.performance.measure.PerformanceMeasure.Duration;
import static org.sonar.java.telemetry.TelemetryKey.JAVA_DEPENDENCY_LOMBOK;
import static org.sonar.java.telemetry.TelemetryKey.JAVA_DEPENDENCY_SPRING_BOOT;
import static org.sonar.java.telemetry.TelemetryKey.JAVA_DEPENDENCY_SPRING_WEB;
public class JavaFrontend {
private static final Logger LOG = LoggerFactory.getLogger(JavaFrontend.class);
private static final String BATCH_ERROR_MESSAGE = "Batch Mode failed, analysis of Java Files stopped.";
/**
* List of libraries, whose presence or absence we want to report.
*/
private static final Map<TelemetryKey, String> REPORTED_DEPENDENCIES = Map.of(
JAVA_DEPENDENCY_LOMBOK, "lombok",
JAVA_DEPENDENCY_SPRING_BOOT, "spring-boot",
JAVA_DEPENDENCY_SPRING_WEB, "spring-web"
);
private final JavaVersion javaVersion;
private final SonarComponents sonarComponents;
private final Telemetry telemetry;
private final List<File> globalClasspath;
private final JavaAstScanner astScanner;
private final JavaAstScanner astScannerForTests;
private final JavaAstScanner astScannerForGeneratedFiles;
public JavaFrontend(JavaVersion javaVersion, SonarComponents sonarComponents, Measurer measurer, Telemetry telemetry,
JavaResourceLocator javaResourceLocator, @Nullable SonarJavaIssueFilter postAnalysisIssueFilter, JavaCheck... visitors) {
this.javaVersion = javaVersion;
this.sonarComponents = sonarComponents;
this.telemetry = telemetry;
List<JavaCheck> commonVisitors = new ArrayList<>();
commonVisitors.add(javaResourceLocator);
commonVisitors.add(new Java25FeaturesTelemetryVisitor(telemetry));
if (postAnalysisIssueFilter != null) {
commonVisitors.add(postAnalysisIssueFilter);
}
List<JavaCheck> codeVisitors = new ArrayList<>();
codeVisitors.add(measurer);
codeVisitors.addAll(commonVisitors);
codeVisitors.addAll(Arrays.asList(visitors));
List<JavaCheck> testCodeVisitors = new ArrayList<>(commonVisitors);
testCodeVisitors.add(measurer.new TestFileMeasurer());
if (!sonarComponents.isSonarLintContext()) {
codeVisitors.add(new FileLinesVisitor(sonarComponents));
codeVisitors.add(new SyntaxHighlighterVisitor(sonarComponents));
testCodeVisitors.add(new SyntaxHighlighterVisitor(sonarComponents));
}
List<File> classpath = sonarComponents.getJavaClasspath();
List<File> testClasspath = sonarComponents.getJavaTestClasspath();
List<File> jspClasspath = sonarComponents.getJspClasspath();
testCodeVisitors.addAll(sonarComponents.testChecks());
List<JavaCheck> jspCodeVisitors = sonarComponents.jspChecks();
boolean inAndroidContext = sonarComponents.inAndroidContext();
globalClasspath = Stream.of(classpath, testClasspath, jspClasspath)
.flatMap(Collection::stream).distinct().toList();
//AstScanner for main files
astScanner = new JavaAstScanner(sonarComponents, telemetry, TelemetryKey.JAVA_ANALYSIS_MAIN);
astScanner.setVisitorBridge(new VisitorsBridge(codeVisitors, classpath, sonarComponents, javaVersion, inAndroidContext));
//AstScanner for test files
astScannerForTests = new JavaAstScanner(sonarComponents, telemetry, TelemetryKey.JAVA_ANALYSIS_TEST);
astScannerForTests.setVisitorBridge(new VisitorsBridge(testCodeVisitors, testClasspath, sonarComponents, javaVersion, inAndroidContext));
//AstScanner for generated files
astScannerForGeneratedFiles = new JavaAstScanner(sonarComponents, telemetry, TelemetryKey.JAVA_ANALYSIS_GENERATED);
astScannerForGeneratedFiles.setVisitorBridge(new VisitorsBridge(jspCodeVisitors, jspClasspath, sonarComponents, javaVersion, inAndroidContext));
}
public void scan(Iterable<InputFile> sourceFiles, Iterable<InputFile> testFiles, Iterable<? extends InputFile> generatedFiles) {
if (canOptimizeScanning()) {
long successfullyScanned = 0L;
long total = 0L;
Map<Boolean, List<InputFile>> mainFilesScannedWithoutParsing = astScanner.scanWithoutParsing(sourceFiles);
sourceFiles = mainFilesScannedWithoutParsing.get(false);
successfullyScanned += mainFilesScannedWithoutParsing.get(true).size();
total += mainFilesScannedWithoutParsing.get(true).size() + mainFilesScannedWithoutParsing.get(false).size();
Map<Boolean, List<InputFile>> testFilesScannedWithoutParsing = astScannerForTests.scanWithoutParsing(testFiles);
testFiles = testFilesScannedWithoutParsing.get(false);
successfullyScanned += testFilesScannedWithoutParsing.get(true).size();
total += testFilesScannedWithoutParsing.get(true).size() + testFilesScannedWithoutParsing.get(false).size();
total += StreamSupport.stream(generatedFiles.spliterator(), false).count();
LOG.info(
"Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for {} out of {} files. These files will not be parsed.",
successfullyScanned,
total
);
} else if (isCacheEnabled()) {
LOG.info("Server-side caching is enabled. The Java analyzer will not try to leverage data from a previous analysis.");
} else {
LOG.info("Server-side caching is not enabled. The Java analyzer will not try to leverage data from a previous analysis.");
}
// SonarLint is not compatible with batch mode, it needs InputFile#contents() and batch mode use InputFile#absolutePath()
boolean fileByFileMode = sonarComponents.isSonarLintContext() || sonarComponents.isFileByFileEnabled();
if (fileByFileMode) {
scanAndMeasureTask(sourceFiles, astScanner::scan, "Main");
scanAndMeasureTask(testFiles, astScannerForTests::scan, "Test");
scanAndMeasureTask(generatedFiles, astScannerForGeneratedFiles::scan, "Generated");
} else if (sonarComponents.isAutoScan()) {
scanAsBatch(new AutoScanBatchContext(), sourceFiles, testFiles);
} else {
scanAsBatch(new DefaultBatchModeContext(astScanner, "Main"), sourceFiles);
scanAsBatch(new DefaultBatchModeContext(astScannerForTests, "Test"), testFiles);
scanAsBatch(new DefaultBatchModeContext(astScannerForGeneratedFiles, "Generated"), generatedFiles);
}
DependencyVersionInference dependencyService = new DependencyVersionInference();
for (Map.Entry<TelemetryKey, String> dep : REPORTED_DEPENDENCIES.entrySet()) {
dependencyService
.infer(dep.getValue(), globalClasspath)
.ifPresentOrElse(
version -> telemetry.aggregateAsSortedSet(dep.getKey(), version.toString()),
() -> telemetry.aggregateAsSortedSet(dep.getKey())
);
}
}
/**
* Scans the files given as input in batch mode.
*
* The batch size used is determined by configuration.
* This batch size is then used as a threshold: files are added to a batch until the threshold is passed.
* Once the threshold is passed, the batch is processed for analysis.
*
* If no batch size is configured, the input files are scanned as a single batch.
*
* @param inputFiles The collections of files to scan
*/
private void scanAsBatch(BatchModeContext context, Iterable<? extends InputFile>... inputFiles) {
List<InputFile> files = new ArrayList<>();
for (Iterable<? extends InputFile> group : inputFiles) {
files.addAll(astScanner.filterModuleInfo(group).toList());
}
try {
try {
if (!files.isEmpty()) {
scanInBatches(context, files);
} else if (LOG.isInfoEnabled()) {
LOG.info("No \"{}\" source files to scan.", context.descriptor());
}
} finally {
context.endOfAnalysis();
}
} catch (AnalysisException e) {
throw e;
} catch (Exception e) {
astScanner.checkInterrupted(e);
astScannerForTests.checkInterrupted(e);
astScannerForGeneratedFiles.checkInterrupted(e);
LOG.error(BATCH_ERROR_MESSAGE, e);
if (astScanner.shouldFailAnalysis()) {
throw new AnalysisException(BATCH_ERROR_MESSAGE, e);
}
}
}
private void scanInBatches(BatchModeContext context, List<InputFile> allInputFiles) {
String logUsingBatch = String.format("Using ECJ batch to parse %d %s java source files", allInputFiles.size(), context.descriptor());
AnalysisProgress analysisProgress = new AnalysisProgress(allInputFiles.size());
long batchModeSizeInKB = sonarComponents.getBatchModeSizeInKB();
if (batchModeSizeInKB < 0L || batchModeSizeInKB >= Long.MAX_VALUE / 1_000L) {
LOG.info("{} in a single batch.", logUsingBatch);
scanBatch(context, allInputFiles, analysisProgress);
} else {
long batchSize = batchModeSizeInKB * 1_000L;
LOG.info("{} with batch size {} KB.", logUsingBatch, batchModeSizeInKB);
BatchGenerator generator = new BatchGenerator(allInputFiles.iterator(), batchSize);
while (generator.hasNext()) {
List<InputFile> batch = generator.next();
scanBatch(context, batch, analysisProgress);
}
}
}
private void scanBatch(BatchModeContext context, List<InputFile> batchFiles, AnalysisProgress analysisProgress) {
analysisProgress.startBatch(batchFiles.size());
Set<Runnable> environmentsCleaners = new HashSet<>();
JParser.Mode.BATCH
.create(javaVersion, context.getClasspath(), sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage())
.parse(batchFiles, sonarComponents::analysisCancelled, analysisProgress, (input, result) -> scanAsBatchCallback(input, result, context, environmentsCleaners));
// Due to a bug in ECJ, JAR files remain locked after the analysis on Windows, we unlock them manually, at the end of each batches. See SONARJAVA-3609.
environmentsCleaners.forEach(Runnable::run);
analysisProgress.endBatch();
}
private static void scanAsBatchCallback(InputFile inputFile, JParseResult result, BatchModeContext context, Set<Runnable> environmentsCleaners) {
JavaAstScanner scanner = context.selectScanner(inputFile);
Duration duration = PerformanceMeasure.start(context.descriptor(inputFile));
scanner.simpleScan(inputFile, result, ast ->
// In batch mode, we delay the cleaning of the environment as it will be used in later processing.
environmentsCleaners.add(ast.getEnvironmentCleaner())
);
duration.stop();
}
interface BatchModeContext {
String descriptor();
String descriptor(InputFile input);
List<File> getClasspath();
JavaAstScanner selectScanner(InputFile input);
void endOfAnalysis();
}
class AutoScanBatchContext implements BatchModeContext {
@Override
public String descriptor() {
return "Main and Test";
}
@Override
public String descriptor(InputFile input) {
return input.type() == InputFile.Type.TEST ? "Test" : "Main";
}
@Override
public List<File> getClasspath() {
return globalClasspath;
}
@Override
public JavaAstScanner selectScanner(InputFile input) {
return input.type() == InputFile.Type.TEST ? astScannerForTests : astScanner;
}
@Override
public void endOfAnalysis() {
astScanner.endOfAnalysis();
astScannerForTests.endOfAnalysis();
astScannerForGeneratedFiles.endOfAnalysis();
}
}
static class DefaultBatchModeContext implements BatchModeContext {
private final JavaAstScanner scanner;
private final String descriptor;
public DefaultBatchModeContext(JavaAstScanner scanner, String descriptor) {
this.scanner = scanner;
this.descriptor = descriptor;
}
@Override
public String descriptor() {
return descriptor;
}
@Override
public String descriptor(InputFile input) {
return descriptor;
}
@Override
public List<File> getClasspath() {
return scanner.getClasspath();
}
@Override
public JavaAstScanner selectScanner(InputFile input) {
return scanner;
}
@Override
public void endOfAnalysis() {
scanner.endOfAnalysis();
}
}
private boolean isCacheEnabled() {
return CacheContextImpl.of(sonarComponents).isCacheEnabled();
}
private boolean canOptimizeScanning() {
try {
return sonarComponents.canSkipUnchangedFiles() && isCacheEnabled();
} catch (ApiMismatchException e) {
return false;
}
}
private static <T> void scanAndMeasureTask(Iterable<T> files, Consumer<Iterable<T>> action, String descriptor) {
if (CollectionUtils.size(files) > 0) {
Duration mainDuration = PerformanceMeasure.start(descriptor);
action.accept(files);
mainDuration.stop();
} else {
LOG.info("No \"{}\" source files to scan.", descriptor);
}
}
}