From 243a97038bdab8f2ec448df19c88ac2638035531 Mon Sep 17 00:00:00 2001 From: Jens-Heiner Rechtien Date: Wed, 26 Mar 2003 14:44:58 +0000 Subject: MWS_SRX644: migrate branch mws_srx644 -> HEAD --- .../sun/star/lib/uno/typedesc/TypeDescription.java | 216 +++++++------ ridljar/com/sun/star/uno/Any.java | 17 +- ridljar/com/sun/star/uno/ITypeDescription.java | 79 ++++- ridljar/com/sun/star/uno/Type.java | 341 +++++++++++---------- 4 files changed, 382 insertions(+), 271 deletions(-) (limited to 'ridljar/com/sun') diff --git a/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java b/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java index 68f146fd34d1..97757162b177 100644 --- a/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java +++ b/ridljar/com/sun/star/lib/uno/typedesc/TypeDescription.java @@ -2,9 +2,9 @@ * * $RCSfile: TypeDescription.java,v $ * - * $Revision: 1.13 $ + * $Revision: 1.14 $ * - * last change: $Author: jbu $ $Date: 2001-10-26 11:43:05 $ + * last change: $Author: hr $ $Date: 2003-03-26 15:44:53 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -63,6 +63,7 @@ package com.sun.star.lib.uno.typedesc; import java.util.Enumeration; +import java.util.HashMap; import java.util.Hashtable; import java.lang.reflect.Field; @@ -97,7 +98,7 @@ import com.sun.star.lib.uno.typeinfo.TypeInfo; * methods, which may be changed or moved in the furture, so please * do not use these methods. *

- * @version $Revision: 1.13 $ $ $Date: 2001-10-26 11:43:05 $ + * @version $Revision: 1.14 $ $ $Date: 2003-03-26 15:44:53 $ * @author Kay Ramme * @since UDK2.0 */ @@ -123,71 +124,75 @@ public class TypeDescription implements ITypeDescription { public static final TypeDescription __type_TypeDescription = new TypeDescription(TypeClass.TYPE, "type", "[Lcom.sun.star.uno.Type;", Type.class); // TYPE public static final TypeDescription __any_TypeDescription = new TypeDescription(TypeClass.ANY, "any", "[Ljava.lang.Object;", Object.class); // ANY - - static private final Hashtable __classToTypeDescription = new Hashtable(); - static private final Hashtable __typeNameToTypeDescription = new Hashtable(); - static private final Hashtable __typeClassToTypeName = new Hashtable(); - - static private final Object[][] __typeClassToTypeDescription = new Object[][]{ - new Object[]{"java.lang.Void", "V", __void_TypeDescription}, // VOID - new Object[]{"java.lang.Character", "C", __char_TypeDescription}, // CHAR - new Object[]{"java.lang.Boolean", "Z", __boolean_TypeDescription}, // BOOLEAN - new Object[]{"java.lang.Byte", "B", __byte_TypeDescription}, // BYTE - new Object[]{"java.lang.Short", "S", __short_TypeDescription}, // SHORT - new Object[]{"java.lang.Short", "S", __ushort_TypeDescription}, // UNSIGNED SHORT - new Object[]{"java.lang.Integer", "I", __long_TypeDescription}, // LONG - new Object[]{"java.lang.Integer", "I", __ulong_TypeDescription}, // UNSIGNED_LONG - new Object[]{"java.lang.Long", "J", __hyper_TypeDescription}, // HYPER - new Object[]{"java.lang.Long", "J", __uhyper_TypeDescription}, // UNSIGNED_HYPER - new Object[]{"java.lang.Float", "F", __float_TypeDescription}, // FLOAT - new Object[]{"java.lang.Double", "D", __double_TypeDescription}, // DOUBLE - new Object[]{"java.lang.String", "Ljava.lang.String;", __string_TypeDescription}, // STRING - new Object[]{"com.sun.star.uno.Type", "Lcom.sun.star.uno.Type;", __type_TypeDescription}, // TYPE - new Object[]{"java.lang.Object", "Ljava.lang.Object;", __any_TypeDescription} // ANY - }; - + static private final HashMap __typeNameToTypeDescription = new HashMap(); static { - __typeNameToTypeDescription.put("boolean", __boolean_TypeDescription); - __typeNameToTypeDescription.put("short", __short_TypeDescription); - __typeNameToTypeDescription.put("unsigned short", __ushort_TypeDescription); - __typeNameToTypeDescription.put("long", __long_TypeDescription); - __typeNameToTypeDescription.put("unsigned long", __ulong_TypeDescription); - __typeNameToTypeDescription.put("hyper", __hyper_TypeDescription); - __typeNameToTypeDescription.put("unsigned hyper", __uhyper_TypeDescription); - __typeNameToTypeDescription.put("float", __float_TypeDescription); - __typeNameToTypeDescription.put("double", __double_TypeDescription); - __typeNameToTypeDescription.put("char", __char_TypeDescription); - __typeNameToTypeDescription.put("byte", __byte_TypeDescription); - __typeNameToTypeDescription.put("string", __string_TypeDescription); - __typeNameToTypeDescription.put("any", __any_TypeDescription); - __typeNameToTypeDescription.put("void", __void_TypeDescription); - __typeNameToTypeDescription.put("type", __type_TypeDescription); - - __classToTypeDescription.put(Void.class, __void_TypeDescription); - __classToTypeDescription.put(void.class, __void_TypeDescription); + __typeNameToTypeDescription.put("void", __void_TypeDescription); + __typeNameToTypeDescription.put("boolean", __boolean_TypeDescription); + __typeNameToTypeDescription.put("char", __char_TypeDescription); + __typeNameToTypeDescription.put("byte", __byte_TypeDescription); + __typeNameToTypeDescription.put("short", __short_TypeDescription); + __typeNameToTypeDescription.put("unsigned short", + __ushort_TypeDescription); + __typeNameToTypeDescription.put("long", __long_TypeDescription); + __typeNameToTypeDescription.put("unsigned long", + __ulong_TypeDescription); + __typeNameToTypeDescription.put("hyper", __hyper_TypeDescription); + __typeNameToTypeDescription.put("unsigned hyper", + __uhyper_TypeDescription); + __typeNameToTypeDescription.put("float", __float_TypeDescription); + __typeNameToTypeDescription.put("double", __double_TypeDescription); + __typeNameToTypeDescription.put("string", __string_TypeDescription); + __typeNameToTypeDescription.put("type", __type_TypeDescription); + __typeNameToTypeDescription.put("any", __any_TypeDescription); + } + + static private final HashMap __classToTypeDescription = new HashMap(); + static { + __classToTypeDescription.put(void.class, __void_TypeDescription); + __classToTypeDescription.put(Void.class, __void_TypeDescription); + __classToTypeDescription.put(boolean.class, __boolean_TypeDescription); + __classToTypeDescription.put(Boolean.class, __boolean_TypeDescription); + __classToTypeDescription.put(char.class, __char_TypeDescription); __classToTypeDescription.put(Character.class, __char_TypeDescription); - __classToTypeDescription.put(char.class, __char_TypeDescription); - __classToTypeDescription.put(Boolean.class, __boolean_TypeDescription); - __classToTypeDescription.put(boolean.class, __boolean_TypeDescription); - __classToTypeDescription.put(Byte.class, __byte_TypeDescription); - __classToTypeDescription.put(byte.class, __byte_TypeDescription); - __classToTypeDescription.put(Short.class, __short_TypeDescription); - __classToTypeDescription.put(short.class, __short_TypeDescription); - __classToTypeDescription.put(Integer.class, __long_TypeDescription); - __classToTypeDescription.put(int.class, __long_TypeDescription); - __classToTypeDescription.put(Long.class, __hyper_TypeDescription); - __classToTypeDescription.put(long.class, __hyper_TypeDescription); - __classToTypeDescription.put(Float.class, __float_TypeDescription); - __classToTypeDescription.put(float.class, __float_TypeDescription); - __classToTypeDescription.put(Double.class, __double_TypeDescription); - __classToTypeDescription.put(double.class, __double_TypeDescription); - __classToTypeDescription.put(String.class, __string_TypeDescription); - __classToTypeDescription.put(Type.class, __type_TypeDescription); - __classToTypeDescription.put(Any.class, __any_TypeDescription); - __classToTypeDescription.put(Object.class, __any_TypeDescription); - } - - static public boolean isTypeClassSimple(TypeClass typeClass) { + __classToTypeDescription.put(byte.class, __byte_TypeDescription); + __classToTypeDescription.put(Byte.class, __byte_TypeDescription); + __classToTypeDescription.put(short.class, __short_TypeDescription); + __classToTypeDescription.put(Short.class, __short_TypeDescription); + __classToTypeDescription.put(int.class, __long_TypeDescription); + __classToTypeDescription.put(Integer.class, __long_TypeDescription); + __classToTypeDescription.put(long.class, __hyper_TypeDescription); + __classToTypeDescription.put(Long.class, __hyper_TypeDescription); + __classToTypeDescription.put(float.class, __float_TypeDescription); + __classToTypeDescription.put(Float.class, __float_TypeDescription); + __classToTypeDescription.put(double.class, __double_TypeDescription); + __classToTypeDescription.put(Double.class, __double_TypeDescription); + __classToTypeDescription.put(String.class, __string_TypeDescription); + __classToTypeDescription.put(Type.class, __type_TypeDescription); + __classToTypeDescription.put(Object.class, __any_TypeDescription); + __classToTypeDescription.put(Any.class, __any_TypeDescription); + } + + // must be sorted same as TypeClass: + private static final TypeDescription[] __typeClassToTypeDescription + = new TypeDescription[] { + __void_TypeDescription, + __char_TypeDescription, + __boolean_TypeDescription, + __byte_TypeDescription, + __short_TypeDescription, + __ushort_TypeDescription, + __long_TypeDescription, + __ulong_TypeDescription, + __hyper_TypeDescription, + __uhyper_TypeDescription, + __float_TypeDescription, + __double_TypeDescription, + __string_TypeDescription, + __type_TypeDescription, + __any_TypeDescription + }; + + public static boolean isTypeClassSimple(TypeClass typeClass) { return typeClass.getValue() < __typeClassToTypeDescription.length; } @@ -424,6 +429,23 @@ public class TypeDescription implements ITypeDescription { static private void addToCache(TypeDescription typeDescription) { if(DEBUG_CACHE) System.err.println("addToCache:" + typeDescription.getTypeName()); + // If typeDescription is a sequence type whose base component type is + // any of SHORT, UNSIGNED SHORT, LONG, UNSIGNED LONG, HYPER, UNSIGNED + // HYPER, ANY or com::sun::star::uno::XInterface, do not add it to the + // cache. Those UNO types have corresponding Java types that are not + // unique (e.g., both a sequence of SHORT and a sequence of UNSIGNED + // SHORT map to the Java type short[]), so using __typeCache_class to + // map from such a Java type to a type description could give a wrong + // result: + String n = typeDescription.getTypeName(); + if (n.endsWith("[]short") || n.endsWith("[]unsigned short") + || n.endsWith("[]long") || n.endsWith("[]unsigned long") + || n.endsWith("[]hyper") || n.endsWith("[]unsigned hyper") + || n.endsWith("[]any") + || n.endsWith("[]com.sun.star.uno.XInterface")) { + return; + } + synchronized(__cacheEntrys) { if(DEBUG_CACHE) listCache(); @@ -533,35 +555,30 @@ public class TypeDescription implements ITypeDescription { return typeDescription; } - static public ITypeDescription getTypeDescription(Type type) throws ClassNotFoundException { - ITypeDescription iTypeDescription = type.getTypeDescription(); - - if(iTypeDescription == null) { - if(type.getZClass() != null) - iTypeDescription = getTypeDescription(type.getZClass()); - - else if(type.getTypeClass() != null && type.getTypeClass() != TypeClass.UNKNOWN) - iTypeDescription = getTypeDescription(type.getTypeClass()); - - else if(type.getTypeName() != null) - iTypeDescription = getTypeDescription(type.getTypeName()); - - type.setTypeDescription(iTypeDescription); + public static ITypeDescription getTypeDescription(Type type) + throws ClassNotFoundException + { + ITypeDescription td = type.getTypeDescription(); + if (td == null) { + if (type.getZClass() != null) { + td = getTypeDescription(type.getZClass()); + } else { + td = getTypeDescription(type.getTypeClass()); + if (td == null) { + td = getTypeDescription(type.getTypeName()); + } + } + type.setTypeDescription(td); } - - return iTypeDescription; + return td; } - static public TypeDescription getTypeDescription(TypeClass typeClass) { - TypeDescription typeDescription = null; - - if(typeClass.getValue() < __typeClassToTypeDescription.length) - typeDescription = (TypeDescription)__typeClassToTypeDescription[typeClass.getValue()][2]; - - return typeDescription; + public static TypeDescription getTypeDescription(TypeClass typeClass) { + int n = typeClass.getValue(); + return n < __typeClassToTypeDescription.length + ? __typeClassToTypeDescription[n] : null; } - protected TypeClass _typeClass; protected String _typeName; protected String _arrayTypeName; @@ -659,6 +676,8 @@ public class TypeDescription implements ITypeDescription { _typeName = typeName; _arrayTypeName = arrayTypeName; _class = zClass; + // _componentType == null is ok, since only primitive TypeDescriptions + // are created with this constructor } private TypeDescription(Class zClass) { @@ -887,19 +906,14 @@ public class TypeDescription implements ITypeDescription { } /** - * Gets the component TypeDescription if - * this is an array type. - *

- * @return the TypeDescription + * Gets the component TypeDescription if this is a sequence + * type. + * + * @return the component TypeDescription, or null + * if this is not a sequence type */ public ITypeDescription getComponentType() { - ITypeDescription iTypeDescription = null; - - Class componentClass = getZClass().getComponentType(); - if(componentClass != null) - iTypeDescription = getTypeDescription(componentClass); - - return iTypeDescription; + return _componentType; } /** diff --git a/ridljar/com/sun/star/uno/Any.java b/ridljar/com/sun/star/uno/Any.java index 1dddc264a7cf..3fca2cbfa51f 100644 --- a/ridljar/com/sun/star/uno/Any.java +++ b/ridljar/com/sun/star/uno/Any.java @@ -2,9 +2,9 @@ * * $RCSfile: Any.java,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: jbu $ $Date: 2002-01-18 14:04:55 $ + * last change: $Author: hr $ $Date: 2003-03-26 15:44:54 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -72,7 +72,7 @@ package com.sun.star.uno; * an explicit interface type, so the remote counterpart doesn't need to invoke * a queryInterface). *

- * @version $Revision: 1.4 $ $ $Date: 2002-01-18 14:04:55 $ + * @version $Revision: 1.5 $ $ $Date: 2003-03-26 15:44:54 $ * @since UDK1.0 */ public class Any { @@ -90,6 +90,10 @@ public class Any { */ protected Object _object; + public static final Any VOID = new Any(new Type("void", TypeClass.VOID), + null); + // do not use Type.VOID here to avoid circular dependencies between + // static members of Any and Type /** * Constructs a new any. @@ -129,6 +133,9 @@ public class Any { public Object getObject() { return _object; } -} - + // @see java.lang.Object#toString + public String toString() { + return "Any[" + _type + ", " + _object + "]"; + } +} diff --git a/ridljar/com/sun/star/uno/ITypeDescription.java b/ridljar/com/sun/star/uno/ITypeDescription.java index 7788076df264..88250fb649ef 100644 --- a/ridljar/com/sun/star/uno/ITypeDescription.java +++ b/ridljar/com/sun/star/uno/ITypeDescription.java @@ -2,9 +2,9 @@ * * $RCSfile: ITypeDescription.java,v $ * - * $Revision: 1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: kr $ $Date: 2001-05-08 09:34:18 $ + * last change: $Author: hr $ $Date: 2003-03-26 15:44:54 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -65,7 +65,7 @@ package com.sun.star.uno; * The ITypeDescription allows to examine a type * in detail (e.g. it is used for marshaling/unmarshaling). *

- * @version $Revision: 1.1 $ $ $Date: 2001-05-08 09:34:18 $ + * @version $Revision: 1.2 $ $ $Date: 2003-03-26 15:44:54 $ * @author Kay Ramme * @since UDK3.0 */ @@ -139,16 +139,77 @@ public interface ITypeDescription { ITypeDescription getComponentType(); /** - * Gets the type name. - *

- * @return the type name. + * Gets the (UNO) type name. + * + *

The following table lists how UNO types map to type names:

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * "hyper" + * + * "unsigned hyper" + * + * "float" + * "double" + * "string" + * "type" + * "any" + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
UNO typetype name
VOID"void"
BOOLEAN"boolean"
CHAR"char"
BYTE"byte"
SHORT"short"
UNSIGNED SHORT"unsigned short"
LONG"long"
UNSIGNED LONG"unsigned long"
HYPER
UNSIGNED HYPER
FLOAT
DOUBLE
STRING
TYPE
ANY
sequence type of base type T"[]" followed by type name for T
enum type named NN (see below)
struct type named NN (see below)
exception type named N + * N (see below)
interface type named NN (see below)
+ *

For a UNO type named N, consisting of a sequence of module + * names M1, ..., Mn followed by + * a simple name S, the corresponding type name consists of the + * same sequence of module names and simple name, with "." + * seperating the individual elements.

+ * + * @return the type name. */ String getTypeName(); /** - * Gets the array type name. - *

- * @return the array type name. + * Gets the (Java) array type name. + * + *

The array type name is defined to be the Java class name (as returned + * by Class.forName) of the Java array class that corresponds + * to the UNO sequence type with this type (the UNO type represented by this + * ITypeDescription instance) as base type. For an + * ITypeDescription instance representing the UNO type VOID, + * the array type name is defined to be + * "[Ljava.lang.Void;".

+ * + * @return the array type name. */ String getArrayTypeName(); diff --git a/ridljar/com/sun/star/uno/Type.java b/ridljar/com/sun/star/uno/Type.java index 7fd378ad1529..9e368500d15a 100644 --- a/ridljar/com/sun/star/uno/Type.java +++ b/ridljar/com/sun/star/uno/Type.java @@ -2,9 +2,9 @@ * * $RCSfile: Type.java,v $ * - * $Revision: 1.8 $ + * $Revision: 1.9 $ * - * last change: $Author: dbo $ $Date: 2002-10-30 11:10:41 $ + * last change: $Author: hr $ $Date: 2003-03-26 15:44:55 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -61,28 +61,35 @@ package com.sun.star.uno; - - -import java.util.Hashtable; +import java.util.HashMap; /** - * The Type class represents the IDL builtin type type. - *

- * The IDL type is not directly mapped to java.lang.Class, - * because it can be necessary to describe a type which is unknown - * to the java runtime system, e.g. for delaying the need of a class, - * so that it is possible to generate it on the fly. - *

- * @version $Revision: 1.8 $ $ $Date: 2002-10-30 11:10:41 $ - * @since UDK1.0 + * Represents the UNO built-in type TYPE. + * + *

The UNO type is not directly mapped to java.lang.Class for at + * least two reasons. For one, some UNO types (like UNSIGNED + * SHORT) do not have a matching Java class. For another, it can be + * necessary to describe a type which is unknown to the Java runtime system + * (for example, for delaying the need of a class, so that it is possible to + * generate it on the fly.)

+ * + *

A Type is uniquely determined by its type class (a + * TypeClass) and its type name (a String); these two + * will never be null. A Type may have an additional + * "z class" (a java.lang.Class), giving a Java class type that + * corresponds to the UNO type. Also, a Type can cache a type + * description (a com.sun.star.uno.ITypeDescription), which can be + * computed and set by TypeDescription.getTypeDescription. + * + * @since UDK1.0 */ public class Type { - /** - * When set to true, enables various debugging output. - */ - private static final boolean DEBUG = false; + // The following private static members and static initializer must come + // first in the class definition, so that the class can be initialized + // sucessfully: - static private final String[] __typeClassToTypeName = new String[]{ + // must be sorted same as TypeClass: + private static final String[] __typeClassToTypeName = new String[] { "void", "char", "boolean", @@ -100,67 +107,90 @@ public class Type { "any" }; - static private final Hashtable __javaClassToTypeName = new Hashtable(); + private static final HashMap __javaClassToTypeName = new HashMap(); - static private final Hashtable __typeNameToTypeClass = new Hashtable(); + private static final HashMap __typeNameToTypeClass = new HashMap(); static { - for(int i = 0; i < __typeClassToTypeName.length; ++ i) - __typeNameToTypeClass.put(__typeClassToTypeName[i], TypeClass.fromInt(i)); - - __javaClassToTypeName.put(Void.class, "void"); - __javaClassToTypeName.put(void.class, "void"); + for (int i = 0; i < __typeClassToTypeName.length; ++i) { + __typeNameToTypeClass.put(__typeClassToTypeName[i], + TypeClass.fromInt(i)); + } + __javaClassToTypeName.put(Void.class, "void"); + __javaClassToTypeName.put(void.class, "void"); __javaClassToTypeName.put(Character.class, "char"); __javaClassToTypeName.put(char.class, "char"); - __javaClassToTypeName.put(Boolean.class, "boolean"); - __javaClassToTypeName.put(boolean.class, "boolean"); - __javaClassToTypeName.put(Byte.class, "byte"); - __javaClassToTypeName.put(byte.class, "byte"); - __javaClassToTypeName.put(Short.class, "short"); - __javaClassToTypeName.put(short.class, "short"); - __javaClassToTypeName.put(Integer.class, "long"); - __javaClassToTypeName.put(int.class, "long"); - __javaClassToTypeName.put(Long.class, "hyper"); - __javaClassToTypeName.put(long.class, "hyper"); - __javaClassToTypeName.put(Float.class, "float"); - __javaClassToTypeName.put(float.class, "float"); - __javaClassToTypeName.put(Double.class, "double"); - __javaClassToTypeName.put(double.class, "double"); - __javaClassToTypeName.put(String.class, "string"); - __javaClassToTypeName.put(Type.class, "type"); - __javaClassToTypeName.put(Any.class, "any"); - __javaClassToTypeName.put(Object.class, "any"); + __javaClassToTypeName.put(Boolean.class, "boolean"); + __javaClassToTypeName.put(boolean.class, "boolean"); + __javaClassToTypeName.put(Byte.class, "byte"); + __javaClassToTypeName.put(byte.class, "byte"); + __javaClassToTypeName.put(Short.class, "short"); + __javaClassToTypeName.put(short.class, "short"); + __javaClassToTypeName.put(Integer.class, "long"); + __javaClassToTypeName.put(int.class, "long"); + __javaClassToTypeName.put(Long.class, "hyper"); + __javaClassToTypeName.put(long.class, "hyper"); + __javaClassToTypeName.put(Float.class, "float"); + __javaClassToTypeName.put(float.class, "float"); + __javaClassToTypeName.put(Double.class, "double"); + __javaClassToTypeName.put(double.class, "double"); + __javaClassToTypeName.put(String.class, "string"); + __javaClassToTypeName.put(Type.class, "type"); + __javaClassToTypeName.put(Any.class, "any"); + __javaClassToTypeName.put(Object.class, "any"); } - static private boolean __isTypeClassPrimitive(TypeClass typeClass) { - return typeClass.getValue() < 15; - } - - - protected String _typeName; - protected Class _class; - protected TypeClass _typeClass; - - protected ITypeDescription _iTypeDescription; - + public static final Type VOID = new Type(void.class); + public static final Type CHAR = new Type(char.class); + public static final Type BOOLEAN = new Type(boolean.class); + public static final Type BYTE = new Type(byte.class); + public static final Type SHORT = new Type(short.class); + public static final Type UNSIGNED_SHORT = new Type( + "unsigned short", TypeClass.UNSIGNED_SHORT); + public static final Type LONG = new Type(int.class); + public static final Type UNSIGNED_LONG = new Type( + "unsigned long", TypeClass.UNSIGNED_LONG); + public static final Type HYPER = new Type(long.class); + public static final Type UNSIGNED_HYPER = new Type( + "unsigned hyper", TypeClass.UNSIGNED_HYPER); + public static final Type FLOAT = new Type(float.class); + public static final Type DOUBLE = new Type(double.class); + public static final Type STRING = new Type(String.class); + public static final Type TYPE = new Type(Type.class); + public static final Type ANY = new Type(Any.class); /** - * Constructs a new Type which defaults - * to void - *

- * @since UDK3.0 + * Constructs a new Type which defaults to VOID. + * + * @since UDK3.0 */ public Type() { this(void.class); } + /** + * Constructs a new Type with the given type class and type + * name. + * + * @param typeName the type name. Must not be null. + * @param typeClass the type class. Must not be null, and must + * match the typeName (for example, it is illegal to + * combine a typeName of "void" with a + * typeClass of BOOLEAN). + */ + public Type(String typeName, TypeClass typeClass) { + _typeClass = typeClass; + _typeName = typeName; + } /** - * Constructs a new Type with - * the given class. - *

- * @since UDK3.0 - * @param zClass the java class of this type + * Constructs a new Type from the given + * java.lang.Class. + * + * @param zClass the Java class of this type. Must not be + * null. + * + * @since UDK3.0 */ public Type(Class zClass) { _class = zClass; @@ -202,27 +232,37 @@ public class Type { } /** - * Constructs a new Type with - * the given type description. - *

- * @since UDK3.0 - * @param typeDescription a type description + * Constructs a new Type from the given type description. + * + * @param typeDescription a type description. Must not be + * null. + * + * @since UDK3.0 */ - public Type(ITypeDescription iTypeDescription) { - _typeName = iTypeDescription.getTypeName(); - _typeClass = iTypeDescription.getTypeClass(); - _iTypeDescription = iTypeDescription; + public Type(ITypeDescription typeDescription) { + _typeName = typeDescription.getTypeName(); + _typeClass = typeDescription.getTypeClass(); + _iTypeDescription = typeDescription; } /** - * Constructs a new Type with - * the given type name. - *

- * @since UDK3.0 - * @param typeName the name of this type. For simple types - * (numbers,string,type,any), the typeclass is calculated, - * for complex types (structs,interfaces), the - * typeclass of this object is set to UNKNOWN + * Constructs a new Type with the given type name. + * + *

TODO: This constructor is dangerous, as it can create a + * Type with an UNKNOWN type class. It would be + * better if this constructor threw a IllegalArgumentException + * instead.

+ * + * @param typeName the name of this type; must not be null. + * For simple types (VOID, BOOLEAN, + * CHAR, BYTE, SHORT, + * UNSIGNED SHORT, LONG, UNSIGNED + * LONG, HYPER, UNSIGNED HYPER, + * FLOAT, DOUBLE, STRING, + * TYPE, ANY), the type class is calculated; + * for other types, the type class is set to UNKNOWN. + * + * @since UDK3.0 */ public Type(String typeName) { _typeClass = (TypeClass)__typeNameToTypeClass.get(typeName); @@ -233,26 +273,20 @@ public class Type { _typeName = typeName; } - public Type( String type_name, TypeClass type_class ) - { - _typeClass = type_class; - _typeName = type_name; - } - - /** - * Constructs a new Type with - * the given TypeClass. - *

- * @since UDK3.0 - * @param typeClass the TypeClass of this type. Only typeclass for - * simple types is allowed. + * Constructs a new Type with the given type class. + * + * @param typeClass the type class of this type; must not be + * null. Only type classes for simple types are allowed + * here. + * + * @throws IllegalArgumentException if the given typeClass is + * not simple (for example, a struct or an interface type). This + * constructor could not find out the type name in such a case. * - * @throws IllegalArgumentException when the typeClass is not simple (e.g. - a struct or an interface. The Constructor cannot find out the - name of the type in this case. + * @since UDK3.0 */ - public Type(TypeClass typeClass) throws IllegalArgumentException { + public Type(TypeClass typeClass) { if(__isTypeClassPrimitive(typeClass)) { _typeClass = typeClass; _typeName = __typeClassToTypeName[typeClass.getValue()]; @@ -262,93 +296,88 @@ public class Type { } /** - * Gives the type description of this type. - *

- * @since UDK3.0 - * @return the type description - */ - public ITypeDescription getTypeDescription() { - return _iTypeDescription; - } - - /** - * Sets the type description for this type. - *

- * @since UDK3.0 - * @return the type description + * Gets the type class. + * + * @return the type class. Will never be null, but might be + * UNKNOWN. + * + * @since UDK1.0 */ - public void setTypeDescription(ITypeDescription iTypeDescription) { - _iTypeDescription = iTypeDescription; + public TypeClass getTypeClass() { + return _typeClass; } /** * Gets the type name. - * Returns null if this - * type has not been constructed by name. - *

- * @since UDK1.0 - * @return the type name. + * + * @return the type name; will never be null + * + * @since UDK1.0 */ public String getTypeName() { return _typeName; } /** - * Gets the java class. - * Returns null if this - * type has not been constructed by Class. - *

- * @since UDK1.0 - * @return the type name. Maybe null. + * Gets the Java class. + * + *

Returns null if this type has not been constructed by + * Class.

+ * + * @return the type name; may be null + * + * @since UDK1.0 */ public Class getZClass() { return _class; } /** - * Gets the typeClass. - * Returns null if this - * type has not been constructed by TypeClass. - *

- * @since UDK1.0 - * @return the type class. May be TypeClass.UNKNOWN. + * Gives the type description of this type. + * + * @return the type description; may be null + * + * @since UDK3.0 */ - public TypeClass getTypeClass() { - return _typeClass; + public ITypeDescription getTypeDescription() { + return _iTypeDescription; } /** - * Compares two types. - *

- * @return true, if the given type and this type are equal + * Sets the type description for this type. + * + * @param typeDescription the type description + * + * @since UDK3.0 */ - public boolean equals(Object object) { - boolean result = false; - - Type type = (Type)object; - - if(type != null) - result = _typeName.equals(type._typeName); + public void setTypeDescription(ITypeDescription typeDescription) { + _iTypeDescription = typeDescription; + } - return result; + // @see java.lang.Object#equals + public boolean equals(Object obj) { + return this == obj + || (obj instanceof Type + && _typeName.equals(((Type) obj)._typeName)); } - /** - * Calculates the hash code. - *

- * @return the hash code - */ + // @see java.lang.Object#hashCode public int hashCode() { return _typeName.hashCode(); } - /** - * Constructs a descriptive String for the type. - *

- * @return a descriptive String - */ + // @see java.lang.Object#toString public String toString() { - return "Type<" + _typeName + ">"; + return "Type[" + _typeName + "]"; } -} + private static boolean __isTypeClassPrimitive(TypeClass typeClass) { + return typeClass.getValue() < __typeClassToTypeName.length; + } + + protected TypeClass _typeClass; // TODO should be final + protected String _typeName; // TODO should be final + protected Class _class; // TODO should be final + + protected ITypeDescription _iTypeDescription; +} -- cgit