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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
37
38
39
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
68
69
70
71
72 private Object defaultProject(ResultSet rs) throws SQLException {
73 try {
74 if (!sourceClassConstructor.isAccessible()) {
75 sourceClassConstructor.setAccessible(true);
76 }
77 return sourceClassConstructor.newInstance(new Object[] {rs});
78 } catch (Exception e) {
79 throw new SQLExceptionEx(e);
80 }
81 }
82
83
84
85
86
87
88
89
90
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);
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