KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > joinpoint > plugins > Config


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.joinpoint.plugins;
23
24 import java.util.Arrays JavaDoc;
25
26 import org.jboss.joinpoint.spi.ConstructorJoinpoint;
27 import org.jboss.joinpoint.spi.FieldGetJoinpoint;
28 import org.jboss.joinpoint.spi.FieldSetJoinpoint;
29 import org.jboss.joinpoint.spi.JoinpointException;
30 import org.jboss.joinpoint.spi.JoinpointFactory;
31 import org.jboss.joinpoint.spi.MethodJoinpoint;
32 import org.jboss.logging.Logger;
33 import org.jboss.reflect.spi.ClassInfo;
34 import org.jboss.reflect.spi.ConstructorInfo;
35 import org.jboss.reflect.spi.FieldInfo;
36 import org.jboss.reflect.spi.MethodInfo;
37 import org.jboss.reflect.spi.TypeInfo;
38
39 /**
40  * Config utilities.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 56399 $
44  */

45 public class Config
46 {
47    /** The log */
48    protected static final Logger log = Logger.getLogger(Config.class);
49
50    /** No parameter types */
51    private static final String JavaDoc[] NO_PARAMS_TYPES = new String JavaDoc[0];
52
53    /** No parameters */
54    private static final Object JavaDoc[] NO_PARAMS = new Object JavaDoc[0];
55
56    /**
57     * Instantiate an object
58     *
59     * @param jpf the join point factory
60     * @param paramTypes the parameter types
61     * @param params the parameters
62     * @return the instantiated object
63     * @throws Throwable for any error
64     */

65    public static Object JavaDoc instantiate(JoinpointFactory jpf, String JavaDoc[] paramTypes, Object JavaDoc[] params) throws Throwable JavaDoc
66    {
67       ConstructorJoinpoint joinpoint = getConstructorJoinpoint(jpf, paramTypes, params);
68       return joinpoint.dispatch();
69    }
70
71    /**
72     * Configure a field
73     *
74     * @param object the object to configure
75     * @param jpf the join point factory
76     * @param name the name of the field
77     * @param value the value
78     * @throws Throwable for any error
79     */

80    public static void configure(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name, Object JavaDoc value) throws Throwable JavaDoc
81    {
82       FieldSetJoinpoint joinpoint = getFieldSetJoinpoint(object, jpf, name, value);
83       joinpoint.dispatch();
84    }
85
86    /**
87     * Unconfigure a field
88     *
89     * @param object the object to unconfigure
90     * @param jpf the join point factory
91     * @param name the name of the field
92     * @throws Throwable for any error
93     */

94    public static void unconfigure(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name) throws Throwable JavaDoc
95    {
96       FieldSetJoinpoint joinpoint = getFieldSetJoinpoint(object, jpf, name, null);
97       joinpoint.dispatch();
98    }
99
100    /**
101     * Invoke a method
102     *
103     * @param object the object to invoke
104     * @param jpf the join point factory
105     * @param name the name of the method
106     * @param paramTypes the parameter types
107     * @param params the parameters
108     * @return the result of the invocation
109     * @throws Throwable for any error
110     */

111    public static Object JavaDoc invoke(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name, String JavaDoc[] paramTypes, Object JavaDoc[] params) throws Throwable JavaDoc
112    {
113       MethodJoinpoint joinpoint = getMethodJoinpoint(object, jpf, name, paramTypes, params);
114       return joinpoint.dispatch();
115    }
116
117    /**
118     * Get a constructor Joinpoint
119     *
120     * @param jpf the join point factory
121     * @return the Joinpoint
122     * @throws Throwable for any error
123     */

124    public static ConstructorJoinpoint getConstructorJoinpoint(JoinpointFactory jpf) throws Throwable JavaDoc
125    {
126       return getConstructorJoinpoint(jpf, null, null);
127    }
128
129    /**
130     * Get a constructor Joinpoint
131     *
132     * @param jpf the join point factory
133     * @param paramTypes the parameter types
134     * @param params the parameters
135     * @return the Joinpoint
136     * @throws Throwable for any error
137     */

138    public static ConstructorJoinpoint getConstructorJoinpoint(JoinpointFactory jpf, String JavaDoc[] paramTypes, Object JavaDoc[] params) throws Throwable JavaDoc
139    {
140       if (paramTypes == null)
141          paramTypes = NO_PARAMS_TYPES;
142
143       if (params == null)
144          params = NO_PARAMS;
145
146       boolean trace = log.isTraceEnabled();
147       if (trace)
148          log.trace("Get constructor Joinpoint jpf=" + jpf + " paramTypes=" + Arrays.asList(paramTypes) + " params=" + Arrays.asList(params));
149
150       ConstructorInfo constructorInfo = findConstructorInfo(jpf.getClassInfo(), paramTypes);
151       ConstructorJoinpoint joinpoint = jpf.getConstructorJoinpoint(constructorInfo);
152       joinpoint.setArguments(params);
153       return joinpoint;
154    }
155
156    /**
157     * Get a field get joinpoint
158     *
159     * @param object the object to configure
160     * @param jpf the join point factory
161     * @param name the name of the field
162     * @return the Joinpoint
163     * @throws Throwable for any error
164     */

165    public static FieldGetJoinpoint getFieldGetJoinpoint(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name) throws Throwable JavaDoc
166    {
167       boolean trace = log.isTraceEnabled();
168       if (trace)
169          log.trace("Get field get Joinpoint jpf=" + jpf + " target=" + object + " name=" + name);
170
171       FieldInfo fieldInfo = findFieldInfo(jpf.getClassInfo(), name);
172       FieldGetJoinpoint joinpoint = jpf.getFieldGetJoinpoint(fieldInfo);
173       joinpoint.setTarget(object);
174       return joinpoint;
175    }
176
177    /**
178     * Get a field set joinpoint
179     *
180     * @param object the object to configure
181     * @param jpf the join point factory
182     * @param name the name of the field
183     * @param value the value
184     * @return the Joinpoint
185     * @throws Throwable for any error
186     */

187    public static FieldSetJoinpoint getFieldSetJoinpoint(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name, Object JavaDoc value) throws Throwable JavaDoc
188    {
189       boolean trace = log.isTraceEnabled();
190       if (trace)
191          log.trace("Get field set Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " value=" + value);
192
193       FieldInfo fieldInfo = findFieldInfo(jpf.getClassInfo(), name);
194       FieldSetJoinpoint joinpoint = jpf.getFieldSetJoinpoint(fieldInfo);
195       joinpoint.setTarget(object);
196       joinpoint.setValue(value);
197       return joinpoint;
198    }
199
200    /**
201     * Get a method joinpoint
202     *
203     * @param object the object to invoke
204     * @param jpf the join point factory
205     * @param name the name of the method
206     * @param paramTypes the parameter types
207     * @param params the parameters
208     * @return the join point
209     * @throws Throwable for any error
210     */

211    public static MethodJoinpoint getMethodJoinpoint(Object JavaDoc object, JoinpointFactory jpf, String JavaDoc name, String JavaDoc[] paramTypes, Object JavaDoc[] params) throws Throwable JavaDoc
212    {
213       boolean trace = log.isTraceEnabled();
214       if (trace)
215       {
216          if (paramTypes != null)
217             log.trace("Get method Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " paramTypes=" + Arrays.asList(paramTypes));
218          else
219             log.trace("Get method Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " paramTypes=()");
220       }
221
222       MethodInfo methodInfo = findMethodInfo(jpf.getClassInfo(), name, paramTypes);
223       MethodJoinpoint joinpoint = jpf.getMethodJoinpoint(methodInfo);
224       joinpoint.setTarget(object);
225       joinpoint.setArguments(params);
226       return joinpoint;
227    }
228
229    /**
230     * Find constructor info
231     *
232     * @param classInfo the class info
233     * @param paramTypes the parameter types
234     * @return the constructor info
235     * @throws JoinpointException when no such constructor
236     */

237    public static ConstructorInfo findConstructorInfo(ClassInfo classInfo, String JavaDoc[] paramTypes) throws JoinpointException
238    {
239       ConstructorInfo[] constructors = classInfo.getDeclaredConstructors();
240       if (constructors != null)
241       {
242          for (int i = 0; i < constructors.length; ++i)
243          {
244             if (equals(paramTypes, constructors[i].getParameterTypes()))
245                return constructors[i];
246          }
247          throw new JoinpointException("Constructor not found " + classInfo.getName() + Arrays.asList(paramTypes) + " in " + Arrays.asList(constructors));
248       }
249       throw new JoinpointException("Constructor not found " + classInfo.getName() + Arrays.asList(paramTypes) + " no constructors");
250    }
251
252    /**
253     * Find field info
254     *
255     * @param classInfo the class info
256     * @param name the field name
257     * @return the field info
258     * @throws JoinpointException when no such field
259     */

260    public static FieldInfo findFieldInfo(ClassInfo classInfo, String JavaDoc name) throws JoinpointException
261    {
262       ClassInfo current = classInfo;
263       while (current != null)
264       {
265          FieldInfo result = locateFieldInfo(current, name);
266          if (result != null)
267             return result;
268          current = current.getSuperclass();
269       }
270       throw new JoinpointException("Field not found '" + name + "' for class " + classInfo.getName());
271    }
272
273    /**
274     * Find field info
275     *
276     * @param fieldInfo the field info
277     * @param name the field name
278     * @return the field info or null if not found
279     */

280    private static FieldInfo locateFieldInfo(ClassInfo classInfo, String JavaDoc name)
281    {
282       FieldInfo[] fields = classInfo.getDeclaredFields();
283       if (fields != null)
284       {
285          for (int i = 0; i < fields.length; ++i)
286          {
287             if (name.equals(fields[i].getName()))
288                return fields[i];
289          }
290       }
291       return null;
292    }
293
294    /**
295     * Find method info
296     *
297     * @param classInfo the class info
298     * @param name the method name
299     * @param paramTypes the parameter types
300     * @return the method info
301     * @throws JoinpointException when no such method
302     */

303    public static MethodInfo findMethodInfo(ClassInfo classInfo, String JavaDoc name, String JavaDoc[] paramTypes) throws JoinpointException
304    {
305       return findMethodInfo(classInfo, name, paramTypes, false, true);
306    }
307
308    /**
309     * Find method info
310     *
311     * @param classInfo the class info
312     * @param name the method name
313     * @param paramTypes the parameter types
314     * @param isStatic must the method be static
315     * @param isPublic must the method be public
316     * @return the method info
317     * @throws JoinpointException when no such method
318     */

319    public static MethodInfo findMethodInfo(ClassInfo classInfo, String JavaDoc name, String JavaDoc[] paramTypes, boolean isStatic, boolean isPublic) throws JoinpointException
320    {
321       if (paramTypes == null)
322          paramTypes = NO_PARAMS_TYPES;
323
324       ClassInfo current = classInfo;
325       while (current != null)
326       {
327          MethodInfo result = locateMethodInfo(current, name, paramTypes, isStatic, isPublic);
328          if (result != null)
329             return result;
330          current = current.getSuperclass();
331       }
332       throw new JoinpointException("Method not found " + name + Arrays.asList(paramTypes) + " for class " + classInfo.getName());
333    }
334
335    /**
336     * Find method info
337     *
338     * @param classInfo the class info
339     * @param name the method name
340     * @param paramTypes the parameter types
341     * @param isStatic must the method be static
342     * @param isPublic must the method be public
343     * @return the method info or null if not found
344     */

345    private static MethodInfo locateMethodInfo(ClassInfo classInfo, String JavaDoc name, String JavaDoc[] paramTypes, boolean isStatic, boolean isPublic)
346    {
347       MethodInfo[] methods = classInfo.getDeclaredMethods();
348       if (methods != null)
349       {
350          for (int i = 0; i < methods.length; ++i)
351          {
352             if (name.equals(methods[i].getName()) &&
353                 equals(paramTypes, methods[i].getParameterTypes()) &&
354                 methods[i].isStatic() == isStatic &&
355                 methods[i].isPublic() == isPublic)
356                return methods[i];
357          }
358       }
359       return null;
360    }
361
362    /**
363     * Test whether type names are equal to type infos
364     *
365     * @param typeNames the type names
366     * @param typeInfos the type infos
367     * @return true when they are equal
368     */

369    public static boolean equals(String JavaDoc[] typeNames, TypeInfo[] typeInfos)
370    {
371       if (typeNames == null || typeInfos == null)
372          return false;
373       if (typeNames.length != typeInfos.length)
374          return false;
375       for (int i = 0; i < typeNames.length; ++i)
376       {
377          if (typeNames[i] != null && typeNames[i].equals(typeInfos[i].getName()) == false)
378             return false;
379       }
380       return true;
381    }
382 }
383
Popular Tags