001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.injection.lang.reflect;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026:
027: import org.jboss.logging.Logger;
028:
029: /**
030: * Morphs a setter method into a bean property.
031: *
032: * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
033: * @version $Revision: $
034: */
035: public class MethodBeanProperty extends
036: AbstractAccessibleObjectBeanProperty<Method> {
037: private final Logger log = Logger
038: .getLogger(MethodBeanProperty.class);
039:
040: private String name = null; // lazily initialized
041:
042: /**
043: * @param method
044: */
045: public MethodBeanProperty(Method method) {
046: super (method);
047:
048: assert method.getReturnType() == Void.TYPE;
049: assert method.getParameterTypes().length == 1;
050: assert method.getName().startsWith("set");
051: }
052:
053: public Class<?> getDeclaringClass() {
054: return getMethod().getDeclaringClass();
055: }
056:
057: public String getName() {
058: if (name == null) {
059: String name = getMethod().getName().substring(3);
060: if (name.length() > 1) {
061: name = name.substring(0, 1).toLowerCase()
062: + name.substring(1);
063: } else {
064: name = name.toLowerCase();
065: }
066: this .name = name; // atomair, no synch
067: }
068: return name;
069: }
070:
071: protected Method getMethod() {
072: return getAccessibleObject();
073: }
074:
075: public Class<?> getType() {
076: return getMethod().getParameterTypes()[0];
077: }
078:
079: /* (non-Javadoc)
080: * @see org.jboss.injection.lang.reflect.BeanProperty#set(java.lang.Object, java.lang.Object)
081: */
082: public void set(Object instance, Object value) {
083: Method method = getMethod();
084: Object args[] = { value };
085: try {
086: method.invoke(instance, args);
087: } catch (IllegalAccessException e) {
088: log.fatal("illegal access on method " + method, e);
089: throw new RuntimeException(e);
090: } catch (IllegalArgumentException e) {
091: String msg = "failed to set value " + value
092: + " with setter " + method;
093: log.error(msg, e);
094: throw new IllegalArgumentException(msg);
095: } catch (InvocationTargetException e) {
096: Throwable cause = e.getCause();
097: if (cause instanceof Error)
098: throw (Error) cause;
099: if (cause instanceof RuntimeException)
100: throw (RuntimeException) cause;
101: throw new RuntimeException(cause);
102: }
103: }
104:
105: }
|