Skip to content

Commit cf2b282

Browse files
asya-vorobevaclaude
andcommitted
SONARJAVA-4647: Fix cache misses for default-package and cache-hit files
- scanFile now always writes to cache (empty string for default package), so unchanged files without a package declaration are correctly skipped via scanWithoutParsing on subsequent runs - scanWithoutParsing now calls copyFromPrevious to propagate the cache entry to the write cache, preventing it from disappearing after a hit - Empty package name is excluded from badPackageNames in both paths Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a5ed94a commit cf2b282

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/naming/BadPackageNameCheck.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,27 @@ public class BadPackageNameCheck implements JavaFileScanner, EndOfAnalysis {
4848

4949
@Override
5050
public boolean scanWithoutParsing(InputFileScannerContext context) {
51-
var bytes = context.getCacheContext().getReadCache().readBytes(CACHE_KEY_PREFIX + context.getInputFile().key());
51+
var cacheKey = CACHE_KEY_PREFIX + context.getInputFile().key();
52+
var bytes = context.getCacheContext().getReadCache().readBytes(cacheKey);
5253
if (bytes == null) {
5354
return false;
5455
}
55-
handlePackageName(new String(bytes, StandardCharsets.UTF_8));
56+
context.getCacheContext().getWriteCache().copyFromPrevious(cacheKey);
57+
String name = new String(bytes, StandardCharsets.UTF_8);
58+
if (!name.isEmpty()) {
59+
handlePackageName(name);
60+
}
5661
return true;
5762
}
5863

5964
@Override
6065
public void scanFile(JavaFileScannerContext context) {
6166
var packageDeclaration = context.getTree().packageDeclaration();
62-
if (packageDeclaration != null) {
63-
String name = PackageUtils.packageName(packageDeclaration, ".");
64-
if (context.getCacheContext().isCacheEnabled()) {
65-
context.getCacheContext().getWriteCache().write(CACHE_KEY_PREFIX + context.getInputFile().key(), name.getBytes(StandardCharsets.UTF_8));
66-
}
67+
String name = packageDeclaration != null ? PackageUtils.packageName(packageDeclaration, ".") : "";
68+
if (context.getCacheContext().isCacheEnabled()) {
69+
context.getCacheContext().getWriteCache().write(CACHE_KEY_PREFIX + context.getInputFile().key(), name.getBytes(StandardCharsets.UTF_8));
70+
}
71+
if (!name.isEmpty()) {
6772
handlePackageName(name);
6873
}
6974
}

java-checks/src/test/java/org/sonar/java/checks/naming/BadPackageNameCheckTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import org.sonar.java.checks.verifier.internal.InternalReadCache;
2525
import org.sonar.java.checks.verifier.internal.InternalWriteCache;
2626

27+
import static org.assertj.core.api.Assertions.assertThat;
2728
import static org.mockito.ArgumentMatchers.any;
2829
import static org.mockito.Mockito.spy;
2930
import static org.mockito.Mockito.times;
3031
import static org.mockito.Mockito.verify;
32+
import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
3133

3234
class BadPackageNameCheckTest {
3335

@@ -99,6 +101,31 @@ void caching() {
99101

100102
verify(check, times(0)).scanFile(any());
101103
verify(check, times(1)).scanWithoutParsing(any());
104+
assertThat(writeCache2.getData()).containsExactlyInAnyOrderEntriesOf(writeCache.getData());
105+
}
106+
107+
@Test
108+
void caching_default_package() {
109+
String defaultPackageFile = mainCodeSourcesPath("DefaultPackage.java");
110+
111+
CheckVerifier.newVerifier()
112+
.onFile(defaultPackageFile)
113+
.withCheck(new BadPackageNameCheck())
114+
.withCache(readCache, writeCache)
115+
.verifyNoIssues();
116+
117+
var check = spy(new BadPackageNameCheck());
118+
var populatedReadCache = new InternalReadCache().putAll(writeCache);
119+
var writeCache2 = new InternalWriteCache().bind(populatedReadCache);
120+
CheckVerifier.newVerifier()
121+
.withCache(populatedReadCache, writeCache2)
122+
.addFiles(InputFile.Status.SAME, defaultPackageFile)
123+
.withCheck(check)
124+
.verifyNoIssues();
125+
126+
verify(check, times(0)).scanFile(any());
127+
verify(check, times(1)).scanWithoutParsing(any());
128+
assertThat(writeCache2.getData()).containsExactlyInAnyOrderEntriesOf(writeCache.getData());
102129
}
103130

104131
@Test

0 commit comments

Comments
 (0)