/*
 * Copyright (C) 2004, 2005 Joe Walnes.
 * Copyright (C) 2006, 2007, 2009 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 04. April 2004 by Joe Walnes
 */
package com.thoughtworks.xstream.converters.extended;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;

/**
 * Converts a java.lang.Class to XML.
 * 
 * @author Aslak Helles&oslash;y
 * @author Joe Walnes
 * @author Matthew Sandoz
 * @author J&ouml;rg Schaible
 */
public class JavaClassConverter extends AbstractSingleValueConverter {

    private ClassLoader classLoader;

    public JavaClassConverter(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public boolean canConvert(Class clazz) {
        return Class.class.equals(clazz); // :)
    }

    public String toString(Object obj) {
        return ((Class) obj).getName();
    }

    public Object fromString(String str) {
        try {
            return loadClass(str);
        } catch (ClassNotFoundException e) {
            throw new ConversionException("Cannot load java class " + str, e);
        }
    }

    private Class loadClass(String className) throws ClassNotFoundException {
        Class resultingClass = primitiveClassForName(className);
        if( resultingClass != null ){
            return resultingClass;
        }
        int dimension;
        for(dimension = 0; className.charAt(dimension) == '['; ++dimension);
        if (dimension > 0) {
            final ClassLoader classLoaderToUse;
            if (className.charAt(dimension) == 'L') {
                String componentTypeName = className.substring(dimension + 1, className.length() - 1);
                classLoaderToUse = classLoader.loadClass(componentTypeName).getClassLoader();
            } else {
                classLoaderToUse = null;
            }
            return Class.forName(className, false, classLoaderToUse);
        }
        return classLoader.loadClass(className);
    }

    /**
     * Lookup table for primitive types.
     */
    private Class primitiveClassForName(String name) {
        return  name.equals("void") ? Void.TYPE :
                name.equals("boolean") ? Boolean.TYPE :
                name.equals("byte") ? Byte.TYPE :
                name.equals("char") ? Character.TYPE :
                name.equals("short") ? Short.TYPE :
                name.equals("int") ? Integer.TYPE :
                name.equals("long") ? Long.TYPE :
                name.equals("float") ? Float.TYPE :
                name.equals("double") ? Double.TYPE :
                null;
    }

}

@@ -69,16 +69,22 @@
 07, 2009
+, 2011
  XStream
@@ -96,16 +96,16 @@
 itters.%0A
-
  * All r
@@ -545,16 +545,70 @@
 nverter;
+%0Aimport com.thoughtworks.xstream.core.util.Primitives;
 %0A%0A/**%0A *
@@ -1506,25 +1506,25 @@
 Class = 
-p
+P
 rimitive
 ClassFor
@@ -1515,27 +1515,30 @@
 rimitive
-ClassForNam
+s.primitiveTyp
 e(classN
@@ -2263,625 +2263,12 @@
 e);%0A
+
     %7D%0A
-%0A    /**%0A     * Lookup table for primitive types.%0A     */%0A    private Class primitiveClassForName(String name) %7B%0A        return  name.equals(%22void%22) ? Void.TYPE :%0A                name.equals(%22boolean%22) ? Boolean.TYPE :%0A                name.equals(%22byte%22) ? Byte.TYPE :%0A                name.equals(%22char%22) ? Character.TYPE :%0A                name.equals(%22short%22) ? Short.TYPE :%0A                name.equals(%22int%22) ? Integer.TYPE :%0A                name.equals(%22long%22) ? Long.TYPE :%0A                name.equals(%22float%22) ? Float.TYPE :%0A                name.equals(%22double%22) ? Double.TYPE :%0A                null;%0A    %7D%0A%0A
 %7D%0A

Use Primitives.primitiveType().