BeanContext.java

biz/hammurapi/config/BeanContext.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 48:23
Java Inspector 049 Use a Collection instead of arrays Object[] 2 49:23
Java Inspector 049 Use a Collection instead of arrays Object[] 2 53:30
Java Inspector 049 Use a Collection instead of arrays Object[] 2 61:63
Java Inspector 049 Use a Collection instead of arrays Object[] 2 77:30
Java Inspector 082 Parenthesis are redundant. 2 56:37
Java Inspector 082 Parenthesis are redundant. 2 56:95
Java Inspector 089 Undocumented constructor 2 42:9
Java Inspector 089 Undocumented method 2 47:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 67:97
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 69:97
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 83:89
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 83:115
Java Inspector 054 Discourage usage of instance variables like a, j by enforcing minimal variable name length (3). 3 38: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.config;
24
25import java.lang.reflect.Field;
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28
29import biz.hammurapi.convert.ConvertingService;
30
31/**
32 * Populates a bean with parameters from HttpRequest.
33 * @author Pavel Vlasov
34 *
35 * @version $Revision: 1.3 $
36 */
37public class BeanContext implements Context {
38 private static final String IS = "is";
39 private static final String GET = "get";
40 private Object bean;
41
42 public BeanContext(Object bean) {
43 super();
44 this.bean=bean;
45 }
46
47 public Object get(String name) {
48 String[] ni=split(name);
49 Method[] ma=bean.getClass().getMethods();
50
51 for (int i=0; i<ma.length; i++) {
52 Method candidate = ma[i];
53 Class[] cpt = candidate.getParameterTypes();
54 if (cpt.length==ni.length-1 && !candidate.getReturnType().equals(void.class)) {
55 String mname = candidate.getName();
56 if ((mname.startsWith(GET) && mname.length()>GET.length()) || (mname.startsWith(IS) && mname.length()>IS.length())) {
57 int prefixLength = mname.startsWith(GET) ? GET.length() : IS.length();
58 String pName = mname.length()==prefixLength+1 ? mname.substring(prefixLength).toLowerCase() : mname.substring(prefixLength, prefixLength+1).toLowerCase() + mname.substring(prefixLength+1);
59 if (ni[0].equals(pName)) {
60 try {
61 Object[] args=new Object[ni.length-1];
62 for (int j=0; j<args.length; j++) {
63 args[j]=ConvertingService.convert(ni[j+1], cpt[j]);
64 }
65 return candidate.invoke(bean, args);
66 } catch (IllegalAccessException e) {
67 throw new RuntimeConfigurationException("Cannot invoke method "+candidate, e);
68 } catch (InvocationTargetException e) {
69 throw new RuntimeConfigurationException("Cannot invoke method "+candidate, e);
70 }
71 }
72 }
73 }
74 }
75
76 if (ni.length==1) {
77 Field[] fa=bean.getClass().getFields();
78 for (int i=0; i<fa.length; i++) {
79 if (fa[i].getName().equals(ni[0])) {
80 try {
81 return fa[i].get(bean);
82 } catch (IllegalAccessException e) {
83 throw new RuntimeConfigurationException("Cannot get field "+fa[i]+" value", e);
84 }
85 }
86 }
87 }
88
89 return null;
90 }
91
92 /**
93 * Splits property name into getter name and indexes.
94 * Override this method to achieve desired functionality.
95 * @param name Property name.
96 * @return Name split into getter name and indexes, new String[] {name} if not overriden.
97 */
98 protected String[] split(String name) {
99 return new String [] {name};
100 }
101
102}
103