XmlBeanBase.java

biz/hammurapi/xml/dom/XmlBeanBase.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 43:38
Java Inspector 049 Use a Collection instead of arrays Object[] 2 65:38
Java Inspector 049 Use a Collection instead of arrays Object[] 2 118:30
Java Inspector 049 Use a Collection instead of arrays Object[] 2 146:22
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 61:17
Java Inspector 087 Chain exceptions. If exception is thrown from an exception handler (wrapping exceptions), pass the cause exception to the new exception constructor. 2 107:31
Java Inspector 089 Type is not documented 2 37:1
Java Inspector 089 Undocumented method 2 39:9
Java Inspector 089 Undocumented method 2 60:9
Java Inspector 089 Undocumented parameter arg 2 103:9
Java Inspector 089 Undocumented parameter targetClass 2 103:9
Java Inspector 089 Undocumented parameter otherBean 2 114:5
Java Inspector 089 Method return value is not documented 2 114:5
Java Inspector 089 Undocumented method 2 144:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 40:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 55:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 62:28
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 68:52
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 71:52
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 77:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 83:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 86:28
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 107:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 135:60
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 155:52
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 115:9
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 117:16
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 128:41

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.xml.dom;
24
25import java.lang.reflect.Field;
26
27import org.w3c.dom.Element;
28
29import biz.hammurapi.RuntimeException;
30import biz.hammurapi.convert.ConvertingService;
31
32
33/**
34 * @author Pavel Vlasov
35 * @version $Revision: 1.4 $
36 */
37public class XmlBeanBase implements DomSerializable {
38
39 public void toDom(Element holder) {
40 holder.setAttribute("type", getClass().getName());
41 for (Class clazz=getClass(); !clazz.equals(XmlBeanBase.class); clazz=clazz.getSuperclass()) {
42 try {
43 Field[] fields = clazz.getDeclaredFields();
44 for (int i=0, j=fields.length; i<j; i++) {
45 boolean accessible=fields[i].isAccessible();
46 fields[i].setAccessible(true);
47 Object fieldValue=fields[i].get(this);
48 fields[i].setAccessible(accessible);
49 if (fieldValue!=null) {
50 DomSerializable fds = (DomSerializable) ConvertingService.convert(fieldValue, DomSerializable.class);
51 fds.toDom(AbstractDomObject.addElement(holder,fields[i].getName()));
52 }
53 }
54 } catch (IllegalAccessException iae) {
55 throw new RuntimeException("Cannot access field: "+iae, iae);
56 }
57 }
58 }
59
60 public String toString() {
61 StringBuffer ret=new StringBuffer(getClass().getName());
62 ret.append("[");
63 for (Class clazz=getClass(); !clazz.equals(XmlBeanBase.class); clazz=clazz.getSuperclass()) {
64 try {
65 Field[] fields = clazz.getDeclaredFields();
66 for (int i=0, j=fields.length; i<j; i++) {
67 if (i>0) {
68 ret.append(", ");
69 }
70 ret.append(fields[i].getName());
71 ret.append("=");
72 boolean accessible=fields[i].isAccessible();
73 fields[i].setAccessible(true);
74 Object fieldValue=fields[i].get(this);
75 fields[i].setAccessible(accessible);
76 if (fieldValue==null) {
77 ret.append("(null)");
78 } else {
79 ret.append(fieldValue);
80 }
81 }
82 } catch (IllegalAccessException iae) {
83 throw new RuntimeException("Cannot access field: "+iae, iae);
84 }
85 }
86 ret.append("]");
87 return ret.toString();
88 }
89
90
91// /**
92// * @param rs
93// * @throws SQLException
94// */
95// protected static void checkObject(Object o, String column, String targetType) throws SQLException {
96// System.out.println(column+" -> ("+targetType+") "+(o==null ? "null" : o.getClass().getName()));
97// }
98
99 /**
100 * Converts one type to another if possible
101 * @return Converted object.
102 */
103 public static Object convert(Object arg, String targetClass) {
104 try {
105 return ConvertingService.convert(arg, Class.forName(targetClass));
106 } catch (ClassNotFoundException e) {
107 throw new ClassCastException("Target class not found: "+targetClass);
108 }
109 }
110
111 /**
112 * Two objects are considered equal and all their fields are equal.
113 */
114 public boolean equals(Object otherBean) {
115 if (otherBean==null) {
116 return false;
117 } else if (getClass().equals(otherBean.getClass())) {
118 Field[] fields = getClass().getFields();
119 for (int i=0, j=fields.length; i<j; i++) {
120 try {
121 Object fieldValue=fields[i].get(this);
122 Object otherFieldValue=fields[i].get(otherBean);
123 if (fieldValue==null) {
124 if (otherFieldValue!=null) {
125 return false;
126 }
127 } else {
128 if (otherFieldValue==null) {
129 return false;
130 } else if (!fieldValue.equals(otherFieldValue)) {
131 return false;
132 }
133 }
134 } catch (IllegalAccessException e) {
135 throw new RuntimeException("Cannot compare objects", e);
136 }
137 }
138 return true;
139 } else {
140 return false;
141 }
142 }
143
144 public int hashCode() {
145 int ret=0;
146 Field[] fields = getClass().getFields();
147 for (int i=0, j=fields.length; i<j; i++) {
148 try {
149 ret^=fields[i].getName().hashCode();
150 Object fieldValue=fields[i].get(this);
151 if (fieldValue!=null) {
152 ret^=fieldValue.hashCode();
153 }
154 } catch (IllegalAccessException e) {
155 throw new RuntimeException("Cannot calculate hash code", e);
156 }
157 }
158 return ret;
159 }
160}
161