/** Get the Java types of the arguments. */ public static Class[] getTypes( Object[] args ) { if ( args == null ) return new Class[0]; Class[] types = new Class[ args.length ]; for( int i=0; i<args.length; i++ ) { if ( args[i] == null ) types[i] = null; else if ( args[i] instanceof Primitive ) types[i] = ((Primitive)args[i]).getType(); else types[i] = args[i].getClass(); } return types; }
/** Find the type of an object boxed or not. * @param arg the object to query. * @param boxed whether to get a primitive or boxed type. * @return null if arg null, type of Primitive or getClass. */ public static Class<?> getType( Object arg, boolean boxed ) { if ( null == arg || Primitive.NULL == arg ) return null; if ( arg instanceof Primitive && !boxed ) return ((Primitive) arg).getType(); return Primitive.unwrap(arg).getClass(); }
/** Type from value to string. * @param value for type * @return string type */ public static String typeString(Object value) { return null == value || Primitive.NULL == value ? "null" : value instanceof Primitive ? ((Primitive) value).getType().getSimpleName() : typeString(value.getClass()); }
/** Cast this bsh.Primitive value to a new bsh.Primitive value This is usually a numeric type cast. Other cases include: A boolean can be cast to boolen null can be cast to any object type and remains null Attempting to cast a void causes an exception @param toType is the java object or primitive TYPE class */ public Primitive castToType( Class<?> toType, int operation ) throws UtilEvalError { return castPrimitive( toType, getType()/*fromType*/, this/*fromValue*/, false/*checkOnly*/, operation ); }
/** Cast this bsh.Primitive value to a new bsh.Primitive value This is usually a numeric type cast. Other cases include: A boolean can be cast to boolen null can be cast to any object type and remains null Attempting to cast a void causes an exception @param toType is the java object or primitive TYPE class */ public Primitive castToType( Class toType, int operation ) throws UtilEvalError { return castPrimitive( toType, getType()/*fromType*/, this/*fromValue*/, false/*checkOnly*/, operation ); }
private void throwTypeError( BSHArrayInitializer node, Class baseType, Object initializer, int argNum, CallStack callstack ) throws EvalError { String rhsType; if (initializer instanceof Primitive) rhsType = ((Primitive)initializer).getType().getName(); else rhsType = Reflect.normalizeClassName( initializer.getClass()); throw new EvalError ( "Incompatible type: " + rhsType +" in initializer of array type: "+ baseType +" at position: "+argNum, node, callstack ); }
/** 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. <p/> This method can handle fromValue Primitive types (representing primitive casts) as well as fromValue object casts requiring interface generation, etc. @param toType the class type of the cast result, which may include primitive types, e.g. Byte.TYPE @param fromValue an Object or bsh.Primitive primitive value (including Primitive.NULL or Primitive.VOID ) @see #isBshAssignable( Class, Class ) */ public static Object castObject( Object fromValue, Class toType, int operation ) throws UtilEvalError { if ( fromValue == null ) throw new InterpreterError("null fromValue"); Class fromType = fromValue instanceof Primitive ? ((Primitive)fromValue).getType() : fromValue.getClass(); return castObject( toType, fromType, fromValue, operation, false/*checkonly*/ ); }
/** Primitives compare equal with other Primitives containing an equal wrapped value. */ public boolean equals( Object obj ) { if ( !( obj instanceof Primitive ) ) if ( wrapperMap.containsKey(obj.getClass()) ) obj = new Primitive(obj); else return false; Primitive pobj = (Primitive) obj; if ( pobj.isNumber() && this.isNumber() ) { if ( this.getType() == BigDecimal.class ) return this.value.equals(castNumber(BigDecimal.class, pobj.numberValue())); if ( pobj.getType() == BigDecimal.class ) return pobj.value.equals(castNumber(BigDecimal.class, this.numberValue())); if (Types.isFloatingpoint(this.value) || Types.isFloatingpoint(pobj.value)) return this.numberValue().doubleValue() == pobj.numberValue().doubleValue(); if ( this.getType() == BigInteger.class ) return this.value.equals(castNumber(BigInteger.class, pobj.numberValue())); if ( pobj.getType() == BigInteger.class ) return pobj.value.equals(castNumber(BigInteger.class, this.numberValue())); return this.numberValue().longValue() == pobj.numberValue().longValue(); } return this.value.equals( pobj.value ); }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace(_interpreter.getClassManager(), "BeanshellExecutable"); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { nameSpace.setVariable(entry.getKey(), entry.getValue(), false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(new Object[0], new BshEvaluatingVisitor(null, _interpreter)); if (result instanceof Primitive) { if (( (Primitive) result).getType() == Void.TYPE) { return null; } return ( (Primitive) result).getValue(); } return result; }
((Primitive)val).getType().getName() : val.getClass().getName(); throw new UtilEvalError(
"illegal use of undefined object or 'void' literal"); Class<?> operandType = val.getType();
"illegal use of undefined object or 'void' literal"); Class operandType = val.getType(); Object operand = promoteToInteger(val.getValue());