Skip to content

Commit

Permalink
Upgrade to pmd 7.0-rc1 (static analyzer)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Mar 26, 2023
1 parent d6ca518 commit 588eae6
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.benmanes.caffeine.cache.impl.ConcurrentHashMapV7;
import com.github.benmanes.caffeine.cache.impl.ConcurrentMapCache;
import com.github.benmanes.caffeine.cache.impl.Ehcache3;
import com.github.benmanes.caffeine.cache.impl.ExpiringMapCache;
import com.github.benmanes.caffeine.cache.impl.GuavaCache;
import com.github.benmanes.caffeine.cache.impl.HazelcastCache;
import com.github.benmanes.caffeine.cache.impl.LinkedHashMapCache;
Expand All @@ -36,6 +35,7 @@
import com.trivago.triava.tcache.EvictionPolicy;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

/**
* A factory for creating a {@link BasicCache} implementation.
Expand Down Expand Up @@ -111,12 +111,18 @@ public enum CacheType {
},
ExpiringMap_Fifo {
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new ExpiringMapCache<>(maximumSize, ExpirationPolicy.CREATED);
return new ConcurrentMapCache<>(ExpiringMap.builder()
.expirationPolicy(ExpirationPolicy.CREATED)
.maxSize(maximumSize)
.build());
}
},
ExpiringMap_Lru {
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new ExpiringMapCache<>(maximumSize, ExpirationPolicy.ACCESSED);
return new ConcurrentMapCache<>(ExpiringMap.builder()
.expirationPolicy(ExpirationPolicy.ACCESSED)
.maxSize(maximumSize)
.build());
}
},
Guava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static java.util.Locale.US;
import static java.util.function.Function.identity;

import java.io.PrintStream;
import java.math.RoundingMode;
import java.time.Duration;
import java.util.Map;
Expand Down Expand Up @@ -59,13 +58,12 @@ public final class MemoryBenchmark {
final MemoryMeter meter = new MemoryMeter()
.withGuessing(Guess.FALLBACK_BEST)
.ignoreKnownSingletons();
final PrintStream out = System.out;

public void run() {
if (!MemoryMeter.hasInstrumentation()) {
out.println("WARNING: Java agent not installed - guessing instead");
System.out.println("WARNING: Java agent not installed - guessing instead");
}
out.println();
System.out.println();
unbounded();
maximumSize();
maximumSize_expireAfterAccess();
Expand Down Expand Up @@ -213,12 +211,12 @@ private void compare(String label, Cache<Integer, Integer> caffeine,
guava.cleanUp();

int leftPadded = Math.max((36 - label.length()) / 2 - 1, 1);
out.printf(US, " %2$-" + leftPadded + "s %s%n", label, " ");
System.out.printf(US, " %2$-" + leftPadded + "s %s%n", label, " ");
String result = FlipTable.of(new String[] { "Cache", "Baseline", "Per Entry" }, new String[][] {
evaluate("Caffeine", caffeine.asMap()),
evaluate("Guava", guava.asMap())
});
out.println(result);
System.out.println(result);
}

private String[] evaluate(String label, Map<Integer, Integer> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
@SuppressWarnings({"deprecation", "unchecked"})
public final class CoherenceCache<K, V> implements BasicCache<K, V> {
@SuppressWarnings("PMD.LooseCoupling")
private final LocalCache cache;

public CoherenceCache(int maximumSize, int evictionPolicyType) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ static long saturatedToNanos(Duration duration) {
*/
@Override
public String toString() {
StringBuilder s = new StringBuilder(75);
StringBuilder s = new StringBuilder(200);
s.append(getClass().getSimpleName()).append('{');
int baseLength = s.length();
if (initialCapacity != UNSET_INT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ final class CacheView<K, V> extends AbstractCacheView<K, V> {
abstract class AbstractCacheView<K, V> implements Cache<K, V>, Serializable {
private static final long serialVersionUID = 1L;

transient @Nullable AsMapView<K, V> asMapView;
transient @Nullable ConcurrentMap<K, V> asMapView;

abstract LocalAsyncCache<K, V> asyncCache();

Expand Down Expand Up @@ -1002,15 +1002,15 @@ public int hashCode() {

@Override
public String toString() {
var result = new StringBuilder().append('{');
var result = new StringBuilder(50).append('{');
for (var iterator = new EntryIterator(); iterator.hasNext();) {
var entry = iterator.next();
result.append((entry.getKey() == this) ? "(this Map)" : entry.getKey());
result.append('=');
result.append((entry.getValue() == this) ? "(this Map)" : entry.getValue());
result.append((entry.getKey() == this) ? "(this Map)" : entry.getKey())
.append('=')
.append((entry.getValue() == this) ? "(this Map)" : entry.getValue());

if (iterator.hasNext()) {
result.append(',').append(' ');
result.append(", ");
}
}
return result.append('}').toString();
Expand Down Expand Up @@ -1059,7 +1059,7 @@ public boolean remove(Object o) {
}
for (var entry : delegate.entrySet()) {
V value = Async.getIfReady(entry.getValue());
if ((value != null) && o.equals(value) && AsMapView.this.remove(entry.getKey(), value)) {
if ((value != null) && value.equals(o) && AsMapView.this.remove(entry.getKey(), value)) {
return true;
}
}
Expand Down Expand Up @@ -1103,7 +1103,7 @@ public void forEach(Consumer<? super V> action) {
@Override
public Iterator<V> iterator() {
return new Iterator<V>() {
Iterator<Entry<K, V>> iterator = entrySet().iterator();
final Iterator<Entry<K, V>> iterator = entrySet().iterator();

@Override
public boolean hasNext() {
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public Iterator<Entry<K, V>> iterator() {
}

private final class EntryIterator implements Iterator<Entry<K, V>> {
Iterator<Entry<K, CompletableFuture<V>>> iterator;
final Iterator<Entry<K, CompletableFuture<V>>> iterator;
@Nullable Entry<K, V> cursor;
@Nullable K removalKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.github.benmanes.caffeine.cache;

import static com.github.benmanes.caffeine.cache.Caffeine.calculateHashMapCapacity;
import static com.github.benmanes.caffeine.cache.LocalAsyncCache.composeResult;
import static com.github.benmanes.caffeine.cache.LocalAsyncCache.composeResult; // NOPMD
import static java.util.Objects.requireNonNull;

import java.lang.System.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ final class TimerWheel<K, V> implements Iterable<Node<K, V>> {
* @param cache the instance that the entries belong to
* @param currentTimeNanos the current time, in nanoseconds
*/
@SuppressWarnings("PMD.UnusedAssignment")
public void advance(BoundedLocalCache<K, V> cache, long currentTimeNanos) {
long previousTimeNanos = nanos;
nanos = currentTimeNanos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.github.benmanes.caffeine.cache;

import static com.github.benmanes.caffeine.cache.Caffeine.calculateHashMapCapacity;
import static com.github.benmanes.caffeine.cache.LocalLoadingCache.newBulkMappingFunction;
import static com.github.benmanes.caffeine.cache.LocalLoadingCache.newMappingFunction;
import static com.github.benmanes.caffeine.cache.LocalLoadingCache.newBulkMappingFunction; // NOPMD
import static com.github.benmanes.caffeine.cache.LocalLoadingCache.newMappingFunction; // NOPMD
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

Expand Down Expand Up @@ -552,14 +552,14 @@ public int hashCode() {

@Override
public String toString() {
var result = new StringBuilder().append('{');
var result = new StringBuilder(50).append('{');
data.forEach((key, value) -> {
if (result.length() != 1) {
result.append(',').append(' ');
result.append(", ");
}
result.append((key == this) ? "(this Map)" : key);
result.append('=');
result.append((value == this) ? "(this Map)" : value);
result.append((key == this) ? "(this Map)" : key)
.append('=')
.append((value == this) ? "(this Map)" : value);
});
return result.append('}').toString();
}
Expand Down
32 changes: 25 additions & 7 deletions config/pmd/rulesSets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,39 @@

<rule ref="category/java/bestpractices.xml">
<exclude name="UseVarargs"/>
<exclude name="LooseCoupling"/>
<exclude name="SystemPrintln"/>
<exclude name="MissingOverride"/>
<exclude name="GuardLogStatement"/>
<exclude name="OneDeclarationPerLine"/>
<exclude name="ReplaceHashtableWithMap"/>
<exclude name="AvoidReassigningParameters"/>
<exclude name="LiteralsFirstInComparisons"/>
<exclude name="PositionLiteralsFirstInComparisons"/>
<exclude name="PositionLiteralsFirstInCaseInsensitiveComparisons"/>
</rule>

<rule ref="category/java/bestpractices.xml/LooseCoupling">
<properties>
<property name="allowedTypes" value="
java.util.Properties
|java.util.concurrent.ConcurrentHashMap
|com.google.common.collect.ImmutableMap
|com.google.common.collect.ImmutableSet
|com.google.common.collect.ImmutableList
|com.github.benmanes.caffeine.cache.BoundedLocalCache
|com.github.benmanes.caffeine.cache.UnboundedLocalCache
"/>
</properties>
</rule>

<rule ref="category/java/codestyle.xml">
<exclude name="LongVariable"/>
<exclude name="OnlyOneReturn"/>
<exclude name="ShortVariable"/>
<exclude name="AbstractNaming"/>
<exclude name="DefaultPackage"/>
<exclude name="GenericsNaming"/>
<exclude name="ShortClassName"/>
<exclude name="ShortMethodName"/>
<exclude name="ConfusingTernary"/>
<exclude name="DontImportJavaLang"/>
<exclude name="UnnecessaryReturn"/>
<exclude name="UseDiamondOperator"/>
<exclude name="UselessParentheses"/>
<exclude name="PrematureDeclaration"/>
Expand All @@ -37,9 +50,9 @@
<exclude name="ClassNamingConventions"/>
<exclude name="FieldNamingConventions"/>
<exclude name="LocalVariableCouldBeFinal"/>
<exclude name="VariableNamingConventions"/>
<exclude name="MethodArgumentCouldBeFinal"/>
<exclude name="CommentDefaultAccessModifier"/>
<exclude name="UnnecessaryFullyQualifiedName"/>
<exclude name="FieldDeclarationsShouldBeAtStartOfClass"/>
<exclude name="EmptyMethodInAbstractClassShouldBeAbstract"/>
</rule>
Expand All @@ -48,6 +61,12 @@
<property name="ignoreElseIf" value="true"/>
</properties>
</rule>
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName">
<properties>
<property name="reportStaticFields" value="false"/>
<property name="reportStaticMethods" value="false"/>
</properties>
</rule>

<rule ref="category/java/design.xml">
<exclude name="GodClass"/>
Expand Down Expand Up @@ -86,7 +105,6 @@
<exclude name="UseProperClassLoader"/>
<exclude name="AvoidCatchingThrowable"/>
<exclude name="AvoidDuplicateLiterals"/>
<exclude name="DataflowAnomalyAnalysis"/>
<exclude name="CompareObjectsWithEquals"/>
<exclude name="AvoidLiteralsInIfCondition"/>
<exclude name="AvoidFieldNameMatchingMethodName"/>
Expand Down
1 change: 1 addition & 0 deletions gradle/codeQuality.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
spotbugsPlugins gradlePlugins.spotbugsContrib
spotbugsPlugins gradlePlugins.findsecbugs
checkstyleConfig gradlePlugins.checkstyle
pmd gradlePlugins.pmd

errorprone layout.buildDirectory.files('libs/caffeine-local.jar')
errorprone libraries.errorproneMockito
Expand Down
16 changes: 10 additions & 6 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ext {
googleJavaFormat: '1.16.0',
guava: '31.1-jre',
hazelcast: '5.3.0-BETA-1',
jackrabbit: '1.48.0',
jackrabbit: '1.50.0',
jamm: '0.3.3',
javaObjectLayout: '0.17',
javapoet: '1.13.0',
Expand Down Expand Up @@ -90,9 +90,9 @@ ext {
]
pluginVersions = [
bnd: '6.4.0',
checkstyle: '10.9.2',
checkstyle: '10.9.3',
coveralls: '2.12.2',
dependencyCheck: '8.2.0',
dependencyCheck: '8.2.1',
errorprone: '3.0.1',
findsecbugs: '1.12.0',
forbiddenApis: '3.4',
Expand All @@ -101,7 +101,7 @@ ext {
jmhReport: '0.9.0',
nexusPublish: '1.3.0',
nullaway: '1.5.0',
pmd: '6.55.0',
pmd: '7.0.0-rc1',
semanticVersioning: '1.1.0',
snyk: '0.4',
sonarqube: '4.0.0.2929',
Expand All @@ -111,7 +111,7 @@ ext {
versions: '0.46.0',
]
platformVersions = [
asm: '9.4',
asm: '9.5',
kotlin: '1.8.10',
]

Expand Down Expand Up @@ -237,7 +237,11 @@ ext {
jmhReport: "gradle.plugin.io.morethan.jmhreport:gradle-jmh-report:${pluginVersions.jmhReport}",
nexusPublish: "io.github.gradle-nexus:publish-plugin:${pluginVersions.nexusPublish}",
nullaway: "net.ltgt.gradle:gradle-nullaway-plugin:${pluginVersions.nullaway}",
pmd: "net.sourceforge.pmd:pmd:${pluginVersions.pmd}",
pmd: [
"net.sourceforge.pmd:pmd:${pluginVersions.pmd}",
"net.sourceforge.pmd:pmd-ant:${pluginVersions.pmd}",
"net.sourceforge.pmd:pmd-java:${pluginVersions.pmd}",
],
semanticVersioning: "io.ehdev:gradle-semantic-versioning:${pluginVersions.semanticVersioning}",
snyk: "gradle.plugin.io.snyk.gradle.plugin:snyk:${pluginVersions.snyk}",
sonarqube: "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:${pluginVersions.sonarqube}",
Expand Down
Loading

0 comments on commit 588eae6

Please sign in to comment.