SmartProjector.java

biz/hammurapi/sql/SmartProjector.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented method 2 47:9
Java Inspector 089 Undocumented constructor 2 104:9
Java Inspector 089 Undocumented constructor 2 115:5
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 109:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:120
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 122:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 124:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 124:120
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 104:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 115:5

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.sql;
24
25import java.lang.reflect.Constructor;
26import java.lang.reflect.InvocationTargetException;
27import java.sql.ResultSet;
28import java.sql.SQLException;
29
30import biz.hammurapi.config.RuntimeConfigurationException;
31import biz.hammurapi.convert.ConverterClosure;
32import biz.hammurapi.convert.ConvertingService;
33
34
35/**
36 * Base class for SQLC generated projectors.
37 * @author Pavel Vlasov
38 *
39 * @version $Revision: 1.3 $
40 */
41public class SmartProjector implements Projector {
42 private ConverterClosure converter;
43 private Class targetClass;
44 private Class sourceClass;
45 private Constructor sourceClassConstructor;
46
47 public Object project(ResultSet rs) throws SQLException {
48 Object ret;
49 if (targetClass==null) {
50 ret = defaultProject(rs);
51 } else {
52 if (sourceClass.isAssignableFrom(targetClass)) {
53 ret=project(rs, targetClass);
54 } else {
55 ret=ConvertingService.convert(defaultProject(rs), targetClass);
56 }
57 }
58
59 if (converter==null) {
60 return ret;
61 }
62
63 return converter.convert(ret);
64 }
65
66 /**
67 * @param rs
68 * @param ret
69 * @return
70 * @throws SQLException
71 */
72 private Object defaultProject(ResultSet rs) throws SQLException {
73 try {
74 if (!sourceClassConstructor.isAccessible()) {
75 sourceClassConstructor.setAccessible(true); // To use private nested classes.
76 }
77 return sourceClassConstructor.newInstance(new Object[] {rs});
78 } catch (Exception e) {
79 throw new SQLExceptionEx(e);
80 }
81 }
82
83 /**
84 * @param rs
85 * @return
86 * @throws SQLException
87 * @throws InstantiationException
88 * @throws IllegalAccessException
89 * @throws InvocationTargetException
90 * @throws NoSuchMethodException
91 */
92 private Object project(ResultSet rs, Class clazz) throws SQLException {
93 try {
94 Constructor constructor = clazz.getConstructor(new Class[] {ResultSet.class});
95 if (!constructor.isAccessible()) {
96 constructor.setAccessible(true); // To use private nested classes.
97 }
98 return constructor.newInstance(new Object[] {rs});
99 } catch (Exception e) {
100 throw new SQLExceptionEx(e);
101 }
102 }
103
104 public SmartProjector(Class sourceClass) {
105 this.sourceClass=sourceClass;
106 try {
107 this.sourceClassConstructor=sourceClass.getConstructor(new Class[] {ResultSet.class});
108 } catch (SecurityException e) {
109 throw new RuntimeConfigurationException("Caused by: "+e, e);
110 } catch (NoSuchMethodException e) {
111 throw new RuntimeConfigurationException("Constructor from ResultSet not found in "+sourceClass+": "+e, e);
112 }
113 }
114
115 public SmartProjector(Class sourceClass, Class targetClass, ConverterClosure converter) {
116 this.converter=converter;
117 this.targetClass=targetClass;
118 this.sourceClass=sourceClass;
119 try {
120 this.sourceClassConstructor=sourceClass.getConstructor(new Class[] {ResultSet.class});
121 } catch (SecurityException e) {
122 throw new RuntimeConfigurationException("Caused by: "+e, e);
123 } catch (NoSuchMethodException e) {
124 throw new RuntimeConfigurationException("Constructor from ResultSet not found in "+sourceClass+": "+e, e);
125 }
126 }
127}
128