KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > ConfigParamIntrospector


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet;
6
7 import java.beans.Introspector JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.lang.reflect.Modifier JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14 import org.apache.commons.logging.Log;
15
16 import xdoclet.util.LogUtil;
17
18 /**
19  * Used by DocletTask. Creates and returns a HashMap of config params for a DocletTask or SubTask.
20  *
21  * @author Ara Abrahamian (ara_e@email.com)
22  * @created Jan 19, 2002
23  * @version $Revision: 1.8 $
24  */

25 public final class ConfigParamIntrospector
26 {
27     public final static Object JavaDoc NULL = new NullObject();
28
29     /**
30      * Describe what the method does
31      *
32      * @param name Describe what the parameter does
33      * @return Describe the return value
34      */

35     public static String JavaDoc capitalize(String JavaDoc name)
36     {
37         if (name == null || name.trim().length() == 0) {
38             return name;
39         }
40
41         if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
42             Character.isLowerCase(name.charAt(0))) {
43             return name;
44         }
45
46         char chars[] = name.toCharArray();
47
48         chars[0] = Character.toUpperCase(chars[0]);
49
50         return new String JavaDoc(chars);
51     }
52
53     /**
54      * Describe what the method does
55      *
56      * @param javabean Describe what the parameter does
57      * @param capitalPropertyName Describe what the parameter does
58      * @return Describe the return value
59      */

60     public static Method JavaDoc findGetterMethod(Object JavaDoc javabean, String JavaDoc capitalPropertyName)
61     {
62         Log log = LogUtil.getLog(ConfigParamIntrospector.class, "findGetterMethod");
63
64         Method JavaDoc getterMethod = null;
65
66         capitalPropertyName = capitalize(capitalPropertyName);
67
68         try {
69             getterMethod = javabean.getClass().getMethod("get" + capitalPropertyName, null);
70         }
71         catch (NoSuchMethodException JavaDoc e) {
72             if (log.isDebugEnabled()) {
73                 log.error("Method get" + capitalPropertyName + " not found.");
74             }
75         }
76
77         // try isBlabla if boolean
78
if (getterMethod == null) {
79             // && args[0].getClass().equals( boolean.class ) )
80

81             try {
82                 getterMethod = javabean.getClass().getMethod("is" + capitalPropertyName, null);
83             }
84             catch (NoSuchMethodException JavaDoc e) {
85                 if (log.isDebugEnabled()) {
86                     log.error("Method is" + capitalPropertyName + " not found.");
87                 }
88             }
89         }
90
91         return getterMethod;
92     }
93
94     /**
95      * Describe what the method does
96      *
97      * @param task Describe what the parameter does
98      * @param configs Describe what the parameter does
99      */

100     static void fillConfigParamsFor(DocletTask task, HashMap JavaDoc configs)
101     {
102         fillConfigParamsHashMapUsingReflectionFor(task, configs, "");
103     }
104
105     /**
106      * Describe what the method does
107      *
108      * @param subtask Describe what the parameter does
109      * @param configs Describe what the parameter does
110      */

111     static void fillConfigParamsFor(SubTask subtask, HashMap JavaDoc configs)
112     {
113         fillConfigParamsHashMapUsingReflectionFor(subtask, configs, subtask.getSubTaskName() + '.');
114     }
115
116     /**
117      * Extract the name of a property from a method name - subtracting a given prefix.
118      *
119      * @param methodName The method name
120      * @param prefix The prefix
121      * @return The property name
122      */

123     private static String JavaDoc getPropertyName(String JavaDoc methodName, String JavaDoc prefix)
124     {
125         int start = prefix.length();
126
127         return Introspector.decapitalize(methodName.substring(start));
128     }
129
130     /**
131      * Uses reflection to find javabean properties of an object (should have both setter and getter and fills the
132      * hashtable with key="name of the property" and value="the value of that property".
133      *
134      * @param javabean
135      * @param configs
136      * @param propertyPrefix
137      */

138     private static void fillConfigParamsHashMapUsingReflectionFor(Object JavaDoc javabean, HashMap JavaDoc configs, String JavaDoc propertyPrefix)
139     {
140         Log log = LogUtil.getLog(ConfigParamIntrospector.class, "fillConfigParamsHashMapUsingReflectionFor");
141
142         if (log.isDebugEnabled()) {
143             log.debug("javabean=" + javabean);
144             log.debug("javabean.getClass()=" + javabean.getClass());
145             log.debug("configs.size()=" + configs.size());
146         }
147
148         try {
149             Method JavaDoc[] methods = javabean.getClass().getMethods();
150
151             for (int i = 0; i < methods.length; i++) {
152                 final Method JavaDoc method = methods[i];
153                 final String JavaDoc name = method.getName();
154                 Class JavaDoc returnType = method.getReturnType();
155                 Class JavaDoc[] args = method.getParameterTypes();
156
157                 // if is a setter method
158
if (name.startsWith("set") &&
159                     Modifier.isPublic(method.getModifiers()) &&
160                     Void.TYPE.equals(returnType) &&
161                     args.length == 1) {
162
163                     String JavaDoc propertyName = getPropertyName(name, "set");
164                     String JavaDoc capitalPropertyName = capitalize(propertyName);
165                     Method JavaDoc getterMethod = null;
166
167                     if (log.isDebugEnabled()) {
168                         log.debug("name=" + name);
169                         log.debug("propertyName=" + propertyName);
170                         log.debug("capitalPropertyName=" + capitalPropertyName);
171                     }
172
173                     getterMethod = findGetterMethod(javabean, capitalPropertyName);
174
175                     // discard this property if getter not found
176
if (getterMethod == null) {
177                         if (log.isDebugEnabled()) {
178                             log.error("Getter method not found.");
179                         }
180                         continue;
181                     }
182
183                     // get its value
184
Object JavaDoc propertyValue = null;
185
186                     try {
187                         propertyValue = getterMethod.invoke(javabean, null);
188                     }
189                     catch (IllegalAccessException JavaDoc e) {
190                         if (log.isDebugEnabled()) {
191                             log.error("IllegalAccessException", e);
192                         }
193                         continue;
194                     }
195                     catch (IllegalArgumentException JavaDoc e) {
196                         if (log.isDebugEnabled()) {
197                             log.error("IllegalArgumentException", e);
198                         }
199                         continue;
200                     }
201                     catch (InvocationTargetException JavaDoc e) {
202                         if (log.isDebugEnabled()) {
203                             log.error("InvocationTargetException", e);
204                         }
205                         continue;
206                     }
207
208                     if (propertyValue == null) {
209                         // hashtable doesn't accept null values
210
propertyValue = NULL;
211                     }
212
213                     // for now only accept serializable properties, later when xjavadocs
214
// is ready we can remove this limitation
215
if (!(propertyValue instanceof Serializable JavaDoc)) {
216                         continue;
217                     }
218
219                     if (log.isDebugEnabled()) {
220                         log.debug("putting propertyName=" + (propertyPrefix + propertyName));
221                         log.debug("putting propertyValue=" + propertyValue);
222                     }
223
224                     configs.put((propertyPrefix + propertyName).toLowerCase(), propertyValue);
225                 }
226             }
227         }
228         catch (SecurityException JavaDoc e) {
229             log.error("A SecurityException exception!!", e);
230         }
231
232         log.debug("configs.size()=" + configs.size());
233     }
234
235     /**
236      * @author Aslak Hellesøy
237      * @created January 20, 2002
238      */

239     private final static class NullObject implements Serializable JavaDoc
240     {
241         /**
242          * Simply equals if is of type NullObject (NullObject treated like a singleton).
243          *
244          * @param obj
245          * @return true if argument is a NullObject
246          */

247         public boolean equals(Object JavaDoc obj)
248         {
249             return obj instanceof NullObject;
250         }
251     }
252 }
253
Popular Tags