TypeHandlerRegistry and enum type handlers

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

TypeHandlerRegistry and enum type handlers

Nicolai Willems
Hello mybatis

I'm having some thoughts about the TypeHandlerRegistry
(http://code.google.com/p/mybatis/source/browse/trunk/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java).

First of all, when dealing with Enum's it would be nice if that could
be overrideable.
As for now(See line 133) a "static" link to the EnumTypeHandler is
created. This also means that you
can't override who handles Enums.
I think this could be solved by introducing some magic constant(could
be java.lang.Enum) added to the
handler-map, where the Enum type handler would be registered by
default - then you could in your
TypeHandler section of your configuration, say that SomeTypeHandler
should handle
javaType="MAGIC_ENUM_CONST".

So on line 133 instead of:
    handler = new EnumTypeHandler(type);
It could read:
    handler = TYPE_HANDLER_MAP.get(java.lang.Enum.class).get(null);

This approach only leaves a minor thing about supplying the type to the handler.
If on the other hand the handler map shouldn't be polluted by this
enum stuff, it could be stored
as a private final variable as for UNKNOWN, that approach just leaves
the check to the registering-
mechanism: check if supplied type is java.lang.Enum.class, then set
the enum handler field.

Secondly, why on earth are every type handler instantiated more than
once? This seems a bit
inconsistent, since UNKNOWN_TYPE_HANDLER, is used to handle the following types:
* Object
* Object, Jdbc.UNKNOWN
* Jdbc.OTHER

This seems like a lot of overhead, or am I on a totally wrong track here?

I hope this feedback is helpful
/Nicolai W.