com.go.tea.compiler
Class Type

java.lang.Object
  |
  +--com.go.tea.compiler.Type
All Implemented Interfaces:
java.io.Serializable

public class Type
extends java.lang.Object
implements java.io.Serializable

Immutable representation of an expression's type.

Version:
45 , 01/02/05
Author:
Brian S O'Neill
See Also:
Expression, Serialized Form

Field Summary
static Type BOOLEAN_TYPE
          Type for representing booleans, provided as a convenience
static Type INT_TYPE
          Type for representing ints, provided as a convenience
static Type LONG_TYPE
          Type for representing longs, provided as a convenience
static Type NON_NULL_STRING_TYPE
          Type for representing non-null Strings, provided as a convenience
static Type NULL_TYPE
          Type that is compatble with all other types
static Type OBJECT_TYPE
          Type that represents all objects, provided as a convenience
static Type STRING_TYPE
          Type for representing Strings, provided as a convenience
static Type VOID_TYPE
          Type that represents void, provided as a convenience
 
Constructor Summary
Type(java.lang.Class type)
           
 
Method Summary
 int convertableFrom(Type other)
          Returns the conversion cost of assigning the given type to this type.
 boolean equals(java.lang.Object another)
           
static java.lang.Class findCommonBaseClass(java.lang.Class a, java.lang.Class b)
          Returns the most specific common superclass or interface that can be used to represent both of the specified classes.
 java.lang.reflect.Method[] getArrayAccessMethods()
          If this Type supports array lookup, then return all of the methods that can be called to access the array.
 Type getArrayElementType()
          If this Type supports array lookup, then return the element type.
 Type[] getArrayIndexTypes()
          If this Type supports array lookup, then return the index type.
 Type getCompatibleType(Type other)
          Returns a type that is compatible with this type, and the one passed in.
 java.lang.String getFullName()
           
 Type getIterationElementType()
          If this type supports iteration, then the element type is returned.
 java.lang.Class getNaturalClass()
          Returns the natural class for this type.
 java.lang.Class getObjectClass()
          Class returned never represents a primitive type.
 java.lang.String getSimpleName()
           
 int hashCode()
           
 boolean hasPrimitivePeer()
          Returns true if this type is not primitive, but it has a primitive type peer.
 boolean isNonNull()
          Returns true if this type cannot reference null.
 boolean isNullable()
          Returns true if this type can reference null, or simply the opposite result of isNonNull.
 boolean isPrimitive()
          Returns true if this type is a primitive.
 boolean isReverseIterationSupported()
          Returns true if this type supports iteration in the reverse direction.
 Type toNonNull()
          Returns this type converted such that it cannot reference null.
 Type toNonPrimitive()
          Returns a new type from this one that represents a non-primitive type.
 Type toNullable()
          Returns this type converted such that it can reference null.
 Type toPrimitive()
          Returns a new type from this one that represents a primitive type.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL_TYPE

public static final Type NULL_TYPE
Type that is compatble with all other types

VOID_TYPE

public static final Type VOID_TYPE
Type that represents void, provided as a convenience

OBJECT_TYPE

public static final Type OBJECT_TYPE
Type that represents all objects, provided as a convenience

INT_TYPE

public static final Type INT_TYPE
Type for representing ints, provided as a convenience

LONG_TYPE

public static final Type LONG_TYPE
Type for representing longs, provided as a convenience

BOOLEAN_TYPE

public static final Type BOOLEAN_TYPE
Type for representing booleans, provided as a convenience

STRING_TYPE

public static final Type STRING_TYPE
Type for representing Strings, provided as a convenience

NON_NULL_STRING_TYPE

public static final Type NON_NULL_STRING_TYPE
Type for representing non-null Strings, provided as a convenience
Constructor Detail

Type

public Type(java.lang.Class type)
Method Detail

getObjectClass

public java.lang.Class getObjectClass()
Class returned never represents a primitive type.

getNaturalClass

public java.lang.Class getNaturalClass()
Returns the natural class for this type. If type is primitive, then its primitive peer is returned.

isPrimitive

public boolean isPrimitive()
Returns true if this type is a primitive.

hasPrimitivePeer

public boolean hasPrimitivePeer()
Returns true if this type is not primitive, but it has a primitive type peer.

toPrimitive

public Type toPrimitive()
Returns a new type from this one that represents a primitive type. If this type cannot be represented by a primitive, then this is returned.

toNonPrimitive

public Type toNonPrimitive()
Returns a new type from this one that represents a non-primitive type. If this type actually is primitive, the returned type is marked as not being able to reference null. i.e. if this type is int, new Type(Integer.class).toNonNull() is returned.

isNonNull

public boolean isNonNull()
Returns true if this type cannot reference null. For primitive types, true is always returned.

isNullable

public boolean isNullable()
Returns true if this type can reference null, or simply the opposite result of isNonNull.

toNonNull

public Type toNonNull()
Returns this type converted such that it cannot reference null.

toNullable

public Type toNullable()
Returns this type converted such that it can reference null. The resulting type is never primitive.

getArrayElementType

public Type getArrayElementType()
                         throws java.beans.IntrospectionException
If this Type supports array lookup, then return the element type. Otherwise, null is returned.

getArrayIndexTypes

public Type[] getArrayIndexTypes()
                          throws java.beans.IntrospectionException
If this Type supports array lookup, then return the index type. Because the index type may be overloaded, an array is returned. Null is returned if this Type doesn't support array lookup.

getArrayAccessMethods

public java.lang.reflect.Method[] getArrayAccessMethods()
                                                 throws java.beans.IntrospectionException
If this Type supports array lookup, then return all of the methods that can be called to access the array. If there are no methods, then an empty array is returned. Null is returned only if this Type doesn't support array lookup.

getIterationElementType

public Type getIterationElementType()
                             throws java.beans.IntrospectionException
If this type supports iteration, then the element type is returned. Otherwise, null is returned.

isReverseIterationSupported

public boolean isReverseIterationSupported()
Returns true if this type supports iteration in the reverse direction.

getSimpleName

public java.lang.String getSimpleName()

getFullName

public java.lang.String getFullName()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object another)
Overrides:
equals in class java.lang.Object

getCompatibleType

public Type getCompatibleType(Type other)
Returns a type that is compatible with this type, and the one passed in. The type returned is selected using a best-fit algorithm.

If the type passed in represents a primitive type, but this type is not, the type returned is an object (or a subclass of), but never a primitive type. Compatible primitive types are returned when both this and the parameter type were already primitive types.

Input types which are arrays are also supported by this method.

Returns null if the given type isn't compatible with this one.


findCommonBaseClass

public static java.lang.Class findCommonBaseClass(java.lang.Class a,
                                                  java.lang.Class b)
Returns the most specific common superclass or interface that can be used to represent both of the specified classes. Null is only returned if either class refers to a primitive type and isn't the same as the other class.

convertableFrom

public int convertableFrom(Type other)
Returns the conversion cost of assigning the given type to this type. Conversions are allowed between arrays as well if they have the same dimensions. If no legal conversion exists, -1 is returned. The conversion costs are as follows:
  1. any type is assignable by its own type
  2. any superclass type is assignable by a subclass type
  3. any object with a primitive peer can be converted to its primitive
  4. any primitive can be converted to its object peer
  5. any byte or short can be widened to an int
  6. any byte, short or int can be widened to a long
  7. any byte or short can be widened to a float
  8. any primitive number can be widened to a double
  9. any Number object can be converted to a primitive and widened
  10. any primitive number can be converted to a Number object
  11. any primitive number can be widened to a Number object
  12. any Number object can be widened to another Number object
  13. any primitive number can be narrowed to a double
  14. any Number object can be narrowed to a double
  15. any primitive number can be narrowed to a Double object
  16. any Number object can be narrowed to a Double object
  17. any primitive number can be narrowed to a long
  18. any Number object can be narrowed to a long
  19. any primitive number can be narrowed to a Long object
  20. any Number object can be narrowed to a Long object
  21. any primitive number can be narrowed to a float
  22. any Number object can be narrowed to a float
  23. any primitive number can be narrowed to a Float object
  24. any Number object can be narrowed to a Float object
  25. any primitive number can be narrowed to a int
  26. any Number object can be narrowed to a int
  27. any primitive number can be narrowed to a Integer object
  28. any Number object can be narrowed to a Integer object
  29. any primitive number can be narrowed to a short
  30. any Number object can be narrowed to a short
  31. any primitive number can be narrowed to a Short object
  32. any Number object can be narrowed to a Short object
  33. any primitive number can be narrowed to a byte
  34. any Number object can be narrowed to a byte
  35. any primitive number can be narrowed to a Byte object
  36. any Number object can be narrowed to a Byte object
  37. any primitive can be converted to an object
  38. NULL_TYPE can be converted to any object
  39. anything can be converted to a String
Returns:
the conversion cost, or -1 if the other can't assign to this