View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.reflect.support;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * Holds a reference to the getter and setter methods of a JavaBeans property.
26   *  
27   * @author Matt Sgarlata
28   * @author Alexander Volanis
29   * @since Feb 3, 2005
30   */
31  public class MethodHolder {
32  	
33  	private static final Log log = LogFactory.getLog(MethodHolder.class);
34  	
35      private Method mutator;
36      private Method indexedMutator;
37      private Method accessor;
38      private Method indexedAccessor;
39  
40  	public Method getAccessor() {
41  		return accessor;
42  	}
43  	public void setAccessor(Method accessor) {
44  		this.accessor = accessor;
45  	}
46  	public Method getMutator() {
47  		return mutator;
48  	}
49  	public void setMutator(Method mutator) {
50  		this.mutator = mutator;
51  	}
52      public Method getIndexedAccessor() {
53          return indexedAccessor;
54      }
55      public void setIndexedAccessor(Method indexedAccessor) {
56          this.indexedAccessor = indexedAccessor;
57      }
58      public Method getIndexedMutator() {
59          return indexedMutator;
60      }
61      public void setIndexedMutator(Method indexedMutator) {
62          this.indexedMutator = indexedMutator;
63      }
64  
65      protected String methodToString(Method method) {
66  		StringBuffer buffer = new StringBuffer();
67  		buffer.append("<");
68  		buffer.append(method.getReturnType() == null ? "void" : method.getReturnType().getName());
69  		buffer.append("> ");
70  		buffer.append(method.getName());
71  		buffer.append("(");
72  		Class[] parameterTypes = method.getParameterTypes();
73  		if (parameterTypes != null) {
74  			for (int i=0; i<parameterTypes.length; i++) {
75  				buffer.append(parameterTypes[i].getName());
76  				if (i != parameterTypes.length - 1) {
77  					buffer.append(",");
78  				}
79  			}
80  		}
81  		buffer.append(")");
82  		return buffer.toString();
83  	}
84  	
85      public void invokeMutator(Object bean, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
86          if (log.isTraceEnabled()) {
87              log.trace("Invoking mutator " + methodToString(mutator) + " on bean of class " + bean.getClass().getName());
88          }
89          mutator.invoke(bean, new Object[] { value });
90      }
91      public Object invokeAccessor(Object bean) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
92          if (log.isTraceEnabled()) {
93              log.trace("Invoking accessor " + methodToString(accessor) + " on bean of class " + bean.getClass().getName());
94          }
95          return accessor.invoke(bean, (Object[]) null);
96      }
97      public void invokeIndexedMutator(Object bean, Object index, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
98          if (log.isTraceEnabled()) {
99              log.trace("Invoking mutator " + methodToString(indexedMutator) + " on bean of class " + bean.getClass().getName());
100         }
101         indexedMutator.invoke(bean, new Object[] { index, value });
102     }
103     public Object invokeIndexedAccessor(Object bean, Object index) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
104         if (log.isTraceEnabled()) {
105             log.trace("Invoking accessor " + methodToString(indexedAccessor) + " on bean of class " + bean.getClass().getName());
106         }
107         return indexedAccessor.invoke(bean, new Object[] { index });
108     }
109 }