From 8cce5ad64a87303a516d2a0ee962f9d9dd947300 Mon Sep 17 00:00:00 2001 From: michelou Date: Thu, 28 Apr 2005 16:29:06 +0000 Subject: - fixed (de-)serialization error using readReso... - fixed (de-)serialization error using readResolve. --- sources/scala/Type.java | 28 ++++++------- sources/scala/runtime/types/ScalaClassType.java | 53 ++++++++++++++++++++++++- sources/scala/runtime/types/TypeAll.java | 9 ++++- sources/scala/runtime/types/TypeAllRef.java | 9 ++++- sources/scala/runtime/types/TypeAny.java | 9 ++++- sources/scala/runtime/types/TypeAnyVal.java | 9 ++++- sources/scala/runtime/types/TypeBoolean.java | 9 ++++- sources/scala/runtime/types/TypeByte.java | 9 ++++- sources/scala/runtime/types/TypeChar.java | 9 ++++- sources/scala/runtime/types/TypeDouble.java | 8 +++- sources/scala/runtime/types/TypeFloat.java | 8 +++- sources/scala/runtime/types/TypeInt.java | 9 ++++- sources/scala/runtime/types/TypeLong.java | 9 ++++- sources/scala/runtime/types/TypeShort.java | 9 ++++- sources/scala/runtime/types/TypeUnit.java | 9 ++++- 15 files changed, 155 insertions(+), 41 deletions(-) diff --git a/sources/scala/Type.java b/sources/scala/Type.java index c150c6ecc5..389ee5701a 100644 --- a/sources/scala/Type.java +++ b/sources/scala/Type.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -107,21 +107,21 @@ abstract public class Type implements java.io.Serializable { } // Value types - public static final TypeDouble Double = new TypeDouble(); - public static final TypeFloat Float = new TypeFloat(); - public static final TypeLong Long = new TypeLong(); - public static final TypeInt Int = new TypeInt(); - public static final TypeShort Short = new TypeShort(); - public static final TypeChar Char = new TypeChar(); - public static final TypeByte Byte = new TypeByte(); - public static final TypeBoolean Boolean = new TypeBoolean(); - public static final TypeUnit Unit = new TypeUnit(); + public static final TypeDouble Double = TypeDouble.INSTANCE; + public static final TypeFloat Float = TypeFloat.INSTANCE; + public static final TypeLong Long = TypeLong.INSTANCE; + public static final TypeInt Int = TypeInt.INSTANCE; + public static final TypeShort Short = TypeShort.INSTANCE; + public static final TypeChar Char = TypeChar.INSTANCE; + public static final TypeByte Byte = TypeByte.INSTANCE; + public static final TypeBoolean Boolean = TypeBoolean.INSTANCE; + public static final TypeUnit Unit = TypeUnit.INSTANCE; // "Special" types - public static final TypeAny Any = new TypeAny(); - public static final TypeAnyVal AnyVal = new TypeAnyVal(); - public static final TypeAllRef AllRef = new TypeAllRef(); - public static final TypeAll All = new TypeAll(); + public static final TypeAny Any = TypeAny.INSTANCE; + public static final TypeAnyVal AnyVal = TypeAnyVal.INSTANCE; + public static final TypeAllRef AllRef = TypeAllRef.INSTANCE; + public static final TypeAll All = TypeAll.INSTANCE; private static JavaClassType JavaLangObject; diff --git a/sources/scala/runtime/types/ScalaClassType.java b/sources/scala/runtime/types/ScalaClassType.java index 861abd0c51..e8f0a984e0 100644 --- a/sources/scala/runtime/types/ScalaClassType.java +++ b/sources/scala/runtime/types/ScalaClassType.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -17,6 +17,17 @@ import scala.runtime.RunTime; import scala.runtime.FNV_Hash; import scala.runtime.PearsonHash; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +/** + * Provides a run-time representation of the Scala types. + * + * @author Michel Schinz + * @version 1.0 + */ + public class ScalaClassType extends ClassType { public static final ScalaClassType[] EMPTY_ARRAY = new ScalaClassType[0]; @@ -236,4 +247,44 @@ public class ScalaClassType extends ClassType { } } } + + private static final ClassLoader loader = + ClassLoader.getSystemClassLoader(); + + // Must match value defined in class scalac.util.Names ! + private static final String INSTANTIATE_PREFIX = "instantiate$"; + + // Enforces uniqueness of the instance when serializing and + // deserializing the same Scala type object many times. + private Object readResolve() { + String fullName = constr.clazz.getName(); + Class instClazz = constr.clazz; + if (constr.clazz.isInterface()) { + try { + instClazz = Class.forName(fullName + "$class", false, loader); + } + catch (ClassNotFoundException e) { + throw new Error(e); + } + } + try { + int inx = fullName.lastIndexOf('.'); + String className = (inx < 0) ? fullName : fullName.substring(inx + 1); + String name = INSTANTIATE_PREFIX + className + "$"; + Class[] paramTypes = new Class[]{ Type[].class }; + Method instMeth = instClazz.getDeclaredMethod(name, paramTypes); + assert Modifier.isStatic(instMeth.getModifiers()); + return instMeth.invoke(null, new Object[]{ inst }); + } + catch (NoSuchMethodException e) { + throw new Error(e); + } + catch (IllegalAccessException e) { + throw new Error(e); + } + catch (InvocationTargetException e) { + throw new Error(e); + } + } + } diff --git a/sources/scala/runtime/types/TypeAll.java b/sources/scala/runtime/types/TypeAll.java index 07054bbc8b..9c79646c69 100644 --- a/sources/scala/runtime/types/TypeAll.java +++ b/sources/scala/runtime/types/TypeAll.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -26,4 +26,9 @@ public class TypeAll extends SpecialType { public String toString() { return "scala.All"; } public int hashCode() { return 0xAAAAAAAA; } -}; + + // Make TypeAll a serializable singleton + public static TypeAll INSTANCE = new TypeAll(); + protected TypeAll() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeAllRef.java b/sources/scala/runtime/types/TypeAllRef.java index 71f9097b09..e037a340f0 100644 --- a/sources/scala/runtime/types/TypeAllRef.java +++ b/sources/scala/runtime/types/TypeAllRef.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -27,4 +27,9 @@ public class TypeAllRef extends SpecialType { public String toString() { return "scala.AllRef"; } public int hashCode() { return 0xDDDDDDDD; } -}; + + // Make TypeAllRef a serializable singleton + public static TypeAllRef INSTANCE = new TypeAllRef(); + protected TypeAllRef() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeAny.java b/sources/scala/runtime/types/TypeAny.java index 07d36ba2b8..2f1e2d08bd 100644 --- a/sources/scala/runtime/types/TypeAny.java +++ b/sources/scala/runtime/types/TypeAny.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -27,4 +27,9 @@ public class TypeAny extends SpecialType { public String toString() { return "scala.Any"; } public int hashCode() { return 0xBBBBBBBB; } -}; + + // Make TypeAll a serializable singleton + public static TypeAny INSTANCE = new TypeAny(); + protected TypeAny() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeAnyVal.java b/sources/scala/runtime/types/TypeAnyVal.java index b606cc71e3..9fe397b576 100644 --- a/sources/scala/runtime/types/TypeAnyVal.java +++ b/sources/scala/runtime/types/TypeAnyVal.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -26,4 +26,9 @@ public class TypeAnyVal extends SpecialType { public String toString() { return "scala.AnyVal"; } public int hashCode() { return 0xCCCCCCCC; } -}; + + // Make TypeAnyVal a serializable singleton + public static TypeAnyVal INSTANCE = new TypeAnyVal(); + protected TypeAnyVal() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeBoolean.java b/sources/scala/runtime/types/TypeBoolean.java index dab68955db..69788a9752 100644 --- a/sources/scala/runtime/types/TypeBoolean.java +++ b/sources/scala/runtime/types/TypeBoolean.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -29,4 +29,9 @@ public class TypeBoolean extends ValueType { } public String toString() { return "scala.Boolean"; } public int hashCode() { return 0x88888888; } -}; + + // Make TypeBoolean a serializable singleton + public static TypeBoolean INSTANCE = new TypeBoolean(); + protected TypeBoolean() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeByte.java b/sources/scala/runtime/types/TypeByte.java index bb87a1ff1e..efdd977b5c 100644 --- a/sources/scala/runtime/types/TypeByte.java +++ b/sources/scala/runtime/types/TypeByte.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -29,4 +29,9 @@ public class TypeByte extends ValueType { } public String toString() { return "scala.Byte"; } public int hashCode() { return 0x77777777; } -}; + + // Make TypeByte a serializable singleton + public static TypeByte INSTANCE = new TypeByte(); + protected TypeByte() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeChar.java b/sources/scala/runtime/types/TypeChar.java index 2f26ffb866..970451bfcd 100644 --- a/sources/scala/runtime/types/TypeChar.java +++ b/sources/scala/runtime/types/TypeChar.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -29,4 +29,9 @@ public class TypeChar extends ValueType { } public String toString() { return "scala.Char"; } public int hashCode() { return 0x66666666; } -}; + + // Make TypeChar a serializable singleton + public static TypeChar INSTANCE = new TypeChar(); + protected TypeChar() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeDouble.java b/sources/scala/runtime/types/TypeDouble.java index 79e6a19178..bf8537c162 100644 --- a/sources/scala/runtime/types/TypeDouble.java +++ b/sources/scala/runtime/types/TypeDouble.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -28,5 +28,9 @@ public public class TypeDouble extends ValueType { } public String toString() { return "scala.Double"; } public int hashCode() { return 0x11111111; } -}; + // Make TypeDouble a serializable singleton + public static TypeDouble INSTANCE = new TypeDouble(); + protected TypeDouble() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeFloat.java b/sources/scala/runtime/types/TypeFloat.java index ae66212991..ea998bd71a 100644 --- a/sources/scala/runtime/types/TypeFloat.java +++ b/sources/scala/runtime/types/TypeFloat.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -28,5 +28,9 @@ public class TypeFloat extends ValueType { } public String toString() { return "scala.Float"; } public int hashCode() { return 0x22222222; } -}; + // Make TypeFloat a serializable singleton + public static TypeFloat INSTANCE = new TypeFloat(); + protected TypeFloat() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeInt.java b/sources/scala/runtime/types/TypeInt.java index 0109f3e1b3..129a8f0c87 100644 --- a/sources/scala/runtime/types/TypeInt.java +++ b/sources/scala/runtime/types/TypeInt.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -29,4 +29,9 @@ public class TypeInt extends ValueType { } public String toString() { return "scala.Int"; } public int hashCode() { return 0x44444444; } -}; + + // Make TypeInt a serializable singleton + public static TypeInt INSTANCE = new TypeInt(); + protected TypeInt() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeLong.java b/sources/scala/runtime/types/TypeLong.java index 4258224191..2e0381fb0f 100644 --- a/sources/scala/runtime/types/TypeLong.java +++ b/sources/scala/runtime/types/TypeLong.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -28,4 +28,9 @@ public class TypeLong extends ValueType { } public String toString() { return "scala.Long"; } public int hashCode() { return 0x33333333; } -}; + + // Make TypeLong a serializable singleton + public static TypeLong INSTANCE = new TypeLong(); + protected TypeLong() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeShort.java b/sources/scala/runtime/types/TypeShort.java index ce4af21e18..e6abb617e0 100644 --- a/sources/scala/runtime/types/TypeShort.java +++ b/sources/scala/runtime/types/TypeShort.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -28,4 +28,9 @@ public class TypeShort extends ValueType { } public String toString() { return "scala.Short"; } public int hashCode() { return 0x55555555; } -}; + + // Make TypeShort a serializable singleton + public static TypeShort INSTANCE = new TypeShort(); + protected TypeShort() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} diff --git a/sources/scala/runtime/types/TypeUnit.java b/sources/scala/runtime/types/TypeUnit.java index d42a994f89..29edaa325f 100644 --- a/sources/scala/runtime/types/TypeUnit.java +++ b/sources/scala/runtime/types/TypeUnit.java @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -29,4 +29,9 @@ public class TypeUnit extends ValueType { } public String toString() { return "scala.Unit"; } public int hashCode() { return 0x99999999; } -}; + + // Make TypeUnit a serializable singleton + public static TypeUnit INSTANCE = new TypeUnit(); + protected TypeUnit() { /* exists only to that instantiation */ } + private Object readResolve() { return INSTANCE; } +} -- cgit v1.2.3