Skip to content

Commit 917c41f

Browse files
committed
Use Set.of() for constant sets where appropriate
1 parent 0c878d2 commit 917c41f

File tree

12 files changed

+117
-157
lines changed

12 files changed

+117
-157
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,17 +132,17 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
132132
private static final int STEP_REFERENCE_PCUT_BINDING = 7;
133133
private static final int STEP_FINISHED = 8;
134134

135-
private static final Set<String> singleValuedAnnotationPcds = new HashSet<>();
135+
private static final Set<String> singleValuedAnnotationPcds = Set.of(
136+
"@this",
137+
"@target",
138+
"@within",
139+
"@withincode",
140+
"@annotation");
141+
136142
private static final Set<String> nonReferencePointcutTokens = new HashSet<>();
137143

138144

139145
static {
140-
singleValuedAnnotationPcds.add("@this");
141-
singleValuedAnnotationPcds.add("@target");
142-
singleValuedAnnotationPcds.add("@within");
143-
singleValuedAnnotationPcds.add("@withincode");
144-
singleValuedAnnotationPcds.add("@annotation");
145-
146146
Set<PointcutPrimitive> pointcutPrimitives = PointcutParser.getAllSupportedPointcutPrimitives();
147147
for (PointcutPrimitive primitive : pointcutPrimitives) {
148148
nonReferencePointcutTokens.add(primitive.getName());

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.reflect.Method;
2222
import java.lang.reflect.Proxy;
2323
import java.util.Arrays;
24-
import java.util.HashSet;
2524
import java.util.Map;
2625
import java.util.Set;
2726
import java.util.concurrent.ConcurrentHashMap;
@@ -85,21 +84,17 @@
8584
public class AspectJExpressionPointcut extends AbstractExpressionPointcut
8685
implements ClassFilter, IntroductionAwareMethodMatcher, BeanFactoryAware {
8786

88-
private static final Set<PointcutPrimitive> SUPPORTED_PRIMITIVES = new HashSet<>();
89-
90-
static {
91-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.EXECUTION);
92-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.ARGS);
93-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.REFERENCE);
94-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.THIS);
95-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.TARGET);
96-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.WITHIN);
97-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ANNOTATION);
98-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_WITHIN);
99-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ARGS);
100-
SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_TARGET);
101-
}
102-
87+
private static final Set<PointcutPrimitive> SUPPORTED_PRIMITIVES = Set.of(
88+
PointcutPrimitive.EXECUTION,
89+
PointcutPrimitive.ARGS,
90+
PointcutPrimitive.REFERENCE,
91+
PointcutPrimitive.THIS,
92+
PointcutPrimitive.TARGET,
93+
PointcutPrimitive.WITHIN,
94+
PointcutPrimitive.AT_ANNOTATION,
95+
PointcutPrimitive.AT_WITHIN,
96+
PointcutPrimitive.AT_ARGS,
97+
PointcutPrimitive.AT_TARGET);
10398

10499
private static final Log logger = LogFactory.getLog(AspectJExpressionPointcut.class);
105100

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.context.annotation;
1818

1919
import java.io.IOException;
20-
import java.util.HashSet;
2120
import java.util.Map;
2221
import java.util.Set;
2322

@@ -66,14 +65,12 @@ public abstract class ConfigurationClassUtils {
6665

6766
private static final Log logger = LogFactory.getLog(ConfigurationClassUtils.class);
6867

69-
private static final Set<String> candidateIndicators = new HashSet<>(8);
68+
private static final Set<String> candidateIndicators = Set.of(
69+
Component.class.getName(),
70+
ComponentScan.class.getName(),
71+
Import.class.getName(),
72+
ImportResource.class.getName());
7073

71-
static {
72-
candidateIndicators.add(Component.class.getName());
73-
candidateIndicators.add(ComponentScan.class.getName());
74-
candidateIndicators.add(Import.class.getName());
75-
candidateIndicators.add(ImportResource.class.getName());
76-
}
7774

7875
/**
7976
* Initialize a configuration class proxy for the specified class.

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.io.Serializable;
2020
import java.util.ArrayList;
21-
import java.util.HashSet;
2221
import java.util.LinkedHashSet;
2322
import java.util.List;
2423
import java.util.Map;
@@ -66,13 +65,8 @@
6665
*/
6766
public class SpringValidatorAdapter implements SmartValidator, jakarta.validation.Validator {
6867

69-
private static final Set<String> internalAnnotationAttributes = new HashSet<>(4);
68+
private static final Set<String> internalAnnotationAttributes = Set.of("message", "groups", "payload");
7069

71-
static {
72-
internalAnnotationAttributes.add("message");
73-
internalAnnotationAttributes.add("groups");
74-
internalAnnotationAttributes.add("payload");
75-
}
7670

7771
@Nullable
7872
private jakarta.validation.Validator targetValidator;

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,31 @@
5555
*/
5656
public final class CollectionFactory {
5757

58-
private static final Set<Class<?>> approximableCollectionTypes = new HashSet<>();
58+
private static final Set<Class<?>> approximableCollectionTypes = Set.of(
59+
// Standard collection interfaces
60+
Collection.class,
61+
List.class,
62+
Set.class,
63+
SortedSet.class,
64+
NavigableSet.class,
65+
// Common concrete collection classes
66+
ArrayList.class,
67+
LinkedList.class,
68+
HashSet.class,
69+
LinkedHashSet.class,
70+
TreeSet.class,
71+
EnumSet.class);
5972

60-
private static final Set<Class<?>> approximableMapTypes = new HashSet<>();
61-
62-
63-
static {
64-
// Standard collection interfaces
65-
approximableCollectionTypes.add(Collection.class);
66-
approximableCollectionTypes.add(List.class);
67-
approximableCollectionTypes.add(Set.class);
68-
approximableCollectionTypes.add(SortedSet.class);
69-
approximableCollectionTypes.add(NavigableSet.class);
70-
approximableMapTypes.add(Map.class);
71-
approximableMapTypes.add(SortedMap.class);
72-
approximableMapTypes.add(NavigableMap.class);
73-
74-
// Common concrete collection classes
75-
approximableCollectionTypes.add(ArrayList.class);
76-
approximableCollectionTypes.add(LinkedList.class);
77-
approximableCollectionTypes.add(HashSet.class);
78-
approximableCollectionTypes.add(LinkedHashSet.class);
79-
approximableCollectionTypes.add(TreeSet.class);
80-
approximableCollectionTypes.add(EnumSet.class);
81-
approximableMapTypes.add(HashMap.class);
82-
approximableMapTypes.add(LinkedHashMap.class);
83-
approximableMapTypes.add(TreeMap.class);
84-
approximableMapTypes.add(EnumMap.class);
85-
}
73+
private static final Set<Class<?>> approximableMapTypes = Set.of(
74+
// Standard map interfaces
75+
Map.class,
76+
SortedMap.class,
77+
NavigableMap.class,
78+
// Common concrete map classes
79+
HashMap.class,
80+
LinkedHashMap.class,
81+
TreeMap.class,
82+
EnumMap.class);
8683

8784

8885
private CollectionFactory() {

spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.jdbc.support;
1818

1919
import java.sql.SQLException;
20-
import java.util.HashSet;
2120
import java.util.Set;
2221

2322
import org.springframework.dao.ConcurrencyFailureException;
@@ -46,45 +45,42 @@
4645
*/
4746
public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLExceptionTranslator {
4847

49-
private static final Set<String> BAD_SQL_GRAMMAR_CODES = new HashSet<>(8);
50-
51-
private static final Set<String> DATA_INTEGRITY_VIOLATION_CODES = new HashSet<>(8);
52-
53-
private static final Set<String> DATA_ACCESS_RESOURCE_FAILURE_CODES = new HashSet<>(8);
54-
55-
private static final Set<String> TRANSIENT_DATA_ACCESS_RESOURCE_CODES = new HashSet<>(8);
56-
57-
private static final Set<String> CONCURRENCY_FAILURE_CODES = new HashSet<>(4);
58-
59-
60-
static {
61-
BAD_SQL_GRAMMAR_CODES.add("07"); // Dynamic SQL error
62-
BAD_SQL_GRAMMAR_CODES.add("21"); // Cardinality violation
63-
BAD_SQL_GRAMMAR_CODES.add("2A"); // Syntax error direct SQL
64-
BAD_SQL_GRAMMAR_CODES.add("37"); // Syntax error dynamic SQL
65-
BAD_SQL_GRAMMAR_CODES.add("42"); // General SQL syntax error
66-
BAD_SQL_GRAMMAR_CODES.add("65"); // Oracle: unknown identifier
67-
68-
DATA_INTEGRITY_VIOLATION_CODES.add("01"); // Data truncation
69-
DATA_INTEGRITY_VIOLATION_CODES.add("02"); // No data found
70-
DATA_INTEGRITY_VIOLATION_CODES.add("22"); // Value out of range
71-
DATA_INTEGRITY_VIOLATION_CODES.add("23"); // Integrity constraint violation
72-
DATA_INTEGRITY_VIOLATION_CODES.add("27"); // Triggered data change violation
73-
DATA_INTEGRITY_VIOLATION_CODES.add("44"); // With check violation
74-
75-
DATA_ACCESS_RESOURCE_FAILURE_CODES.add("08"); // Connection exception
76-
DATA_ACCESS_RESOURCE_FAILURE_CODES.add("53"); // PostgreSQL: insufficient resources (e.g. disk full)
77-
DATA_ACCESS_RESOURCE_FAILURE_CODES.add("54"); // PostgreSQL: program limit exceeded (e.g. statement too complex)
78-
DATA_ACCESS_RESOURCE_FAILURE_CODES.add("57"); // DB2: out-of-memory exception / database not started
79-
DATA_ACCESS_RESOURCE_FAILURE_CODES.add("58"); // DB2: unexpected system error
80-
81-
TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("JW"); // Sybase: internal I/O error
82-
TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("JZ"); // Sybase: unexpected I/O error
83-
TRANSIENT_DATA_ACCESS_RESOURCE_CODES.add("S1"); // DB2: communication failure
84-
85-
CONCURRENCY_FAILURE_CODES.add("40"); // Transaction rollback
86-
CONCURRENCY_FAILURE_CODES.add("61"); // Oracle: deadlock
87-
}
48+
private static final Set<String> BAD_SQL_GRAMMAR_CODES = Set.of(
49+
"07", // Dynamic SQL error
50+
"21", // Cardinality violation
51+
"2A", // Syntax error direct SQL
52+
"37", // Syntax error dynamic SQL
53+
"42", // General SQL syntax error
54+
"65" // Oracle: unknown identifier
55+
);
56+
57+
private static final Set<String> DATA_INTEGRITY_VIOLATION_CODES = Set.of(
58+
"01", // Data truncation
59+
"02", // No data found
60+
"22", // Value out of range
61+
"23", // Integrity constraint violation
62+
"27", // Triggered data change violation
63+
"44" // With check violation
64+
);
65+
66+
private static final Set<String> DATA_ACCESS_RESOURCE_FAILURE_CODES = Set.of(
67+
"08", // Connection exception
68+
"53", // PostgreSQL: insufficient resources (e.g. disk full)
69+
"54", // PostgreSQL: program limit exceeded (e.g. statement too complex)
70+
"57", // DB2: out-of-memory exception / database not started
71+
"58" // DB2: unexpected system error
72+
);
73+
74+
private static final Set<String> TRANSIENT_DATA_ACCESS_RESOURCE_CODES = Set.of(
75+
"JW", // Sybase: internal I/O error
76+
"JZ", // Sybase: unexpected I/O error
77+
"S1" // DB2: communication failure
78+
);
79+
80+
private static final Set<String> CONCURRENCY_FAILURE_CODES = Set.of(
81+
"40", // Transaction rollback
82+
"61" // Oracle: deadlock
83+
);
8884

8985

9086
@Override

spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.lang.reflect.InvocationTargetException;
2424
import java.lang.reflect.Method;
2525
import java.lang.reflect.Proxy;
26-
import java.util.HashSet;
2726
import java.util.LinkedHashMap;
2827
import java.util.Map;
2928
import java.util.Set;
@@ -73,25 +72,22 @@ public abstract class SharedEntityManagerCreator {
7372

7473
private static final Map<Class<?>, Class<?>[]> cachedQueryInterfaces = new ConcurrentReferenceHashMap<>(4);
7574

76-
private static final Set<String> transactionRequiringMethods = new HashSet<>(8);
77-
78-
private static final Set<String> queryTerminatingMethods = new HashSet<>(8);
79-
80-
static {
81-
transactionRequiringMethods.add("joinTransaction");
82-
transactionRequiringMethods.add("flush");
83-
transactionRequiringMethods.add("persist");
84-
transactionRequiringMethods.add("merge");
85-
transactionRequiringMethods.add("remove");
86-
transactionRequiringMethods.add("refresh");
87-
88-
queryTerminatingMethods.add("execute"); // JPA 2.1 StoredProcedureQuery
89-
queryTerminatingMethods.add("executeUpdate");
90-
queryTerminatingMethods.add("getSingleResult");
91-
queryTerminatingMethods.add("getResultStream");
92-
queryTerminatingMethods.add("getResultList");
93-
queryTerminatingMethods.add("list"); // Hibernate Query.list() method
94-
}
75+
private static final Set<String> transactionRequiringMethods = Set.of(
76+
"joinTransaction",
77+
"flush",
78+
"persist",
79+
"merge",
80+
"remove",
81+
"refresh");
82+
83+
private static final Set<String> queryTerminatingMethods = Set.of(
84+
"execute", // JPA 2.1 StoredProcedureQuery
85+
"executeUpdate",
86+
"getSingleResult",
87+
"getResultStream",
88+
"getResultList",
89+
"list" // Hibernate Query.list() method
90+
);
9591

9692

9793
/**

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesScanner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ public final class PersistenceManagedTypesScanner {
5858

5959
private static final String PACKAGE_INFO_SUFFIX = ".package-info";
6060

61-
private static final Set<AnnotationTypeFilter> entityTypeFilters;
61+
private static final Set<AnnotationTypeFilter> entityTypeFilters = new LinkedHashSet<>(4);
6262

6363
static {
64-
entityTypeFilters = new LinkedHashSet<>(8);
6564
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
6665
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
6766
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));

spring-test/src/main/java/org/springframework/test/context/MergedContextConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public class MergedContextConfiguration implements Serializable {
7575
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
7676

7777
private static final Set<Class<? extends ApplicationContextInitializer<?>>> EMPTY_INITIALIZER_CLASSES =
78-
Collections.<Class<? extends ApplicationContextInitializer<?>>> emptySet();
78+
Collections.emptySet();
7979

80-
private static final Set<ContextCustomizer> EMPTY_CONTEXT_CUSTOMIZERS = Collections.<ContextCustomizer> emptySet();
80+
private static final Set<ContextCustomizer> EMPTY_CONTEXT_CUSTOMIZERS = Collections.emptySet();
8181

8282

8383
private final Class<?> testClass;

spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
2323
import java.io.StringReader;
24-
import java.util.HashSet;
2524
import java.util.Set;
2625

2726
import javax.xml.parsers.DocumentBuilder;
@@ -75,15 +74,12 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
7574
private static final XMLResolver NO_OP_XML_RESOLVER =
7675
(publicID, systemID, base, ns) -> InputStream.nullInputStream();
7776

78-
private static final Set<Class<?>> SUPPORTED_CLASSES = new HashSet<>(8);
79-
80-
static {
81-
SUPPORTED_CLASSES.add(DOMSource.class);
82-
SUPPORTED_CLASSES.add(SAXSource.class);
83-
SUPPORTED_CLASSES.add(StAXSource.class);
84-
SUPPORTED_CLASSES.add(StreamSource.class);
85-
SUPPORTED_CLASSES.add(Source.class);
86-
}
77+
private static final Set<Class<?>> SUPPORTED_CLASSES = Set.of(
78+
DOMSource.class,
79+
SAXSource.class,
80+
StAXSource.class,
81+
StreamSource.class,
82+
Source.class);
8783

8884

8985
private final TransformerFactory transformerFactory = TransformerFactory.newInstance();

0 commit comments

Comments
 (0)