diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpringMvcAnnotation.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpringMvcAnnotation.java index dbc27412b8..df6a19177d 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpringMvcAnnotation.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/SpringMvcAnnotation.java @@ -54,7 +54,7 @@ public final class SpringMvcAnnotation extends BugChecker implements AnnotationT .put("PATCH", "PatchMapping") .put("POST", "PostMapping") .put("PUT", "PutMapping") - .build(); + .buildOrThrow(); /** Instantiates a new {@link SpringMvcAnnotation} instance. */ public SpringMvcAnnotation() {} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableMapRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableMapRules.java index 3afed69cd2..792a137be0 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableMapRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableMapRules.java @@ -15,6 +15,7 @@ import com.google.errorprone.refaster.annotation.Matches; import com.google.errorprone.refaster.annotation.MayOptionallyUse; import com.google.errorprone.refaster.annotation.Placeholder; +import com.google.errorprone.refaster.annotation.Repeated; import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.util.Collection; import java.util.Iterator; @@ -44,12 +45,28 @@ ImmutableMap.Builder after() { } } + /** + * Prefer {@link ImmutableMap.Builder#buildOrThrow()} over the less explicit {@link + * ImmutableMap.Builder#build()}. + */ + static final class ImmutableMapBuilderBuildOrThrow { + @BeforeTemplate + ImmutableMap before(ImmutableMap.Builder builder) { + return builder.build(); + } + + @AfterTemplate + ImmutableMap after(ImmutableMap.Builder builder) { + return builder.buildOrThrow(); + } + } + /** Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives. */ static final class EntryToImmutableMap { @BeforeTemplate ImmutableMap before(Map.Entry entry) { return Refaster.anyOf( - ImmutableMap.builder().put(entry).build(), + ImmutableMap.builder().put(entry).buildOrThrow(), Stream.of(entry).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); } @@ -104,16 +121,17 @@ ImmutableMap after( /** Prefer {@link ImmutableMap#copyOf(Iterable)} over more contrived alternatives. */ static final class EntryIterableToImmutableMap { @BeforeTemplate - ImmutableMap before(Map iterable) { + Map before(Map iterable) { return Refaster.anyOf( ImmutableMap.copyOf(iterable.entrySet()), - ImmutableMap.builder().putAll(iterable).build()); + ImmutableMap.builder().putAll(iterable).buildOrThrow(), + Map.copyOf(iterable)); } @BeforeTemplate ImmutableMap before(Iterable> iterable) { return Refaster.anyOf( - ImmutableMap.builder().putAll(iterable).build(), + ImmutableMap.builder().putAll(iterable).buildOrThrow(), Streams.stream(iterable).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); } @@ -139,8 +157,6 @@ abstract static class StreamOfMapEntriesToImmutableMap { @Placeholder(allowsIdentity = true) abstract V valueFunction(@MayOptionallyUse E element); - // XXX: We could add variants in which the entry is created some other way, but we have another - // rule that covers canonicalization to `Map.entry`. @BeforeTemplate ImmutableMap before(Stream stream) { return stream @@ -224,7 +240,11 @@ ImmutableMap after(Map map) { static final class ImmutableMapOf { @BeforeTemplate Map before() { - return Refaster.anyOf(ImmutableMap.builder().build(), emptyMap(), Map.of()); + return Refaster.anyOf( + ImmutableMap.builder().buildOrThrow(), + ImmutableMap.ofEntries(), + emptyMap(), + Map.of()); } @AfterTemplate @@ -243,7 +263,10 @@ static final class ImmutableMapOf1 { @BeforeTemplate Map before(K k1, V v1) { return Refaster.anyOf( - ImmutableMap.builder().put(k1, v1).build(), singletonMap(k1, v1), Map.of(k1, v1)); + ImmutableMap.builder().put(k1, v1).buildOrThrow(), + ImmutableMap.ofEntries(Map.entry(k1, v1)), + singletonMap(k1, v1), + Map.of(k1, v1)); } @AfterTemplate @@ -261,7 +284,8 @@ ImmutableMap after(K k1, V v1) { static final class ImmutableMapOf2 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2) { - return Map.of(k1, v1, k2, v2); + return Refaster.anyOf( + ImmutableMap.ofEntries(Map.entry(k1, v1), Map.entry(k2, v2)), Map.of(k1, v1, k2, v2)); } @AfterTemplate @@ -279,7 +303,9 @@ ImmutableMap after(K k1, V v1, K k2, V v2) { static final class ImmutableMapOf3 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3) { - return Map.of(k1, v1, k2, v2, k3, v3); + return Refaster.anyOf( + ImmutableMap.ofEntries(Map.entry(k1, v1), Map.entry(k2, v2), Map.entry(k3, v3)), + Map.of(k1, v1, k2, v2, k3, v3)); } @AfterTemplate @@ -299,7 +325,10 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3) { static final class ImmutableMapOf4 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { - return Map.of(k1, v1, k2, v2, k3, v3, k4, v4); + return Refaster.anyOf( + ImmutableMap.ofEntries( + Map.entry(k1, v1), Map.entry(k2, v2), Map.entry(k3, v3), Map.entry(k4, v4)), + Map.of(k1, v1, k2, v2, k3, v3, k4, v4)); } @AfterTemplate @@ -319,7 +348,14 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { static final class ImmutableMapOf5 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { - return Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); + return Refaster.anyOf( + ImmutableMap.ofEntries( + Map.entry(k1, v1), + Map.entry(k2, v2), + Map.entry(k3, v3), + Map.entry(k4, v4), + Map.entry(k5, v5)), + Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); } @AfterTemplate @@ -370,6 +406,22 @@ ImmutableMap after(Map map) { } } + /** + * Prefer {@link ImmutableMap#ofEntries(Map.Entry[])} over alternatives that don't communicate the + * immutability of the resulting map at the type level. + */ + static final class ImmutableMapOfEntries { + @BeforeTemplate + Map before(@Repeated Map.Entry entries) { + return Map.ofEntries(entries); + } + + @AfterTemplate + ImmutableMap after(@Repeated Map.Entry entries) { + return ImmutableMap.ofEntries(entries); + } + } + // XXX: Add a rule for this: // Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf) // -> diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRules.java index e2cf20ef0f..2a30e31ac7 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRules.java @@ -71,7 +71,7 @@ ImmutableSortedMap.Builder after() { static final class EmptyImmutableSortedMap, V> { @BeforeTemplate ImmutableSortedMap before() { - return ImmutableSortedMap.naturalOrder().build(); + return ImmutableSortedMap.naturalOrder().buildOrThrow(); } @AfterTemplate @@ -89,7 +89,7 @@ ImmutableSortedMap after() { static final class PairToImmutableSortedMap, V> { @BeforeTemplate ImmutableSortedMap before(K key, V value) { - return ImmutableSortedMap.naturalOrder().put(key, value).build(); + return ImmutableSortedMap.naturalOrder().put(key, value).buildOrThrow(); } @AfterTemplate @@ -105,7 +105,7 @@ static final class EntryToImmutableSortedMap, V> @BeforeTemplate ImmutableSortedMap before(Map.Entry entry) { return Refaster.anyOf( - ImmutableSortedMap.naturalOrder().put(entry).build(), + ImmutableSortedMap.naturalOrder().put(entry).buildOrThrow(), Stream.of(entry) .collect( toImmutableSortedMap(naturalOrder(), Map.Entry::getKey, Map.Entry::getValue))); @@ -126,7 +126,7 @@ ImmutableMap before(Map iterable) { return Refaster.anyOf( ImmutableSortedMap.copyOf(iterable, naturalOrder()), ImmutableSortedMap.copyOf(iterable.entrySet()), - ImmutableSortedMap.naturalOrder().putAll(iterable).build()); + ImmutableSortedMap.naturalOrder().putAll(iterable).buildOrThrow()); } @BeforeTemplate @@ -134,7 +134,7 @@ ImmutableSortedMap before( Iterable> iterable) { return Refaster.anyOf( ImmutableSortedMap.copyOf(iterable, naturalOrder()), - ImmutableSortedMap.naturalOrder().putAll(iterable).build(), + ImmutableSortedMap.naturalOrder().putAll(iterable).buildOrThrow(), Streams.stream(iterable) .collect( toImmutableSortedMap( diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestInput.java index 2302bb4c9e..7531a5f83e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestInput.java @@ -24,9 +24,13 @@ ImmutableMap.Builder testImmutableMapBuilder() { return new ImmutableMap.Builder<>(); } + ImmutableMap testImmutableMapBuilderBuildOrThrow() { + return ImmutableMap.builder().build(); + } + ImmutableSet> testEntryToImmutableMap() { return ImmutableSet.of( - ImmutableMap.builder().put(Map.entry("foo", 1)).build(), + ImmutableMap.builder().put(Map.entry("foo", 1)).buildOrThrow(), Stream.of(Map.entry("foo", 1)) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); } @@ -51,13 +55,14 @@ ImmutableSet> testIterableToImmutableMap() { ImmutableMap.copyOf(Maps.asMap(ImmutableSet.of(10), Integer::valueOf))); } - ImmutableSet> testEntryIterableToImmutableMap() { + ImmutableSet> testEntryIterableToImmutableMap() { return ImmutableSet.of( ImmutableMap.copyOf(ImmutableMap.of("foo", 1).entrySet()), - ImmutableMap.builder().putAll(ImmutableMap.of("foo", 1)).build(), + ImmutableMap.builder().putAll(ImmutableMap.of("foo", 1)).buildOrThrow(), + Map.copyOf(ImmutableMap.of("foo", 1)), ImmutableMap.builder() .putAll(ImmutableMap.of("foo", 1).entrySet()) - .build(), + .buildOrThrow(), ImmutableMap.of("foo", 1).entrySet().stream() .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)), Streams.stream(Iterables.cycle(Map.entry("foo", 1))) @@ -100,32 +105,51 @@ ImmutableSet> testTransformMapValuesToImmutableMap ImmutableSet> testImmutableMapOf() { return ImmutableSet.of( - ImmutableMap.builder().build(), + ImmutableMap.builder().buildOrThrow(), + ImmutableMap.ofEntries(), Collections.emptyMap(), Map.of()); } ImmutableSet> testImmutableMapOf1() { return ImmutableSet.of( - ImmutableMap.builder().put("k1", "v1").build(), + ImmutableMap.builder().put("k1", "v1").buildOrThrow(), + ImmutableMap.ofEntries(Map.entry("k1", "v1")), Collections.singletonMap("k1", "v1"), Map.of("k1", "v1")); } - Map testImmutableMapOf2() { - return Map.of("k1", "v1", "k2", "v2"); + ImmutableSet> testImmutableMapOf2() { + return ImmutableSet.of( + ImmutableMap.ofEntries(Map.entry("k1", "v1"), Map.entry("k2", "v2")), + Map.of("k1", "v1", "k2", "v2")); } - Map testImmutableMapOf3() { - return Map.of("k1", "v1", "k2", "v2", "k3", "v3"); + ImmutableSet> testImmutableMapOf3() { + return ImmutableSet.of( + ImmutableMap.ofEntries(Map.entry("k1", "v1"), Map.entry("k2", "v2"), Map.entry("k3", "v3")), + Map.of("k1", "v1", "k2", "v2", "k3", "v3")); } - Map testImmutableMapOf4() { - return Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"); + ImmutableSet> testImmutableMapOf4() { + return ImmutableSet.of( + ImmutableMap.ofEntries( + Map.entry("k1", "v1"), + Map.entry("k2", "v2"), + Map.entry("k3", "v3"), + Map.entry("k4", "v4")), + Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4")); } - Map testImmutableMapOf5() { - return Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"); + ImmutableSet> testImmutableMapOf5() { + return ImmutableSet.of( + ImmutableMap.ofEntries( + Map.entry("k1", "v1"), + Map.entry("k2", "v2"), + Map.entry("k3", "v3"), + Map.entry("k4", "v4"), + Map.entry("k5", "v5")), + Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); } ImmutableMap testImmutableMapCopyOfMapsFilterKeys() { @@ -139,4 +163,11 @@ ImmutableMap testImmutableMapCopyOfMapsFilterValues() { .filter(entry -> entry.getValue() > 0) .collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)); } + + ImmutableSet> testImmutableMapOfEntries() { + return ImmutableSet.of( + Map.ofEntries(), + Map.ofEntries(Map.entry("foo", 1)), + Map.ofEntries(Map.entry("bar", 2), Map.entry("baz", 3))); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestOutput.java index 3365f3fd59..0482e0724b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableMapRulesTestOutput.java @@ -24,6 +24,10 @@ ImmutableMap.Builder testImmutableMapBuilder() { return ImmutableMap.builder(); } + ImmutableMap testImmutableMapBuilderBuildOrThrow() { + return ImmutableMap.builder().buildOrThrow(); + } + ImmutableSet> testEntryToImmutableMap() { return ImmutableSet.of( ImmutableMap.of(Map.entry("foo", 1).getKey(), Map.entry("foo", 1).getValue()), @@ -46,8 +50,9 @@ ImmutableSet> testIterableToImmutableMap() { Maps.toMap(ImmutableSet.of(10), Integer::valueOf)); } - ImmutableSet> testEntryIterableToImmutableMap() { + ImmutableSet> testEntryIterableToImmutableMap() { return ImmutableSet.of( + ImmutableMap.copyOf(ImmutableMap.of("foo", 1)), ImmutableMap.copyOf(ImmutableMap.of("foo", 1)), ImmutableMap.copyOf(ImmutableMap.of("foo", 1)), ImmutableMap.copyOf(ImmutableMap.of("foo", 1).entrySet()), @@ -83,28 +88,39 @@ ImmutableSet> testTransformMapValuesToImmutableMap } ImmutableSet> testImmutableMapOf() { - return ImmutableSet.of(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of()); + return ImmutableSet.of( + ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of()); } ImmutableSet> testImmutableMapOf1() { return ImmutableSet.of( - ImmutableMap.of("k1", "v1"), ImmutableMap.of("k1", "v1"), ImmutableMap.of("k1", "v1")); + ImmutableMap.of("k1", "v1"), + ImmutableMap.of("k1", "v1"), + ImmutableMap.of("k1", "v1"), + ImmutableMap.of("k1", "v1")); } - Map testImmutableMapOf2() { - return ImmutableMap.of("k1", "v1", "k2", "v2"); + ImmutableSet> testImmutableMapOf2() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1", "k2", "v2"), ImmutableMap.of("k1", "v1", "k2", "v2")); } - Map testImmutableMapOf3() { - return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"); + ImmutableSet> testImmutableMapOf3() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3")); } - Map testImmutableMapOf4() { - return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"); + ImmutableSet> testImmutableMapOf4() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4")); } - Map testImmutableMapOf5() { - return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"); + ImmutableSet> testImmutableMapOf5() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); } ImmutableMap testImmutableMapCopyOfMapsFilterKeys() { @@ -114,4 +130,11 @@ ImmutableMap testImmutableMapCopyOfMapsFilterKeys() { ImmutableMap testImmutableMapCopyOfMapsFilterValues() { return ImmutableMap.copyOf(Maps.filterValues(ImmutableMap.of("foo", 1), v -> v > 0)); } + + ImmutableSet> testImmutableMapOfEntries() { + return ImmutableSet.of( + ImmutableMap.ofEntries(), + ImmutableMap.ofEntries(Map.entry("foo", 1)), + ImmutableMap.ofEntries(Map.entry("bar", 2), Map.entry("baz", 3))); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRulesTestInput.java index 3dc4071701..46e9ffdaa6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSortedMapRulesTestInput.java @@ -32,16 +32,16 @@ ImmutableSortedMap.Builder testImmutableSortedMapReverseOrderBu } ImmutableSortedMap testEmptyImmutableSortedMap() { - return ImmutableSortedMap.naturalOrder().build(); + return ImmutableSortedMap.naturalOrder().buildOrThrow(); } ImmutableSortedMap testPairToImmutableSortedMap() { - return ImmutableSortedMap.naturalOrder().put("foo", 1).build(); + return ImmutableSortedMap.naturalOrder().put("foo", 1).buildOrThrow(); } ImmutableSet> testEntryToImmutableSortedMap() { return ImmutableSet.of( - ImmutableSortedMap.naturalOrder().put(Map.entry("foo", 1)).build(), + ImmutableSortedMap.naturalOrder().put(Map.entry("foo", 1)).buildOrThrow(), Stream.of(Map.entry("foo", 1)) .collect(toImmutableSortedMap(naturalOrder(), Map.Entry::getKey, Map.Entry::getValue))); } @@ -52,10 +52,10 @@ ImmutableSet> testIterableToImmutableSortedM ImmutableSortedMap.copyOf(ImmutableSortedMap.of("foo", 1).entrySet()), ImmutableSortedMap.naturalOrder() .putAll(ImmutableSortedMap.of("foo", 1)) - .build(), + .buildOrThrow(), ImmutableSortedMap.naturalOrder() .putAll(ImmutableSortedMap.of("foo", 1).entrySet()) - .build(), + .buildOrThrow(), ImmutableSortedMap.of("foo", 1).entrySet().stream() .collect(toImmutableSortedMap(naturalOrder(), Map.Entry::getKey, Map.Entry::getValue)), Streams.stream(Iterables.cycle(Map.entry("foo", 1))) diff --git a/integration-tests/checkstyle-expected-changes.patch b/integration-tests/checkstyle-expected-changes.patch index a7b24179e6..252a145d76 100644 --- a/integration-tests/checkstyle-expected-changes.patch +++ b/integration-tests/checkstyle-expected-changes.patch @@ -14875,7 +14875,7 @@ final String defaultValue = getDefaultValue(propertyName, field, instance); --- a/src/main/java/com/puppycrawl/tools/checkstyle/site/SiteUtil.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/site/SiteUtil.java -@@ -19,6 +19,14 @@ +@@ -19,6 +19,15 @@ package com.puppycrawl.tools.checkstyle.site; @@ -14886,11 +14886,12 @@ + +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; ++import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; -@@ -55,7 +63,6 @@ import java.net.URI; +@@ -55,7 +64,6 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -14898,7 +14899,7 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; -@@ -72,7 +79,6 @@ import java.util.Optional; +@@ -72,7 +80,6 @@ import java.util.Optional; import java.util.Set; import java.util.TreeSet; import java.util.regex.Pattern; @@ -14906,7 +14907,31 @@ import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.Nullable; -@@ -156,7 +162,7 @@ public final class SiteUtil { +@@ -131,12 +138,17 @@ public final class SiteUtil { + + /** Class name and their corresponding parent module name. */ + private static final Map, String> CLASS_TO_PARENT_MODULE = +- Map.ofEntries( +- Map.entry(AbstractCheck.class, TreeWalker.class.getSimpleName()), +- Map.entry(TreeWalkerFilter.class, TreeWalker.class.getSimpleName()), +- Map.entry(AbstractFileSetCheck.class, Checker.class.getSimpleName()), +- Map.entry(Filter.class, Checker.class.getSimpleName()), +- Map.entry(BeforeExecutionFileFilter.class, Checker.class.getSimpleName())); ++ ImmutableMap.of( ++ AbstractCheck.class, ++ TreeWalker.class.getSimpleName(), ++ TreeWalkerFilter.class, ++ TreeWalker.class.getSimpleName(), ++ AbstractFileSetCheck.class, ++ Checker.class.getSimpleName(), ++ Filter.class, ++ Checker.class.getSimpleName(), ++ BeforeExecutionFileFilter.class, ++ Checker.class.getSimpleName()); + + /** Set of properties that every check has. */ + private static final Set CHECK_PROPERTIES = getProperties(AbstractCheck.class); +@@ -156,7 +168,7 @@ public final class SiteUtil { /** Set of properties that are undocumented. Those are internal properties. */ private static final Set UNDOCUMENTED_PROPERTIES = @@ -14915,7 +14940,7 @@ "SuppressWithNearbyCommentFilter.fileContents", "SuppressionCommentFilter.fileContents"); /** Properties that can not be gathered from class instance. */ -@@ -294,27 +300,25 @@ public final class SiteUtil { +@@ -294,27 +306,25 @@ public final class SiteUtil { /** Path to main source code folder. */ private static final String MAIN_FOLDER_PATH = @@ -14932,16 +14957,16 @@ + new File(Path.of(MAIN_FOLDER_PATH, CHECKS, NAMING, "AbstractNameCheck.java").toString()), new File( - Paths.get(MAIN_FOLDER_PATH, CHECKS, NAMING, "AbstractNameCheck.java").toString()), -- new File( -- Paths.get(MAIN_FOLDER_PATH, CHECKS, "javadoc", "AbstractJavadocCheck.java") -- .toString()), -- new File(Paths.get(MAIN_FOLDER_PATH, "api", "AbstractFileSetCheck.java").toString()), + Path.of(MAIN_FOLDER_PATH, CHECKS, "javadoc", "AbstractJavadocCheck.java").toString()), + new File(Path.of(MAIN_FOLDER_PATH, "api", "AbstractFileSetCheck.java").toString()), new File( -- Paths.get(MAIN_FOLDER_PATH, CHECKS, "header", "AbstractHeaderCheck.java").toString()), +- Paths.get(MAIN_FOLDER_PATH, CHECKS, "javadoc", "AbstractJavadocCheck.java") +- .toString()), +- new File(Paths.get(MAIN_FOLDER_PATH, "api", "AbstractFileSetCheck.java").toString()), + Path.of(MAIN_FOLDER_PATH, CHECKS, "header", "AbstractHeaderCheck.java").toString()), new File( +- Paths.get(MAIN_FOLDER_PATH, CHECKS, "header", "AbstractHeaderCheck.java").toString()), +- new File( - Paths.get(MAIN_FOLDER_PATH, CHECKS, "metrics", "AbstractClassCouplingCheck.java") + Path.of(MAIN_FOLDER_PATH, CHECKS, "metrics", "AbstractClassCouplingCheck.java") .toString()), @@ -14951,7 +14976,7 @@ .toString())); /** Private utility constructor. */ -@@ -475,7 +479,7 @@ public final class SiteUtil { +@@ -475,7 +485,7 @@ public final class SiteUtil { * @throws MacroExecutionException if an I/O error occurs. */ public static Set getXdocsTemplatesFilePaths() throws MacroExecutionException { @@ -14960,7 +14985,7 @@ try (Stream stream = Files.find( directory, -@@ -483,7 +487,7 @@ public final class SiteUtil { +@@ -483,7 +493,7 @@ public final class SiteUtil { (path, attr) -> { return attr.isRegularFile() && path.toString().endsWith(".xml.template"); })) { @@ -14969,7 +14994,7 @@ } catch (IOException ioException) { throw new MacroExecutionException("Failed to find xdocs templates", ioException); } -@@ -510,7 +514,7 @@ public final class SiteUtil { +@@ -510,7 +520,7 @@ public final class SiteUtil { } // If parent class is not found, check interfaces @@ -14978,7 +15003,7 @@ final Class[] interfaces = moduleClass.getInterfaces(); for (Class interfaceClass : interfaces) { parentModuleName = CLASS_TO_PARENT_MODULE.get(interfaceClass); -@@ -520,7 +524,7 @@ public final class SiteUtil { +@@ -520,7 +530,7 @@ public final class SiteUtil { } } @@ -14987,7 +15012,7 @@ final String message = String.format( Locale.ROOT, "Failed to find parent module for %s", moduleClass.getSimpleName()); -@@ -544,7 +548,7 @@ public final class SiteUtil { +@@ -544,7 +554,7 @@ public final class SiteUtil { prop -> { return !isGlobalProperty(clss, prop) && !isUndocumentedProperty(clss, prop); }) @@ -14996,7 +15021,7 @@ properties.addAll(getNonExplicitProperties(instance, clss)); return new TreeSet<>(properties); } -@@ -663,7 +667,7 @@ public final class SiteUtil { +@@ -663,7 +673,7 @@ public final class SiteUtil { treeWalkerConfig.addChild(scraperCheckConfig); try { checker.configure(defaultConfiguration); @@ -15005,7 +15030,7 @@ checker.process(filesToProcess); checker.destroy(); } catch (CheckstyleException checkstyleException) { -@@ -986,9 +990,7 @@ public final class SiteUtil { +@@ -986,9 +996,7 @@ public final class SiteUtil { if (value != null && Array.getLength(value) > 0) { result = removeSquareBrackets( @@ -15016,7 +15041,7 @@ } if (result.isEmpty()) { -@@ -1020,8 +1022,7 @@ public final class SiteUtil { +@@ -1020,8 +1028,7 @@ public final class SiteUtil { result = ""; } else { try (Stream valuesStream = getValuesStream(value)) { @@ -15026,7 +15051,7 @@ } } -@@ -1062,10 +1063,7 @@ public final class SiteUtil { +@@ -1062,10 +1069,7 @@ public final class SiteUtil { private static String getIntArrayPropertyValue(Object value) { try (IntStream stream = getIntStream(value)) { String result = @@ -15038,7 +15063,7 @@ if (result.isEmpty()) { result = CURLY_BRACKETS; } -@@ -1170,11 +1168,11 @@ public final class SiteUtil { +@@ -1170,11 +1174,11 @@ public final class SiteUtil { */ public static List getDifference(int[] tokens, int... subtractions) { final Set subtractionsSet = @@ -15052,7 +15077,7 @@ } /** -@@ -1221,7 +1219,7 @@ public final class SiteUtil { +@@ -1221,7 +1225,7 @@ public final class SiteUtil { throw new MacroExecutionException("Failed to get parent path for " + templatePath); } return templatePathParent @@ -15418,12 +15443,13 @@ } --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/UnmodifiableCollectionUtil.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/UnmodifiableCollectionUtil.java -@@ -19,13 +19,15 @@ +@@ -19,13 +19,16 @@ package com.puppycrawl.tools.checkstyle.utils; +import static java.util.stream.Collectors.toUnmodifiableList; + ++import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import java.util.Arrays; import java.util.Collection; @@ -15435,7 +15461,7 @@ /** *
Note: it simply wraps the existing JDK methods to provide a workaround for Pitest survival -@@ -57,7 +59,7 @@ public final class UnmodifiableCollectionUtil { +@@ -57,7 +60,7 @@ public final class UnmodifiableCollectionUtil { * @return An unmodifiable List containing elements of the specified type. */ public static List unmodifiableList(Collection items, Class elementType) { @@ -15444,7 +15470,16 @@ } /** -@@ -92,6 +94,6 @@ public final class UnmodifiableCollectionUtil { +@@ -81,7 +84,7 @@ public final class UnmodifiableCollectionUtil { + * @return an immutable copy of the input map + */ + public static Map copyOfMap(Map map) { +- return Map.copyOf(map); ++ return ImmutableMap.copyOf(map); + } + + /** +@@ -92,6 +95,6 @@ public final class UnmodifiableCollectionUtil { * @return immutable set */ public static Set singleton(T obj) { @@ -56937,6 +56972,15 @@ private static final Map> FULLY_QUALIFIED_CLASS_NAMES = ImmutableMap.>builder() .put("int", int.class) +@@ -97,7 +96,7 @@ public class XdocsJavaDocsTest extends AbstractModuleTestSupport { + .put("URI", URI.class) + .put("WrapOption", WrapOption.class) + .put("PARAM_LITERAL", int[].class) +- .build(); ++ .buildOrThrow(); + + private static final List> CHECK_PROPERTIES = new ArrayList<>(); + private static final Map CHECK_PROPERTY_DOC = new HashMap<>(); @@ -115,14 +114,14 @@ public class XdocsJavaDocsTest extends AbstractModuleTestSupport { } diff --git a/integration-tests/prometheus-java-client-expected-changes.patch b/integration-tests/prometheus-java-client-expected-changes.patch index 8c83b749c3..0d5a79ba0f 100644 --- a/integration-tests/prometheus-java-client-expected-changes.patch +++ b/integration-tests/prometheus-java-client-expected-changes.patch @@ -4459,6 +4459,15 @@ "values from otel have precedence over builder", new TestCase() .expectedProperties( +@@ -113,7 +113,7 @@ class OtelAutoConfigTest { + .put("otel.exporter.otlp.timeout", Optional.of("13s")) + .put("otel.exporter.otlp.metrics.timeout", Optional.empty()) + .put("otel.service.name", Optional.of("otel-service")) +- .build()) ++ .buildOrThrow()) + .expectedResourceAttributes( + ImmutableMap.of( + "key", @@ -128,7 +128,7 @@ class OtelAutoConfigTest { "otel-version")) .exporterBuilder(OtelAutoConfigTest::setBuilderValues) @@ -4468,15 +4477,44 @@ "values from prom properties have precedence over builder and otel", new TestCase() .expectedProperties( -@@ -177,7 +177,7 @@ class OtelAutoConfigTest { +@@ -143,7 +143,7 @@ class OtelAutoConfigTest { + .put("otel.exporter.otlp.metrics.timeout", Optional.of("23s")) + .put("otel.exporter.otlp.timeout", Optional.of("13s")) + .put("otel.service.name", Optional.of("prom-service")) +- .build()) ++ .buildOrThrow()) + .expectedResourceAttributes( + ImmutableMap.of( + "key", +@@ -176,8 +176,8 @@ class OtelAutoConfigTest { + .put( "io.prometheus.exporter.opentelemetry.resourceAttributes", "key=prom-value") - .build())), +- .build())), - Arguments.of( ++ .buildOrThrow())), + arguments( "values from prom properties builder have precedence over builder and otel", new TestCase() .expectedProperties( +@@ -192,7 +192,7 @@ class OtelAutoConfigTest { + .put("otel.exporter.otlp.metrics.timeout", Optional.of("23s")) + .put("otel.exporter.otlp.timeout", Optional.of("13s")) + .put("otel.service.name", Optional.of("prom-service")) +- .build()) ++ .buildOrThrow()) + .expectedResourceAttributes( + ImmutableMap.of( + "key", +@@ -233,7 +233,7 @@ class OtelAutoConfigTest { + .put( + "otel.resource.attributes", + "key=otel-value,service.namespace=otel-namespace,service.instance.id=otel-instance,service.version=otel-version") +- .build(); ++ .buildOrThrow(); + } + + private static void setBuilderValues(OpenTelemetryExporter.Builder builder) { @@ -250,8 +250,8 @@ class OtelAutoConfigTest { .resourceAttribute("key", "builder-value"); } @@ -4565,7 +4603,12 @@ } --- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusMetricDataTest.java +++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusMetricDataTest.java -@@ -7,7 +7,7 @@ import io.prometheus.metrics.model.snapshots.Unit; +@@ -3,14 +3,15 @@ package io.prometheus.metrics.exporter.opentelemetry.otelmodel; + import static java.util.Map.entry; + import static org.junit.jupiter.api.Assertions.*; + ++import com.google.common.collect.ImmutableMap; + import io.prometheus.metrics.model.snapshots.Unit; import java.util.Map; import org.junit.jupiter.api.Test; @@ -4573,7 +4616,11 @@ +final class PrometheusMetricDataTest { Map translations = - Map.ofEntries( +- Map.ofEntries( ++ ImmutableMap.ofEntries( + entry("days", "d"), + entry("hours", "h"), + entry("minutes", "min"), --- a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/DefaultJobLabelDetector.java +++ b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/DefaultJobLabelDetector.java @@ -3,7 +3,6 @@ package io.prometheus.metrics.exporter.pushgateway;