KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > util > Reflect


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.util;
18
19
20
21 /** <p>A set of utility methods for using Java reflection.</p>
22  *
23  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann
24  */

25 public class Reflect {
26   /** <p>Assigns the value <code>pValue</code> to the beans
27    * <code>pBean</code> property <code>pPropertyName</code>.
28    * For example, if the property name is <samp>type</samp>
29    * and the String pValue is <samp>2</samp>, calls
30    * <pre>
31    * pBean.setType("2");
32    * </pre>
33    * or
34    * <pre>
35    * pBean.setType(2);
36    * </pre>
37    * if the respective method exists. Does nothing, if no
38    * matching method can be found.</p>
39    */

40   public static void assignBeanValue(Object JavaDoc pBean,
41                                        String JavaDoc pPropertyName,
42                                        String JavaDoc pValue)
43       throws IllegalAccessException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
44     assignBeanValue(pBean, pPropertyName, pValue, null);
45   }
46
47   /** <p>Assigns the value <code>pValue</code> to the beans
48    * <code>pBean</code> property <code>pPropertyName</code>.
49    * For example, if the property name is <samp>type</samp>
50    * and the String pValue is <samp>2</samp>, calls
51    * <pre>
52    * pBean.setType("2");
53    * </pre>
54    * or
55    * <pre>
56    * pBean.setType(2);
57    * </pre>
58    * if the respective method exists. Does nothing, if no
59    * matching method can be found.</p>
60    */

61   public static void assignBeanValue(Object JavaDoc pBean,
62                                        String JavaDoc pPropertyName,
63                                        String JavaDoc pValue,
64                                        ReflectResolver pResolver)
65       throws IllegalAccessException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
66     Class JavaDoc c = pBean.getClass();
67     java.lang.reflect.Method JavaDoc[] methods = c.getMethods();
68     String JavaDoc methodName = "set" + Character.toUpperCase(pPropertyName.charAt(0)) +
69       pPropertyName.substring(1);
70     for (int i = 0; i < methods.length; i++) {
71       java.lang.reflect.Method JavaDoc m = methods[i];
72       if (methodName.equals(m.getName())) {
73         Class JavaDoc[] parameterTypes = m.getParameterTypes();
74         if (parameterTypes.length == 1) {
75           Class JavaDoc parameterClass = parameterTypes[0];
76           Object JavaDoc[] parameters = null;
77           if (pResolver != null) {
78             parameters = pResolver.resolve(parameterClass, pValue);
79           }
80           if (parameters == null) {
81             if (parameterClass == String JavaDoc.class) {
82               parameters = new Object JavaDoc[]{pValue};
83             } else if (parameterClass == Long JavaDoc.class ||
84                         parameterClass == long.class) {
85               parameters = new Object JavaDoc[]{new Long JavaDoc(pValue)};
86             } else if (parameterClass == Integer JavaDoc.class ||
87                         parameterClass == int.class) {
88               parameters = new Object JavaDoc[]{new Integer JavaDoc(pValue)};
89             } else if (parameterClass == Short JavaDoc.class ||
90                         parameterClass == short.class) {
91               parameters = new Object JavaDoc[]{new Short JavaDoc(pValue)};
92             } else if (parameterClass == Byte JavaDoc.class ||
93                         parameterClass == byte.class) {
94               parameters = new Object JavaDoc[]{new Byte JavaDoc(pValue)};
95             } else if (parameterClass == Double JavaDoc.class ||
96                         parameterClass == double.class) {
97               parameters = new Object JavaDoc[]{new Double JavaDoc(pValue)};
98             } else if (parameterClass == Float JavaDoc.class ||
99                         parameterClass == float.class) {
100               parameters = new Object JavaDoc[]{new Float JavaDoc(pValue)};
101             } else if (parameterClass == Boolean JavaDoc.class ||
102                         parameterClass == boolean.class) {
103               parameters = new Object JavaDoc[]{new Boolean JavaDoc(pValue)};
104             }
105           }
106           if (parameters != null) {
107             m.invoke(pBean, parameters);
108             return;
109           }
110         }
111       }
112     }
113   }
114 }
115
Popular Tags