Consistently use constructor-based instantiation instead of Class.newInstance / BeanUtils.instantiate
Issue: SPR-14486
This commit is contained in:
@@ -60,7 +60,7 @@ import org.springframework.util.StringUtils;
|
||||
* <p>To facilitate mapping between columns and fields that don't have matching names,
|
||||
* try using column aliases in the SQL statement like "select fname as first_name from customer".
|
||||
*
|
||||
* <p>For 'null' values read from the databasem, we will attempt to call the setter, but in the case of
|
||||
* <p>For 'null' values read from the database, we will attempt to call the setter, but in the case of
|
||||
* Java primitives, this causes a TypeMismatchException. This class can be configured (using the
|
||||
* primitivesDefaultedForNullValue property) to trap this exception and use the primitives default value.
|
||||
* Be aware that if you use the values from the generated bean to update the database the primitive value
|
||||
@@ -274,7 +274,7 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
|
||||
@Override
|
||||
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
|
||||
Assert.state(this.mappedClass != null, "Mapped class was not specified");
|
||||
T mappedObject = BeanUtils.instantiate(this.mappedClass);
|
||||
T mappedObject = BeanUtils.instantiateClass(this.mappedClass);
|
||||
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
|
||||
initBeanWrapper(bw);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.jdbc.support;
|
||||
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -190,9 +191,10 @@ public class SQLErrorCodes {
|
||||
public void setCustomSqlExceptionTranslatorClass(Class<? extends SQLExceptionTranslator> customTranslatorClass) {
|
||||
if (customTranslatorClass != null) {
|
||||
try {
|
||||
this.customSqlExceptionTranslator = customTranslatorClass.newInstance();
|
||||
this.customSqlExceptionTranslator =
|
||||
ReflectionUtils.accessibleConstructor(customTranslatorClass).newInstance();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("Unable to instantiate custom translator", ex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user