KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.converters.ConversionException;
4 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;
5
6 /**
7  * Converts a java.lang.Class to XML.
8  *
9  * @author Aslak Hellesøy
10  * @author Joe Walnes
11  */

12 public class JavaClassConverter extends AbstractBasicConverter {
13
14     private ClassLoader JavaDoc classLoader;
15
16     public JavaClassConverter() {
17         this(Thread.currentThread().getContextClassLoader());
18     }
19
20     public JavaClassConverter(ClassLoader JavaDoc classLoader) {
21         this.classLoader = classLoader;
22     }
23
24     public boolean canConvert(Class JavaDoc clazz) {
25         return Class JavaDoc.class.equals(clazz); // :)
26
}
27
28     protected String JavaDoc toString(Object JavaDoc obj) {
29         return ((Class JavaDoc) obj).getName();
30     }
31
32     protected Object JavaDoc fromString(String JavaDoc str) {
33         try {
34             return classLoader.loadClass(str);
35         } catch (ClassNotFoundException JavaDoc e) {
36             throw new ConversionException("Cannot load java class " + str, e);
37         }
38     }
39
40 }
41
Popular Tags