/** Determine if the type is assignable via Java boxing/unboxing rules. */ static boolean isJavaBoxTypesAssignable( Class lhsType, Class rhsType ) { // Assignment to loose type... defer to bsh extensions if ( lhsType == null ) return false; // prim can be boxed and assigned to Object if ( lhsType == Object.class ) return true; // null rhs type corresponds to type of Primitive.NULL // assignable to any object type but not array if (rhsType == null) return !lhsType.isPrimitive() && !lhsType.isArray(); // prim numeric type can be boxed and assigned to number if ( lhsType == Number.class && rhsType != Character.TYPE && rhsType != Boolean.TYPE ) return true; // General case prim type to wrapper or vice versa. // I don't know if this is faster than a flat list of 'if's like above. // wrapperMap maps both prim to wrapper and wrapper to prim types, // so this test is symmetric if ( Primitive.wrapperMap.get( lhsType ) == rhsType ) return true; return isJavaBaseAssignable(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). The rules include Java 5 autoboxing/unboxing. <p/> 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?) <p/> 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). <p/> Note that the getAssignableForm() method is the primary bsh method for checking assignability. It adds additional bsh conversions, etc. @see #isBshAssignable( Class, Class ) @param lhsType assigning from rhsType to lhsType @param rhsType assigning from rhsType to lhsType */ static boolean isJavaAssignable( Class lhsType, Class rhsType ) { return isJavaBaseAssignable( lhsType, rhsType ) || isJavaBoxTypesAssignable( 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). The rules include Java 5 autoboxing/unboxing. <p/> 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?) <p/> 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). <p/> Note that the getAssignableForm() method is the primary bsh method for checking assignability. It adds additional bsh conversions, etc. @see #isBshAssignable( Class, Class ) @param lhsType assigning from rhsType to lhsType @param rhsType assigning from rhsType to lhsType */ public static boolean isJavaAssignable( Class lhsType, Class rhsType ) { return isJavaBaseAssignable( lhsType, rhsType ) || isJavaBoxTypesAssignable( lhsType, rhsType ); }
if ( !isJavaBaseAssignable( to[i], from[i] ) ) return false; return true;
if ( !isJavaBaseAssignable( to[i], from[i] ) ) return false; return true;
boolean ret = Types.isJavaBaseAssignable( rhs, lhs.getClass() ); return new Primitive(ret);
return Types.isJavaBaseAssignable( rhs, lhs.getClass() ) ? Primitive.TRUE : Primitive.FALSE;