001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 12. April 2005 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.javabean;
013:
014: import java.lang.reflect.InvocationTargetException;
015: import java.lang.reflect.Method;
016: import java.lang.reflect.UndeclaredThrowableException;
017:
018: /**
019: * Provide access to a bean property.
020: *
021: * @author <a href="mailto:andrea.aime@aliceposta.it">Andrea Aime</a>
022: */
023: public class BeanProperty {
024:
025: /** the target class */
026: private Class memberClass;
027:
028: /** the property name */
029: private String propertyName;
030:
031: /** the property type */
032: private Class type;
033:
034: /** the getter */
035: protected Method getter;
036:
037: /** the setter */
038: private Method setter;
039:
040: private static final Object[] EMPTY_ARGS = new Object[0];
041:
042: /**
043: * Creates a new {@link BeanProperty}that gets the specified property from
044: * the specified class.
045: */
046: public BeanProperty(Class memberClass, String propertyName,
047: Class propertyType) {
048: this .memberClass = memberClass;
049: this .propertyName = propertyName;
050: this .type = propertyType;
051: }
052:
053: /**
054: * Gets the base class that this getter accesses.
055: */
056: public Class getBeanClass() {
057: return memberClass;
058: }
059:
060: /**
061: * Returns the property type
062: */
063: public Class getType() {
064: return type;
065: }
066:
067: /**
068: * Gets the name of the property that this getter extracts.
069: */
070: public String getName() {
071: return propertyName;
072: }
073:
074: /**
075: * Gets whether this property can get get.
076: */
077: public boolean isReadable() {
078: return (getter != null);
079: }
080:
081: /**
082: * Gets whether this property can be set.
083: */
084: public boolean isWritable() {
085: return (setter != null);
086: }
087:
088: /**
089: * Gets the value of this property for the specified Object.
090: * @throws IllegalAccessException
091: * @throws IllegalArgumentException
092: */
093: public Object get(Object member) throws IllegalArgumentException,
094: IllegalAccessException {
095: if (!isReadable())
096: throw new IllegalStateException("Property " + propertyName
097: + " of " + memberClass + " not readable");
098:
099: try {
100: return getter.invoke(member, EMPTY_ARGS);
101: } catch (InvocationTargetException e) {
102: throw new UndeclaredThrowableException(e
103: .getTargetException());
104: }
105: }
106:
107: /**
108: * Sets the value of this property for the specified Object.
109: * @throws IllegalAccessException
110: * @throws IllegalArgumentException
111: */
112: public Object set(Object member, Object newValue)
113: throws IllegalArgumentException, IllegalAccessException {
114: if (!isWritable())
115: throw new IllegalStateException("Property " + propertyName
116: + " of " + memberClass + " not writable");
117:
118: try {
119: return setter.invoke(member, new Object[] { newValue });
120: } catch (InvocationTargetException e) {
121: throw new UndeclaredThrowableException(e
122: .getTargetException());
123: }
124: }
125:
126: /**
127: * @param method
128: */
129: public void setGetterMethod(Method method) {
130: this .getter = method;
131:
132: }
133:
134: /**
135: * @param method
136: */
137: public void setSetterMethod(Method method) {
138: this.setter = method;
139: }
140: }
|