1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* --------------------------
28
* BeanObjectDescription.java
29
* --------------------------
30
* (C)opyright 2003-2005, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: BeanObjectDescription.java,v 1.6 2005/10/18 13:31:58 mungady Exp $
37
* Changes (from 19-Feb-2003)
38
* -------------------------
39
* 19-Feb-2003 : Added standard header and Javadocs (DG);
40
* 29-Apr-2003 : Distilled from the JFreeReport project and moved into
42
* 18-Aug-2005 : Added casts to suppress compiler warnings, as suggested in
47
package org.jfree.xml.factory.objects;
49
import java.lang.reflect.Method;
50
import java.lang.reflect.Modifier;
51
import java.util.Iterator;
52
import java.util.TreeSet;
53
import java.util.HashMap;
54
import java.beans.Introspector;
55
import java.beans.IntrospectionException;
56
import java.beans.BeanInfo;
57
import java.beans.PropertyDescriptor;
58
import java.io.ObjectInputStream;
59
import java.io.IOException;
61
import org.jfree.util.Log;
64
* An object-description for a bean object. This object description
65
* is very dangerous, if the bean contains properties with undefined
68
* @author Thomas Morgner
70
public class BeanObjectDescription extends AbstractObjectDescription {
72
private TreeSet ignoredParameters;
73
private transient HashMap properties;
76
* Creates a new object description.
78
* @param className the class.
80
public BeanObjectDescription(final Class className) {
81
this(className, true);
85
* Creates a new object description.
87
* @param className the class.
88
* @param init set to true, to autmaoticly initialise the object
89
* description. If set to false, the initialisation is
92
public BeanObjectDescription(final Class className, final boolean init) {
94
// now create some method descriptions ..
95
this.ignoredParameters = new TreeSet();
96
readBeanDescription(className, init);
99
private boolean isValidMethod (final Method method, final int parCount)
101
if (method == null) {
104
if (!Modifier.isPublic(method.getModifiers())) {
107
if (Modifier.isStatic(method.getModifiers())) {
110
if (method.getParameterTypes().length != parCount) {
117
* Creates an object based on this description.
119
* @return The object.
121
public Object createObject() {
123
final Object o = getObjectClass().newInstance();
124
// now add the various parameters ...
126
final Iterator it = getParameterNames();
127
while (it.hasNext()) {
128
final String name = (String) it.next();
130
if (isParameterIgnored(name)) {
134
final Method method = findSetMethod(name);
135
final Object parameterValue = getParameter(name);
136
if (parameterValue == null) {
137
// Log.debug ("Parameter: " + name + " is null");
140
method.invoke(o, new Object[]{parameterValue});
145
catch (Exception e) {
146
Log.error("Unable to invoke bean method", e);
152
* Finds a set method in the bean.
154
* @param parameterName the parameter name.
156
* @return The method.
158
private Method findSetMethod(final String parameterName) {
159
final PropertyDescriptor descriptor
160
= (PropertyDescriptor) this.properties.get(parameterName);
161
return descriptor.getWriteMethod();
165
* Finds a get method in the bean.
167
* @param parameterName the paramater name.
168
* @return The method.
170
private Method findGetMethod(final String parameterName) {
171
final PropertyDescriptor descriptor
172
= (PropertyDescriptor) this.properties.get(parameterName);
173
return descriptor.getReadMethod();
177
* Sets the parameters in the description to match the supplied object.
179
* @param o the object (<code>null</code> not allowed).
181
* @throws ObjectFactoryException if there is a problem.
183
public void setParameterFromObject(final Object o)
184
throws ObjectFactoryException {
186
throw new NullPointerException("Given object is null");
188
final Class c = getObjectClass();
189
if (!c.isInstance(o)) {
190
throw new ObjectFactoryException("Object is no instance of " + c
191
+ "(is " + o.getClass() + ")");
194
final Iterator it = getParameterNames();
195
while (it.hasNext()) {
196
final String propertyName = (String) it.next();
198
if (isParameterIgnored(propertyName)) {
203
final Method method = findGetMethod(propertyName);
204
final Object retval = method.invoke(o, (Object[]) null);
205
if (retval != null) {
206
setParameter(propertyName, retval);
209
catch (Exception e) {
210
Log.info("Exception on method invokation.", e);
217
* Adds a parameter to the ignored parameters.
219
* @param parameter the parameter.
221
protected void ignoreParameter(final String parameter) {
222
this.ignoredParameters.add (parameter);
226
* Returns a flag that indicates whether or not the specified parameter is
229
* @param parameter the parameter.
233
protected boolean isParameterIgnored (final String parameter) {
234
return this.ignoredParameters.contains(parameter);
237
private void readObject(final ObjectInputStream in)
238
throws IOException, ClassNotFoundException {
239
in.defaultReadObject();
240
readBeanDescription(getObjectClass(), false);
243
private void readBeanDescription(final Class className, final boolean init) {
245
this.properties = new HashMap();
247
final BeanInfo bi = Introspector.getBeanInfo(className);
248
final PropertyDescriptor[] propertyDescriptors
249
= bi.getPropertyDescriptors();
250
for (int i = 0; i < propertyDescriptors.length; i++)
252
final PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
253
final Method readMethod = propertyDescriptor.getReadMethod();
254
final Method writeMethod = propertyDescriptor.getWriteMethod();
255
if (isValidMethod(readMethod, 0) && isValidMethod(writeMethod, 1))
257
final String name = propertyDescriptor.getName();
258
this.properties.put(name, propertyDescriptor);
260
super.setParameterDefinition(name,
261
propertyDescriptor.getPropertyType());
266
catch (IntrospectionException e) {
267
Log.error ("Unable to build bean description", e);