Bean2ContextConfigurableAdapter.java

biz/hammurapi/config/Bean2ContextConfigurableAdapter.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 46:23
Java Inspector 049 Use a Collection instead of arrays Object[] 2 64:22
Java Inspector 089 Undocumented constructor 2 40:9
Java Inspector 089 Undocumented method 2 45:9
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 49:72
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 50:64
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 50:98
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 50:119
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 50:122
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 50:157
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 49:46
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 56:82
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 56:112
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 58:82
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 58:112
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 71:74
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 71:100

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.config;
24
25import java.lang.reflect.Field;
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28
29import biz.hammurapi.convert.Converter;
30
31/**
32 * Populates a bean with parameters from HttpRequest.
33 * @author Pavel Vlasov
34 *
35 * @version $Revision: 1.2 $
36 */
37public class Bean2ContextConfigurableAdapter implements ContextConfigurable {
38 private Object bean;
39
40 public Bean2ContextConfigurableAdapter(Object bean) {
41 super();
42 this.bean=bean;
43 }
44
45 public void configure(Context context, Converter converter) throws ConfigurationException {
46 Method[] ma=bean.getClass().getMethods();
47 for (int i=0; i<ma.length; i++) {
48 String mname = ma[i].getName();
49 if (mname.startsWith("set") && mname.length()>=4 && ma[i].getParameterTypes().length==1 && ma[i].getReturnType().equals(void.class)) {
50 String pName = mname.length()==4 ? mname.toLowerCase().substring(3) : mname.substring(3, 4).toLowerCase() + mname.substring(4);
51 Object value=context.get(pName);
52 if (value!=null) {
53 try {
54 ma[i].invoke(bean, new Object[] {converter.convert(value, ma[i].getParameterTypes()[0], null)});
55 } catch (IllegalAccessException e) {
56 throw new ConfigurationException("Cannot invoke method "+ma[i]+" to set parameter value "+value, e);
57 } catch (InvocationTargetException e) {
58 throw new ConfigurationException("Cannot invoke method "+ma[i]+" to set parameter value "+value, e);
59 }
60 }
61 }
62 }
63
64 Field[] fa=bean.getClass().getFields();
65 for (int i=0; i<fa.length; i++) {
66 Object value=context.get(fa[i].getName());
67 if (value!=null) {
68 try {
69 fa[i].set(bean, converter.convert(value, fa[i].getType(), null));
70 } catch (IllegalAccessException e) {
71 throw new ConfigurationException("Cannot set field "+fa[i]+" to value "+value, e);
72 }
73 }
74 }
75 }
76}
77