Fix serialization compiler warnings with Java 18

As of Java 18, the serial lint warning in javac has been expanded to
check for class fields that are not marked as `Serializable`.
See https://www.oracle.com/java/technologies/javase/18all-relnotes.html#JDK-8202056

In the Spring Framework codebase, this can happen with `Map`, `Set` or
`List` attributes which are often assigned with an unmodifiable
implementation variant. Such implementations are `Serializable` but
cannot be used as field types.

This commit ensures that the following changes are applied:
* fields are marked as transient if they can't be serialized
* classes are marked as `Serializable` if this was missing
* `@SuppressWarnings("serial")` is applied where relevant
This commit is contained in:
Brian Clozel
2022-09-09 20:24:26 +02:00
parent 82a4e374a1
commit 2b5ca63339
29 changed files with 42 additions and 11 deletions

View File

@@ -72,12 +72,14 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
/** Package-protected to allow direct access for efficiency. */
@SuppressWarnings("serial")
TargetSource targetSource = EMPTY_TARGET_SOURCE;
/** Whether the Advisors are already filtered for the specific target class. */
private boolean preFiltered = false;
/** The AdvisorChainFactory to use. */
@SuppressWarnings("serial")
AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory();
/** Cache with Method as key and advisor chain List as value. */
@@ -87,12 +89,14 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
* Interfaces to be implemented by the proxy. Held in List to keep the order
* of registration, to create JDK proxy with specified order of interfaces.
*/
@SuppressWarnings("serial")
private List<Class<?>> interfaces = new ArrayList<>();
/**
* List of Advisors. If an Advice is added, it will be wrapped
* in an Advisor before being added to this List.
*/
@SuppressWarnings("serial")
private List<Advisor> advisors = new ArrayList<>();

View File

@@ -46,8 +46,10 @@ public class ComposablePointcut implements Pointcut, Serializable {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = -2743223737633663832L;
@SuppressWarnings("serial")
private ClassFilter classFilter;
@SuppressWarnings("serial")
private MethodMatcher methodMatcher;

View File

@@ -54,7 +54,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
/** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());
protected final transient Log logger = LogFactory.getLog(getClass());
/** Name of the target bean we will create on each invocation. */
private String targetBeanName;
@@ -66,6 +66,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
* BeanFactory that owns this TargetSource. We need to hold onto this
* reference so that we can create new prototype instances as necessary.
*/
@SuppressWarnings("serial")
private BeanFactory beanFactory;

View File

@@ -42,6 +42,7 @@ public class HotSwappableTargetSource implements TargetSource, Serializable {
/** The current target object. */
@SuppressWarnings("serial")
private Object target;

View File

@@ -42,6 +42,7 @@ public class SingletonTargetSource implements TargetSource, Serializable {
/** Target cached and invoked using reflection. */
@SuppressWarnings("serial")
private final Object target;

View File

@@ -71,7 +71,7 @@ public class PrototypeBasedTargetSourceTests {
* Nonserializable test field to check that subclass
* state can't prevent serialization from working
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "serial"})
private TestBean thisFieldIsNotSerializable = new TestBean();
@Override

View File

@@ -102,6 +102,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
private final String subtype;
@SuppressWarnings("serial")
private final Map<String, String> parameters;
@Nullable

View File

@@ -47,7 +47,7 @@ final class UnmodifiableMultiValueMap<K,V> implements MultiValueMap<K,V>, Serial
private static final long serialVersionUID = -8697084563854098920L;
@SuppressWarnings("serial")
private final MultiValueMap<K, V> delegate;
@Nullable
@@ -266,7 +266,7 @@ final class UnmodifiableMultiValueMap<K,V> implements MultiValueMap<K,V>, Serial
private static final long serialVersionUID = 2407578793783925203L;
@SuppressWarnings("serial")
private final Set<Entry<K, List<V>>> delegate;
@@ -531,6 +531,7 @@ final class UnmodifiableMultiValueMap<K,V> implements MultiValueMap<K,V>, Serial
private static final long serialVersionUID = 5518377583904339588L;
@SuppressWarnings("serial")
private final Collection<List<V>> delegate;

View File

@@ -71,7 +71,7 @@ class SerializationConverterTests {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "serial"})
private Object object;
}

View File

@@ -67,11 +67,13 @@ public class ResultSetWrappingSqlRowSet implements SqlRowSet {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = -4688694393146734764L;
@SuppressWarnings("serial")
private final ResultSet resultSet;
@SuppressWarnings("serial")
private final SqlRowSetMetaData rowSetMetaData;
@SuppressWarnings("serial")
private final Map<String, Integer> columnLabelMap;

View File

@@ -115,7 +115,7 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
@Nullable
private static volatile IdGenerator idGenerator;
@SuppressWarnings("serial")
private final Map<String, Object> headers;

View File

@@ -104,6 +104,7 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
public static final String RECEIPT_ID = "receipt-id";
@SuppressWarnings("serial")
private final Map<String, List<String>> headers;

View File

@@ -46,6 +46,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
private static final long serialVersionUID = -5470210965279837728L;
@Nullable
@SuppressWarnings("serial")
private final Message<?> originalMessage;

View File

@@ -38,7 +38,7 @@ public class GenericMessage<T> implements Message<T>, Serializable {
private static final long serialVersionUID = 4268801052358035098L;
@SuppressWarnings("serial")
private final T payload;
private final MessageHeaders headers;

View File

@@ -22,6 +22,7 @@ package org.springframework.messaging.protobuf;
/**
* Protobuf type {@code Msg}
*/
@SuppressWarnings("serial")
public final class Msg extends
com.google.protobuf.GeneratedMessage
implements MsgOrBuilder {

View File

@@ -6,6 +6,7 @@ package org.springframework.messaging.protobuf;
/**
* Protobuf type {@code SecondMsg}
*/
@SuppressWarnings("serial")
public final class SecondMsg extends
com.google.protobuf.GeneratedMessage
implements SecondMsgOrBuilder {

View File

@@ -530,9 +530,9 @@ class DefaultDatabaseClient implements DatabaseClient {
private static final long serialVersionUID = -8994138383301201380L;
final Connection connection;
final transient Connection connection;
final Function<Connection, Publisher<Void>> closeFunction;
final transient Function<Connection, Publisher<Void>> closeFunction;
ConnectionCloseHolder(Connection connection,
Function<Connection, Publisher<Void>> closeFunction) {

View File

@@ -84,6 +84,7 @@ public class MergedContextConfiguration implements Serializable {
private final Class<?>[] classes;
@SuppressWarnings("serial")
private final Set<Class<? extends ApplicationContextInitializer<?>>> contextInitializerClasses;
private final String[] activeProfiles;
@@ -92,11 +93,14 @@ public class MergedContextConfiguration implements Serializable {
private final String[] propertySourceProperties;
@SuppressWarnings("serial")
private final Set<ContextCustomizer> contextCustomizers;
@SuppressWarnings("serial")
private final ContextLoader contextLoader;
@Nullable
@SuppressWarnings("serial")
private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate;
@Nullable

View File

@@ -40,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Rob Harrop
* @since 4.0
*/
@SuppressWarnings("serial")
public class DefaultTestContext implements TestContext {
private static final long serialVersionUID = -5827157174866681233L;

View File

@@ -420,6 +420,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
};
@SuppressWarnings("serial")
final MultiValueMap<String, String> headers;

View File

@@ -43,6 +43,7 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
private MediaType cachedContentType;
@Nullable
@SuppressWarnings("serial")
private List<MediaType> cachedAccept;

View File

@@ -54,6 +54,7 @@ public class RestClientResponseException extends RestClientException {
private final String responseCharset;
@Nullable
@SuppressWarnings("serial")
private Function<ResolvableType, ?> bodyConvertFunction;

View File

@@ -36,7 +36,7 @@ public class UnknownContentTypeException extends RestClientException {
private static final long serialVersionUID = 2759516676367274084L;
@SuppressWarnings("serial")
private final Type targetType;
private final MediaType contentType;

View File

@@ -22,6 +22,7 @@ package org.springframework.protobuf;
/**
* Protobuf type {@code Msg}
*/
@SuppressWarnings("serial")
public final class Msg extends
com.google.protobuf.GeneratedMessage
implements MsgOrBuilder {

View File

@@ -6,6 +6,7 @@ package org.springframework.protobuf;
/**
* Protobuf type {@code SecondMsg}
*/
@SuppressWarnings("serial")
public final class SecondMsg extends
com.google.protobuf.GeneratedMessage
implements SecondMsgOrBuilder {

View File

@@ -52,6 +52,7 @@ public class WebClientResponseException extends WebClientException {
private final HttpHeaders headers;
@Nullable
@SuppressWarnings("serial")
private final Charset responseCharset;
@Nullable

View File

@@ -22,6 +22,7 @@ package org.springframework.web.reactive.protobuf;
/**
* Protobuf type {@code Msg}
*/
@SuppressWarnings("serial")
public final class Msg extends
com.google.protobuf.GeneratedMessage
implements MsgOrBuilder {

View File

@@ -6,6 +6,7 @@ package org.springframework.web.reactive.protobuf;
/**
* Protobuf type {@code SecondMsg}
*/
@SuppressWarnings("serial")
public final class SecondMsg extends
com.google.protobuf.GeneratedMessage
implements SecondMsgOrBuilder {

View File

@@ -2765,6 +2765,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
@Controller
@RequestMapping("/myPath.do")
@SuppressWarnings("serial")
static class MyParameterDispatchingController implements Serializable {
private static final long serialVersionUID = 1L;