Author: cbegin
Date: Sun Jun 10 16:37:14 2007
New Revision: 545978

URL: http://svn.apache.org/viewvc?view=rev&rev=545978
Log:
Improved type safety for ResultMapConfig

Modified:
    
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
    
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
    
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java

Modified: 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java?view=diff&rev=545978&r1=545977&r2=545978
==============================================================================
--- 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
 (original)
+++ 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/builder/xml/SqlMapParser.java
 Sun Jun 10 16:37:14 2007
@@ -218,7 +218,16 @@
         String extended = 
state.applyNamespace(attributes.getProperty("extends"));
         String xmlName = attributes.getProperty("xmlName");
         String groupBy = attributes.getProperty("groupBy");
-        ResultMapConfig resultConf = state.getConfig().newResultMapConfig(id, 
resultClassName, groupBy, extended, xmlName);
+
+        resultClassName = 
state.getConfig().getTypeHandlerFactory().resolveAlias(resultClassName);
+        Class resultClass;
+        try {
+          state.getConfig().getErrorContext().setMoreInfo("Check the result 
class.");
+          resultClass = Resources.classForName(resultClassName);
+        } catch (Exception e) {
+          throw new RuntimeException("Error configuring Result.  Could not set 
ResultClass.  Cause: " + e, e);
+        }
+        ResultMapConfig resultConf = state.getConfig().newResultMapConfig(id, 
resultClass, groupBy, extended, xmlName);
         state.setResultConfig(resultConf);
       }
     });
@@ -230,12 +239,43 @@
         String jdbcType = childAttributes.getProperty("jdbcType");
         String javaType = childAttributes.getProperty("javaType");
         String columnName = childAttributes.getProperty("column");
-        String columnIndex = childAttributes.getProperty("columnIndex");
+        String columnIndexProp = childAttributes.getProperty("columnIndex");
         String statementName = childAttributes.getProperty("select");
         String resultMapName = childAttributes.getProperty("resultMap");
         String callback = childAttributes.getProperty("typeHandler");
 
-        state.getResultConfig().addResultMapping(propertyName, columnName, 
columnIndex, javaType, jdbcType, nullValue, statementName, resultMapName, 
callback);
+        state.getConfig().getErrorContext().setMoreInfo("Check the result 
mapping property type or name.");
+        Class javaClass = null;
+        try {
+          javaType = 
state.getConfig().getTypeHandlerFactory().resolveAlias(javaType);
+          if (javaType != null && javaType.length() > 0) {
+            javaClass = Resources.classForName(javaType);
+          }
+        } catch (ClassNotFoundException e) {
+          throw new RuntimeException("Error setting java type on result 
discriminator mapping.  Cause: " + e);
+        }
+
+        state.getConfig().getErrorContext().setMoreInfo("Check the result 
mapping typeHandler attribute '" + callback + "' (must be a TypeHandler or 
TypeHandlerCallback implementation).");
+        Object typeHandlerImpl = null;
+        try {
+          if (callback != null && callback.length() > 0) {
+            callback = 
state.getConfig().getTypeHandlerFactory().resolveAlias(callback);
+            typeHandlerImpl = Resources.instantiate(callback);
+          }
+        } catch (Exception e) {
+          throw new RuntimeException("Error occurred during custom type 
handler configuration.  Cause: " + e, e);
+        }
+
+        Integer columnIndex = null;
+        if (columnIndexProp != null) {
+          try {
+            columnIndex = new Integer(columnIndexProp);
+          } catch (Exception e) {
+            throw new RuntimeException("Error parsing column index.  Cause: " 
+ e, e);
+          }
+        }
+
+        state.getResultConfig().addResultMapping(propertyName, columnName, 
columnIndex, javaClass, jdbcType, nullValue, statementName, resultMapName, 
typeHandlerImpl);
       }
     });
 
@@ -256,10 +296,41 @@
         String jdbcType = childAttributes.getProperty("jdbcType");
         String javaType = childAttributes.getProperty("javaType");
         String columnName = childAttributes.getProperty("column");
-        String columnIndex = childAttributes.getProperty("columnIndex");
+        String columnIndexProp = childAttributes.getProperty("columnIndex");
         String callback = childAttributes.getProperty("typeHandler");
 
-        state.getResultConfig().setDiscriminator(columnName, columnIndex, 
javaType, jdbcType, nullValue, callback);
+        state.getConfig().getErrorContext().setMoreInfo("Check the 
disriminator type or name.");
+        Class javaClass = null;
+        try {
+          javaType = 
state.getConfig().getTypeHandlerFactory().resolveAlias(javaType);
+          if (javaType != null && javaType.length() > 0) {
+            javaClass = Resources.classForName(javaType);
+          }
+        } catch (ClassNotFoundException e) {
+          throw new RuntimeException("Error setting java type on result 
discriminator mapping.  Cause: " + e);
+        }
+
+        state.getConfig().getErrorContext().setMoreInfo("Check the result 
mapping discriminator typeHandler attribute '" + callback + "' (must be a 
TypeHandlerCallback implementation).");
+        Object typeHandlerImpl = null;
+        try {
+          if (callback != null && callback.length() > 0) {
+            callback = 
state.getConfig().getTypeHandlerFactory().resolveAlias(callback);
+            typeHandlerImpl = Resources.instantiate(callback);
+          }
+        } catch (Exception e) {
+          throw new RuntimeException("Error occurred during custom type 
handler configuration.  Cause: " + e, e);
+        }
+
+        Integer columnIndex = null;
+        if (columnIndexProp != null) {
+          try {
+            columnIndex = new Integer(columnIndexProp);
+          } catch (Exception e) {
+            throw new RuntimeException("Error parsing column index.  Cause: " 
+ e, e);
+          }
+        }
+
+        state.getResultConfig().setDiscriminator(columnName, columnIndex, 
javaClass, jdbcType, nullValue, typeHandlerImpl);
       }
     });
   }

Modified: 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java?view=diff&rev=545978&r1=545977&r2=545978
==============================================================================
--- 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
 (original)
+++ 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/ResultMapConfig.java
 Sun Jun 10 16:37:14 2007
@@ -1,6 +1,5 @@
 package com.ibatis.sqlmap.engine.conifg;
 
-import com.ibatis.common.resources.*;
 import com.ibatis.sqlmap.client.extensions.*;
 import com.ibatis.sqlmap.engine.impl.*;
 import com.ibatis.sqlmap.engine.mapping.result.*;
@@ -20,7 +19,7 @@
   private int resultMappingIndex;
   private Discriminator discriminator;
 
-  ResultMapConfig(SqlMapConfiguration config, String id, String 
resultClassName, String groupBy, String extended, String xmlName) {
+  ResultMapConfig(SqlMapConfiguration config, String id, Class resultClass, 
String groupBy, String extendsResultMap, String xmlName) {
     this.config = config;
     this.errorContext = config.getErrorContext();
     this.client = config.getClient();
@@ -29,7 +28,6 @@
     this.resultMap = new BasicResultMap(client.getDelegate());
     this.resultMappingList = new ArrayList();
     errorContext.setActivity("building a result map");
-    resultClassName = typeHandlerFactory.resolveAlias(resultClassName);
     errorContext.setObjectId(id + " result map");
     resultMap.setId(id);
     resultMap.setXmlName(xmlName);
@@ -40,17 +38,10 @@
         resultMap.addGroupByProperty(parser.nextToken());
       }
     }
-    Class resultClass;
-    try {
-      errorContext.setMoreInfo("Check the result class.");
-      resultClass = Resources.classForName(resultClassName);
-    } catch (Exception e) {
-      throw new RuntimeException("Error configuring Result.  Could not set 
ResultClass.  Cause: " + e, e);
-    }
     resultMap.setResultClass(resultClass);
     errorContext.setMoreInfo("Check the extended result map.");
-    if (extended != null) {
-      BasicResultMap extendedResultMap = (BasicResultMap) 
client.getDelegate().getResultMap(extended);
+    if (extendsResultMap != null) {
+      BasicResultMap extendedResultMap = (BasicResultMap) 
client.getDelegate().getResultMap(extendsResultMap);
       ResultMapping[] resultMappings = extendedResultMap.getResultMappings();
       for (int i = 0; i < resultMappings.length; i++) {
         resultMappingList.add(resultMappings[i]);
@@ -77,50 +68,27 @@
     client.getDelegate().addResultMap(resultMap);
   }
 
-  public void setDiscriminator(String columnName, String columnIndex, String 
javaType, String jdbcType, String nullValue, String callback) {
-    callback = typeHandlerFactory.resolveAlias(callback);
-    javaType = typeHandlerFactory.resolveAlias(javaType);
+  public void setDiscriminator(String columnName, Integer columnIndex, Class 
javaClass, String jdbcType, String nullValue, Object typeHandlerImpl) {
     TypeHandler handler;
-    if (callback != null) {
-      errorContext.setMoreInfo("Check the result mapping typeHandler attribute 
'" + callback + "' (must be a TypeHandlerCallback implementation).");
-      try {
-        Object impl = Resources.instantiate(callback);
-        if (impl instanceof TypeHandlerCallback) {
-          handler = new CustomTypeHandler((TypeHandlerCallback) impl);
-        } else if (impl instanceof TypeHandler) {
-          handler = (TypeHandler) impl;
-        } else {
-          throw new RuntimeException("The class '' is not a valid 
implementation of TypeHandler or TypeHandlerCallback");
-        }
-      } catch (Exception e) {
-        throw new RuntimeException("Error occurred during custom type handler 
configuration.  Cause: " + e, e);
+    if (typeHandlerImpl != null) {
+      if (typeHandlerImpl instanceof TypeHandlerCallback) {
+        handler = new CustomTypeHandler((TypeHandlerCallback) typeHandlerImpl);
+      } else if (typeHandlerImpl instanceof TypeHandler) {
+        handler = (TypeHandler) typeHandlerImpl;
+      } else {
+        throw new RuntimeException("The class '' is not a valid implementation 
of TypeHandler or TypeHandlerCallback");
       }
     } else {
-      errorContext.setMoreInfo("Check the result mapping property type or 
name.");
-      try {
-        Class javaClass = null;
-        if (javaType != null) {
-          javaClass = Resources.classForName(javaType);
-        }
-        handler = 
config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), 
resultMap.getResultClass(), "", javaClass, jdbcType, true);
-      } catch (ClassNotFoundException e) {
-        throw new RuntimeException("Error setting type handler on parameter 
mapping.  Cause: " + e);
-      }
+      handler = 
config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), 
resultMap.getResultClass(), "", javaClass, jdbcType, true);
     }
     BasicResultMapping mapping = new BasicResultMapping();
     mapping.setColumnName(columnName);
     mapping.setJdbcTypeName(jdbcType);
     mapping.setTypeHandler(handler);
     mapping.setNullValue(nullValue);
-    try {
-      if (javaType != null && javaType.length() > 0) {
-        mapping.setJavaType(Resources.classForName(javaType));
-      }
-    } catch (ClassNotFoundException e) {
-      throw new RuntimeException("Error setting javaType on result mapping.  
Cause: " + e);
-    }
-    if (columnIndex != null && columnIndex.length() > 0) {
-      mapping.setColumnIndex(Integer.parseInt(columnIndex));
+    mapping.setJavaType(javaClass);
+    if (columnIndex != null) {
+      mapping.setColumnIndex(columnIndex.intValue());
     }
     discriminator = new Discriminator(delegate, mapping);
     resultMap.setDiscriminator(discriminator);
@@ -133,36 +101,19 @@
     discriminator.addSubMap(value.toString(), resultMap);
   }
 
-  public void addResultMapping(String propertyName, String columnName, String 
columnIndex, String javaType, String jdbcType, String nullValue, String 
statementName, String resultMapName, String callback) {
-    callback = typeHandlerFactory.resolveAlias(callback);
-    javaType = typeHandlerFactory.resolveAlias(javaType);
+  public void addResultMapping(String propertyName, String columnName, Integer 
columnIndex, Class javaClass, String jdbcType, String nullValue, String 
statementName, String resultMapName, Object impl) {
     errorContext.setObjectId(propertyName + " mapping of the " + 
resultMap.getId() + " result map");
     TypeHandler handler;
-    if (callback != null) {
-      errorContext.setMoreInfo("Check the result mapping typeHandler attribute 
'" + callback + "' (must be a TypeHandler or TypeHandlerCallback 
implementation).");
-      try {
-        Object impl = Resources.instantiate(callback);
-        if (impl instanceof TypeHandlerCallback) {
-          handler = new CustomTypeHandler((TypeHandlerCallback) impl);
-        } else if (impl instanceof TypeHandler) {
-          handler = (TypeHandler) impl;
-        } else {
-          throw new RuntimeException("The class '" + callback + "' is not a 
valid implementation of TypeHandler or TypeHandlerCallback");
-        }
-      } catch (Exception e) {
-        throw new RuntimeException("Error occurred during custom type handler 
configuration.  Cause: " + e, e);
+    if (impl != null) {
+      if (impl instanceof TypeHandlerCallback) {
+        handler = new CustomTypeHandler((TypeHandlerCallback) impl);
+      } else if (impl instanceof TypeHandler) {
+        handler = (TypeHandler) impl;
+      } else {
+        throw new RuntimeException("The class '" + impl + "' is not a valid 
implementation of TypeHandler or TypeHandlerCallback");
       }
     } else {
-      errorContext.setMoreInfo("Check the result mapping property type or 
name.");
-      try {
-        Class javaClass = null;
-        if (javaType != null) {
-          javaClass = Resources.classForName(javaType);
-        }
-        handler = 
config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), 
resultMap.getResultClass(), propertyName, javaClass, jdbcType, true);
-      } catch (ClassNotFoundException e) {
-        throw new RuntimeException("Error setting type handler on parameter 
mapping.  Cause: " + e);
-      }
+      handler = 
config.resolveTypeHandler(client.getDelegate().getTypeHandlerFactory(), 
resultMap.getResultClass(), propertyName, javaClass, jdbcType, true);
     }
     BasicResultMapping mapping = new BasicResultMapping();
     mapping.setPropertyName(propertyName);
@@ -175,15 +126,9 @@
     if (resultMapName != null && resultMapName.length() > 0) {
       resultMap.addNestedResultMappings(mapping);
     }
-    try {
-      if (javaType != null && javaType.length() > 0) {
-        mapping.setJavaType(Resources.classForName(javaType));
-      }
-    } catch (ClassNotFoundException e) {
-      throw new RuntimeException("Error setting javaType on result mapping.  
Cause: " + e);
-    }
-    if (columnIndex != null && columnIndex.length() > 0) {
-      mapping.setColumnIndex(Integer.parseInt(columnIndex));
+    mapping.setJavaType(javaClass);
+    if (columnIndex != null) {
+      mapping.setColumnIndex(columnIndex.intValue());
     } else {
       resultMappingIndex++;
       mapping.setColumnIndex(resultMappingIndex);

Modified: 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java
URL: 
http://svn.apache.org/viewvc/ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java?view=diff&rev=545978&r1=545977&r2=545978
==============================================================================
--- 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java
 (original)
+++ 
ibatis/trunk/java/mapper/mapper2/src/com/ibatis/sqlmap/engine/conifg/SqlMapConfiguration.java
 Sun Jun 10 16:37:14 2007
@@ -137,8 +137,8 @@
     return new ParameterMapConfig(this, id, parameterClass);
   }
 
-  public ResultMapConfig newResultMapConfig(String id, String resultClassName, 
String groupBy, String extended, String xmlName) {
-    return new ResultMapConfig(this, id, resultClassName, groupBy, extended, 
xmlName);
+  public ResultMapConfig newResultMapConfig(String id, Class resultClass, 
String groupBy, String extended, String xmlName) {
+    return new ResultMapConfig(this, id, resultClass, groupBy, extended, 
xmlName);
   }
 
   public MappedStatementConfig newMappedStatementConfig(String id, 
GeneralStatement statement, SqlSource processor, String parameterMapName, 
String parameterClassName, String resultMapName, String[] 
additionalResultMapNames, String resultClassName, String[] 
additionalResultClasses, String resultSetType, String fetchSize, String 
allowRemapping, String timeout, String cacheModelName, String xmlResultName) {


Reply via email to