Skip to content

Commit 48ae732

Browse files
committed
Introduce ErrorProne, fix compiler warnings
Signed-off-by: Stefano Cordio <[email protected]>
1 parent 46d42ab commit 48ae732

File tree

168 files changed

+621
-687
lines changed

Some content is hidden

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

168 files changed

+621
-687
lines changed

.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

pom.xml

+31
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,38 @@
181181
<release>${java.version}</release>
182182
<compilerArgs>
183183
<compilerArg>-parameters</compilerArg>
184+
<!-- https://errorprone.info/docs/installation#maven -->
185+
<compilerArg>-XDcompilePolicy=simple</compilerArg>
186+
<compilerArg>--should-stop=ifError=FLOW</compilerArg>
187+
<compilerArg>
188+
-Xplugin:ErrorProne
189+
-Xep:DefaultCharset:OFF
190+
-Xep:DirectInvocationOnMock:OFF
191+
-Xep:EqualsGetClass:OFF
192+
-Xep:Finally:OFF
193+
-Xep:HidingField:OFF
194+
-Xep:JavaTimeDefaultTimeZone:OFF
195+
-Xep:JdkObsolete:OFF
196+
-Xep:MissingSummary:OFF
197+
-Xep:MixedMutabilityReturnType:OFF
198+
-Xep:MutablePublicArray:OFF
199+
-Xep:NonAtomicVolatileUpdate:OFF
200+
-Xep:ReferenceEquality:OFF
201+
-Xep:StringCaseLocaleUsage:OFF
202+
-Xep:StringSplitter:OFF
203+
-Xep:SynchronizeOnNonFinalField:OFF
204+
-Xep:UndefinedEquals:OFF
205+
-Xep:UnnecessaryStringBuilder:OFF
206+
</compilerArg>
184207
</compilerArgs>
208+
<failOnWarning>true</failOnWarning>
209+
<annotationProcessorPaths>
210+
<path>
211+
<groupId>com.google.errorprone</groupId>
212+
<artifactId>error_prone_core</artifactId>
213+
<version>2.38.0</version>
214+
</path>
215+
</annotationProcessorPaths>
185216
</configuration>
186217
</plugin>
187218
<plugin>

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
}

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) {

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
}

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

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
}

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

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

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

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

5553
private static final String JOB_LAUNCHER = "jobLauncher";
@@ -80,7 +78,8 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
8078
private void validateState(AnnotationMetadata importingClassMetadata) {
8179
if (!importingClassMetadata.isAnnotated(EnableBatchProcessing.class.getName())) {
8280
String className = importingClassMetadata.getClassName();
83-
String errorMessage = String.format(MISSING_ANNOTATION_ERROR_MESSAGE, className);
81+
String errorMessage = "EnableBatchProcessing is not present on importing class '%s' as expected"
82+
.formatted(className);
8483
throw new IllegalStateException(errorMessage);
8584
}
8685
}

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);

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
}

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

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
}

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
}

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);

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

+6-11
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,13 @@ protected AbstractBeanDefinition parseStep(Element stepElement, ParserContext pa
118118
new TaskletParser().parseTasklet(stepElement, nestedElement, bd, parserContext, stepUnderspecified);
119119
}
120120
else if (FLOW_ELE.equals(name)) {
121-
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
122-
parseFlow(stepElement, nestedElement, bd, parserContext, stepUnderspecified);
121+
parseFlow(stepElement, nestedElement, bd);
123122
}
124123
else if (PARTITION_ELE.equals(name)) {
125-
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
126-
parsePartition(stepElement, nestedElement, bd, parserContext, stepUnderspecified, jobFactoryRef);
124+
parsePartition(stepElement, nestedElement, bd, parserContext, jobFactoryRef);
127125
}
128126
else if (JOB_ELE.equals(name)) {
129-
boolean stepUnderspecified = CoreNamespaceUtils.isUnderspecified(stepElement);
130-
parseJob(stepElement, nestedElement, bd, parserContext, stepUnderspecified);
127+
parseJob(nestedElement, bd, parserContext);
131128
}
132129
else if ("description".equals(name)) {
133130
bd.setDescription(nestedElement.getTextContent());
@@ -199,7 +196,7 @@ else if (ns.equals("http://www.springframework.org/schema/batch")) {
199196
}
200197

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

204201
bd.setBeanClass(StepParserStepFactoryBean.class);
205202
bd.setAttribute("isNamespaceStep", true);
@@ -258,8 +255,7 @@ else if (inlineStepElement != null) {
258255

259256
}
260257

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

264260
bd.setBeanClass(StepParserStepFactoryBean.class);
265261
bd.setAttribute("isNamespaceStep", true);
@@ -285,8 +281,7 @@ private void parseJob(Element stepElement, Element jobElement, AbstractBeanDefin
285281

286282
}
287283

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

291286
bd.setBeanClass(StepParserStepFactoryBean.class);
292287
bd.setAttribute("isNamespaceStep", true);

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/CoreNamespacePostProcessor.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
115115
* @return the bean with default collaborators injected into it
116116
*/
117117
private Object injectDefaults(Object bean) {
118-
if (bean instanceof JobParserJobFactoryBean) {
119-
JobParserJobFactoryBean fb = (JobParserJobFactoryBean) bean;
118+
if (bean instanceof JobParserJobFactoryBean fb) {
120119
JobRepository jobRepository = fb.getJobRepository();
121120
if (jobRepository == null) {
122121
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));
123122
}
124123
}
125-
else if (bean instanceof StepParserStepFactoryBean) {
126-
StepParserStepFactoryBean<?, ?> fb = (StepParserStepFactoryBean<?, ?>) bean;
124+
else if (bean instanceof StepParserStepFactoryBean<?, ?> fb) {
127125
JobRepository jobRepository = fb.getJobRepository();
128126
if (jobRepository == null) {
129127
fb.setJobRepository((JobRepository) applicationContext.getBean(DEFAULT_JOB_REPOSITORY_NAME));

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/ExceptionElementParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public ManagedMap<TypedStringValue, Boolean> parse(Element element, ParserContex
3232
if (children.size() == 1) {
3333
ManagedMap<TypedStringValue, Boolean> map = new ManagedMap<>();
3434
Element exceptionClassesElement = children.get(0);
35-
addExceptionClasses("include", true, exceptionClassesElement, map, parserContext);
36-
addExceptionClasses("exclude", false, exceptionClassesElement, map, parserContext);
35+
addExceptionClasses("include", true, exceptionClassesElement, map);
36+
addExceptionClasses("exclude", false, exceptionClassesElement, map);
3737
map.put(new TypedStringValue(ForceRollbackForWriteSkipException.class.getName(), Class.class), true);
3838
return map;
3939
}
@@ -46,7 +46,7 @@ else if (children.size() > 1) {
4646
}
4747

4848
private void addExceptionClasses(String elementName, boolean include, Element exceptionClassesElement,
49-
ManagedMap<TypedStringValue, Boolean> map, ParserContext parserContext) {
49+
ManagedMap<TypedStringValue, Boolean> map) {
5050
for (Element child : DomUtils.getChildElementsByTagName(exceptionClassesElement, elementName)) {
5151
String className = child.getAttribute("class");
5252
map.put(new TypedStringValue(className, Class.class), include);

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/JobParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
103103
builder.addPropertyValue("restartable", restartableAttribute);
104104
}
105105

106-
String incrementer = (element.getAttribute("incrementer"));
106+
String incrementer = element.getAttribute("incrementer");
107107
if (StringUtils.hasText(incrementer)) {
108108
builder.addPropertyReference("jobParametersIncrementer", incrementer);
109109
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/SimpleFlowFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public FlowExecutionStatus handle(FlowExecutor executor) throws Exception {
205205

206206
@Override
207207
public Collection<Flow> getFlows() {
208-
return (state instanceof FlowHolder) ? ((FlowHolder) state).getFlows() : Collections.<Flow>emptyList();
208+
return (state instanceof FlowHolder flowHolder) ? flowHolder.getFlows() : Collections.emptyList();
209209
}
210210

211211
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBean.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ protected void enhanceCommonStep(StepBuilderHelper<?> builder) {
273273
builder.startLimit(startLimit);
274274
}
275275
for (Object listener : stepExecutionListeners) {
276-
if (listener instanceof StepExecutionListener) {
277-
builder.listener((StepExecutionListener) listener);
276+
if (listener instanceof StepExecutionListener stepExecutionListener) {
277+
builder.listener(stepExecutionListener);
278278
}
279279
}
280280
}
@@ -566,16 +566,16 @@ private void validateDependency(String dependentName, Object dependentValue, Str
566566
* @return {@code true} if the object has a value
567567
*/
568568
private boolean isPresent(Object o) {
569-
if (o instanceof Integer) {
570-
return isPositive((Integer) o);
569+
if (o instanceof Integer i) {
570+
return isPositive(i);
571571
}
572-
if (o instanceof Collection) {
573-
return !((Collection<?>) o).isEmpty();
572+
if (o instanceof Collection<?> collection) {
573+
return !collection.isEmpty();
574574
}
575-
if (o instanceof Map) {
576-
return !((Map<?, ?>) o).isEmpty();
575+
if (o instanceof Map<?, ?> map) {
576+
return !map.isEmpty();
577577
}
578-
return o != null;
578+
return true;
579579
}
580580

581581
/**

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/TaskletParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private void handleExceptionElement(Element element, ParserContext parserContext
213213
ManagedList<TypedStringValue> list = new ManagedList<>();
214214
list.setMergeEnabled(exceptionClassesElement.hasAttribute(MERGE_ATTR)
215215
&& Boolean.parseBoolean(exceptionClassesElement.getAttribute(MERGE_ATTR)));
216-
addExceptionClasses("include", exceptionClassesElement, list, parserContext);
216+
addExceptionClasses("include", exceptionClassesElement, list);
217217
propertyValues.addPropertyValue(propertyName, list);
218218
}
219219
else if (children.size() > 1) {
@@ -224,7 +224,7 @@ else if (children.size() > 1) {
224224
}
225225

226226
private void addExceptionClasses(String elementName, Element exceptionClassesElement,
227-
ManagedList<TypedStringValue> list, ParserContext parserContext) {
227+
ManagedList<TypedStringValue> list) {
228228
for (Element child : DomUtils.getChildElementsByTagName(exceptionClassesElement, elementName)) {
229229
String className = child.getAttribute("class");
230230
list.add(new TypedStringValue(className, Class.class));

0 commit comments

Comments
 (0)