001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.type;
017:
018: import java.lang.reflect.Method;
019: import java.sql.CallableStatement;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.lang.NoSuchMethodException;
024:
025: /**
026: * Implementation of TypeHandler for dealing with unknown types
027: */
028: public class UnknownTypeHandler extends BaseTypeHandler implements
029: TypeHandler {
030:
031: private TypeHandlerFactory factory;
032:
033: static private boolean usingJavaPre5 = false;
034:
035: static {
036: try {
037: // try getBaseClass, if it throws no exception
038: // were in Java <5
039: getBaseClass(Class.class);
040: usingJavaPre5 = false;
041: } catch (NoSuchMethodException ex) {
042: usingJavaPre5 = true;
043: }
044: };
045:
046: /**
047: * Constructor to create via a factory
048: *
049: * @param factory - the factory to associate this with
050: */
051: public UnknownTypeHandler(TypeHandlerFactory factory) {
052: this .factory = factory;
053: }
054:
055: public void setParameter(PreparedStatement ps, int i,
056: Object parameter, String jdbcType) throws SQLException {
057: Class searchClass = parameter.getClass();
058: if (usingJavaPre5) {
059: try {
060: searchClass = getBaseClass(searchClass);
061: } catch (Exception ex) {
062: searchClass = null;
063: }
064: }
065: if (searchClass == null) {
066: searchClass = parameter.getClass();
067: }
068: TypeHandler handler = factory.getTypeHandler(searchClass,
069: jdbcType);
070: handler.setParameter(ps, i, parameter, jdbcType);
071: }
072:
073: public Object getResult(ResultSet rs, String columnName)
074: throws SQLException {
075: Object object = rs.getObject(columnName);
076: if (rs.wasNull()) {
077: return null;
078: } else {
079: return object;
080: }
081: }
082:
083: public Object getResult(ResultSet rs, int columnIndex)
084: throws SQLException {
085: Object object = rs.getObject(columnIndex);
086: if (rs.wasNull()) {
087: return null;
088: } else {
089: return object;
090: }
091: }
092:
093: public Object getResult(CallableStatement cs, int columnIndex)
094: throws SQLException {
095: Object object = cs.getObject(columnIndex);
096: if (cs.wasNull()) {
097: return null;
098: } else {
099: return object;
100: }
101: }
102:
103: public Object valueOf(String s) {
104: return s;
105: }
106:
107: public boolean equals(Object object, String string) {
108: if (object == null || string == null) {
109: return object == string;
110: } else {
111: TypeHandler handler = factory.getTypeHandler(object
112: .getClass());
113: Object castedObject = handler.valueOf(string);
114: return object.equals(castedObject);
115: }
116: }
117:
118: /**
119: * Get the base class of classParam, for top level classes
120: * this returns null. For enums, inner and anonymous
121: * classes it returns the enclosing class. The intent
122: * is to use this for enum support in Java 5+.
123: *
124: * @param classParam class to get enclosing class of
125: * @return Enclosing class
126: * @throws NoSuchMethodException when run in pre Java 5.
127: */
128: private static Class getBaseClass(Class classParam)
129: throws NoSuchMethodException {
130: String methodName = "getEnclosingClass";
131:
132: Method method = null;
133: Class result = null;
134: try {
135: method = classParam.getClass().getMethod(methodName,
136: (Class[]) null);
137: result = (Class) method.invoke(classParam, (Object[]) null);
138: } catch (Exception ex) {
139: throw new NoSuchMethodException(ex.getMessage());
140: }
141: return result;
142: }
143: }
|