KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > eziba > sql > ReflectiveResultSetRowGenerator


1 /*
2  * Copyright (C) 2001 eZiba.com, Inc.
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution. Neither the name of eZiba.com nor the
15  * names of its contributors may be used to endorse or promote
16  * products derived from this software without specific prior
17  * written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * eZiba.com OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.mockobjects.eziba.sql;
34 import java.sql.SQLException JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;public class ReflectiveResultSetRowGenerator
38     implements ResultSetRowGenerator
39 {
40
41     public ReflectiveResultSetRowGenerator(String JavaDoc [] p_propertyNames)
42     {
43         m_propertyNames = p_propertyNames;
44     }
45
46     private final String JavaDoc [] m_propertyNames;
47
48     public Object JavaDoc [] createRow(Object JavaDoc p_sourceObj)
49         throws SQLException JavaDoc
50     {
51         Vector JavaDoc result = new Vector JavaDoc();
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 JavaDoc getColumnData(Object JavaDoc p_obj, String JavaDoc p_colName)
60         throws SQLException JavaDoc
61     {
62         try
63         {
64             Method JavaDoc m = p_obj.getClass().getMethod("get" + p_colName,
65                                                   new Class JavaDoc[0]);
66             if (m.getReturnType().equals(Void.TYPE))
67             {
68                 throw new SQLException JavaDoc( "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 JavaDoc[0]);
74         }
75         catch (NoSuchMethodException JavaDoc e)
76         {
77             choke(e,p_obj,p_colName);
78         }
79         catch (InvocationTargetException JavaDoc e)
80         {
81             choke(e,p_obj,p_colName);
82         }
83         catch (IllegalAccessException JavaDoc e)
84         {
85             choke(e,p_obj,p_colName);
86         }
87         // never get here, but compiler needs it
88
return null;
89     }
90
91     private void choke(Throwable JavaDoc p_throwable,
92                        Object JavaDoc p_obj,
93                        String JavaDoc p_colName)
94         throws SQLException JavaDoc
95     {
96         throw new SQLException JavaDoc("while looking for " + p_colName + " on "
97                                + p_obj.getClass() + " got " +
98                                p_throwable.toString());
99     }
100 }
Popular Tags