In both classes EnumTypeHandler
and EnumOrdinalTypeHandler
type defined as: private Class<E> type;
:
* https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java#L28
* https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/type/EnumOrdinalTypeHandler.java#L28-L29
So in most cases I should copy/paste implementation if I want customize it.
Please change it to protected
Comment From: hazendaz
If you need to extend it seems you might be replacing mostly what it does. If that is the case wouldn't it make more sense to write a new typehandler?
Get Outlook for Androidhttps://aka.ms/ghei36
On Sun, Jan 15, 2017 at 1:49 PM -0500, "Pavel Alexeev aka Pahan-Hubbitus" notifications@github.comamp#110;amp#111;amp#116;amp#105;amp#102;amp#105;amp#99;amp#97;amp#116;amp#105;amp#111;amp#110;amp#115;amp#64;amp#103;amp#105;amp#116;amp#104;amp#117;amp#98;amp#46;amp#99;amp#111;amp#109; wrote:
In both classes EnumTypeHandler and EnumOrdinalTypeHandler type defined as: private Class
- https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/type/EnumTypeHandler.java#L28
- https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/type/EnumOrdinalTypeHandler.java#L28-L29
So in most cases I should copy/paste implementation if I want customize it. Please change it to protected
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/mybatis/mybatis-3/issues/898, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AA7howUpMMU4pSzywmJV-4vjxwXQV0nAks5rSmovgaJpZM4Lj_uz.
Comment From: harawata
I agree with @hazendaz . Please let us know if we misunderstood your point.
Comment From: Hubbitus
My intention was to generalize idea from http://eminmamedov.org/?p=313 (sorry on Russian). But intention was to construct Enum
just by reusing Jackson annotation @JsonCreator
and do not register different type for any Enum
, but have universal one.
Unfortunately EnumTypeHandler
has private type field, so we can't just extend it and mostly copy/paste
Now prototype implemented exactly as you said:
public class EnumJsonCreatorAnnotationTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
but on my mind 80% is just copy/paste of EnumTypeHandler
code instead of extending just on differences.
Comment From: harawata
@Hubbitus ,
Could you post both implementations of your EnumJsonCreatorAnnotationTypeHandler
?
- Current implementation.
- The ideal implementation when the modifier of
EnumTypeHandler.type
isprotected
.
I am not sure how they can be so different.
Comment From: Hubbitus
@harawata sorry for the long delay. My current implementation:
public class EnumJsonCreatorAnnotationTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
private Class<E> type;
/**
* Full copy/paste from {@see EnumTypeHandler}. See https://github.com/mybatis/mybatis-3/issues/898
*/
public EnumJsonCreatorAnnotationTypeHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
}
/**
* Full copy/paste from {@see EnumTypeHandler}. See https://github.com/mybatis/mybatis-3/issues/898
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
if (jdbcType == null) {
ps.setString(i, parameter.name());
} else {
ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589
}
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
return enumFromDbValue(rs.getString(columnName));
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return enumFromDbValue(rs.getString(columnIndex));
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return enumFromDbValue(cs.getString(columnIndex));
}
private E enumFromDbValue(String str){
try {
return str == null ? null : EnumUtils.enumFromValue(type, str);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
So, I would like to remove unnecessary code copy pasting for the first two methods.
Comment From: harawata
Thanks for the update, @Hubbitus ,
Even if we change the access modifier to protected
, you still need to define the constructor, so it does not make much difference.
Extending the BaseTypeHandler
is the right implementation.