Index: src/main/java/org/exolab/castor/xml/Introspector.java =================================================================== --- src/main/java/org/exolab/castor/xml/Introspector.java (Revision 6994) +++ src/main/java/org/exolab/castor/xml/Introspector.java (Arbeitskopie) @@ -65,6 +65,7 @@ import org.exolab.castor.mapping.loader.TypeInfo; import org.exolab.castor.util.Configuration; import org.exolab.castor.util.LocalConfiguration; +import org.exolab.castor.util.ReflectionUtil; import org.exolab.castor.xml.descriptors.CoreDescriptors; import org.exolab.castor.xml.handlers.ContainerFieldHandler; import org.exolab.castor.xml.handlers.DateFieldHandler; @@ -1187,6 +1188,19 @@ //-- make sure type is not Void, or Class; if (type == Void.class || type == Class.class ) return false; + //-- check whether it is a Java 5.0 enum + float javaVersion = Float.valueOf(System.getProperty("java.specification.version")).floatValue(); + if (javaVersion >= 1.5) { + try { + Boolean isEnum = ReflectionUtil.isEnumViaReflection(type); + if (isEnum.booleanValue()) { + return true; + } + } catch (Exception e) { + // nothing to report; implies that there's no such method + } + } + if ( (!type.isInterface()) && (type != Object.class) && (!isPrimitive(type))) { Index: src/main/java/org/exolab/castor/xml/MarshalFramework.java =================================================================== --- src/main/java/org/exolab/castor/xml/MarshalFramework.java (Revision 6994) +++ src/main/java/org/exolab/castor/xml/MarshalFramework.java (Arbeitskopie) @@ -50,7 +50,10 @@ import org.exolab.castor.mapping.FieldDescriptor; import org.exolab.castor.mapping.MappingException; import org.exolab.castor.mapping.loader.CollectionHandlers; +import org.exolab.castor.util.ReflectionUtil; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Iterator; import java.util.Vector; @@ -204,6 +207,50 @@ } //-- isPrimitive /** + * Returns true if the given class should be treated as an enum type. This method + * will return true for all Java 5 (or later) enums, and for enum-style + * classes. + * + * @return true if the given class should be treated as an enum + **/ + static boolean isEnum(Class type) { + + if (type == null) { + return false; + } + + float javaVersion = Float.valueOf(System.getProperty("java.specification.version")).floatValue(); + if (javaVersion >= 1.5) { + try { + Boolean isEnum = ReflectionUtil.isEnumViaReflection(type); + return isEnum.booleanValue(); + } catch (Exception e) { + // nothing to report; implies that there's no such method + } + } + + // TODO: add code to cover 1.4 enum-stype classes as well. + + return false; + + } //-- isPrimitive + +// /** +// * Calls isEnum() method on target class vi areflection to find out +// * whether the given type is a Java 5 enumeration. +// * @param type The type to analyze. +// * @return True if the type given is a Java 5.0 enum. +// * @throws NoSuchMethodException If the method can not be found. +// * @throws IllegalAccessException If access to this method is illegal +// * @throws InvocationTargetException If the target method can not be invoked. +// */ +// private static Boolean isEnumViaReflection(Class type) +// throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { +// Method isEnumMethod = type.getClass().getMethod("isEnum", (Class[]) null); +// return (Boolean) isEnumMethod.invoke(type, (Object[]) null); +// } +// + /** * Returns true if any of the fields associated with the given * XMLClassDescriptor are located at, or beneath, the given location. * Index: src/main/java/org/exolab/castor/xml/Marshaller.java =================================================================== --- src/main/java/org/exolab/castor/xml/Marshaller.java (Revision 6994) +++ src/main/java/org/exolab/castor/xml/Marshaller.java (Arbeitskopie) @@ -1706,6 +1706,16 @@ throw new MarshalException(sx); } } + else if (isEnum(_class)) { + char[] chars = object.toString().toCharArray(); + try { + handler.characters(chars,0,chars.length); + } + catch(org.xml.sax.SAXException sx) { + throw new MarshalException(sx); + } + + } } //--------------------------- Index: src/main/java/org/exolab/castor/util/ReflectionUtil.java =================================================================== --- src/main/java/org/exolab/castor/util/ReflectionUtil.java (Revision 0) +++ src/main/java/org/exolab/castor/util/ReflectionUtil.java (Revision 0) @@ -0,0 +1,44 @@ +/* + * Copyright 2007 Werner Guttmann + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Utility class to support reflection-based operations. + * + * @since 1.1.2 + */ +public class ReflectionUtil { + + /** + * Calls isEnum() method on target class vi areflection to find out + * whether the given type is a Java 5 enumeration. + * @param type The type to analyze. + * @return True if the type given is a Java 5.0 enum. + * @throws NoSuchMethodException If the method can not be found. + * @throws IllegalAccessException If access to this method is illegal + * @throws InvocationTargetException If the target method can not be invoked. + */ + public static Boolean isEnumViaReflection(Class type) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method isEnumMethod = type.getClass().getMethod("isEnum", (Class[]) null); + return (Boolean) isEnumMethod.invoke(type, (Object[]) null); + } + + +}