Skip to content

Commit f0efc64

Browse files
committed
Introduce ErrorProne, fix compiler warnings
Signed-off-by: Stefano Cordio <[email protected]>
1 parent 2bd5b84 commit f0efc64

File tree

105 files changed

+339
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+339
-291
lines changed

Diff for: .mvn/jvm.config

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
2+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
3+
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
4+
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
5+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
6+
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
7+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
8+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
9+
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
10+
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

Diff for: pom.xml

+27
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,34 @@
180180
<release>${java.version}</release>
181181
<compilerArgs>
182182
<compilerArg>-parameters</compilerArg>
183+
<!-- https://errorprone.info/docs/installation#maven -->
184+
<compilerArg>-XDcompilePolicy=simple</compilerArg>
185+
<compilerArg>--should-stop=ifError=FLOW</compilerArg>
186+
<compilerArg>
187+
-Xplugin:ErrorProne
188+
-Xep:DefaultCharset:OFF
189+
-Xep:EqualsGetClass:OFF
190+
-Xep:Finally:OFF
191+
-Xep:HidingField:OFF
192+
-Xep:JavaTimeDefaultTimeZone:OFF
193+
-Xep:JdkObsolete:OFF
194+
-Xep:MixedMutabilityReturnType:OFF
195+
-Xep:MutablePublicArray:OFF
196+
-Xep:NonAtomicVolatileUpdate:OFF
197+
-Xep:ReferenceEquality:OFF
198+
-Xep:StringCaseLocaleUsage:OFF
199+
-Xep:StringSplitter:OFF
200+
-Xep:SynchronizeOnNonFinalField:OFF
201+
</compilerArg>
183202
</compilerArgs>
203+
<failOnWarning>true</failOnWarning>
204+
<annotationProcessorPaths>
205+
<path>
206+
<groupId>com.google.errorprone</groupId>
207+
<artifactId>error_prone_core</artifactId>
208+
<version>2.37.0</version>
209+
</path>
210+
</annotationProcessorPaths>
184211
</configuration>
185212
</plugin>
186213
<plugin>

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/Entity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public boolean equals(Object other) {
146146
@Override
147147
public int hashCode() {
148148
if (id == null) {
149-
return super.hashCode();
149+
return System.identityHashCode(this);
150150
}
151151
return 39 + 87 * id.hashCode();
152152
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.ObjectInputStream;
21+
import java.time.Clock;
2122
import java.time.LocalDateTime;
2223
import java.util.ArrayList;
2324
import java.util.Collection;
@@ -203,7 +204,7 @@ public void upgradeStatus(BatchStatus status) {
203204
/**
204205
* Convenience getter for the {@code id} of the enclosing job. Useful for DAO
205206
* implementations.
206-
* @return the @{code id} of the enclosing job.
207+
* @return the {@code id} of the enclosing job.
207208
*/
208209
public Long getJobId() {
209210
if (jobInstance != null) {

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public String toString() {
373373
for (Map.Entry<String, JobParameter<?>> entry : this.parameters.entrySet()) {
374374
parameters.add(String.format("'%s':'%s'", entry.getKey(), entry.getValue()));
375375
}
376-
return new StringBuilder("{").append(String.join(",", parameters)).append("}").toString();
376+
return "{" + String.join(",", parameters) + "}";
377377
}
378378

379379
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/StepExecution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public boolean equals(Object obj) {
492492
return super.equals(obj);
493493
}
494494

495-
return stepName.equals(other.getStepName()) && (jobExecutionId.equals(other.getJobExecutionId()))
495+
return stepName.equals(other.getStepName()) && jobExecutionId.equals(other.getJobExecutionId())
496496
&& getId().equals(other.getId());
497497
}
498498

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/JobFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface JobFactory {
3232
Job createJob();
3333

3434
/**
35-
* @return The {@link String} that contains the {@link Job} name.
35+
* Return the {@link String} that contains the {@link Job} name.
3636
*/
3737
String getJobName();
3838

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchObservabilityBeanPostProcessor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
5050
try {
5151
if (bean instanceof AbstractJob || bean instanceof AbstractStep) {
5252
ObservationRegistry observationRegistry = this.beanFactory.getBean(ObservationRegistry.class);
53-
if (bean instanceof AbstractJob) {
54-
((AbstractJob) bean).setObservationRegistry(observationRegistry);
53+
if (bean instanceof AbstractJob job) {
54+
job.setObservationRegistry(observationRegistry);
5555
}
56-
if (bean instanceof AbstractStep) {
57-
((AbstractStep) bean).setObservationRegistry(observationRegistry);
56+
if (bean instanceof AbstractStep step) {
57+
step.setObservationRegistry(observationRegistry);
5858
}
5959
}
6060
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchRegistrar.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class BatchRegistrar implements ImportBeanDefinitionRegistrar {
4949

5050
private static final Log LOGGER = LogFactory.getLog(BatchRegistrar.class);
5151

52-
private static final String MISSING_ANNOTATION_ERROR_MESSAGE = "EnableBatchProcessing is not present on importing class '%s' as expected";
53-
5452
private static final String JOB_REPOSITORY = "jobRepository";
5553

5654
private static final String JOB_EXPLORER = "jobExplorer";
@@ -84,7 +82,8 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
8482
private void validateState(AnnotationMetadata importingClassMetadata) {
8583
if (!importingClassMetadata.isAnnotated(EnableBatchProcessing.class.getName())) {
8684
String className = importingClassMetadata.getClassName();
87-
String errorMessage = String.format(MISSING_ANNOTATION_ERROR_MESSAGE, className);
85+
String errorMessage = "EnableBatchProcessing is not present on importing class '%s' as expected"
86+
.formatted(className);
8887
throw new IllegalStateException(errorMessage);
8988
}
9089
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AbstractApplicationContextFactory.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.List;
2324

2425
import org.apache.commons.logging.Log;
@@ -196,13 +197,11 @@ protected void prepareContext(ConfigurableApplicationContext parent, Configurabl
196197
protected void prepareBeanFactory(ConfigurableListableBeanFactory parent,
197198
ConfigurableListableBeanFactory beanFactory) {
198199
if (copyConfiguration && parent != null) {
199-
List<BeanPostProcessor> parentPostProcessors = new ArrayList<>();
200-
List<BeanPostProcessor> childPostProcessors = new ArrayList<>();
201-
202-
childPostProcessors.addAll(beanFactory instanceof AbstractBeanFactory
203-
? ((AbstractBeanFactory) beanFactory).getBeanPostProcessors() : new ArrayList<>());
204-
parentPostProcessors.addAll(parent instanceof AbstractBeanFactory
205-
? ((AbstractBeanFactory) parent).getBeanPostProcessors() : new ArrayList<>());
200+
List<BeanPostProcessor> childPostProcessors = new ArrayList<>(
201+
beanFactory instanceof AbstractBeanFactory factory ? factory.getBeanPostProcessors()
202+
: new ArrayList<>());
203+
List<BeanPostProcessor> parentPostProcessors = new ArrayList<>(parent instanceof AbstractBeanFactory factory
204+
? factory.getBeanPostProcessors() : new ArrayList<>());
206205

207206
try {
208207
Class<?> applicationContextAwareProcessorClass = ClassUtils.forName(
@@ -237,8 +236,8 @@ protected void prepareBeanFactory(ConfigurableListableBeanFactory parent,
237236

238237
beanFactory.copyConfigurationFrom(parent);
239238

240-
List<BeanPostProcessor> beanPostProcessors = beanFactory instanceof AbstractBeanFactory
241-
? ((AbstractBeanFactory) beanFactory).getBeanPostProcessors() : new ArrayList<>();
239+
List<BeanPostProcessor> beanPostProcessors = beanFactory instanceof AbstractBeanFactory abstractBeanFactory
240+
? abstractBeanFactory.getBeanPostProcessors() : new ArrayList<>();
242241

243242
beanPostProcessors.clear();
244243
beanPostProcessors.addAll(aggregatedPostProcessors);

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/AutomaticJobRegistrar.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void setApplicationContext(ApplicationContext applicationContext) {
7979
* use
8080
*/
8181
public void addApplicationContextFactory(ApplicationContextFactory applicationContextFactory) {
82-
if (applicationContextFactory instanceof ApplicationContextAware) {
83-
((ApplicationContextAware) applicationContextFactory).setApplicationContext(applicationContext);
82+
if (applicationContextFactory instanceof ApplicationContextAware applicationContextAware) {
83+
applicationContextAware.setApplicationContext(applicationContext);
8484
}
8585
this.applicationContextFactories.add(applicationContextFactory);
8686
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ private void doRegister(ConfigurableApplicationContext context, Job job) throws
255255
jobRegistry.register(jobFactory);
256256

257257
if (stepRegistry != null) {
258-
if (!(job instanceof StepLocator)) {
258+
if (!(job instanceof StepLocator stepLocator)) {
259259
throw new UnsupportedOperationException("Cannot locate steps from a Job that is not a StepLocator: job="
260260
+ job.getName() + " does not implement StepLocator");
261261
}
262-
stepRegistry.register(job.getName(), getSteps((StepLocator) job, context));
262+
stepRegistry.register(job.getName(), getSteps(stepLocator, context));
263263
}
264264
}
265265

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GenericApplicationContextFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
126126
GenericApplicationContextFactory.this.prepareBeanFactory(parentBeanFactory, beanFactory);
127127
for (Class<? extends BeanFactoryPostProcessor> cls : getBeanFactoryPostProcessorClasses()) {
128128
for (String name : parent.getBeanNamesForType(cls)) {
129-
beanFactory.registerSingleton(name, (parent.getBean(name)));
129+
beanFactory.registerSingleton(name, parent.getBean(name));
130130
}
131131
}
132132
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/GroupAwareJob.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public JobParametersValidator getJobParametersValidator() {
9999

100100
@Override
101101
public boolean equals(Object obj) {
102-
if (obj instanceof GroupAwareJob) {
103-
return ((GroupAwareJob) obj).delegate.equals(delegate);
102+
if (obj instanceof GroupAwareJob groupAwareJob) {
103+
return groupAwareJob.delegate.equals(delegate);
104104
}
105105
return false;
106106
}

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/JobRegistryBeanPostProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public void setJobRegistry(JobRegistry jobRegistry) {
8787

8888
@Override
8989
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
90-
if (beanFactory instanceof DefaultListableBeanFactory) {
91-
this.beanFactory = (DefaultListableBeanFactory) beanFactory;
90+
if (beanFactory instanceof DefaultListableBeanFactory defaultListableBeanFactory) {
91+
this.beanFactory = defaultListableBeanFactory;
9292
}
9393
}
9494

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/AbstractFlowParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ protected static Collection<BeanDefinition> createTransition(FlowExecutionStatus
404404
endBuilder.addConstructorArgValue(exitCodeExists ? exitCode : status.getName());
405405

406406
String endName = (status == FlowExecutionStatus.STOPPED ? STOP_ELE
407-
: status == FlowExecutionStatus.FAILED ? FAIL_ELE : END_ELE) + (endCounter++);
407+
: status == FlowExecutionStatus.FAILED ? FAIL_ELE : END_ELE) + endCounter++;
408408
endBuilder.addConstructorArgValue(endName);
409409

410410
endBuilder.addConstructorArgValue(abandon);

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/AbstractStepParser.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ protected AbstractBeanDefinition parseStep(Element stepElement, ParserContext pa
119119
}
120120
else if (FLOW_ELE.equals(name)) {
121121
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
122-
parseFlow(stepElement, nestedElement, bd, parserContext, stepUnderspecified);
122+
parseFlow(stepElement, nestedElement, bd);
123123
}
124124
else if (PARTITION_ELE.equals(name)) {
125125
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
126-
parsePartition(stepElement, nestedElement, bd, parserContext, stepUnderspecified, jobFactoryRef);
126+
parsePartition(stepElement, nestedElement, bd, parserContext, jobFactoryRef);
127127
}
128128
else if (JOB_ELE.equals(name)) {
129129
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
130-
parseJob(stepElement, nestedElement, bd, parserContext, stepUnderspecified);
130+
parseJob(nestedElement, bd, parserContext);
131131
}
132132
else if ("description".equals(name)) {
133133
bd.setDescription(nestedElement.getTextContent());
@@ -199,7 +199,7 @@ else if (ns.equals("http://www.springframework.org/schema/batch")) {
199199
}
200200

201201
private void parsePartition(Element stepElement, Element partitionElement, AbstractBeanDefinition bd,
202-
ParserContext parserContext, boolean stepUnderspecified, String jobFactoryRef) {
202+
ParserContext parserContext, String jobFactoryRef) {
203203

204204
bd.setBeanClass(StepParserStepFactoryBean.class);
205205
bd.setAttribute("isNamespaceStep", true);
@@ -258,8 +258,7 @@ else if (inlineStepElement != null) {
258258

259259
}
260260

261-
private void parseJob(Element stepElement, Element jobElement, AbstractBeanDefinition bd,
262-
ParserContext parserContext, boolean stepUnderspecified) {
261+
private void parseJob(Element jobElement, AbstractBeanDefinition bd, ParserContext parserContext) {
263262

264263
bd.setBeanClass(StepParserStepFactoryBean.class);
265264
bd.setAttribute("isNamespaceStep", true);
@@ -285,8 +284,7 @@ private void parseJob(Element stepElement, Element jobElement, AbstractBeanDefin
285284

286285
}
287286

288-
private void parseFlow(Element stepElement, Element flowElement, AbstractBeanDefinition bd,
289-
ParserContext parserContext, boolean stepUnderspecified) {
287+
private void parseFlow(Element stepElement, Element flowElement, AbstractBeanDefinition bd) {
290288

291289
bd.setBeanClass(StepParserStepFactoryBean.class);
292290
bd.setAttribute("isNamespaceStep", true);

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/listener/AbstractListenerFactoryBean.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public Object getObject() {
150150
// create a proxy listener for only the interfaces that have methods to
151151
// be called
152152
ProxyFactory proxyFactory = new ProxyFactory();
153-
if (delegate instanceof Advised) {
154-
proxyFactory.setTargetSource(((Advised) delegate).getTargetSource());
153+
if (delegate instanceof Advised advised) {
154+
proxyFactory.setTargetSource(advised.getTargetSource());
155155
}
156156
else {
157157
proxyFactory.setTarget(delegate);

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/scope/BatchScopeSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ protected Object resolveValue(Object value) {
185185

186186
BeanDefinition definition = null;
187187
String beanName = null;
188-
if (value instanceof BeanDefinition) {
189-
definition = (BeanDefinition) value;
188+
if (value instanceof BeanDefinition beanDefinition) {
189+
definition = beanDefinition;
190190
beanName = BeanDefinitionReaderUtils.generateBeanName(definition, registry);
191191
}
192192
else if (value instanceof BeanDefinitionHolder holder) {

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public void close() {
161161
}
162162

163163
Exception error = errors.get(0);
164-
if (error instanceof RuntimeException) {
165-
throw (RuntimeException) error;
164+
if (error instanceof RuntimeException runtimeException) {
165+
throw runtimeException;
166166
}
167167
else {
168168
throw new UnexpectedJobExecutionException(

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeItemReadListenerTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static org.mockito.Mockito.mock;
1919

20-
import java.util.ArrayList;
20+
import java.util.List;
2121

2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
@@ -67,11 +67,7 @@ void testOnReadError() {
6767

6868
@Test
6969
void testSetListeners() {
70-
compositeListener.setListeners(new ArrayList<>() {
71-
{
72-
add(listener);
73-
}
74-
});
70+
compositeListener.setListeners(List.of(listener));
7571
listener.beforeRead();
7672
compositeListener.beforeRead();
7773
}

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeItemWriteListenerTests.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.listener;
1717

18-
import java.util.ArrayList;
18+
import java.util.List;
1919

2020
import org.junit.jupiter.api.BeforeEach;
2121
import org.junit.jupiter.api.Test;
@@ -69,11 +69,7 @@ void testOnWriteError() {
6969

7070
@Test
7171
void testSetListeners() {
72-
compositeListener.setListeners(new ArrayList<>() {
73-
{
74-
add(listener);
75-
}
76-
});
72+
compositeListener.setListeners(List.of(listener));
7773
Chunk<Object> item = Chunk.of(new Object());
7874
listener.beforeWrite(item);
7975
compositeListener.beforeWrite(item);

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/ConfigurableSystemProcessExitCodeMapperTests.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ class ConfigurableSystemProcessExitCodeMapperTests {
3636
*/
3737
@Test
3838
void testMapping() {
39-
Map<Object, ExitStatus> mappings = new HashMap<>() {
40-
{
41-
put(0, ExitStatus.COMPLETED);
42-
put(1, ExitStatus.FAILED);
43-
put(2, ExitStatus.EXECUTING);
44-
put(3, ExitStatus.NOOP);
45-
put(4, ExitStatus.UNKNOWN);
46-
put(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.UNKNOWN);
47-
}
48-
};
39+
Map<Object, ExitStatus> mappings = Map.of( //
40+
0, ExitStatus.COMPLETED, //
41+
1, ExitStatus.FAILED, //
42+
2, ExitStatus.EXECUTING, //
43+
3, ExitStatus.NOOP, //
44+
4, ExitStatus.UNKNOWN, //
45+
ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.UNKNOWN);
4946

5047
mapper.setMappings(mappings);
5148

0 commit comments

Comments
 (0)