Code cleanup

- Inline variables
- Remove unused variables
- Remove redundant array creation
- Remove redundant `@SuppressWarnings`
- Remove unnecessary exceptions from throws list
- Remove redundant initializers
- Use List.subList().clear() where appropriate
- Use try-with-resources where needed
- Use pattern variables where appropriate
This commit is contained in:
Mahmoud Ben Hassine
2023-07-12 23:25:18 +02:00
parent 4763480c57
commit 52064c0541
103 changed files with 169 additions and 224 deletions

View File

@@ -36,7 +36,6 @@ import org.springframework.lang.Nullable;
* @author Douglas Kaminsky
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class ExecutionContext implements Serializable {
private volatile boolean dirty = false;
@@ -340,13 +339,12 @@ public class ExecutionContext implements Serializable {
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof ExecutionContext == false) {
if (!(obj instanceof ExecutionContext rhs)) {
return false;
}
if (this == obj) {
return true;
}
ExecutionContext rhs = (ExecutionContext) obj;
return this.entrySet().equals(rhs.entrySet());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.batch.item;
* A base exception class that all exceptions thrown from an {@link ItemReader} extend.
*
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public abstract class ItemReaderException extends RuntimeException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.batch.item;
*
* @author Dave Syer
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class ItemStreamException extends RuntimeException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.batch.item;
* A base exception class that all exceptions thrown from an {@link ItemWriter} extend.
*
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public abstract class ItemWriterException extends RuntimeException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.batch.item;
* the exception should be considered fatal.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class NonTransientResourceException extends ItemReaderException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@ package org.springframework.batch.item;
*
* @author Lucas Ward
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class ParseException extends ItemReaderException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@ package org.springframework.batch.item;
* Exception indicating that an {@link ItemReader} needed to be opened before read.
*
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class ReaderNotOpenException extends ItemReaderException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,8 +23,8 @@ package org.springframework.batch.item;
*
* @author Dave Syer
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class UnexpectedInputException extends ItemReaderException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@ package org.springframework.batch.item;
*
* @author Lucas Ward
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class WriteFailedException extends ItemWriterException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ package org.springframework.batch.item;
* written to.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class WriterNotOpenException extends ItemWriterException {
/**

View File

@@ -68,7 +68,7 @@ public abstract class AbstractMethodInvokingDelegator<T> implements Initializing
*/
protected T invokeDelegateMethodWithArgument(Object object) throws Exception {
MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod);
invoker.setArguments(new Object[] { object });
invoker.setArguments(object);
return doInvoke(invoker);
}
@@ -221,7 +221,6 @@ public abstract class AbstractMethodInvokingDelegator<T> implements Initializing
*
* @author Robert Kasanicky
*/
@SuppressWarnings("serial")
public static class InvocationTargetThrowableWrapper extends RuntimeException {
public InvocationTargetThrowableWrapper(Throwable cause) {

View File

@@ -117,7 +117,7 @@ public class RepositoryItemWriter<T> implements ItemWriter<T>, InitializingBean
MethodInvoker invoker = createMethodInvoker(repository, methodName);
for (T object : items) {
invoker.setArguments(new Object[] { object });
invoker.setArguments(object);
doInvoke(invoker);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -146,7 +146,7 @@ public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T> {
* @param connection to the database
*/
@Override
protected void cleanupOnClose(Connection connection) throws Exception {
protected void cleanupOnClose(Connection connection) {
JdbcUtils.closeStatement(this.preparedStatement);
JdbcUtils.closeConnection(connection);
}

View File

@@ -239,7 +239,7 @@ public class StoredProcedureItemReader<T> extends AbstractCursorItemReader<T> {
* @param connection to the database
*/
@Override
protected void cleanupOnClose(Connection connection) throws Exception {
protected void cleanupOnClose(Connection connection) {
JdbcUtils.closeStatement(this.callableStatement);
JdbcUtils.closeConnection(connection);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,12 +24,13 @@ import org.springframework.core.io.Resource;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @since 2.1
*/
public class DefaultBufferedReaderFactory implements BufferedReaderFactory {
@Override
public BufferedReader create(Resource resource, String encoding) throws UnsupportedEncodingException, IOException {
public BufferedReader create(Resource resource, String encoding) throws IOException {
return new BufferedReader(new InputStreamReader(resource.getInputStream(), encoding));
}

View File

@@ -26,7 +26,6 @@ import org.springframework.batch.item.ParseException;
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class FlatFileParseException extends ParseException {
private final String input;

View File

@@ -23,7 +23,6 @@ import org.springframework.batch.item.NonTransientResourceException;
* @author Dave Syer
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class NonTransientFlatFileException extends NonTransientResourceException {
private final String input;

View File

@@ -74,7 +74,7 @@ public class SimpleBinaryBufferedReaderFactory implements BufferedReaderFactory
@Override
public String readLine() throws IOException {
StringBuilder buffer = null;
StringBuilder buffer;
synchronized (lock) {

View File

@@ -23,6 +23,7 @@ import org.springframework.util.StringUtils;
* they do not have unterminated quotes, and do not end in a continuation marker.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class DefaultRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
@@ -31,9 +32,9 @@ public class DefaultRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
private static final String CONTINUATION = "\\";
private String quoteCharacter = QUOTE;
private String quoteCharacter;
private String continuation = CONTINUATION;
private String continuation;
/**
* Default constructor.

View File

@@ -162,9 +162,7 @@ public abstract class AbstractLineTokenizer implements LineTokenizer {
}
else {
// truncate token list to match the number of expected tokens
for (int i = tokensSize - 1; i >= nameLength; i--) {
tokens.remove(i);
}
tokens.subList(nameLength, tokensSize).clear();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@ package org.springframework.batch.item.file.transform;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
@SuppressWarnings("serial")
public class ConversionException extends RuntimeException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,9 +21,9 @@ package org.springframework.batch.item.file.transform;
*
* @author Lucas Ward
* @author Michael Minella
* @author Mahmoud Ben Hassine
*
*/
@SuppressWarnings("serial")
public class FlatFileFormatException extends RuntimeException {
private String input;

View File

@@ -23,7 +23,6 @@ package org.springframework.batch.item.file.transform;
* @author Mahmoud Ben Hassine
* @since 1.1
*/
@SuppressWarnings("serial")
public class IncorrectLineLengthException extends FlatFileFormatException {
private final int actualLength;

View File

@@ -24,7 +24,6 @@ package org.springframework.batch.item.file.transform;
* @author Mahmoud Ben Hassine
* @since 1.1
*/
@SuppressWarnings("serial")
public class IncorrectTokenCountException extends FlatFileFormatException {
private final int actualCount;

View File

@@ -586,7 +586,8 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
return writer;
}
else {
Writer writer = new BufferedWriter(Channels.newWriter(fileChannel, encoding)) {
return new BufferedWriter(Channels.newWriter(fileChannel, encoding)) {
@Override
public void flush() throws IOException {
super.flush();
@@ -595,8 +596,6 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
}
}
};
return writer;
}
}
catch (UnsupportedCharsetException ucse) {
@@ -612,7 +611,7 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
* @throws IOException if there is an IO problem
*/
private void checkFileSize() throws IOException {
long size = -1;
long size;
outputBufferedWriter.flush();
size = fileChannel.size();

View File

@@ -87,7 +87,7 @@ public abstract class AbstractItemCountingItemStreamItemReader<T> extends Abstra
@Nullable
@Override
public T read() throws Exception, UnexpectedInputException, ParseException {
public T read() throws Exception {
if (currentItemCount >= maxItemCount) {
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ import org.springframework.lang.Nullable;
* </p>
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, PeekableItemReader<T> {
@@ -67,11 +68,10 @@ public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, Pee
*/
@Nullable
@Override
public T read() throws Exception, UnexpectedInputException, ParseException {
public T read() throws Exception {
if (next != null) {
T item = next;
next = null;
// executionContext = new ExecutionContext();
return item;
}
return delegate.read();
@@ -86,7 +86,7 @@ public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, Pee
*/
@Nullable
@Override
public T peek() throws Exception, UnexpectedInputException, ParseException {
public T peek() throws Exception {
if (next == null) {
updateDelegate(executionContext);
next = delegate.read();

View File

@@ -50,8 +50,7 @@ public class SynchronizedItemStreamReader<T> implements ItemStreamReader<T>, Ini
* This delegates to the read method of the <code>delegate</code>
*/
@Nullable
public synchronized T read()
throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
public synchronized T read() throws Exception {
return this.delegate.read();
}

View File

@@ -39,9 +39,10 @@ public class BeanValidatingItemProcessor<T> extends ValidatingItemProcessor<T> {
* configuration.
*/
public BeanValidatingItemProcessor() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.afterPropertiesSet();
this.validator = localValidatorFactoryBean.getValidator();
try (LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean()) {
localValidatorFactoryBean.afterPropertiesSet();
this.validator = localValidatorFactoryBean.getValidator();
}
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,8 +22,8 @@ import org.springframework.batch.item.ItemReaderException;
* This exception should be thrown when there are validation errors.
*
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class ValidationException extends ItemReaderException {
/**

View File

@@ -259,7 +259,7 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
T item = null;
boolean success = false;
boolean success;
try {
success = moveCursorToNextFragment(fragmentReader);
}

View File

@@ -445,8 +445,8 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
private void open(long position) {
File file;
FileOutputStream os = null;
FileChannel fileChannel = null;
FileOutputStream os;
FileChannel fileChannel;
try {
file = resource.getFile();
@@ -479,9 +479,9 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
}
try {
final FileChannel channel = fileChannel;
if (transactional) {
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, this::closeStream);
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(fileChannel,
this::closeStream);
writer.setEncoding(encoding);
writer.setForceSync(forceSync);
@@ -496,7 +496,7 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
if (!restarted) {
startDocument(delegateEventWriter);
if (forceSync) {
channel.force(false);
fileChannel.force(false);
}
}
}
@@ -539,8 +539,7 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T>
* loaded.
*/
protected XMLEventFactory createXmlEventFactory() throws FactoryConfigurationError {
XMLEventFactory factory = XMLEventFactory.newInstance();
return factory;
return XMLEventFactory.newInstance();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ import org.springframework.batch.item.ItemStreamException;
* Default implementation of {@link FragmentEventReader}
*
* @author Robert Kasanicky
* @author Mahmoud Ben Hassine
*/
public class DefaultFragmentEventReader extends AbstractEventReaderWrapper implements FragmentEventReader {
@@ -49,9 +50,9 @@ public class DefaultFragmentEventReader extends AbstractEventReaderWrapper imple
// true when reader should behave like the cursor was at the end of document
private boolean fakeDocumentEnd = false;
private StartDocument startDocumentEvent = null;
private final StartDocument startDocumentEvent;
private EndDocument endDocumentEvent = null;
private final EndDocument endDocumentEvent;
// fragment root name is remembered so that the matching closing element can
// be identified

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2010 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import java.util.concurrent.TimeoutException;
* background thread to do the polling).
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @param <S> the type of the result
*/
public class DirectPoller<S> implements Poller<S> {
@@ -93,7 +94,7 @@ public class DirectPoller<S> implements Poller<S> {
throw new ExecutionException(e);
}
Long nextExecutionTime = startTime + interval;
long nextExecutionTime = startTime + interval;
long currentTimeMillis = System.currentTimeMillis();
long timeoutMillis = TimeUnit.MILLISECONDS.convert(timeout, unit);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.batch.repeat;
import org.springframework.core.NestedRuntimeException;
@SuppressWarnings("serial")
public class RepeatException extends NestedRuntimeException {
public RepeatException(String msg) {

View File

@@ -132,7 +132,6 @@ public class RepeatOperationsInterceptor implements MethodInterceptor {
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
private static class RepeatOperationsInterceptorException extends RepeatException {
public RepeatOperationsInterceptorException(String message, Throwable e) {

View File

@@ -64,7 +64,7 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar {
this.customEditors = new HashMap<>();
for (Entry<?, ? extends PropertyEditor> entry : customEditors.entrySet()) {
Object key = entry.getKey();
Class<?> requiredType = null;
Class<?> requiredType;
if (key instanceof Class<?>) {
requiredType = (Class<?>) key;
}

View File

@@ -33,6 +33,7 @@ import org.springframework.util.ReflectionUtils;
* Utility methods for create MethodInvoker instances.
*
* @author Lucas Ward
* @author Mahmoud Ben Hassine
* @since 2.0
*/
public class MethodInvokerUtils {
@@ -179,11 +180,9 @@ public class MethodInvokerUtils {
/**
* Create a {@link MethodInvoker} for the delegate from a single public method.
* @param target an object to search for an appropriate method.
* @param <C> the class.
* @param <T> the type.
* @return a {@link MethodInvoker} that calls a method on the delegate.
*/
public static <C, T> MethodInvoker getMethodInvokerForSingleArgument(Object target) {
public static MethodInvoker getMethodInvokerForSingleArgument(Object target) {
final AtomicReference<Method> methodHolder = new AtomicReference<>();
ReflectionUtils.doWithMethods(target.getClass(), method -> {
if (method.getParameterTypes() == null || method.getParameterTypes().length != 1) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2008 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@ package org.springframework.batch.support.transaction;
*
* @author Lucas Ward
* @author Ben Hale
* @author Mahmoud Ben Hassine
*/
@SuppressWarnings("serial")
public class FlushFailedException extends RuntimeException {
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@ import org.springframework.transaction.support.AbstractPlatformTransactionManage
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@SuppressWarnings("serial")
public class ResourcelessTransactionManager extends AbstractPlatformTransactionManager {
@Override