diff options
author | jfrijters <jfrijters> | 2009-07-29 13:36:56 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2009-07-29 13:36:56 +0400 |
commit | 43fd0daf0719910095a4f3acffabe0efc85875a4 (patch) | |
tree | 080060099226afed0de23b6fdc996d6b6c470cdc | |
parent | 03892b621b949b016e034415ccd18f0e95e3f330 (diff) |
Implemented support for .NET serialization of Java enums.
(The CreateEnumEnum move in FakeTypes.cs is because java.lang.Enum now has a dependency on custom attribute annotations.)
-rw-r--r-- | ikvmc/FakeTypes.cs | 3 | ||||
-rw-r--r-- | openjdk/java/lang/Enum.java | 35 | ||||
-rw-r--r-- | openjdk/map.xml | 3 | ||||
-rw-r--r-- | runtime/TypeWrapper.cs | 4 |
4 files changed, 43 insertions, 2 deletions
diff --git a/ikvmc/FakeTypes.cs b/ikvmc/FakeTypes.cs index eb4f4364..6b441c8f 100644 --- a/ikvmc/FakeTypes.cs +++ b/ikvmc/FakeTypes.cs @@ -72,13 +72,12 @@ namespace IKVM.Internal tb.DefineGenericParameters("T")[0].SetBaseTypeConstraint(typeof(MulticastDelegate)); genericDelegateInterfaceType = tb.CreateType(); - CreateEnumEnum(modb, loader); - TypeWrapper annotationTypeWrapper = loader.LoadClassByDottedName("java.lang.annotation.Annotation"); annotationTypeWrapper.Finish(); genericAttributeAnnotationType = CreateAnnotationType(modb, DotNetTypeWrapper.GenericAttributeAnnotationTypeName, annotationTypeWrapper); genericAttributeAnnotationMultipleType = CreateAnnotationType(modb, DotNetTypeWrapper.GenericAttributeAnnotationMultipleTypeName, annotationTypeWrapper); genericAttributeAnnotationReturnValueType = CreateAnnotationType(modb, DotNetTypeWrapper.GenericAttributeAnnotationReturnValueTypeName, annotationTypeWrapper); + CreateEnumEnum(modb, loader); } private static void CreateEnumEnum(ModuleBuilder modb, ClassLoaderWrapper loader) diff --git a/openjdk/java/lang/Enum.java b/openjdk/java/lang/Enum.java index 175b4021..e14a2884 100644 --- a/openjdk/java/lang/Enum.java +++ b/openjdk/java/lang/Enum.java @@ -30,6 +30,10 @@ import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.ObjectStreamException; +import cli.System.Runtime.Serialization.IObjectReference; +import cli.System.Runtime.Serialization.SerializationException; +import cli.System.Runtime.Serialization.SerializationInfo; +import cli.System.Runtime.Serialization.StreamingContext; /** * This is the common base class of all Java language enumeration types. @@ -39,6 +43,7 @@ import java.io.ObjectStreamException; * @see Class#getEnumConstants() * @since 1.5 */ +@cli.System.SerializableAttribute.Annotation public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { /** @@ -231,4 +236,34 @@ public abstract class Enum<E extends Enum<E>> private void readObjectNoData() throws ObjectStreamException { throw new InvalidObjectException("can't deserialize enum"); } + + // [IKVM] .NET serialization support starts here + // Note that we don't have a security demand, because the info is harmless. + @cli.IKVM.Attributes.HideFromJavaAttribute.Annotation + public final void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("enumType", getDeclaringClass()); + info.AddValue("name", name); + info.SetType(ikvm.runtime.Util.getInstanceTypeFromClass(EnumSerializationProxy.class)); + } +} + +@cli.System.SerializableAttribute.Annotation +final class EnumSerializationProxy implements IObjectReference +{ + private Class enumType; + private String name; + + public Object GetRealObject(StreamingContext context) + { + try + { + return Enum.valueOf(enumType, name); + } + catch (IllegalArgumentException x) + { + ikvm.runtime.Util.throwException(new SerializationException("Enum value " + name + " not found in " + enumType, x)); + return null; + } + } } diff --git a/openjdk/map.xml b/openjdk/map.xml index 48b5df8a..e2882c6f 100644 --- a/openjdk/map.xml +++ b/openjdk/map.xml @@ -1364,6 +1364,9 @@ </body> </method> </class> + <class name="java.lang.Enum"> + <implements class="cli.System.Runtime.Serialization.ISerializable" /> + </class> <class name="java.lang.Thread"> <field name="parkLock" sig="Ljava.lang.Object;" modifiers="" /> <field name="parkState" sig="I" modifiers="" /> diff --git a/runtime/TypeWrapper.cs b/runtime/TypeWrapper.cs index 76d1f719..355a3aaf 100644 --- a/runtime/TypeWrapper.cs +++ b/runtime/TypeWrapper.cs @@ -4584,6 +4584,10 @@ namespace IKVM.Internal { typeAttribs |= TypeAttributes.BeforeFieldInit; } + if(f.IsEnum) + { + typeAttribs |= TypeAttributes.Serializable; + } #if STATIC_COMPILER bool cantNest = false; bool setModifiers = false; |