Skip to content

Commit b097d2b

Browse files
SONARJAVA-4983 improve logging for partial analysis (#4932)
1 parent c9dd717 commit b097d2b

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

java-frontend/src/main/java/org/sonar/java/SonarComponents.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ private void registerCheckClasses(List<JavaCheck> destinationList, Checks<JavaCh
341341
.sorted(Comparator.comparing(check -> classIndexes.getOrDefault(check.getClass(), Integer.MAX_VALUE)))
342342
.toList();
343343
destinationList.addAll(orderedChecks);
344+
if (LOG.isDebugEnabled()) {
345+
LOG.debug("Registered check: [{}]",
346+
orderedChecks.stream()
347+
.map(c -> c.getClass().getSimpleName() + " (" + createdChecks.ruleKey(c) + ")")
348+
.collect(Collectors.joining(", ")));
349+
}
344350
jspChecks.addAll(orderedChecks.stream().filter(JspCodeVisitor.class::isInstance).toList());
345351
}
346352

java-frontend/src/main/java/org/sonar/java/model/VisitorsBridge.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,21 @@ public boolean scanWithoutParsing(InputFile inputFile) {
181181
if (sonarComponents != null && sonarComponents.fileCanBeSkipped(inputFile)) {
182182
PerformanceMeasure.Duration duration = PerformanceMeasure.start("ScanWithoutParsing");
183183
boolean allScansSucceeded = true;
184+
185+
List<JavaFileScanner> scannersRequiringParsing = new ArrayList<>();
186+
List<JavaFileScanner> scannersNotRequiringParsing = new ArrayList<>();
187+
184188
var fileScannerContext = createScannerContext(sonarComponents, inputFile, javaVersion, inAndroidContext, cacheContext);
185189
for (var scanner: scannersThatCannotBeSkipped) {
186190
boolean exceptionIsBlownUp = false;
187191
PerformanceMeasure.Duration scannerDuration = PerformanceMeasure.start(scanner);
188192
try {
189-
allScansSucceeded &= scanner.scanWithoutParsing(fileScannerContext);
193+
if (scanner.scanWithoutParsing(fileScannerContext)) {
194+
scannersNotRequiringParsing.add(scanner);
195+
} else {
196+
scannersRequiringParsing.add(scanner);
197+
allScansSucceeded = false;
198+
}
190199
} catch (AnalysisException e) {
191200
// In the case where the IssuableSubscriptionVisitorsRunner throws an exception, the problem has already been
192201
// logged and the exception formatted.
@@ -210,6 +219,10 @@ public boolean scanWithoutParsing(InputFile inputFile) {
210219
}
211220
}
212221
duration.stop();
222+
223+
LOG.trace("Scanners that do not require parsing of {}: {}", inputFile, scannersNotRequiringParsing);
224+
LOG.debug("Scanners that require parsing of {}: {}", inputFile, scannersRequiringParsing);
225+
213226
return allScansSucceeded;
214227
} else {
215228
return false;

java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,33 @@ void test_sonar_components() {
226226
assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
227227
}
228228

229+
@Test
230+
void verify_registration_logging_doesnt_trigger_on_info_level() {
231+
logTester.setLevel(Level.INFO);
232+
233+
JavaCheck expectedCheck = new CustomCheck();
234+
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
235+
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null,
236+
null, this.checkFactory, context.activeRules(), new CheckRegistrar[]{expectedRegistrar});
237+
sonarComponents.setSensorContext(context);
238+
239+
assertThat(logTester.getLogs()).isEmpty();
240+
}
241+
242+
@Test
243+
void verify_registration_logging() {
244+
logTester.setLevel(Level.DEBUG);
245+
246+
JavaCheck expectedCheck = new CustomCheck();
247+
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
248+
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null,
249+
null, this.checkFactory, context.activeRules(), new CheckRegistrar[]{expectedRegistrar});
250+
sonarComponents.setSensorContext(context);
251+
252+
assertThat(logTester.getLogs()).hasSize(2);
253+
assertThat(logTester.getLogs().get(0).getRawMsg()).isEqualTo("Registered check: [{}]");
254+
}
255+
229256
@Test
230257
void creation_of_custom_checks() {
231258
JavaCheck expectedCheck = new CustomCheck();

0 commit comments

Comments
 (0)