001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
018: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
019:
020: import java.beans.PropertyDescriptor;
021: import java.lang.annotation.Annotation;
022: import java.lang.reflect.InvocationTargetException;
023: import java.lang.reflect.Method;
024:
025: import org.apache.tapestry.ioc.services.PropertyAdapter;
026:
027: public class PropertyAdapterImpl implements PropertyAdapter {
028: private final String _name;
029:
030: private final Method _readMethod;
031:
032: private final Method _writeMethod;
033:
034: private final Class _type;
035:
036: public PropertyAdapterImpl(String name, Class type,
037: Method readMethod, Method writeMethod) {
038: _name = notBlank(name, "name");
039: _type = notNull(type, "type");
040:
041: _readMethod = readMethod;
042: _writeMethod = writeMethod;
043: }
044:
045: public PropertyAdapterImpl(PropertyDescriptor descriptor) {
046: this (descriptor.getName(), descriptor.getPropertyType(),
047: descriptor.getReadMethod(), descriptor.getWriteMethod());
048: }
049:
050: public String getName() {
051: return _name;
052: }
053:
054: public Method getReadMethod() {
055: return _readMethod;
056: }
057:
058: public Class getType() {
059: return _type;
060: }
061:
062: public Method getWriteMethod() {
063: return _writeMethod;
064: }
065:
066: public boolean isRead() {
067: return _readMethod != null;
068: }
069:
070: public boolean isUpdate() {
071: return _writeMethod != null;
072: }
073:
074: public Object get(Object instance) {
075: if (_readMethod == null)
076: throw new UnsupportedOperationException(ServiceMessages
077: .readNotSupported(instance, _name));
078:
079: Throwable fail = null;
080:
081: try {
082: return _readMethod.invoke(instance);
083: } catch (InvocationTargetException ex) {
084: fail = ex.getTargetException();
085: } catch (Exception ex) {
086: fail = ex;
087: }
088:
089: throw new RuntimeException(ServiceMessages.readFailure(_name,
090: instance, fail), fail);
091: }
092:
093: public void set(Object instance, Object value) {
094: if (_writeMethod == null)
095: throw new UnsupportedOperationException(ServiceMessages
096: .writeNotSupported(instance, _name));
097:
098: Throwable fail = null;
099:
100: try {
101: _writeMethod.invoke(instance, value);
102:
103: return;
104: } catch (InvocationTargetException ex) {
105: fail = ex.getTargetException();
106: } catch (Exception ex) {
107: fail = ex;
108: }
109:
110: throw new RuntimeException(ServiceMessages.writeFailure(_name,
111: instance, fail), fail);
112: }
113:
114: public <T extends Annotation> T getAnnotation(
115: Class<T> annotationClass) {
116: T result = _readMethod != null ? _readMethod
117: .getAnnotation(annotationClass) : null;
118:
119: if (result == null && _writeMethod != null)
120: result = _writeMethod.getAnnotation(annotationClass);
121:
122: return result;
123: }
124: }
|