Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 058 | Make inner classes "private" | 1 | 84:15 |
Java Inspector 070-A | Cyclomatic complexity is too high: 17, maximum allowed is 12 | 2 | 96:17 |
Java Inspector 073 [java.lang.StringBuffer] | In Java 5 use StringBuilder instead of StringBuffer if access is single-threaded, e.g. StringBuffer is used as a local variable . | 2 | 131:33 |
Java Inspector 081 | Avoid static collections, they can grow in size over time. | 2 | 71:9 |
Java Inspector 089 | Type is not documented | 2 | 47:1 |
Java Inspector 089 | Constructor is not properly documented | 2 | 55:9 |
Java Inspector 089 | Parameter typeMap is not documented | 2 | 55:9 |
Java Inspector 089 | Constructor is not properly documented | 2 | 65:9 |
Java Inspector 089 | Parameter typeMap is not documented | 2 | 65:9 |
Java Inspector 089 | Non-private, non-local types shall be documented | 2 | 84:15 |
Java Inspector 089 | Undocumented constructor | 2 | 91:17 |
Java Inspector 089 | Undocumented method | 2 | 96:17 |
Java Inspector 089 | Undocumented method | 2 | 168:9 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 97:55 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 98:73 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 171:96 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 99:65 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 107:84 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 117:65 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 118:80 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 118:106 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 118:117 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 119:65 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 124:72 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 130:29 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 132:44 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 137:52 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 140:52 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 143:60 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 146:51 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 149:29 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 157:75 |
Java Inspector 051 | It is good practice to call in any case super() in a constructor. | 3 | 91:17 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 99:33 |
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.io.Serializable;
26import java.lang.reflect.InvocationHandler;
27import java.lang.reflect.Method;
28import java.lang.reflect.Proxy;
29import java.sql.ResultSet;
30import java.sql.SQLException;
31import java.util.ArrayList;
32import java.util.Collections;
33import java.util.HashMap;
34import java.util.Iterator;
35import java.util.List;
36import java.util.Map;
37import java.util.Map.Entry;
38
39import org.w3c.dom.Element;
40
41import biz.hammurapi.convert.ConvertingService;
42
43/**
44 * @author Pavel Vlasov
45 * @version $Revision: 1.5 $
46 */
47public class InterfaceProjector extends BaseReflectionProjector implements Projector {
48 private Class theInterface;
49 private Object delegate;
50
51 /**
52 * @param typeMap
53 * @param theInterface Iterface projected object should implement.
54 */
55 public InterfaceProjector(Class theInterface, Map typeMap) {
56 super(typeMap);
57 this.theInterface=theInterface;
58 }
59
60 /**
61 * @param typeMap
62 * @param delegate Object to delegate unmatched calls to.
63 * @param theInterface Iterface projected object should implement.
64 */
65 public InterfaceProjector(Class theInterface, Object delegate, Map typeMap) {
66 super(typeMap);
67 this.theInterface=theInterface;
68 this.delegate=delegate;
69 }
70
71 private static Map primitivesMap=new HashMap();
72
73 static {
74 primitivesMap.put(int.class, java.lang.Integer.class);
75 primitivesMap.put(boolean.class, java.lang.Boolean.class);
76 primitivesMap.put(byte.class, java.lang.Byte.class);
77 primitivesMap.put(char.class, java.lang.Character.class);
78 primitivesMap.put(double.class, java.lang.Double.class);
79 primitivesMap.put(float.class, java.lang.Float.class);
80 primitivesMap.put(long.class, java.lang.Long.class);
81 primitivesMap.put(short.class, java.lang.Short.class);
82 }
83
84 class ResultSetInvocationHandler implements InvocationHandler, Serializable {
85 /**
86 * Comment for <code>serialVersionUID</code>
87 */
88 private static final long serialVersionUID = -971624963614852201L;
89 Map fieldMap;
90
91 public ResultSetInvocationHandler(Map fieldMap) {
92 this.fieldMap=fieldMap;
93 }
94
95
96 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
97 if (method.getName().length()>3) {
98 String key = method.getName().substring(3);
99 if (method.getName().startsWith("get") && method.getParameterTypes().length==0 && fieldMap.containsKey(key)) {
100 Class prt=(Class) primitivesMap.get(method.getReturnType());
101 Object value = fieldMap.get(key);
102 if (value==null) {
103 if (prt==null) {
104 return null;
105 }
106
107 throw new IllegalArgumentException("Cannot convert 'null' to "+method.getReturnType());
108 }
109
110 Object ret = ConvertingService.convert(value, method.getReturnType());
111 if (ret==null && prt!=null) {
112 ret = ConvertingService.convert(value, prt);
113 }
114
115 if (ret==null) {
116 throw new IllegalArgumentException(
117 "Cannot convert " +
118 (value==null ? "null" : value.getClass()+" '"+value+"'") +
119 " to " +
120 method.getReturnType().getName());
121 }
122
123 return ret;
124 } else if (method.getName().startsWith("set") && method.getParameterTypes().length==1 && fieldMap.containsKey(key)) {
125 fieldMap.put(key, args[0]);
126 return null;
127 }
128 }
129
130 if ("toString".equals(method.getName()) && method.getParameterTypes().length==0) {
131 StringBuffer ret=new StringBuffer(theInterface.getName());
132 ret.append(" [");
133 List fields=new ArrayList(fieldMap.keySet());
134 Collections.sort(fields);
135 Iterator it=fields.iterator();
136 while (it.hasNext()) {
137 ret.append(" ");
138 String key=(String) it.next();
139 ret.append(key);
140 ret.append(" = ");
141 ret.append(fieldMap.get(key));
142 if (it.hasNext()) {
143 ret.append(";");
144 }
145 }
146 return ret.append(" ]").toString();
147 }
148
149 if ("toDom".equals(method.getName()) && method.getParameterTypes().length==1 && Element.class.isAssignableFrom(method.getParameterTypes()[0])) {
150 Element holder=(Element) args[0];
151 Iterator it=fieldMap.entrySet().iterator();
152 while (it.hasNext()) {
153 Map.Entry entry=(Entry) it.next();
154 Element fieldElement=holder.getOwnerDocument().createElement((String) entry.getKey());
155 holder.appendChild(fieldElement);
156 if (entry.getValue()!=null) {
157 fieldElement.setAttribute("type", entry.getValue().getClass().getName());
158 fieldElement.appendChild(fieldElement.getOwnerDocument().createTextNode(entry.getValue().toString()));
159 }
160 }
161 }
162
163 return (delegate==null ? fieldMap : delegate).getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(delegate, args);
164 }
165
166 }
167
168 public Object project(ResultSet rs) throws SQLException {
169 final Map fieldMap=new HashMap();
170 for (int i=1, c=rs.getMetaData().getColumnCount(); i<=c; i++) {
171 fieldMap.put(accessorName(rs.getMetaData().getColumnName(i)).substring(3), rs.getObject(i));
172 }
173
174 return Proxy.newProxyInstance(theInterface.getClassLoader(), new Class[] {theInterface}, new ResultSetInvocationHandler(fieldMap));
175 }
176}
177