private TypedValue toTypedValue(@Nullable Object object) { return (object != null ? new TypedValue(object) : TypedValue.NULL); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
public MethodValueRef(ExpressionState state, Object[] arguments) { this.evaluationContext = state.getEvaluationContext(); this.value = state.getActiveContextObject().getValue(); this.targetType = state.getActiveContextObject().getTypeDescriptor(); this.arguments = arguments; }
@SuppressWarnings("unchecked") private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) { Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType)); if (result == null) { throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]"); } return (T) result; }
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { // Cache the concatenation of child identifiers if (this.value == null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < getChildCount(); i++) { Object value = this.children[i].getValueInternal(state).getValue(); if (i > 0 && (value == null || !value.toString().startsWith("$"))) { sb.append("."); } sb.append(value); } this.value = new TypedValue(sb.toString()); } return this.value; }
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); TypedValue outValue = new TypedValue(); getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams(); lp.width = 0; lp.weight = 1; tabView.setOnClickListener(tabClickListener); String desc = mContentDescriptions.get(i, null); if (desc != null) {
setContentView(R.layout.activity_main); TypedValue tv = new TypedValue(); if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { mActionBarSize = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics()); VerticalLabelView menuItem1 = (VerticalLabelView)popupView.findViewById(R.id.menu_item1); menuItem1.setOnClickListener(mOnMenuItemClickListener); menuItem1.setText("Vertical menu item 1"); menuItem1.setTextSize(mTextSize); VerticalLabelView menuItem2 = (VerticalLabelView)popupView.findViewById(R.id.menu_item2); menuItem2.setOnClickListener(mOnMenuItemClickListener); menuItem2.setText("Vertical menu item 2"); menuItem2.setTextSize(mTextSize); VerticalLabelView menuItem3 = (VerticalLabelView)popupView.findViewById(R.id.menu_item3); menuItem3.setOnClickListener(mOnMenuItemClickListener); menuItem3.setText("Vertical menu item 3");
@SuppressWarnings("unchecked") @Override @Nullable public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); Object result = this.compiledAst.getValue(context.getRootObject().getValue(), context); if (expectedResultType != null) { return ExpressionUtils.convertTypedValue(context, new TypedValue(result), expectedResultType); throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); ExpressionState expressionState = new ExpressionState(context, this.configuration); TypedValue typedResultValue = this.ast.getTypedValue(expressionState); checkCompile(expressionState); return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
protected void setStatusBarTranslucent(boolean makeTranslucent) { View v = findViewById(R.id.bellow_actionbar); if (v != null) { int paddingTop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? MyScreenUtils.getStatusBarHeight(this) : 0; TypedValue tv = new TypedValue(); getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true); paddingTop += TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); v.setPadding(0, makeTranslucent ? paddingTop : 0, 0, 0); } if (makeTranslucent) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }
public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException { OperatorOverloader overloader = this.relatedContext.getOperatorOverloader(); if (overloader.overridesOperation(op, left, right)) { Object returnValue = overloader.operate(op, left, right); return new TypedValue(returnValue); } else { String leftType = (left == null ? "null" : left.getClass().getName()); String rightType = (right == null? "null" : right.getClass().getName()); throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType); } }
@Override @Nullable public Object getValue(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(context.getRootObject().getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
@Override @Nullable public final Object getValue(ExpressionState expressionState) throws EvaluationException { return getValueInternal(expressionState).getValue(); }
private void writeProperty( TypedValue contextObject, EvaluationContext evalContext, String name, @Nullable Object newValue) throws EvaluationException { if (contextObject.getValue() == null && this.nullSafe) { return; if (contextObject.getValue() == null) { throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, name); if (evalContext.getPropertyAccessors().contains(accessorToUse)) { try { accessorToUse.write(evalContext, contextObject.getValue(), name, newValue); return; getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors()); try { for (PropertyAccessor accessor : accessorsToTry) { if (accessor.canWrite(evalContext, contextObject.getValue(), name)) { this.cachedWriteAccessor = accessor; accessor.write(evalContext, contextObject.getValue(), name, newValue); return; throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, name, ex.getMessage()); throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name, FormatHelper.formatClassNameForMessage(getObjectClass(contextObject.getValue())));
Object operand = op.getValue(); boolean operandIsArray = ObjectUtils.isArray(operand); for (Map.Entry<?, ?> entry : mapData.entrySet()) { try { state.pushActiveContextObject(new TypedValue(entry)); state.enterScope(); result.add(this.children[0].getValueInternal(state).getValue()); return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this); // TODO unable to build correct type descriptor for (Object element : data) { try { state.pushActiveContextObject(new TypedValue(element)); state.enterScope("index", idx); Object value = this.children[0].getValueInternal(state).getValue(); if (value != null && operandIsArray) { arrayElementType = determineCommonType(arrayElementType, value.getClass()); return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray),this); return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this); return ValueRef.NullValueRef.INSTANCE; throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
Object value = typedValue.getValue(); TypedValue returnValue = typedValue; TypedValue newValue = null; Number op1 = (Number) value; if (op1 instanceof BigDecimal) { newValue = new TypedValue(((BigDecimal) op1).add(BigDecimal.ONE), typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.floatValue() + 1.0f, typedValue.getTypeDescriptor()); newValue = new TypedValue(((BigInteger) op1).add(BigInteger.ONE), typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.longValue() + 1L, typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.intValue() + 1, typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.shortValue() + (short) 1, typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.byteValue() + (byte) 1, typedValue.getTypeDescriptor()); newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor()); newValue = state.operate(Operation.ADD, returnValue.getValue(), 1); valueRef.setValue(newValue.getValue());
/** * Specify a default root object to resolve against. * <p>Default is none, expecting an object argument at evaluation time. * @see org.springframework.expression.Expression#getValue(EvaluationContext) * @see org.springframework.expression.Expression#getValue(EvaluationContext, Object) */ public Builder withRootObject(Object rootObject) { this.rootObject = new TypedValue(rootObject); return this; }
@Override protected ValueRef getValueRef(ExpressionState state) throws EvaluationException { TypedValue context = state.getActiveContextObject(); Object target = context.getValue(); TypeDescriptor targetDescriptor = context.getTypeDescriptor(); TypedValue indexValue; Object index; PropertyOrFieldReference reference = (PropertyOrFieldReference) this.children[0]; index = reference.getName(); indexValue = new TypedValue(index); state.pushActiveContextObject(state.getRootContextObject()); indexValue = this.children[0].getValueInternal(state); index = indexValue.getValue(); Assert.state(index != null, "No index"); throw new SpelEvaluationException(getStartPosition(), SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE); TypeDescriptor valueType = indexValue.getTypeDescriptor(); if (valueType != null && String.class == valueType.getType()) { this.indexedType = IndexedType.OBJECT; throw new SpelEvaluationException( getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetDescriptor);
SpelNodeImpl rightOp = getRightOperand(); Object leftOperand = leftOp.getValueInternal(state).getValue(); Object rightOperand = rightOp.getValueInternal(state).getValue(); return new TypedValue(leftBigDecimal.pow(rightNumber.intValue())); return new TypedValue(leftBigInteger.pow(rightNumber.intValue())); return new TypedValue(Math.pow(leftNumber.doubleValue(), rightNumber.doubleValue())); return new TypedValue(Math.pow(leftNumber.floatValue(), rightNumber.floatValue())); return new TypedValue((long) d); return new TypedValue((int) d); return state.operate(Operation.POWER, leftOperand, rightOperand);
@Test public void selectionWithArray() throws Exception { Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]"); EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean()); Object value = expression.getValue(context); assertTrue(value.getClass().isArray()); TypedValue typedValue = new TypedValue(value); assertEquals(Integer.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()); Integer[] array = (Integer[]) value; assertEquals(5, array.length); assertEquals(new Integer(0), array[0]); assertEquals(new Integer(1), array[1]); assertEquals(new Integer(2), array[2]); assertEquals(new Integer(3), array[3]); assertEquals(new Integer(4), array[4]); }