mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-12-22 07:18:51 -05:00
Handle non-public default constructors
This commit is contained in:
parent
ec65aae369
commit
f7b1a52ba1
@ -53,7 +53,8 @@ public class RowToObjectMapper<T> extends RowMapper {
|
|||||||
public static final int TYPE_BOOLEAN_OBJ = _tmf.getTypeId(Boolean.class);//TypeMappingsFactory.TYPE_BOOLEAN_OBJ; // not public?
|
public static final int TYPE_BOOLEAN_OBJ = _tmf.getTypeId(Boolean.class);//TypeMappingsFactory.TYPE_BOOLEAN_OBJ; // not public?
|
||||||
|
|
||||||
protected final int _columnCount;
|
protected final int _columnCount;
|
||||||
protected final Constructor<T> resultSetConstructor;
|
protected final boolean resultSetConstructor;
|
||||||
|
protected final Constructor<T> constructor;
|
||||||
protected final Class<? extends T> _returnTypeClass; // over-ride non-generic version of this in super class
|
protected final Class<? extends T> _returnTypeClass; // over-ride non-generic version of this in super class
|
||||||
|
|
||||||
// only non-null when _returnTypeClass is an array, or a map
|
// only non-null when _returnTypeClass is an array, or a map
|
||||||
@ -99,15 +100,25 @@ public class RowToObjectMapper<T> extends RowMapper {
|
|||||||
_fields = null;
|
_fields = null;
|
||||||
|
|
||||||
// detect if returnTypeClass has a constructor that takes a ResultSet, if so, our job couldn't be easier...
|
// detect if returnTypeClass has a constructor that takes a ResultSet, if so, our job couldn't be easier...
|
||||||
Constructor<T> resultSetConstructor = null;
|
boolean resultSetConstructor = false;
|
||||||
|
Constructor<T> constructor = null;
|
||||||
try {
|
try {
|
||||||
resultSetConstructor = returnTypeClass.getConstructor(ResultSet.class);
|
constructor = returnTypeClass.getConstructor(ResultSet.class);
|
||||||
if (!resultSetConstructor.isAccessible())
|
if (!constructor.isAccessible())
|
||||||
resultSetConstructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
|
resultSetConstructor = true;
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
// do nothing, no such constructor
|
// if no resultSetConstructor find the constructor
|
||||||
|
try {
|
||||||
|
constructor = returnTypeClass.getDeclaredConstructor();
|
||||||
|
if (!constructor.isAccessible())
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
} catch (Throwable e1) {
|
||||||
|
throw new MapperException("Exception when trying to get constructor for : "+returnTypeClass.getName() + " Must have default no-arg constructor or one that takes a single ResultSet.", e1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.resultSetConstructor = resultSetConstructor;
|
this.resultSetConstructor = resultSetConstructor;
|
||||||
|
this.constructor = constructor;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_columnCount = resultSet.getMetaData().getColumnCount();
|
_columnCount = resultSet.getMetaData().getColumnCount();
|
||||||
@ -133,9 +144,9 @@ public class RowToObjectMapper<T> extends RowMapper {
|
|||||||
*/
|
*/
|
||||||
public T mapRowToReturnType() {
|
public T mapRowToReturnType() {
|
||||||
|
|
||||||
if (resultSetConstructor != null)
|
if (resultSetConstructor)
|
||||||
try {
|
try {
|
||||||
return resultSetConstructor.newInstance(_resultSet);
|
return constructor.newInstance(_resultSet);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
|
throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
|
||||||
+ _returnTypeClass.getName() + " sending in a ResultSet object as a parameter", e);
|
+ _returnTypeClass.getName() + " sending in a ResultSet object as a parameter", e);
|
||||||
@ -205,7 +216,7 @@ public class RowToObjectMapper<T> extends RowMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
resultObject = _returnTypeClass.newInstance();
|
resultObject = constructor.newInstance();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
|
throw new MapperException(e.getClass().getName() + " when trying to create instance of : "
|
||||||
+ _returnTypeClass.getName(), e);
|
+ _returnTypeClass.getName(), e);
|
||||||
|
Loading…
Reference in New Issue
Block a user