Top Description Fields Constructors Methods
bsh

pack-priv Class Types

extends Object
Class Inheritance

Static routines supporing type comparison and conversion in BeanShell. The following are notes on type comparison and conversion in BeanShell.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static Primitive
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static Primitive
VALID_CAST

Special value that indicates by identity that the result of a cast operation was a valid cast.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv static UtilEvalError
castError(Class<T> lhsType, Class<T> rhsType, int operation)

Return a UtilEvalError or UtilTargetError wrapping a ClassCastException describing an illegal assignment or illegal cast, respectively.

pack-priv static UtilEvalError
castError(String lhs, String rhs, int operation)

public static Object
castObject(Object
an Object or bsh.Primitive primitive value (including Primitive.NULL or Primitive.VOID )
fromValue
,
Class<T>
the class type of the cast result, which may include primitive types, e.g. Byte.TYPE
toType
,
int operation)

Attempt to cast an object instance to a new type if possible via BeanShell extended syntax rules.

private static Object
castObject(Class<T>
the class type of the cast result, which may include primitive types, e.g. Byte.TYPE. toType may be null to indicate a loose type assignment (which matches any fromType).
toType
,
Class<T>
is the class type of the value to be cast including java primitive TYPE classes for primitives. If fromValue is (or would be) Primitive.NULL then fromType should be null.
fromType
,
Object
an Object or bsh.Primitive primitive value (including Primitive.NULL or Primitive.VOID )
fromValue
,
int
is Types.CAST or Types.ASSIGNMENT
operation
,
boolean
If checkOnly is true then fromValue must be null. FromType is checked for the cast to toType... If checkOnly is false then fromValue must be non-null (Primitive.NULL is ok) and the actual cast is performed.
checkOnly
)

Perform a type conversion or test if a type conversion is possible with respect to BeanShell extended rules.

public static Class<T>[]
getTypes(Object[] args)

Get the Java types of the arguments.

pack-priv static boolean
isBshAssignable(Class<T> toType, Class<T> fromType)

Test if a type can be converted to another type via BeanShell extended syntax rules (a superset of Java conversion rules).

pack-priv static boolean
isJavaAssignable(Class<T>
assigning from rhsType to lhsType
lhsType
,
Class<T>
assigning from rhsType to lhsType
rhsType
)

Test if a conversion of the rhsType type to the lhsType type is legal via standard Java assignment conversion rules (i.e. without a cast).

pack-priv static boolean
isJavaBaseAssignable(Class<T> lhsType, Class<T>
may be null to indicate primitive null value
rhsType
)

Is the assignment legal via original Java (up to version 1.4) assignment rules, not including auto-boxing/unboxing.

pack-priv static boolean
isJavaBoxTypesAssignable(Class<T> lhsType, Class<T> rhsType)

Determine if the type is assignable via Java boxing/unboxing rules.

pack-priv static boolean
isSignatureAssignable(Class<T>[] from, Class<T>[] to, int round)

Is the 'from' signature (argument types) assignable to the 'to' signature (candidate method types) This method handles the special case of null values in 'to' types indicating a loose type and matching anything.

private static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ASSIGNMENTback to summary
pack-priv static final int ASSIGNMENT
BSH_ASSIGNABLEback to summary
pack-priv static final int BSH_ASSIGNABLE
CASTback to summary
pack-priv static final int CAST
FIRST_ROUND_ASSIGNABLEback to summary
pack-priv static final int FIRST_ROUND_ASSIGNABLE
INVALID_CASTback to summary
pack-priv static Primitive INVALID_CAST
JAVA_BASE_ASSIGNABLEback to summary
pack-priv static final int JAVA_BASE_ASSIGNABLE
JAVA_BOX_TYPES_ASSIGABLEback to summary
pack-priv static final int JAVA_BOX_TYPES_ASSIGABLE
JAVA_VARARGS_ASSIGNABLEback to summary
pack-priv static final int JAVA_VARARGS_ASSIGNABLE
LAST_ROUND_ASSIGNABLEback to summary
pack-priv static final int LAST_ROUND_ASSIGNABLE
VALID_CASTback to summary
pack-priv static Primitive VALID_CAST

Special value that indicates by identity that the result of a cast operation was a valid cast. This is used by castObject() and castPrimitive() in the checkOnly mode of operation. This value is a Primitive type so that it can be returned by castPrimitive.

Constructor Detail

Typesback to summary
pack-priv Types()

Method Detail

castErrorback to summary
pack-priv static UtilEvalError castError(Class<T> lhsType, Class<T> rhsType, int operation)

Return a UtilEvalError or UtilTargetError wrapping a ClassCastException describing an illegal assignment or illegal cast, respectively.

castErrorback to summary
pack-priv static UtilEvalError castError(String lhs, String rhs, int operation)
castObjectback to summary
public static Object castObject(Object fromValue, Class<T> toType, int operation) throws UtilEvalError

Attempt to cast an object instance to a new type if possible via BeanShell extended syntax rules. These rules are always a superset of Java conversion rules. If you wish to impose context sensitive conversion rules then you must test before calling this method.

This method can handle fromValue Primitive types (representing primitive casts) as well as fromValue object casts requiring interface generation, etc.

Parameters
fromValue:Object

an Object or bsh.Primitive primitive value (including Primitive.NULL or Primitive.VOID )

toType:Class<T>

the class type of the cast result, which may include primitive types, e.g. Byte.TYPE

See Also
isBshAssignable(Class, Class)
castObjectback to summary
private static Object castObject(Class<T> toType, Class<T> fromType, Object fromValue, int operation, boolean checkOnly) throws UtilEvalError

Perform a type conversion or test if a type conversion is possible with respect to BeanShell extended rules. These rules are always a superset of the Java language rules, so this method can also perform (but not test) any Java language assignment or cast conversion.

This method can perform the functionality of testing if an assignment or cast is ultimately possible (with respect to BeanShell) as well as the functionality of performing the necessary conversion of a value based on the specified target type. This combined functionality is done for expediency and could be separated later.

Other methods such as isJavaAssignable() should be used to determine the suitability of an assignment in a fine grained or restrictive way based on context before calling this method

A CAST is stronger than an ASSIGNMENT operation in that it will attempt to perform primtive operations that cast to a smaller type. e.g. (byte)myLong; These are used in explicit primitive casts, primitive delclarations and array declarations. I don't believe there are any object conversions which are different between ASSIGNMENT and CAST (e.g. scripted object to interface proxy in bsh is done on assignment as well as cast).

This method does not obey strictJava(), you must test first before using this method if you care. (See #isJavaAssignable()).

Parameters
toType:Class<T>

the class type of the cast result, which may include primitive types, e.g. Byte.TYPE. toType may be null to indicate a loose type assignment (which matches any fromType).

fromType:Class<T>

is the class type of the value to be cast including java primitive TYPE classes for primitives. If fromValue is (or would be) Primitive.NULL then fromType should be null.

fromValue:Object

an Object or bsh.Primitive primitive value (including Primitive.NULL or Primitive.VOID )

operation:int

is Types.CAST or Types.ASSIGNMENT

checkOnly:boolean

If checkOnly is true then fromValue must be null. FromType is checked for the cast to toType... If checkOnly is false then fromValue must be non-null (Primitive.NULL is ok) and the actual cast is performed.

Exceptions
UtilEvalError:
on invalid assignment (when operation is assignment ).
UtilTargetError:
wrapping ClassCastException on cast error (when operation is cast)
See Also
bsh.Primitive.getType()
getTypesback to summary
public static Class<T>[] getTypes(Object[] args)

Get the Java types of the arguments.

isBshAssignableback to summary
pack-priv static boolean isBshAssignable(Class<T> toType, Class<T> fromType)

Test if a type can be converted to another type via BeanShell extended syntax rules (a superset of Java conversion rules).

isJavaAssignableback to summary
pack-priv static boolean isJavaAssignable(Class<T> lhsType, Class<T> rhsType)

Test if a conversion of the rhsType type to the lhsType type is legal via standard Java assignment conversion rules (i.e. without a cast). The rules include Java 5 autoboxing/unboxing.

For Java primitive TYPE classes this method takes primitive promotion into account. The ordinary Class.isAssignableFrom() does not take primitive promotion conversions into account. Note that Java allows additional assignments without a cast in combination with variable declarations and array allocations. Those are handled elsewhere (maybe should be here with a flag?)

This class accepts a null rhsType type indicating that the rhsType was the value Primitive.NULL and allows it to be assigned to any reference lhsType type (non primitive).

Note that the getAssignableForm() method is the primary bsh method for checking assignability. It adds additional bsh conversions, etc.

Parameters
lhsType:Class<T>

assigning from rhsType to lhsType

rhsType:Class<T>

assigning from rhsType to lhsType

See Also
isBshAssignable(Class, Class)
isJavaBaseAssignableback to summary
pack-priv static boolean isJavaBaseAssignable(Class<T> lhsType, Class<T> rhsType)

Is the assignment legal via original Java (up to version 1.4) assignment rules, not including auto-boxing/unboxing.

Parameters
rhsType:Class<T>

may be null to indicate primitive null value

isJavaBoxTypesAssignableback to summary
pack-priv static boolean isJavaBoxTypesAssignable(Class<T> lhsType, Class<T> rhsType)

Determine if the type is assignable via Java boxing/unboxing rules.

isSignatureAssignableback to summary
pack-priv static boolean isSignatureAssignable(Class<T>[] from, Class<T>[] to, int round)

Is the 'from' signature (argument types) assignable to the 'to' signature (candidate method types) This method handles the special case of null values in 'to' types indicating a loose type and matching anything.

isSignatureVarargsAssignableback to summary
private static boolean isSignatureVarargsAssignable(Class<T>[] from, Class<T>[] to)