public final Object convertValue ( Object value , IType intrType ) { //================================================================================== // Null handling //================================================================================== if ( intrType == null ) { return null ; } //## todo: This is a horrible hack //## todo: The only known case where this is necessary is when we have an array of parameterized java type e.g., List<String>[] intrType = getBoundingTypeOfTypeVariable ( intrType ) ; if ( value == null ) { return intrType . isPrimitive ( ) ? convertNullAsPrimitive ( intrType , true ) : null ; } IType runtimeType = TypeSystem . getFromObject ( value ) ; //================================================================================== // IPlaceholder type handling //================================================================================== if ( ( intrType instanceof IPlaceholder && ( ( IPlaceholder ) intrType ) . isPlaceholder ( ) ) || ( runtimeType instanceof IPlaceholder && ( ( IPlaceholder ) runtimeType ) . isPlaceholder ( ) ) ) { return value ; } //================================================================================== // Runtime polymorphism (with java array semantics) //================================================================================== if ( intrType == runtimeType ) { return value ; } if ( intrType . equals ( runtimeType ) ) { return value ; } if ( intrType . isAssignableFrom ( runtimeType ) ) { value = extractObjectArray ( intrType , value ) ; return value ; } if ( intrType . isArray ( ) && runtimeType . isArray ( ) ) { if ( intrType . getComponentType ( ) . isAssignableFrom ( runtimeType . getComponentType ( ) ) ) { value = extractObjectArray ( intrType , value ) ; return value ; } else if ( intrType instanceof IGosuArrayClass && value instanceof IGosuObject [ ] ) { return value ; } } // Proxy coercion. The proxy class generated for Java classes is not a super type of the Gosu class. // The following check allows coercion of the Gosu class to the Gosu proxy class needed for the super call. if ( intrType instanceof IJavaType && IGosuClass . ProxyUtil . isProxy ( intrType ) && runtimeType instanceof IGosuClass && intrType . getSupertype ( ) != null && intrType . getSupertype ( ) . isAssignableFrom ( runtimeType ) ) { return value ; } // Check Java world types //noinspection deprecation,unchecked if ( intrType instanceof IJavaType && ( ( IJavaType ) intrType ) . getIntrinsicClass ( ) . isAssignableFrom ( value . getClass ( ) ) ) { return value ; } // casts to structs are dynamic, pass value through unchanged if ( intrType instanceof IGosuClass && ( ( IGosuClass ) intrType ) . isStructure ( ) ) { return value ; } //================================================================================== // Coercion //================================================================================== Object convertedValue = coerce ( intrType , runtimeType , value ) ; if ( convertedValue != null ) { return convertedValue ; } else { //If the null arose from an actual coercion, return it if ( canCoerce ( intrType , runtimeType ) ) { return convertedValue ; } else { //otherwise, return the value itself uncoerced (See comment above) if ( ! runtimeType . isArray ( ) ) { return NO_DICE ; } return value ; } } }
Given a value and a target Class return a compatible value via the target Class .