Skip to content

Commit

Permalink
enable all errorprone checks by default and resolve their nit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Sep 16, 2024
1 parent dad696a commit ed32a06
Show file tree
Hide file tree
Showing 236 changed files with 1,976 additions and 1,795 deletions.
3 changes: 2 additions & 1 deletion .github/scripts/analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ set -eux
./gradlew \
forbiddenApis -DforbiddenApis \
pmdJavaPoet pmdMain pmdCodeGen pmdJmh -Dpmd \
spotbugsJavaPoet spotbugsMain spotbugsCodeGen spotbugsJmh -Dspotbugs
spotbugsJavaPoet spotbugsMain spotbugsCodeGen spotbugsJmh -Dspotbugs \
"$@"
23 changes: 23 additions & 0 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.google.common.collect.Sets
import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis
import kotlin.math.max
import net.ltgt.gradle.errorprone.errorprone
import net.ltgt.gradle.nullaway.nullaway
import org.gradle.api.tasks.PathSensitivity.RELATIVE
import org.gradle.plugins.ide.eclipse.model.Classpath as EclipseClasspath
import org.gradle.plugins.ide.eclipse.model.Library
Expand Down Expand Up @@ -78,6 +79,13 @@ 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("Varifier")
nullaway.disable()
}
}

compileJavaPoetJava.configure {
Expand Down Expand Up @@ -120,6 +128,21 @@ tasks.named<JavaCompile>("compileJava").configure {

tasks.named<JavaCompile>("compileTestJava").configure {
dependsOn(tasks.jar, compileCodeGenJava)
options.errorprone {
disable("MemberName")
disable("SystemOut")
disable("Varifier")
disable("Var")
}
}

tasks.named<JavaCompile>("compileJmhJava").configure {
options.errorprone {
disable("MemberName")
disable("SystemOut")
disable("Varifier")
disable("Var")
}
}

tasks.test.configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private void generate() throws FormatterException, IOException {
}

private void writeJavaFile() throws IOException {
String header = Resources.toString(Resources.getResource("license.txt"), UTF_8).trim();
ZoneId timeZone = ZoneId.of("America/Los_Angeles");
var header = Resources.toString(Resources.getResource("license.txt"), UTF_8).trim();
var timeZone = ZoneId.of("America/Los_Angeles");
for (TypeSpec typeSpec : factoryTypes) {
JavaFile.builder(getClass().getPackageName(), typeSpec)
.addFileComment(header, Year.now(timeZone))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void generate() throws FormatterException, IOException {

private void writeJavaFile() throws IOException {
String header = Resources.toString(Resources.getResource("license.txt"), UTF_8).trim();
ZoneId timeZone = ZoneId.of("America/Los_Angeles");
var timeZone = ZoneId.of("America/Los_Angeles");
for (TypeSpec node : nodeTypes) {
JavaFile.builder(getClass().getPackage().getName(), node)
.addFileComment(header, Year.now(timeZone))
Expand Down Expand Up @@ -185,7 +185,7 @@ private TypeSpec makeNodeSpec(String className, boolean isFinal, ImmutableSet<Fe
return context.build();
}

private Set<List<Object>> combinations() {
private static Set<List<Object>> combinations() {
var keyStrengths = Set.of(Feature.STRONG_KEYS, Feature.WEAK_KEYS);
var valueStrengths = Set.of(Feature.STRONG_VALUES, Feature.WEAK_VALUES, Feature.SOFT_VALUES);
var expireAfterAccess = Set.of(false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void execute(LocalCacheContext context) {
addWriteOrderDeque(context);
}

private void addAccessOrderWindowDeque(LocalCacheContext context) {
private static void addAccessOrderWindowDeque(LocalCacheContext context) {
if (Feature.usesAccessOrderWindowDeque(context.parentFeatures)
|| !Feature.usesAccessOrderWindowDeque(context.generateFeatures)) {
return;
Expand All @@ -52,38 +52,35 @@ private void addAccessOrderWindowDeque(LocalCacheContext context) {
"this.$L = builder.evicts() || builder.expiresAfterAccess()\n? new $T()\n: null",
"accessOrderWindowDeque", ACCESS_ORDER_DEQUE);
addFieldAndMethod(context, ACCESS_ORDER_DEQUE, "accessOrderWindowDeque");
context.suppressedWarnings.add("NullAway");
}

private void addAccessOrderMainDeque(LocalCacheContext context) {
private static void addAccessOrderMainDeque(LocalCacheContext context) {
if (Feature.usesAccessOrderMainDeque(context.parentFeatures)
|| !Feature.usesAccessOrderMainDeque(context.generateFeatures)) {
return;
}
addDeque(context, ACCESS_ORDER_DEQUE, "accessOrderProbationDeque");
addDeque(context, ACCESS_ORDER_DEQUE, "accessOrderProtectedDeque");
context.suppressedWarnings.add("NullAway");
}

private void addWriteOrderDeque(LocalCacheContext context) {
private static void addWriteOrderDeque(LocalCacheContext context) {
if (Feature.usesWriteOrderDeque(context.parentFeatures)
|| !Feature.usesWriteOrderDeque(context.generateFeatures)) {
return;
}
addDeque(context, WRITE_ORDER_DEQUE, "writeOrderDeque");
context.suppressedWarnings.add("NullAway");
}

private void addDeque(LocalCacheContext context, TypeName type, String name) {
private static void addDeque(LocalCacheContext context, TypeName type, String name) {
addConstructor(context, type, name);
addFieldAndMethod(context, type, name);
}

private void addConstructor(LocalCacheContext context, TypeName type, String name) {
private static void addConstructor(LocalCacheContext context, TypeName type, String name) {
context.constructor.addStatement("this.$L = new $T()", name, type);
}

private void addFieldAndMethod(LocalCacheContext context, TypeName type, String name) {
private static void addFieldAndMethod(LocalCacheContext context, TypeName type, String name) {
context.cache.addField(FieldSpec.builder(type, name, Modifier.FINAL).build());
context.cache.addMethod(MethodSpec.methodBuilder(name)
.addModifiers(context.protectedFinalModifiers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ public boolean applies(LocalCacheContext context) {

@Override
public void execute(LocalCacheContext context) {
context.suppressedWarnings.add("NullAway");
variableExpiration(context);
fixedExpiration(context);
}

private void fixedExpiration(LocalCacheContext context) {
private static void fixedExpiration(LocalCacheContext context) {
context.constructor.addStatement(
"this.expiresAfterAccessNanos = builder.getExpiresAfterAccessNanos()");
context.cache.addField(FieldSpec.builder(long.class, "expiresAfterAccessNanos")
Expand All @@ -63,7 +62,7 @@ private void fixedExpiration(LocalCacheContext context) {
.build());
}

private void variableExpiration(LocalCacheContext context) {
private static void variableExpiration(LocalCacheContext context) {
context.cache.addMethod(MethodSpec.methodBuilder("expiresVariable")
.addModifiers(context.protectedFinalModifiers())
.addStatement("return (timerWheel != null)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ public void execute(LocalCacheContext context) {
addValueStrength(context);
}

private void addKeyStrength(LocalCacheContext context) {
private static void addKeyStrength(LocalCacheContext context) {
if (context.generateFeatures.contains(Feature.WEAK_KEYS)) {
addStrength(context, "collectKeys", "keyReferenceQueue", kRefQueueType);
}
}

private void addValueStrength(LocalCacheContext context) {
private static void addValueStrength(LocalCacheContext context) {
if (context.generateFeatures.contains(Feature.INFIRM_VALUES)) {
addStrength(context, "collectValues", "valueReferenceQueue", vRefQueueType);
}
}

/** Adds the reference strength methods for the key or value. */
private void addStrength(LocalCacheContext context,
private static void addStrength(LocalCacheContext context,
String collectName, String queueName, TypeName type) {
context.cache.addMethod(MethodSpec.methodBuilder(queueName)
.addModifiers(context.protectedFinalModifiers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public void execute(LocalCacheContext context) {
addFrequencySketch(context);
}

private void addEvicts(LocalCacheContext context) {
private static void addEvicts(LocalCacheContext context) {
context.cache.addMethod(MethodSpec.methodBuilder("evicts")
.addModifiers(context.protectedFinalModifiers())
.addStatement("return true")
.returns(boolean.class)
.build());
}

private void addMaximumSize(LocalCacheContext context) {
private static void addMaximumSize(LocalCacheContext context) {
addField(context, long.class, "maximum");
addField(context, long.class, "weightedSize");
addField(context, long.class, "windowMaximum");
Expand All @@ -61,15 +61,15 @@ private void addMaximumSize(LocalCacheContext context) {
addField(context, long.class, "mainProtectedWeightedSize");
}

private void addHillClimber(LocalCacheContext context) {
private static void addHillClimber(LocalCacheContext context) {
addField(context, double.class, "stepSize");
addField(context, long.class, "adjustment");
addField(context, int.class, "hitsInSample");
addField(context, int.class, "missesInSample");
addField(context, double.class, "previousSampleHitRate");
}

private void addFrequencySketch(LocalCacheContext context) {
private static void addFrequencySketch(LocalCacheContext context) {
context.cache.addField(FieldSpec.builder(
FREQUENCY_SKETCH, "sketch", Modifier.FINAL).build());
context.constructor.addCode(CodeBlock.builder()
Expand All @@ -86,7 +86,7 @@ private void addFrequencySketch(LocalCacheContext context) {
.build());
}

private void addField(LocalCacheContext context, Class<?> type, String name) {
private static void addField(LocalCacheContext context, Class<?> type, String name) {
context.cache.addField(FieldSpec.builder(type, name).build());
context.cache.addMethod(MethodSpec.methodBuilder(name)
.addModifiers(context.protectedFinalModifiers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public boolean applies(LocalCacheContext context) {

@Override
public void execute(LocalCacheContext context) {
context.suppressedWarnings.add("NullAway");
context.cache.addField(
FieldSpec.builder(REMOVAL_LISTENER, "removalListener", Modifier.FINAL).build());
context.constructor.addStatement("this.removalListener = builder.getRemovalListener(async)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public void execute(LocalCacheContext context) {
addStatsCounter(context);
}

private void addIsRecording(LocalCacheContext context) {
private static void addIsRecording(LocalCacheContext context) {
context.cache.addMethod(MethodSpec.methodBuilder("isRecordingStats")
.addModifiers(context.publicFinalModifiers())
.addStatement("return true")
.returns(boolean.class)
.build());
}

private void addStatsCounter(LocalCacheContext context) {
private static void addStatsCounter(LocalCacheContext context) {
context.constructor.addStatement("this.statsCounter = builder.getStatsCounterSupplier().get()");
context.cache.addField(FieldSpec.builder(
STATS_COUNTER, "statsCounter", Modifier.FINAL).build());
Expand All @@ -60,7 +60,7 @@ private void addStatsCounter(LocalCacheContext context) {
.build());
}

private void addStatsTicker(LocalCacheContext context) {
private static void addStatsTicker(LocalCacheContext context) {
context.cache.addMethod(MethodSpec.methodBuilder("statsTicker")
.addModifiers(context.publicFinalModifiers())
.addStatement("return $T.systemTicker()", TICKER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public boolean applies(LocalCacheContext context) {

@Override
public void execute(LocalCacheContext context) {
context.suppressedWarnings.add("MissingOverride");
context.cache.superclass(context.superClass)
.addJavadoc(getJavaDoc(context))
.addTypeVariable(kTypeVar)
Expand All @@ -47,8 +46,8 @@ public void execute(LocalCacheContext context) {
}
}

private String getJavaDoc(LocalCacheContext context) {
StringBuilder doc = new StringBuilder(200);
private static String getJavaDoc(LocalCacheContext context) {
var doc = new StringBuilder(200);
doc.append("<em>WARNING: GENERATED CODE</em>\n\n"
+ "A cache that provides the following features:\n<ul>");
for (Feature feature : context.generateFeatures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.github.benmanes.caffeine.cache.Specifications.keyRefQueueSpec;
import static com.github.benmanes.caffeine.cache.Specifications.valueRefQueueSpec;
import static com.github.benmanes.caffeine.cache.Specifications.valueSpec;
import static com.github.benmanes.caffeine.cache.node.NodeContext.varHandleName;

import com.squareup.javapoet.MethodSpec;

Expand All @@ -37,14 +38,13 @@ public boolean applies(NodeContext context) {
public void execute(NodeContext context) {
addConstructorByKey(context);
addConstructorByKeyRef(context);
context.suppressedWarnings.add("NullAway");
if (context.isBaseClass()) {
context.suppressedWarnings.add("PMD.UnusedFormalParameter");
}
}

/** Adds the constructor by key to the node type. */
private void addConstructorByKey(NodeContext context) {
private static void addConstructorByKey(NodeContext context) {
context.constructorByKey.addParameter(keyRefQueueSpec);
addCommonParameters(context.constructorByKey);
if (context.isBaseClass()) {
Expand All @@ -55,7 +55,7 @@ private void addConstructorByKey(NodeContext context) {
}

/** Adds the constructor by key reference to the node type. */
private void addConstructorByKeyRef(NodeContext context) {
private static void addConstructorByKeyRef(NodeContext context) {
addCommonParameters(context.constructorByKeyRef);
if (context.isBaseClass()) {
assignKeyRefAndValue(context);
Expand All @@ -64,14 +64,14 @@ private void addConstructorByKeyRef(NodeContext context) {
}
}

private void addCommonParameters(MethodSpec.Builder constructor) {
private static void addCommonParameters(MethodSpec.Builder constructor) {
constructor.addParameter(valueSpec);
constructor.addParameter(valueRefQueueSpec);
constructor.addParameter(int.class, "weight");
constructor.addParameter(long.class, "now");
}

private void callSiblingConstructor(NodeContext context) {
private static void callSiblingConstructor(NodeContext context) {
if (context.isStrongKeys()) {
context.constructorByKey.addStatement("this(key, value, valueReferenceQueue, weight, now)");
} else {
Expand All @@ -81,25 +81,25 @@ private void callSiblingConstructor(NodeContext context) {
}
}

private void assignKeyRefAndValue(NodeContext context) {
private static void assignKeyRefAndValue(NodeContext context) {
if (context.isStrongValues()) {
context.constructorByKeyRef.addStatement("$L.set(this, $N)",
context.varHandleName("key"), "keyReference");
varHandleName("key"), "keyReference");
context.constructorByKeyRef.addStatement("$L.set(this, $N)",
context.varHandleName("value"), "value");
varHandleName("value"), "value");
} else {
context.constructorByKeyRef.addStatement("$L.set(this, new $T($N, $N, $N))",
context.varHandleName("value"), context.valueReferenceType(),
varHandleName("value"), context.valueReferenceType(),
"keyReference", "value", "valueReferenceQueue");
}
}

private void callParentByKey(NodeContext context) {
private static void callParentByKey(NodeContext context) {
context.constructorByKey.addStatement(
"super(key, keyReferenceQueue, value, valueReferenceQueue, weight, now)");
}

private void callParentByKeyRef(NodeContext context) {
private static void callParentByKeyRef(NodeContext context) {
context.constructorByKeyRef.addStatement(
"super(keyReference, value, valueReferenceQueue, weight, now)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void execute(NodeContext context) {
}

/** Adds a simple field, accessor, and mutator for the variable. */
private void addFieldAndGetter(NodeContext context, String varName) {
private static void addFieldAndGetter(NodeContext context, String varName) {
context.nodeSubtype.addField(NODE, varName)
.addMethod(context.newGetter(Strength.STRONG, NODE, varName, Visibility.VOLATILE))
.addMethod(context.newSetter(NODE, varName, Visibility.VOLATILE));
Expand Down
Loading

0 comments on commit ed32a06

Please sign in to comment.