public static Object [] unwrap( Object[] args ) { Object [] oa = new Object[ args.length ]; for(int i=0; i<args.length; i++) oa[i] = unwrap( args[i] ); return oa; }
public static Object [] unwrap( Object[] args ) { if ( args == null ) return null; Object [] oa = new Object[ args.length ]; for(int i=0; i<args.length; i++) oa[i] = unwrap( args[i] ); return oa; }
/** Coerce parameter values to parameter type and unwrap primitives. * @param param the parameter value * @param type the parameter type * @return unwrapped coerced value * @throws Throwable on cast errors */ protected Object coerceToType(Object param, Class<?> type) throws Throwable { if (type != Object.class) param = Types.castObject(param, type, Types.CAST); return Primitive.unwrap(param); }
/** 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(); }
private static Object unwrap(Object val) { return val instanceof ClassIdentifier ? NameSpace.identifierToClass((ClassIdentifier) val) : Primitive.unwrap(val); }
/** * Get the instance bsh namespace field from the object instance. * @return the class instance This object or null if the object has not * been initialized. */ public static This getClassInstanceThis(Object instance, String className) { try { Object o = getObjectFieldValue(instance, BSHTHIS + className); return (This) Primitive.unwrap(o); // unwrap Primitive.Null to null } catch (Exception e) { throw new InterpreterError("Generated class: Error getting This" + e, e); } }
/** * Get the instance bsh namespace field from the object instance. * * @return the class instance This object or null if the object has not * been initialized. */ static This getClassInstanceThis(Object instance, String className) { try { Object o = Reflect.getObjectFieldValue(instance, BSHTHIS + className); return (This) Primitive.unwrap(o); // unwrap Primitive.Null to null } catch (Exception e) { throw new InterpreterError("Generated class: Error getting This" + e); } }
/** Fill boxed numeric types with default numbers instead of nulls. * @param arr the array to fill. */ private void arrayFillDefaultValue(Object arr) { if (null == arr) return; Class<?> clas = arr.getClass(); Class<?> comp = Types.arrayElementType(clas); if ( !comp.isPrimitive() ) if ( Types.arrayDimensions(clas) > 1 ) for ( int i = 0; i < Array.getLength(arr); i++ ) arrayFillDefaultValue(Array.get(arr, i)); else Arrays.fill((Object[]) arr, Primitive.unwrap( Primitive.getDefaultValue(comp))); } }
/** Place an unwrapped value in the external map. BeanShell primitive types are represented by their object wrappers, so it is not possible to differentiate between wrapper types and primitive types via the external Map. */ protected void putExternalMap( String name, Object value ) { if ( value instanceof Variable ) try { value = unwrapVariable( (Variable)value ); } catch ( UtilEvalError ute ) { // There should be no case for this. unwrapVariable throws // UtilEvalError in some cases where it holds an LHS or array // index. throw new InterpreterError("unexpected UtilEvalError"); } if ( value instanceof Primitive ) value = Primitive.unwrap( (Primitive)value ); externalMap.put( name, value ); } }
/** Get the value of the name. name may be any value. e.g. a variable or field */ public Object get( String name ) throws EvalError { try { Object ret = globalNameSpace.get( name, this ); return Primitive.unwrap( ret ); } catch ( UtilEvalError e ) { throw e.toEvalError( SimpleNode.JAVACODE, new CallStack() ); } }
/** Set element value of array or list at index. * Array.set for array or List.set for list. * @param array to set value for. * @param index of the element to set * @param val the value to set * @throws UtilTargetError wrapped target exceptions */ @SuppressWarnings("unchecked") public static void setIndex(Object array, int index, Object val) throws ReflectError, UtilTargetError { try { val = Primitive.unwrap(val); if ( array instanceof List ) ((List<Object>) array).set(index, val); else Array.set(array, index, val); } catch( IllegalArgumentException e1 ) { // fabricated array store exception throw new UtilTargetError( new ArrayStoreException( e1.getMessage() ) ); } catch( IndexOutOfBoundsException e1 ) { int len = array instanceof List ? ((List<?>) array).size() : Array.getLength(array); throw new UtilTargetError("Index " + index + " out-of-bounds for length " + len, e1); } }
public static boolean evaluateCondition( SimpleNode condExp, CallStack callstack, Interpreter interpreter) throws EvalError { Object obj = condExp.eval(callstack, interpreter); if ( obj == Primitive.VOID ) throw new EvalError("Condition evaluates to void type", condExp, callstack ); obj = Primitive.unwrap(obj); if( obj instanceof Boolean ) return ((Boolean) obj).booleanValue(); else throw new EvalError( "Condition must evaluate to a Boolean or boolean.", condExp, callstack ); } }
/** Get the value of the name. name may be any value. e.g. a variable or field */ public Object get( String name ) throws EvalError { try { Object ret = globalNameSpace.get( name, this ); return Primitive.unwrap( ret ); } catch ( UtilEvalError e ) { throw e.toEvalError( SimpleNode.JAVACODE, new CallStack() ); } }
/** Helper method for testing equals on two primitive or boxable objects. yuck: factor this out into Primitive.java */ public boolean primitiveEquals( BSHSwitchStatement node, Object switchVal, Object targetVal, SimpleNode switchExp ) throws EvalError { if ( switchVal instanceof Primitive || targetVal instanceof Primitive ) try { // binaryOperation can return Primitive or wrapper type Object result = Primitive.binaryOperation( switchVal, targetVal, ParserConstants.EQ ); result = Primitive.unwrap( result ); return result.equals( Boolean.TRUE ); } catch ( UtilEvalError e ) { throw e.toEvalError( "Switch value: "+switchExp.getText()+": ", node, callstack ); } else return switchVal.equals( targetVal ); }
public static void setIndex(Object array, int index, Object val) throws ReflectError, UtilTargetError { try { val = Primitive.unwrap(val); Array.set(array, index, val); } catch (ArrayStoreException e2) { throw new UtilTargetError(e2); } catch (IllegalArgumentException e1) { //noinspection ThrowableInstanceNeverThrown throw new UtilTargetError(new ArrayStoreException(e1.toString())); } catch (Exception e) { throw new ReflectError("Array access:" + e); } }
/** Simplified method for class generated method stubs. Uses This.namespace and declaringInterpreter and retains callerInfo from namespace.getNode(). Also unwraps Primitives for convenience. */ public Object invokeMethod( String methodName, Object [] args, boolean declaredOnly ) throws EvalError { CallStack callstack = new CallStack(namespace); SimpleNode node = namespace.getNode(); namespace.setNode(null); try { return Primitive.unwrap(invokeMethod( methodName, args, declaringInterpreter, callstack, node, declaredOnly)); } catch (Exception e) { throw new EvalError(e.getMessage(), node, callstack, e); } }
/** 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 a writable property exists for the given name, set it and return * true, otherwise do nothing and return false. * @param propName the prop name * @param value the value * @param interp the interp * @return true, if successful * @throws UtilEvalError the util eval error */ boolean attemptSetPropertyValue(final String propName, final Object value, final Interpreter interp) throws UtilEvalError { final String accessorName = Reflect.accessorName(Reflect.SET_PREFIX, propName); Object val = Primitive.unwrap(value); final Class<?>[] classArray = new Class<?>[] { val == null ? null : val.getClass()}; final BshMethod m = this.getMethod(accessorName, classArray); if (m != null) try { this.invokeMethod(accessorName, new Object[] {value}, interp); // m.invoke(new Object[] {value}, interp); return true; } catch (final EvalError ee) { throw new UtilEvalError( "'This' property accessor threw exception: " + ee.getMessage(), ee); } return false; }
/** Inspect the supplied object and cast type to delegates. * @param obj the value to iterate of unknown type. * @return the bsh iterator */ public Iterator<?> getBshIterator(final Object obj) { if (obj == null) return this.emptyIt(); if (obj instanceof Primitive) return this.getBshIterator(Primitive.unwrap(obj)); if (obj.getClass().isArray()) return this.arrayIt(obj); if (obj instanceof Iterable) return this.getBshIterator((Iterable<?>) obj); if (obj instanceof Iterator) return this.getBshIterator((Iterator<?>) obj); if (obj instanceof Enumeration) return this.getBshIterator((Enumeration<?>) obj); if (obj instanceof CharSequence) return this.getBshIterator((CharSequence) obj); if (obj instanceof Number) return this.getBshIterator((Number) obj); if (obj instanceof Character) return this.getBshIterator((Character) obj); if (obj instanceof String) return this.getBshIterator((String) obj); return this.reflectNames(obj).iterator(); } }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace("BeanshellExecutable", _interpreter.getClassManager()); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.getParameterModifiers(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { final Object value = entry.getValue(); nameSpace.setVariable(entry.getKey(), value != null ? value : Primitive.NULL, false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(Reflect.ZERO_ARGS, _interpreter); if ( Types.getType(result) == Void.TYPE ) return null; return Primitive.unwrap(result); }