Skip to content

Commit

Permalink
include method parameters in class file for use by static analyzers
Browse files Browse the repository at this point in the history
This allows errorprone to now detects ArgumentSelectionDefectChecker,
BooleanParameter, and ParameterName warnings (now resolved). The jar
size changes are negligible because we exclude this addition in our
generated code where it is not necessary.
  • Loading branch information
ben-manes committed Nov 7, 2024
1 parent 8b835c9 commit 2d4e079
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 92 deletions.
18 changes: 11 additions & 7 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ dependencies {
val compileCodeGenJava by tasks.existing(JavaCompile::class) {
classpath = sourceSets["main"].runtimeClasspath + sourceSets["main"].output
dependsOn(tasks.compileJava)
options.isDebug = false

options.errorprone {
disable("FieldMissingNullable")
disable("MissingOverride")
disable("MemberName")
disable("Varifier")
nullaway.disable()
options.apply {
compilerArgs.remove("-parameters")
isDebug = false

errorprone {
disable("FieldMissingNullable")
disable("MissingOverride")
disable("MemberName")
disable("Varifier")
nullaway.disable()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void reformat() throws IOException {
.filter(path -> path.endsWith(".java"))
.collect(toImmutableList());
ToolProvider.findFirst("google-java-format").ifPresent(formatter -> {
int result = formatter.run(System.err, System.out,
int result = formatter.run(System.out, System.err,
Stream.concat(Stream.of("-i"), files.stream()).toArray(String[]::new));
checkState(result == 0, "Java formatting failed with %s exit code", result);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void reformat() throws IOException {
.filter(path -> path.endsWith(".java"))
.collect(toImmutableList());
ToolProvider.findFirst("google-java-format").ifPresent(formatter -> {
int result = formatter.run(System.err, System.out,
int result = formatter.run(System.out, System.err,
Stream.concat(Stream.of("-i"), files.stream()).toArray(String[]::new));
checkState(result == 0, "Java formatting failed with %s exit code", result);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ default void handleCompletion(K key, CompletableFuture<? extends V> valueFuture,
long startTime, boolean recordMiss) {
var completed = new AtomicBoolean();
valueFuture.whenComplete((value, error) -> {
if (!completed.compareAndSet(false, true)) {
if (!completed.compareAndSet(/* expectedValue= */ false, /* newValue= */ true)) {
// Ignore multiple invocations due to ForkJoinPool retrying on delays
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2562,7 +2562,8 @@ public void cacheFactory_noSuchMethod() {
public void cacheFactory_brokenConstructor() {
var builder = Caffeine.newBuilder();
var factory = LocalCacheFactory.loadFactory("BoundedLocalCacheTest$BadBoundedLocalCache");
assertThrows(IllegalStateException.class, () -> factory.newInstance(builder, null, false));
assertThrows(IllegalStateException.class, () -> factory.newInstance(builder,
/* cacheLoader= */ null, /* isAsync= */ false));
}

@Test(groups = "isolated")
Expand Down Expand Up @@ -2650,7 +2651,7 @@ public void nodeFactory_badConstructor() {
@Test
public void nodeFactory_classNotFound() {
var expected = assertThrows(IllegalStateException.class, () ->
NodeFactory.loadFactory(/* className */ ""));
NodeFactory.loadFactory(/* className= */ ""));
assertThat(expected).hasCauseThat().isInstanceOf(ClassNotFoundException.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class CaffeineSpecGuavaTest extends TestCase {

public void testParse_empty() {
CaffeineSpec spec = parse("");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterAccess);
Expand All @@ -52,9 +52,9 @@ public void testParse_empty() {

public void testParse_initialCapacity() {
CaffeineSpec spec = parse("initialCapacity=10");
assertEquals(10, spec.initialCapacity);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(10);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWrite);
Expand All @@ -71,8 +71,8 @@ public void testParse_initialCapacityRepeated() {

public void testParse_maximumSize() {
CaffeineSpec spec = parse("maximumSize=9000");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(9000, spec.maximumSize);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(9000);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWrite);
Expand All @@ -88,8 +88,8 @@ public void testParse_maximumSizeRepeated() {

public void testParse_maximumWeight() {
CaffeineSpec spec = parse("maximumWeight=9000");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(9000, spec.maximumWeight);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(9000);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWrite);
Expand All @@ -109,10 +109,10 @@ public void testParse_maximumSizeAndMaximumWeight() {

public void testParse_weakKeys() {
CaffeineSpec spec = parse("weakKeys");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertEquals(Strength.WEAK, spec.keyStrength);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertThat(spec.keyStrength).isEqualTo(Strength.WEAK);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWrite);
assertNull(spec.expireAfterAccess);
Expand All @@ -131,11 +131,11 @@ public void testParse_repeatedKeyStrength() {

public void testParse_softValues() {
CaffeineSpec spec = parse("softValues");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertEquals(Strength.SOFT, spec.valueStrength);
assertThat(spec.valueStrength).isEqualTo(Strength.SOFT);
assertNull(spec.expireAfterWrite);
assertNull(spec.expireAfterAccess);
assertNull(spec.refreshAfterWrite);
Expand All @@ -149,11 +149,11 @@ public void testParse_softValuesCannotHaveValue() {

public void testParse_weakValues() {
CaffeineSpec spec = parse("weakValues");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertEquals(Strength.WEAK, spec.valueStrength);
assertThat(spec.valueStrength).isEqualTo(Strength.WEAK);
assertNull(spec.expireAfterWrite);
assertNull(spec.expireAfterAccess);
assertNull(spec.refreshAfterWrite);
Expand All @@ -177,12 +177,12 @@ public void testParse_repeatedValueStrength() {

public void testParse_writeExpirationDays() {
CaffeineSpec spec = parse("expireAfterWrite=10d");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertEquals(Duration.ofDays(10), spec.expireAfterWrite);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofDays(10));
assertNull(spec.expireAfterAccess);
assertNull(spec.refreshAfterWrite);
assertCaffeineEquivalence(
Expand All @@ -191,21 +191,21 @@ public void testParse_writeExpirationDays() {

public void testParse_writeExpirationHours() {
CaffeineSpec spec = parse("expireAfterWrite=150h");
assertEquals(Duration.ofHours(150), spec.expireAfterWrite);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofHours(150));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterWrite(150L, TimeUnit.HOURS), Caffeine.from(spec));
}

public void testParse_writeExpirationMinutes() {
CaffeineSpec spec = parse("expireAfterWrite=10m");
assertEquals(Duration.ofMinutes(10), spec.expireAfterWrite);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofMinutes(10));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterWrite(10L, TimeUnit.MINUTES), Caffeine.from(spec));
}

public void testParse_writeExpirationSeconds() {
CaffeineSpec spec = parse("expireAfterWrite=10s");
assertEquals(Duration.ofSeconds(10), spec.expireAfterWrite);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofSeconds(10));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterWrite(10L, TimeUnit.SECONDS), Caffeine.from(spec));
}
Expand All @@ -217,35 +217,35 @@ public void testParse_writeExpirationRepeated() {

public void testParse_accessExpirationDays() {
CaffeineSpec spec = parse("expireAfterAccess=10d");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertThat(spec.initialCapacity).isEqualTo(UNSET_INT);
assertThat(spec.maximumSize).isEqualTo(UNSET_INT);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWrite);
assertEquals(Duration.ofDays(10), spec.expireAfterAccess);
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofDays(10));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterAccess(10L, TimeUnit.DAYS), Caffeine.from(spec));
}

public void testParse_accessExpirationHours() {
CaffeineSpec spec = parse("expireAfterAccess=150h");
assertEquals(Duration.ofHours(150), spec.expireAfterAccess);
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofHours(150));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterAccess(150L, TimeUnit.HOURS), Caffeine.from(spec));
}

public void testParse_accessExpirationMinutes() {
CaffeineSpec spec = parse("expireAfterAccess=10m");
assertEquals(Duration.ofMinutes(10), spec.expireAfterAccess);
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofMinutes(10));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterAccess(10L, TimeUnit.MINUTES),
Caffeine.from(spec));
}

public void testParse_accessExpirationSeconds() {
CaffeineSpec spec = parse("expireAfterAccess=10s");
assertEquals(Duration.ofSeconds(10), spec.expireAfterAccess);
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofSeconds(10));
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterAccess(10L, TimeUnit.SECONDS),
Caffeine.from(spec));
Expand All @@ -272,8 +272,8 @@ public void testParse_recordStatsRepeated() {

public void testParse_accessExpirationAndWriteExpiration() {
CaffeineSpec spec = parse("expireAfterAccess=10s,expireAfterWrite=9m");
assertEquals(Duration.ofMinutes(9), spec.expireAfterWrite);
assertEquals(Duration.ofSeconds(10), spec.expireAfterAccess);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofMinutes(9));
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofSeconds(10));
assertCaffeineEquivalence(
Caffeine.newBuilder()
.expireAfterAccess(10L, TimeUnit.SECONDS)
Expand All @@ -284,13 +284,13 @@ public void testParse_accessExpirationAndWriteExpiration() {
public void testParse_multipleKeys() {
CaffeineSpec spec = parse("initialCapacity=10,maximumSize=20,"
+ "weakKeys,weakValues,expireAfterAccess=10m,expireAfterWrite=1h");
assertEquals(10, spec.initialCapacity);
assertEquals(20, spec.maximumSize);
assertEquals(spec.maximumWeight, UNSET_INT);
assertEquals(Strength.WEAK, spec.keyStrength);
assertEquals(Strength.WEAK, spec.valueStrength);
assertEquals(Duration.ofHours(1), spec.expireAfterWrite);
assertEquals(Duration.ofMinutes(10), spec.expireAfterAccess);
assertThat(spec.initialCapacity).isEqualTo(10);
assertThat(spec.maximumSize).isEqualTo(20);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertThat(spec.keyStrength).isEqualTo(Strength.WEAK);
assertThat(spec.valueStrength).isEqualTo(Strength.WEAK);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofHours(1));
assertThat(spec.expireAfterAccess).isEqualTo(Duration.ofMinutes(10));
Caffeine<?, ?> expected = Caffeine.newBuilder()
.initialCapacity(10)
.maximumSize(20)
Expand All @@ -304,12 +304,12 @@ public void testParse_multipleKeys() {
public void testParse_whitespaceAllowed() {
CaffeineSpec spec = parse(" initialCapacity=10,\nmaximumSize=20,\t\r"
+ "weakKeys \t ,softValues \n , \r expireAfterWrite \t = 15s\n\n");
assertEquals(10, spec.initialCapacity);
assertEquals(20, spec.maximumSize);
assertEquals(spec.maximumWeight, UNSET_INT);
assertEquals(Strength.WEAK, spec.keyStrength);
assertEquals(Strength.SOFT, spec.valueStrength);
assertEquals(Duration.ofSeconds(15), spec.expireAfterWrite);
assertThat(spec.initialCapacity).isEqualTo(10);
assertThat(spec.maximumSize).isEqualTo(20);
assertThat(spec.maximumWeight).isEqualTo(UNSET_INT);
assertThat(spec.keyStrength).isEqualTo(Strength.WEAK);
assertThat(spec.valueStrength).isEqualTo(Strength.SOFT);
assertThat(spec.expireAfterWrite).isEqualTo(Duration.ofSeconds(15));
assertNull(spec.expireAfterAccess);
Caffeine<?, ?> expected = Caffeine.newBuilder()
.initialCapacity(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void eviction() {

RemovalListener<Long, Val> listener = (k, v, removalCause) -> {
assertThat(v.key).isEqualTo(k);
if (!v.live.compareAndSet(true, false)) {
if (!v.live.compareAndSet(/* expectedValue= */ true, /* newValue= */ false)) {
throw new RuntimeException(String.format(US,
"listener called more than once! k=%s, v=%s, removalCause=%s", k, v, removalCause));
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public void clear() {

RemovalListener<Long, Val> listener = (k, v, removalCause) -> {
assertThat(v.key).isEqualTo(k);
if (!v.live.compareAndSet(true, false)) {
if (!v.live.compareAndSet(/* expectedValue= */ true, /* newValue= */ false)) {
throw new RuntimeException(String.format(US,
"listener called more than once! k=%s, v=%s, removalCause=%s", k, v, removalCause));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
.forEach(logger -> logger.setEnabledLevels(TRACE_LEVELS));
TestLoggerFactory.clear();

if (beforeCleanup.get() || !beforeCleanup.compareAndSet(false, true)) {
if (beforeCleanup.get()
|| !beforeCleanup.compareAndSet(/* expectedValue= */ false, /* newValue= */ true)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@ void checkEmpty(BlockingQueue<?> q) {
assertNull(q.peek());
assertNull(q.poll());
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
assertEquals(q.toString(), "[]");
assertEquals("[]" ,q.toString());
assertTrue(Arrays.equals(q.toArray(), new Object[0]));
assertFalse(q.iterator().hasNext());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bouncycastle-jdk18on = "1.79"
cache2k = "2.6.1.Final"
caffeine = "3.1.8"
checker-framework = "3.48.2"
checkstyle = "10.20.0"
checkstyle = "10.20.1"
coherence = "22.06.2"
commons-collections4 = "4.4"
commons-compress = "1.27.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ tasks.withType<JavaCompile>().configureEach {
}

options.compilerArgs.addAll(listOf("-Xlint:all", "-Xlint:-auxiliaryclass", "-Xlint:-classfile",
"-Xlint:-exports", "-Xlint:-processing", "-Xlint:-removal", "-Xlint:-requires-automatic"))
"-Xlint:-exports", "-Xlint:-processing", "-Xlint:-removal", "-Xlint:-requires-automatic",
"-parameters"))
if (isCI()) {
options.compilerArgs.add("-Werror")
}
if (javaVersion.canCompileOrRun(21)) {
options.compilerArgs.add("-proc:full")
}
Expand Down
Loading

0 comments on commit 2d4e079

Please sign in to comment.