Consistently use constructor-based instantiation instead of Class.newInstance / BeanUtils.instantiate
Issue: SPR-14486
This commit is contained in:
@@ -615,7 +615,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
}
|
||||
if (this.moduleClasses != null) {
|
||||
for (Class<? extends Module> module : this.moduleClasses) {
|
||||
objectMapper.registerModule(BeanUtils.instantiate(module));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(module));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -723,7 +723,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
try {
|
||||
Class<? extends Module> jdk7Module = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk7.Jdk7Module", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(jdk7Module));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(jdk7Module));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-datatype-jdk7 not available
|
||||
@@ -732,7 +732,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
try {
|
||||
Class<? extends Module> jdk8Module = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(jdk8Module));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(jdk8Module));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-datatype-jdk8 not available
|
||||
@@ -743,7 +743,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
try {
|
||||
Class<? extends Module> javaTimeModule = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(javaTimeModule));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-datatype-jsr310 not available
|
||||
@@ -755,7 +755,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
try {
|
||||
Class<? extends Module> jodaModule = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.datatype.joda.JodaModule", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(jodaModule));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(jodaModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-datatype-joda not available
|
||||
@@ -767,7 +767,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
try {
|
||||
Class<? extends Module> kotlinModule = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(kotlinModule));
|
||||
objectMapper.registerModule(BeanUtils.instantiateClass(kotlinModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-module-kotlin not available
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -45,6 +45,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConversionException;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
@@ -185,12 +186,11 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
|
||||
protected T createCollection(Class<?> collectionClass) {
|
||||
if (!collectionClass.isInterface()) {
|
||||
try {
|
||||
return (T) collectionClass.newInstance();
|
||||
return (T) ReflectionUtils.accessibleConstructor(collectionClass).newInstance();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Could not instantiate collection class [" +
|
||||
collectionClass.getName() + "]: " + ex.getMessage());
|
||||
"Could not instantiate collection class: " + collectionClass.getName(), ex);
|
||||
}
|
||||
}
|
||||
else if (List.class == collectionClass) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -36,7 +36,6 @@ import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -154,12 +153,13 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
protected Source processSource(Source source) {
|
||||
if (source instanceof StreamSource) {
|
||||
StreamSource streamSource = (StreamSource) source;
|
||||
InputSource inputSource = new InputSource(streamSource.getInputStream());
|
||||
try {
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
|
||||
String featureName = "http://xml.org/sax/features/external-general-entities";
|
||||
xmlReader.setFeature(featureName, isProcessExternalEntities());
|
||||
|
||||
@@ -45,7 +45,6 @@ import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
@@ -189,16 +188,17 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private SAXSource readSAXSource(InputStream body) throws IOException {
|
||||
try {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
|
||||
reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
|
||||
XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
|
||||
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
|
||||
if (!isProcessExternalEntities()) {
|
||||
reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
|
||||
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
|
||||
}
|
||||
byte[] bytes = StreamUtils.copyToByteArray(body);
|
||||
return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
|
||||
return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes)));
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
|
||||
|
||||
@@ -27,6 +27,7 @@ import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.HandlesTypes;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Servlet 3.0 {@link ServletContainerInitializer} designed to support code-based
|
||||
@@ -149,7 +150,8 @@ public class SpringServletContainerInitializer implements ServletContainerInitia
|
||||
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
|
||||
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
|
||||
try {
|
||||
initializers.add((WebApplicationInitializer) waiClass.newInstance());
|
||||
initializers.add((WebApplicationInitializer)
|
||||
ReflectionUtils.accessibleConstructor(waiClass).newInstance());
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
|
||||
|
||||
@@ -326,40 +326,6 @@ public abstract class WebUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the specified session attribute, creating and setting a new attribute if
|
||||
* no existing found. The given class needs to have a public no-arg constructor.
|
||||
* Useful for on-demand state objects in a web tier, like shopping carts.
|
||||
* @param session current HTTP session
|
||||
* @param name the name of the session attribute
|
||||
* @param clazz the class to instantiate for a new attribute
|
||||
* @return the value of the session attribute, newly created if not found
|
||||
* @throws IllegalArgumentException if the session attribute could not be instantiated
|
||||
*/
|
||||
public static Object getOrCreateSessionAttribute(HttpSession session, String name, Class<?> clazz)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
Assert.notNull(session, "Session must not be null");
|
||||
Object sessionObject = session.getAttribute(name);
|
||||
if (sessionObject == null) {
|
||||
try {
|
||||
sessionObject = clazz.newInstance();
|
||||
}
|
||||
catch (InstantiationException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Could not instantiate class [" + clazz.getName() +
|
||||
"] for session attribute '" + name + "': " + ex.getMessage());
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new IllegalArgumentException(
|
||||
"Could not access default constructor of class [" + clazz.getName() +
|
||||
"] for session attribute '" + name + "': " + ex.getMessage());
|
||||
}
|
||||
session.setAttribute(name, sessionObject);
|
||||
}
|
||||
return sessionObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the best available mutex for the given session:
|
||||
* that is, an object to synchronize on for the given session.
|
||||
|
||||
Reference in New Issue
Block a user