ConstructorProjector.java

biz/hammurapi/sql/ConstructorProjector.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 058 Make inner classes "private" 1 36:9
Java Inspector 049 Use a Collection instead of arrays Object[] 2 44:9
Java Inspector 049 Use a Collection instead of arrays Object[] 2 91:23
Java Inspector 049 Use a Collection instead of arrays Object[] 2 99:23
Java Inspector 089 Non-private, non-local types shall be documented 2 36:9
Java Inspector 089 Undocumented constructor 2 39:17
Java Inspector 089 Parameter constructor is not documented 2 80:9
Java Inspector 089 Parameter typeMap is not documented 2 80:9
Java Inspector 089 Undocumented method 2 98:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 58:60
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 39:17
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 70: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.lang.reflect.Constructor;
26import java.sql.ResultSet;
27import java.sql.SQLException;
28import java.util.Map;
29
30/**
31 * This projector constructs objects using database field values
32 * @author Pavel Vlasov
33 * @version $Revision: 1.2 $
34 */
35public class ConstructorProjector extends BaseReflectionProjector implements Projector {
36 public static class ColumnName {
37 private String name;
38
39 public ColumnName(String name) {
40 this.name=name;
41 }
42 }
43
44 private Object[] args;
45 private Constructor constructor;
46
47 /**
48 * Use this constructor if target constructor shall take not only values from database fields as
49 * parameter, but other objects as well.
50 * @param constructor Constructor to instantiate object.
51 * @param args Constructor arguments. Arguments of type {@link ColumnName} will be replaced
52 * with values from corresponding fields from the database.
53 * @param typeMap See {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}. Can be null.
54 */
55 public ConstructorProjector(Constructor constructor, Object[] args, Map typeMap) {
56 super(typeMap);
57 if (constructor.getParameterTypes().length!=args.length) {
58 throw new IllegalArgumentException("argNames length shall be equal to the number of constructor parameters");
59 }
60 this.constructor=constructor;
61 this.args=args;
62 }
63
64 /**
65 * Use this constructor if only database fields values are used as target constructor parameters.
66 * @param constructor Constructor to instantiate object
67 * @param columnNames Names of columns that shall be passed as parameters to constructor.
68 * @param typeMap See {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}. Can be null.
69 */
70 public ConstructorProjector(Constructor constructor, String[] columnNames, Map typeMap) {
71 this(constructor, columnNamesToArgs(columnNames), typeMap);
72 }
73
74 /**
75 * Use constructor for positioned projection. Column values will be passed to as constructor
76 * arguments according to their position.
77 * @param constructor
78 * @param typeMap
79 */
80 public ConstructorProjector(Constructor constructor, Map typeMap) {
81 super(typeMap);
82 this.constructor=constructor;
83 }
84
85 /**
86 *
87 * @param fieldNames
88 * @return
89 */
90 private static Object[] columnNamesToArgs(String[] fieldNames) {
91 Object[] ret=new Object[fieldNames.length];
92 for (int i=0; i<fieldNames.length; i++) {
93 ret[i]=new ColumnName(fieldNames[i]);
94 }
95 return ret;
96 }
97
98 public Object project(ResultSet rs) throws SQLException {
99 Object[] params=new Object[constructor.getParameterTypes().length];
100 for (int i=0; i<params.length; i++) {
101 if (args==null) {
102 params[i]=getColumn(rs, i+1);
103 } else if (args[i] instanceof ColumnName) {
104 params[i]=getColumn(rs, ((ColumnName) args[i]).name);
105 } else {
106 params[i]=args[i];
107 }
108 }
109
110 try {
111 if (!constructor.isAccessible()) {
112 constructor.setAccessible(true); // To use private nested classes.
113 }
114 return constructor.newInstance(params);
115 } catch (Exception e) {
116 throw new SQLExceptionEx(e);
117 }
118 }
119
120}
121