KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > pattern > CompiledClassReflector


1 package org.apache.ws.jaxme.js.pattern;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 import org.apache.ws.jaxme.js.JavaMethod;
6 import org.apache.ws.jaxme.js.JavaQNameImpl;
7 import org.apache.ws.jaxme.js.JavaSource;
8 import org.apache.ws.jaxme.js.JavaSourceFactory;
9
10
11 /** Reflector for gathering information on a compiled class.
12  */

13 public class CompiledClassReflector implements Reflector {
14     private final Class JavaDoc compiledClass;
15
16     /** Creates a new instance of <code>CompiledClassReflector</code>,
17      * reading information from the given class.
18      * @param pClass
19      */

20     public CompiledClassReflector(Class JavaDoc pClass) {
21         compiledClass = pClass;
22     }
23     
24     /** Creates a new instance of <code>CompiledClassReflector</code>,
25      * which loads the class named <code>pName</code> through
26      * {@link ClassLoader pClassLoader}.
27      */

28     public CompiledClassReflector(String JavaDoc pName, ClassLoader JavaDoc pClassLoader)
29             throws ClassNotFoundException JavaDoc {
30         this(pClassLoader.loadClass(pName));
31     }
32     
33     /** <p>Converts the given {@link Method} into an instance of
34      * {@link JavaSource}.</p>
35      */

36     protected JavaMethod getMethod(JavaSource pSource, Method JavaDoc pMethod) {
37         JavaMethod method = pSource.newJavaMethod(pMethod.getName(),
38                                                   JavaQNameImpl.getInstance(pMethod.getReturnType()),
39                                                   JavaSource.PUBLIC);
40         Class JavaDoc[] classes = pMethod.getParameterTypes();
41         for (int i = 0; i < classes.length; i++) {
42             method.addParam(classes[i], "arg" + i);
43         }
44         Class JavaDoc[] exceptions = pMethod.getExceptionTypes();
45         for (int i = 0; i < exceptions.length; i++) {
46             method.addThrows(exceptions[i]);
47         }
48         return method;
49     }
50     
51     /** Returns the compiled class being used to gather information.
52      */

53     public Class JavaDoc getCompiledClass() {
54         return compiledClass;
55     }
56     
57     /** Reads the interface methods and converts them
58      * into an instance of {@link JavaSource}.
59      */

60     public JavaSource getJavaSource(JavaSourceFactory pFactory) {
61         Class JavaDoc c = getCompiledClass();
62         JavaSource js = new JavaSourceFactory().newJavaSource(JavaQNameImpl.getInstance(c.getName(), true));
63         Method JavaDoc[] methods = c.getMethods();
64         for (int i = 0; i < methods.length; i++) {
65             Method JavaDoc m = methods[i];
66             getMethod(js, m);
67         }
68         return js;
69     }
70 }
71
Popular Tags