private Primitive( Object value ) { if ( value == null ) throw new InterpreterError( "Use Primitve.NULL instead of Primitive(null)"); if ( value != Special.NULL_VALUE && value != Special.VOID_TYPE && !isWrapperType( value.getClass() ) ) throw new InterpreterError( "Not a wrapper type: "+value.getClass()); this.value = value; }
public Primitive( Object value ) { if ( value == null ) throw new InterpreterError( "Use Primitve.NULL instead of Primitive(null)"); if ( value != Special.NULL_VALUE && value != Special.VOID_TYPE && !isWrapperType( value.getClass() ) ) throw new InterpreterError( "Not a wrapper type: "+value); this.value = value; }
/** Wrap primitive values (as indicated by type param) and nulls in the Primitive class. Values not primitive or null are left unchanged. Primitive values are represented by their wrapped values in param value. <p/> The value null is mapped to Primitive.NULL. Any value specified with type Void.TYPE is mapped to Primitive.VOID. */ public static Object wrap( Object value, Class<?> type ) { if ( type == Void.TYPE ) return Primitive.VOID; if ( value == null ) return Primitive.NULL; if(value instanceof Boolean) return ((Boolean)value).booleanValue() ? Primitive.TRUE : Primitive.FALSE; if ( Types.isPrimitive(type) && isWrapperType( value.getClass() ) ) return new Primitive( value ); return value; }
/** Cast a primitive value represented by its java.lang wrapper type to the specified java.lang wrapper type. e.g. Byte(5) to Integer(5) or Integer(5) to Byte(5) @param toType is the java TYPE type @param value is the value in java.lang wrapper. value may not be null. */ static Object castWrapper( Class<?> toType, Object value ) { value = Primitive.unwrap(value); if ( !(Primitive.isWrapperType(toType) || toType.isPrimitive()) ) throw new InterpreterError("invalid type in castWrapper: "+toType); if ( value == null ) throw new InterpreterError("null value in castWrapper, guard"); if ( value instanceof Boolean ) { if ( toType != Boolean.TYPE ) throw new InterpreterError("bad wrapper cast of boolean"); return value; } // first promote char to Number type to avoid duplicating code if ( value instanceof Character ) value = Integer.valueOf(((Character)value).charValue()); if ( !(value instanceof Number) ) throw new InterpreterError("bad type in cast "+StringUtil.typeValueString(value)); return castNumber(toType, (Number) value); }
if ( !isPrimitive(toType) && !Primitive.isWrapperType(toType) ) return Primitive.NULL; else
/** Helper class for type suffixes. */ public static class Suffix { private static final Map<String, Class<?>> m = Collections.unmodifiableMap(new HashMap<String, Class<?>>() { private static final long serialVersionUID = 1L; { put("O", Byte.TYPE); put("S", Short.TYPE); put("I", Integer.TYPE); put("L", Long.TYPE); put("W", BigInteger.class); put("w", BigDecimal.class); put("d", Double.TYPE); put("f", Float.TYPE); } }); private static String toUpperKey(Character key) { return key.toString().toUpperCase(); } private static String toLowerKey(Character key) { return key.toString().toLowerCase(); } public static boolean isIntegral(Character key) { return m.containsKey(toUpperKey(key)); } public static Class<?> getIntegralType(Character key) { return m.get(toUpperKey(key));
} else if ( Primitive.isWrapperType( fromType ) ) if ( Primitive.isWrapperType( toType ) && fromType != Void.TYPE && fromType != null ) if ( Primitive.isWrapperType( toType ) && Primitive.isWrapperType( fromType )
/** Helper function to traverse array dimensions to find the common type. * Recursive calling for each element in an array initializer or finds the * common type relative to a value cell. * @param common the current common type * @param node the node to query * @param callstack the evaluation call stack * @param interpreter the evaluation interpreter * @return the common type for all cells * @throws EvalError thrown at node evaluation */ private Class<?> inferCommonType(Class<?> common, Node node, CallStack callstack, Interpreter interpreter ) throws EvalError { // Object is already the most common type and maps are typed LHS.MapEntry if ( Object.class == common || LHS.MapEntry.class == common ) return common; // inspect value elements for common type if ( !(node instanceof BSHArrayInitializer) ) { Object value = ((SimpleNode) node).eval(callstack, interpreter); Class<?> type = Types.getType(value, Primitive.isWrapperType(common)); return Types.getCommonType(common, Types.arrayElementType(type)); } // avoid traversing maps as arrays when nested in array if ( isMapInArray((BSHArrayInitializer) node) ) return Types.getCommonType(common, Map.class); // recurse through nested array initializer nodes int count = node.jjtGetNumChildren(); for ( int i = 0; i < count; i++ ) common = this.inferCommonType(common, node.jjtGetChild(i), callstack, interpreter); return common; }
if ( !checkOnly && fromValue == null ) throw new InterpreterError("bad cast param 2"); if ( fromType != null && !(isWrapperType(fromType)||fromType.isPrimitive()) ) throw new InterpreterError("bad fromType:" +fromType); if ( fromValue == Primitive.NULL && fromType != null )