Repository navigation
Expand file tree
/
Copy pathJavaAstScanner.java
More file actions
265 lines (234 loc) · 10.5 KB
/
Copy pathJavaAstScanner.java
File metadata and controls
265 lines (234 loc) · 10.5 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
/*
* 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.ast;
import com.sonar.sslr.api.RecognitionException;
import java.io.File;
import java.io.InterruptedIOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.java.AnalysisException;
import org.sonar.java.AnalysisProgress;
import org.sonar.java.SonarComponents;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.model.InputFileUtils;
import org.sonar.java.model.JParserConfig;
import org.sonar.java.model.JProblem;
import org.sonar.java.model.JavaTree;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.java.telemetry.NoOpTelemetry;
import org.sonar.java.telemetry.Telemetry;
import org.sonar.java.telemetry.TelemetryKey;
import org.sonar.java.telemetry.TelemetryKey.JavaAnalysisKeys;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
import static java.lang.System.currentTimeMillis;
public class JavaAstScanner {
private static final Logger LOG = LoggerFactory.getLogger(JavaAstScanner.class);
private static final String LOG_WARN_MISCONFIGURED_JAVA_VERSION = "Analyzing '%s' file with misconfigured Java version."
+ " Please check that property '%s' is correctly configured (currently set to: %d) or exclude 'module-info.java' files from analysis."
+ " Such files only exist in Java9+ projects.";
private final SonarComponents sonarComponents;
private final Telemetry telemetry;
private final JavaAnalysisKeys javaAnalysisKeys;
private VisitorsBridge visitor;
private boolean reportedMisconfiguredVersion = false;
public JavaAstScanner(@Nullable SonarComponents sonarComponents, Telemetry telemetry, JavaAnalysisKeys javaAnalysisKeys) {
this.sonarComponents = sonarComponents;
this.telemetry = telemetry;
this.javaAnalysisKeys = javaAnalysisKeys;
}
public List<File> getClasspath() {
return visitor.getClasspath();
}
/**
* Attempt to scan files without parsing, using the raw input file and cached information.
*
* @param inputFiles The list of files to analyze
* @return A map with 2 lists of inputFiles. Under the {@code true} key, files that have successfully been scanned without parsing and,
* under the {@code false} key, files that need to be parsed for further analysis.
*/
public Map<Boolean, List<InputFile>> scanWithoutParsing(Iterable<? extends InputFile> inputFiles) {
return StreamSupport.stream(inputFiles.spliterator(), false)
// Split files between successfully scanned without parsing and failed to scan without parsing
.collect(Collectors.partitioningBy(visitor::scanWithoutParsing));
}
public void scan(Iterable<? extends InputFile> inputFiles) {
scan(inputFiles, compilationUnitTree -> {});
}
/**
* Scan the given files and modify
*
* @param inputFiles The list of files to analyze
* @param modifyCompilationUnit allow you to modify the ast before running the analysis on it, for example to remove semantic information
*/
@VisibleForTesting
public void scanForTesting(Iterable<? extends InputFile> inputFiles, Consumer<CompilationUnitTree> modifyCompilationUnit) {
scan(inputFiles, modifyCompilationUnit);
}
private void scan(Iterable<? extends InputFile> inputFiles, Consumer<CompilationUnitTree> modifyCompilationUnit) {
List<? extends InputFile> filesNames = filterModuleInfo(inputFiles).toList();
AnalysisProgress analysisProgress = new AnalysisProgress(filesNames.size());
try {
boolean shouldIgnoreUnnamedModuleForSplitPacakge = sonarComponents != null &&
sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage();
JParserConfig.Mode.FILE_BY_FILE
.create(visitor.getJavaVersion(), visitor.getClasspath(), shouldIgnoreUnnamedModuleForSplitPacakge)
.parse(filesNames,
this::analysisCancelled,
analysisProgress,
(i, r) -> simpleScan(i, r,
// Due to a bug in ECJ, JAR files remain locked after the analysis on Windows, we unlock them manually. See SONARJAVA-3609.
JavaAstScanner::cleanUpAst,
modifyCompilationUnit));
} finally {
endOfAnalysis();
}
}
public <T extends InputFile> Stream<T> filterModuleInfo(Iterable<T> inputFiles) {
JavaVersion javaVersion = visitor.getJavaVersion();
return StreamSupport.stream(inputFiles.spliterator(), false)
.filter(file -> {
if (("module-info.java".equals(file.filename())) && !javaVersion.isNotSet() && javaVersion.asInt() <= 8) {
// When the java version is not set, we use the maximum version supported, able to parse module info.
logMisconfiguredVersion("module-info.java", javaVersion);
return false;
}
return true;
});
}
public void endOfAnalysis() {
visitor.endOfAnalysis();
logUndefinedTypes();
}
private void logUndefinedTypes() {
if (sonarComponents != null) {
sonarComponents.logUndefinedTypes();
}
}
private boolean analysisCancelled() {
return sonarComponents != null && sonarComponents.analysisCancelled();
}
public void simpleScan(InputFile inputFile, JParserConfig.Result result, Consumer<JavaTree.CompilationUnitTreeImpl> cleanUp) {
simpleScan(inputFile, result, cleanUp, compilationUnitTree -> {});
}
// modifyCompilationUnit should be used for testing.
public void simpleScan(InputFile inputFile, JParserConfig.Result result, Consumer<JavaTree.CompilationUnitTreeImpl> cleanUp,
Consumer<CompilationUnitTree> modifyCompilationUnit) {
long startTime = currentTimeMillis();
visitor.setCurrentFile(inputFile);
var telemetryAnalysisKeys = javaAnalysisKeys.exceptions();
try {
JavaTree.CompilationUnitTreeImpl ast = result.get();
modifyCompilationUnit.accept(ast);
visitor.visitFile(ast, sonarComponents != null && sonarComponents.fileCanBeSkipped(inputFile));
String path = inputFile.toString();
Set<JProblem> undefinedTypes = ast.undefinedTypes();
collectUndefinedTypes(path, undefinedTypes);
cleanUp.accept(ast);
telemetryAnalysisKeys = javaAnalysisKeys.success();
telemetry.aggregateAsCounter(javaAnalysisKeys.success().typeErrorCountKey(), undefinedTypes.size());
} catch (RecognitionException e) {
checkInterrupted(e);
LOG.error("Unable to parse source file : '{}'", inputFile);
LOG.error(e.getMessage());
parseErrorWalkAndVisit(e, inputFile);
telemetryAnalysisKeys = javaAnalysisKeys.parseErrors();
} catch (AnalysisException e) {
throw e;
} catch (Exception e) {
checkInterrupted(e);
interruptIfFailFast(e, inputFile);
} catch (StackOverflowError error) {
LOG.error("A stack overflow error occurred while analyzing file: '" + inputFile + "'", error);
if (sonarComponents == null || sonarComponents.shouldFailOnStackOverflow()) {
throw error;
}
} finally {
telemetry.aggregateAsCounter(telemetryAnalysisKeys.sizeCharsKey(), InputFileUtils.charCount(inputFile, 0));
telemetry.aggregateAsCounter(telemetryAnalysisKeys.timeMsKey(), currentTimeMillis() - startTime);
}
}
private static void cleanUpAst(JavaTree.CompilationUnitTreeImpl ast) {
// release environment used for semantic resolution
ast.sema.getEnvironmentCleaner().run();
}
private void collectUndefinedTypes(String path, Set<JProblem> undefinedTypes) {
if (sonarComponents != null) {
sonarComponents.collectUndefinedTypes(path, undefinedTypes);
}
}
void logMisconfiguredVersion(String inputFile, JavaVersion javaVersion) {
if (!reportedMisconfiguredVersion) {
if (LOG.isWarnEnabled()) {
LOG.warn(String.format(LOG_WARN_MISCONFIGURED_JAVA_VERSION, inputFile, JavaVersion.SOURCE_VERSION, javaVersion.asInt()));
}
reportedMisconfiguredVersion = true;
}
}
private void interruptIfFailFast(Exception e, InputFile inputFile) {
if (shouldFailAnalysis()) {
throw new AnalysisException(getAnalysisExceptionMessage(inputFile), e);
}
}
public boolean shouldFailAnalysis() {
return sonarComponents != null && sonarComponents.shouldFailAnalysisOnException();
}
public void checkInterrupted(Exception e) {
Throwable cause = ExceptionUtils.getRootCause(e);
if (cause instanceof InterruptedException
|| cause instanceof InterruptedIOException
|| cause instanceof CancellationException
|| analysisCancelled()) {
throw new AnalysisException("Analysis cancelled", e);
}
}
private void parseErrorWalkAndVisit(RecognitionException e, InputFile inputFile) {
try {
visitor.processRecognitionException(e, inputFile);
} catch (Exception e2) {
throw new AnalysisException(getAnalysisExceptionMessage(inputFile), e2);
}
}
private static String getAnalysisExceptionMessage(InputFile file) {
return "Unable to analyze file : '" + file + "'";
}
public void setVisitorBridge(VisitorsBridge visitor) {
this.visitor = visitor;
}
@VisibleForTesting
public static void scanSingleFileForTests(InputFile file, VisitorsBridge visitorsBridge) {
scanSingleFileForTests(file, visitorsBridge, null);
}
@VisibleForTesting
public static void scanSingleFileForTests(InputFile inputFile, VisitorsBridge visitorsBridge, @Nullable SonarComponents sonarComponents) {
JavaAstScanner astScanner = new JavaAstScanner(sonarComponents, new NoOpTelemetry(), TelemetryKey.JAVA_ANALYSIS_MAIN);
astScanner.setVisitorBridge(visitorsBridge);
astScanner.scan(Collections.singleton(inputFile));
}
}