BaseReflectionProjector.java

biz/hammurapi/sql/BaseReflectionProjector.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
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 77:17
Java Inspector 089 Constructor is not properly documented 2 42:9
Java Inspector 089 Parameter rs documentation is too short. It is only 1 words. Should be at least 3 words. 2 54:9
Java Inspector 089 Parameter rs documentation is too short. It is only 1 words. Should be at least 3 words. 2 66:9
Java Inspector 089 Parameter columnName is not documented 2 75:9
Java Inspector 089 Method return value is not properly documented 2 75:9
Java Inspector 089 Parameter columnName is not documented 2 99:9
Java Inspector 089 Method return value is not properly documented 2 99:9
Java Inspector 089 Parameter columnName is not documented 2 109:9
Java Inspector 089 Method return value is not properly documented 2 109:9
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 101:54
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 111:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 76:67
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 101:24
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:24
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 42:9

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.sql.ResultSet;
26import java.sql.SQLException;
27import java.util.Map;
28import java.util.StringTokenizer;
29
30/**
31 * Base class for reflection projectors.
32 * @author Pavel Vlasov
33 * @version $Revision: 1.2 $
34 */
35public class BaseReflectionProjector {
36
37 private Map typeMap;
38
39 /**
40 * @param typeMap type map to be used in java.sql.ResultSet.getField(String/int, Map) method
41 */
42 protected BaseReflectionProjector(Map typeMap) {
43 this.typeMap=typeMap;
44 }
45
46 /**
47 * This implmentation uses ResultSet.getObject() method to obtain field value.
48 * Override this method if needed to provide custom type conversion.
49 * @param rs ResultSet
50 * @param columnName Field name
51 * @return Column value
52 * @throws SQLException
53 */
54 protected Object getColumn(ResultSet rs, String columnName) throws SQLException {
55 return typeMap==null ? rs.getObject(columnName) : rs.getObject(columnName, typeMap);
56 }
57
58 /**
59 * This implmentation uses ResultSet.getObject() method to obtain field value.
60 * Override this method if needed to provide custom type conversion.
61 * @param rs ResultSet
62 * @param columnNo Column number
63 * @return Field value
64 * @throws SQLException
65 */
66 protected Object getColumn(ResultSet rs, int columnNo) throws SQLException {
67 return typeMap==null ? rs.getObject(columnNo) : rs.getObject(columnNo, typeMap);
68 }
69
70 /**
71 * Converts column name such as "MY_COLUMN" to java property name such as myColumn
72 * @param columnName
73 * @return
74 */
75 public static String propertyName(String columnName) {
76 StringTokenizer st=new StringTokenizer(columnName,"_");
77 StringBuffer ret=new StringBuffer();
78 boolean capitalize=false;
79 while (st.hasMoreTokens()) {
80 String token=st.nextToken();
81 if (capitalize) {
82 ret.append(Character.toUpperCase(token.charAt(0)));
83 if (token.length()>1) {
84 ret.append(token.substring(1).toLowerCase());
85 }
86 } else {
87 ret.append(token.toLowerCase());
88 capitalize=true;
89 }
90 }
91 return ret.toString();
92 }
93
94 /**
95 * MY_COLUMN -> getMyColumn
96 * @param columnName
97 * @return
98 */
99 public static String accessorName(String columnName) {
100 String propertyName=propertyName(columnName);
101 return "get"+ (propertyName.length()<2 ? propertyName.toUpperCase() : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
102 }
103
104 /**
105 * MY_COLUMN -&gt; setMyColumn
106 * @param columnName
107 * @return
108 */
109 public static String mutatorName(String columnName) {
110 String propertyName=propertyName(columnName);
111 return "set"+ (propertyName.length()<2 ? propertyName.toUpperCase() : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
112 }
113}
114