@SuppressWarnings("unchecked") public <T> void register(TypeHandler<T> typeHandler) { boolean mappedTypeFound = false; MappedTypes mappedTypes = typeHandler.getClass().getAnnotation(MappedTypes.class); if (mappedTypes != null) { for (Class<?> handledType : mappedTypes.value()) { register(handledType, typeHandler); mappedTypeFound = true; } } // @since 3.1.0 - try to auto-discover the mapped type if (!mappedTypeFound && typeHandler instanceof TypeReference) { try { TypeReference<T> typeReference = (TypeReference<T>) typeHandler; register(typeReference.getRawType(), typeHandler); mappedTypeFound = true; } catch (Throwable t) { // maybe users define the TypeReference with a different type and are not assignable, so // just ignore it } } if (!mappedTypeFound) { register((Class<T>) null, typeHandler); } }
private <T> void register(Type javaType, TypeHandler<? extends T> typeHandler) { MappedJdbcTypes mappedJdbcTypes = typeHandler.getClass().getAnnotation(MappedJdbcTypes.class); if (mappedJdbcTypes != null) { for (JdbcType handledJdbcType : mappedJdbcTypes.value()) { register(javaType, handledJdbcType, typeHandler); } } else { register(javaType, null, typeHandler); } }
private void register(Type javaType, JdbcType jdbcType, TypeHandler<?> handler) { if (javaType != null) { Map<JdbcType, TypeHandler<?>> map = TYPE_HANDLER_MAP.get(javaType); if (map == null) { map = new HashMap<JdbcType, TypeHandler<?>>(); TYPE_HANDLER_MAP.put(javaType, map); } map.put(jdbcType, handler); if (reversePrimitiveMap.containsKey(javaType)) { register(reversePrimitiveMap.get(javaType), jdbcType, handler); } } ALL_TYPE_HANDLERS_MAP.put(handler.getClass(), handler); }
void write(ClassScope scope, Method method, TextOutput out) { MethodHandler.writeMethodDeclarationJava(out, method, paramNames); out.openBlock(); out.append("return "); if (isList) { out.append("readObjectArray(") .append(Util.READER_NAME) .append(", null, new ") .append(scope.requireFactoryGenerationAndGetName(typeHandler)) .append(Util.TYPE_FACTORY_NAME_POSTFIX) .append("()") .append(", false)"); } else { typeHandler.writeInstantiateCode(scope, out); out.append('(').append(Util.READER_NAME); out.comma().space(); out.append(paramNames.size() == 1 ? "null" : "nextName"); out.append(')'); } out.semi(); out.closeBlock(); }
@Test public void shouldGetResultFromResultSet() throws Exception { when(rs.getLong("column")).thenReturn(100l); when(rs.wasNull()).thenReturn(false); assertEquals(new Long(100l), TYPE_HANDLER.getResult(rs, "column")); }
@Test public void shouldGetResultFromCallableStatement() throws Exception { when(cs.getLong(1)).thenReturn(100l); when(cs.wasNull()).thenReturn(false); assertEquals(new Long(100l), TYPE_HANDLER.getResult(cs, 1)); }
@Test public void shouldSetParameter() throws Exception { TYPE_HANDLER.setParameter(ps, 1, 100l, null); verify(ps).setLong(1, 100l); }
@Override Object invoke(Object proxy, InvocationHandlerImpl invocationHandlerImpl, Object[] args) throws JsonProtocolParseException { Object obj = args[0]; return typeHandler.parseRoot(obj); }
@Test public void shouldGetResultFromCallableStatement() throws Exception { when(cs.getTimestamp(1)).thenReturn(TIMESTAMP); when(cs.wasNull()).thenReturn(false); assertEquals(DATE, TYPE_HANDLER.getResult(cs, 1)); }
@Test public void shouldGetResultFromResultSet() throws Exception { when(rs.getTimestamp("column")).thenReturn(TIMESTAMP); when(rs.wasNull()).thenReturn(false); assertEquals(DATE, TYPE_HANDLER.getResult(rs, "column")); }
@Test public void shouldSetParameter() throws Exception { TYPE_HANDLER.setParameter(ps, 1, DATE, null); verify(ps).setTimestamp(1, new java.sql.Timestamp(DATE.getTime())); }