KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > JavaMethodConverter


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4 import com.thoughtworks.xstream.converters.Converter;
5 import com.thoughtworks.xstream.converters.MarshallingContext;
6 import com.thoughtworks.xstream.converters.UnmarshallingContext;
7 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
8 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * Converts a java.lang.reflect.Method to XML.
17  *
18  * @author Aslak Hellesøy
19  */

20 public class JavaMethodConverter implements Converter {
21
22     private final ClassLoader JavaDoc classLoader;
23
24     public JavaMethodConverter() {
25         this(JavaMethodConverter.class.getClassLoader());
26     }
27
28     public JavaMethodConverter(ClassLoader JavaDoc classLoader) {
29         this.classLoader = classLoader;
30     }
31
32     public boolean canConvert(Class JavaDoc type) {
33         return type.equals(Method JavaDoc.class) || type.equals(Constructor JavaDoc.class);
34     }
35
36     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
37         if (source instanceof Method JavaDoc) {
38             Method JavaDoc method = (Method JavaDoc) source;
39             String JavaDoc declaringClassName = method.getDeclaringClass().getName();
40             marshalMethod(writer, declaringClassName, method.getName(), method.getParameterTypes());
41         } else {
42             Constructor JavaDoc method = (Constructor JavaDoc) source;
43             String JavaDoc declaringClassName = method.getDeclaringClass().getName();
44             marshalMethod(writer, declaringClassName, null, method.getParameterTypes());
45         }
46     }
47
48     private void marshalMethod(HierarchicalStreamWriter writer, String JavaDoc declaringClassName, String JavaDoc methodName, Class JavaDoc[] parameterTypes) {
49
50         writer.startNode("class");
51         writer.setValue(declaringClassName);
52         writer.endNode();
53
54         if (methodName != null) {
55             // it's a method and not a ctor
56
writer.startNode("name");
57             writer.setValue(methodName);
58             writer.endNode();
59         }
60
61         writer.startNode("parameter-types");
62         for (int i = 0; i < parameterTypes.length; i++) {
63             Class JavaDoc parameterType = parameterTypes[i];
64             writer.startNode("class");
65             writer.setValue(parameterType.getName());
66             writer.endNode();
67         }
68         writer.endNode();
69     }
70
71     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
72         try {
73             boolean isMethodNotConstructor = context.getRequiredType().equals(Method JavaDoc.class);
74
75             reader.moveDown();
76             String JavaDoc declaringClassName = reader.getValue();
77             Class JavaDoc declaringClass = loadClass(declaringClassName);
78             reader.moveUp();
79
80             String JavaDoc methodName = null;
81             if (isMethodNotConstructor) {
82                 reader.moveDown();
83                 methodName = reader.getValue();
84                 reader.moveUp();
85             }
86
87             reader.moveDown();
88             List JavaDoc parameterTypeList = new ArrayList JavaDoc();
89             while (reader.hasMoreChildren()) {
90                 reader.moveDown();
91                 String JavaDoc parameterTypeName = reader.getValue();
92                 parameterTypeList.add(loadClass(parameterTypeName));
93                 reader.moveUp();
94             }
95             Class JavaDoc[] parameterTypes = (Class JavaDoc[]) parameterTypeList.toArray(new Class JavaDoc[parameterTypeList.size()]);
96             reader.moveUp();
97
98             if (isMethodNotConstructor) {
99                 return declaringClass.getMethod(methodName, parameterTypes);
100             } else {
101                 return declaringClass.getConstructor(parameterTypes);
102             }
103         } catch (ClassNotFoundException JavaDoc e) {
104             throw new ConversionException(e);
105         } catch (NoSuchMethodException JavaDoc e) {
106             throw new ConversionException(e);
107         }
108     }
109
110     private Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
111         return classLoader.loadClass(className);
112     }
113 }
114
Popular Tags