Skip to content

Commit

Permalink
resolve and remove some of the suppressed warnings
Browse files Browse the repository at this point in the history
The code generator required changing how the context was passed around
so that it was not nullable. Otherwise minor touchups and deletion of
suppressions that are not reproducable.
  • Loading branch information
ben-manes committed Aug 26, 2024
1 parent 02ce975 commit 1b2fcf4
Show file tree
Hide file tree
Showing 45 changed files with 523 additions and 577 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public final class LocalCacheFactoryGenerator {
private final List<TypeSpec> factoryTypes;
private final Path directory;

@SuppressWarnings("NullAway.Init")
private LocalCacheFactoryGenerator(Path directory) {
this.directory = requireNonNull(directory);
this.factoryTypes = new ArrayList<>();
Expand Down Expand Up @@ -126,7 +125,7 @@ private void reformat() throws FormatterException, IOException {
}

private void generateLocalCaches() {
NavigableMap<String, Set<Feature>> classNameToFeatures = getClassNameToFeatures();
NavigableMap<String, ImmutableSet<Feature>> classNameToFeatures = getClassNameToFeatures();
classNameToFeatures.forEach((className, features) -> {
String higherKey = classNameToFeatures.higherKey(className);
boolean isLeaf = (higherKey == null) || !higherKey.startsWith(className);
Expand All @@ -135,17 +134,17 @@ private void generateLocalCaches() {
});
}

private NavigableMap<String, Set<Feature>> getClassNameToFeatures() {
var classNameToFeatures = new TreeMap<String, Set<Feature>>();
private NavigableMap<String, ImmutableSet<Feature>> getClassNameToFeatures() {
var classNameToFeatures = new TreeMap<String, ImmutableSet<Feature>>();
for (List<Object> combination : combinations()) {
Set<Feature> features = getFeatures(combination);
ImmutableSet<Feature> features = getFeatures(combination);
String className = encode(Feature.makeClassName(features));
classNameToFeatures.put(className, features);
}
return classNameToFeatures;
}

private Set<Feature> getFeatures(List<Object> combination) {
private ImmutableSet<Feature> getFeatures(List<Object> combination) {
var features = new LinkedHashSet<Feature>();
features.add(((Boolean) combination.get(0)) ? Feature.STRONG_KEYS : Feature.WEAK_KEYS);
features.add(((Boolean) combination.get(1)) ? Feature.STRONG_VALUES : Feature.INFIRM_VALUES);
Expand All @@ -157,36 +156,38 @@ private Set<Feature> getFeatures(List<Object> combination) {
if (features.contains(Feature.MAXIMUM_WEIGHT)) {
features.remove(Feature.MAXIMUM_SIZE);
}
return features;
return ImmutableSet.copyOf(features);
}

private Set<List<Object>> combinations() {
List<Set<Boolean>> sets = Collections.nCopies(featureByIndex.length, Set.of(true, false));
return Sets.cartesianProduct(sets);
}

@SuppressWarnings("NullAway")
private TypeSpec makeLocalCacheSpec(String className, boolean isFinal, Set<Feature> features) {
private TypeSpec makeLocalCacheSpec(String className,
boolean isFinal, ImmutableSet<Feature> features) {
TypeName superClass;
Set<Feature> parentFeatures;
Set<Feature> generateFeatures;
ImmutableSet<Feature> parentFeatures;
ImmutableSet<Feature> generateFeatures;
if (features.size() == 2) {
parentFeatures = Set.of();
parentFeatures = ImmutableSet.of();
generateFeatures = features;
superClass = BOUNDED_LOCAL_CACHE;
} else {
parentFeatures = ImmutableSet.copyOf(Iterables.limit(features, features.size() - 1));
generateFeatures = ImmutableSet.of(Iterables.getLast(features));
generateFeatures = ImmutableSet.of(features.asList().get(features.size() - 1));
superClass = ParameterizedTypeName.get(ClassName.bestGuess(
encode(Feature.makeClassName(parentFeatures))), kTypeVar, vTypeVar);
}

var context = new LocalCacheContext(superClass,
className, isFinal, parentFeatures, generateFeatures);
for (LocalCacheRule rule : rules) {
rule.accept(context);
if (rule.applies(context)) {
rule.execute(context);
}
}
return context.cache.build();
return context.build();
}

/** Returns an encoded form of the class name for compact use. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
*
* @author ben.manes@gmail.com (Ben Manes)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class NodeFactoryGenerator {
private final List<NodeRule> rules = List.of(new AddSubtype(), new AddConstructors(),
new AddKey(), new AddValue(), new AddMaximum(), new AddExpiration(), new AddDeques(),
Expand All @@ -86,7 +85,6 @@ public final class NodeFactoryGenerator {
private final List<TypeSpec> nodeTypes;
private final Path directory;

@SuppressWarnings("NullAway.Init")
private NodeFactoryGenerator(Path directory) {
this.directory = requireNonNull(directory);
this.nodeTypes = new ArrayList<>();
Expand Down Expand Up @@ -129,7 +127,7 @@ private void reformat() throws FormatterException, IOException {
}

private void generatedNodes() {
NavigableMap<String, Set<Feature>> classNameToFeatures = getClassNameToFeatures();
NavigableMap<String, ImmutableSet<Feature>> classNameToFeatures = getClassNameToFeatures();
classNameToFeatures.forEach((className, features) -> {
String higherKey = classNameToFeatures.higherKey(className);
boolean isLeaf = (higherKey == null) || !higherKey.startsWith(className);
Expand All @@ -138,8 +136,8 @@ private void generatedNodes() {
});
}

private NavigableMap<String, Set<Feature>> getClassNameToFeatures() {
var classNameToFeatures = new TreeMap<String, Set<Feature>>();
private NavigableMap<String, ImmutableSet<Feature>> getClassNameToFeatures() {
var classNameToFeatures = new TreeMap<String, ImmutableSet<Feature>>();
for (List<Object> combination : combinations()) {
var features = getFeatures(combination);
var className = Feature.makeClassName(features);
Expand All @@ -163,27 +161,28 @@ private ImmutableSet<Feature> getFeatures(List<Object> combination) {
return ImmutableSet.copyOf(features);
}

@SuppressWarnings("NullAway")
private TypeSpec makeNodeSpec(String className, boolean isFinal, Set<Feature> features) {
private TypeSpec makeNodeSpec(String className, boolean isFinal, ImmutableSet<Feature> features) {
TypeName superClass;
Set<Feature> parentFeatures;
Set<Feature> generateFeatures;
ImmutableSet<Feature> parentFeatures;
ImmutableSet<Feature> generateFeatures;
if (features.size() == 2) {
parentFeatures = Set.of();
parentFeatures = ImmutableSet.of();
generateFeatures = features;
superClass = TypeName.OBJECT;
} else {
parentFeatures = ImmutableSet.copyOf(Iterables.limit(features, features.size() - 1));
generateFeatures = ImmutableSet.of(Iterables.getLast(features));
generateFeatures = ImmutableSet.of(features.asList().get(features.size() - 1));
superClass = ParameterizedTypeName.get(ClassName.get(PACKAGE_NAME,
encode(Feature.makeClassName(parentFeatures))), kTypeVar, vTypeVar);
}

var context = new NodeContext(superClass, className, isFinal, parentFeatures, generateFeatures);
for (NodeRule rule : rules) {
rule.accept(context);
if (rule.applies(context)) {
rule.execute(context);
}
}
return context.nodeSubtype.build();
return context.build();
}

private Set<List<Object>> combinations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@
package com.github.benmanes.caffeine.cache.local;

import static com.github.benmanes.caffeine.cache.Specifications.ASYNC_CACHE_LOADER_PARAM;
import static com.github.benmanes.caffeine.cache.Specifications.LOCAL_CACHE_FACTORY;
import static com.github.benmanes.caffeine.cache.Specifications.BOUNDED_LOCAL_CACHE;
import static com.github.benmanes.caffeine.cache.Specifications.BUILDER_PARAM;
import static com.github.benmanes.caffeine.cache.Specifications.LOCAL_CACHE_FACTORY;

import javax.lang.model.element.Modifier;

import com.squareup.javapoet.FieldSpec;

/**
* Adds the constructor to the cache.
*
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddConstructor extends LocalCacheRule {
public final class AddConstructor implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
return true;
}

@Override
protected void execute() {
public void execute(LocalCacheContext context) {
context.constructor
.addParameter(BUILDER_PARAM)
.addParameter(ASYNC_CACHE_LOADER_PARAM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
/**
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddDeques extends LocalCacheRule {
public final class AddDeques implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
return true;
}

@Override
protected void execute() {
addAccessOrderWindowDeque();
addAccessOrderMainDeque();
addWriteOrderDeque();
public void execute(LocalCacheContext context) {
addAccessOrderWindowDeque(context);
addAccessOrderMainDeque(context);
addWriteOrderDeque(context);
}

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

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

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

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

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

private void addFieldAndMethod(TypeName type, String name) {
private 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 @@ -26,16 +26,16 @@
/**
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddExpirationTicker extends LocalCacheRule {
public final class AddExpirationTicker implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
return !(Feature.usesExpirationTicker(context.parentFeatures)
|| !Feature.usesExpirationTicker(context.generateFeatures));
}

@Override
protected void execute() {
public void execute(LocalCacheContext context) {
context.constructor.addStatement("this.ticker = builder.getTicker()");
context.cache.addField(FieldSpec.builder(TICKER, "ticker", Modifier.FINAL).build());
context.cache.addMethod(MethodSpec.methodBuilder("expirationTicker")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
/**
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddExpireAfterAccess extends LocalCacheRule {
public final class AddExpireAfterAccess implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
return context.generateFeatures.contains(Feature.EXPIRE_ACCESS);
}

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

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

private void variableExpiration() {
private 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 @@ -24,15 +24,15 @@
/**
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddExpireAfterWrite extends LocalCacheRule {
public final class AddExpireAfterWrite implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
return context.generateFeatures.contains(Feature.EXPIRE_WRITE);
}

@Override
protected void execute() {
public void execute(LocalCacheContext context) {
context.constructor.addStatement(
"this.expiresAfterWriteNanos = builder.getExpiresAfterWriteNanos()");
context.cache.addField(FieldSpec.builder(long.class, "expiresAfterWriteNanos")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
/**
* @author ben.manes@gmail.com (Ben Manes)
*/
public final class AddFastPath extends LocalCacheRule {
public final class AddFastPath implements LocalCacheRule {

@Override
protected boolean applies() {
public boolean applies(LocalCacheContext context) {
boolean parentFastPath = Feature.usesFastPath(context.parentFeatures);
boolean fastpath = Feature.usesFastPath(Sets.union(
context.parentFeatures, context.generateFeatures));
return (parentFastPath != fastpath);
}

@Override
protected void execute() {
public void execute(LocalCacheContext context) {
boolean fastpath = Feature.usesFastPath(Sets.union(
context.parentFeatures, context.generateFeatures));
context.cache.addMethod(MethodSpec.methodBuilder("fastpath")
Expand Down
Loading

0 comments on commit 1b2fcf4

Please sign in to comment.