1 32 33 package com.mockobjects.eziba.sql; 34 import java.sql.SQLException ; 35 import java.util.Vector ; 36 import java.lang.reflect.Method ; 37 import java.lang.reflect.InvocationTargetException ;public class ReflectiveResultSetRowGenerator 38 implements ResultSetRowGenerator 39 { 40 41 public ReflectiveResultSetRowGenerator(String [] p_propertyNames) 42 { 43 m_propertyNames = p_propertyNames; 44 } 45 46 private final String [] m_propertyNames; 47 48 public Object [] createRow(Object p_sourceObj) 49 throws SQLException 50 { 51 Vector result = new Vector (); 52 for (int i = 0; i < m_propertyNames.length; ++i) 53 { 54 result.add(getColumnData(p_sourceObj, m_propertyNames[i])); 55 } 56 return result.toArray(); 57 } 58 59 private Object getColumnData(Object p_obj, String p_colName) 60 throws SQLException 61 { 62 try 63 { 64 Method m = p_obj.getClass().getMethod("get" + p_colName, 65 new Class [0]); 66 if (m.getReturnType().equals(Void.TYPE)) 67 { 68 throw new SQLException ( "Column-mapped method " + m 69 + " returns void while looking for " 70 + p_colName + " on " 71 + p_obj.getClass() ); 72 } 73 return m.invoke(p_obj,new Object [0]); 74 } 75 catch (NoSuchMethodException e) 76 { 77 choke(e,p_obj,p_colName); 78 } 79 catch (InvocationTargetException e) 80 { 81 choke(e,p_obj,p_colName); 82 } 83 catch (IllegalAccessException e) 84 { 85 choke(e,p_obj,p_colName); 86 } 87 return null; 89 } 90 91 private void choke(Throwable p_throwable, 92 Object p_obj, 93 String p_colName) 94 throws SQLException 95 { 96 throw new SQLException ("while looking for " + p_colName + " on " 97 + p_obj.getClass() + " got " + 98 p_throwable.toString()); 99 } 100 } | Popular Tags |