Example usage for org.springframework.expression Expression setValue

List of usage examples for org.springframework.expression Expression setValue

Introduction

In this page you can find the example usage for org.springframework.expression Expression setValue.

Prototype

void setValue(EvaluationContext context, Object rootObject, @Nullable Object value) throws EvaluationException;

Source Link

Document

Set this expression in the provided context to the value provided.

Usage

From source file:org.ldaptive.beans.spring.SpringClassDescriptor.java

/** {@inheritDoc} */
@Override/*from   w w  w  . j ava 2 s  . c  om*/
public void initialize(final Class<?> type) {
    // check for entry annotation
    final Entry entryAnnotation = AnnotationUtils.findAnnotation(type, Entry.class);
    if (entryAnnotation != null) {
        if (!entryAnnotation.dn().equals("")) {
            setDnValueMutator(new DnValueMutator() {
                @Override
                public String getValue(final Object object) {
                    final ExpressionParser parser = new SpelExpressionParser();
                    final Expression exp = parser.parseExpression(entryAnnotation.dn());
                    return exp.getValue(context, object, String.class);
                }

                @Override
                public void setValue(final Object object, final String value) {
                }
            });
        }
        for (final Attribute attr : entryAnnotation.attributes()) {
            final String expr = attr.property();
            final ExpressionParser parser = new SpelExpressionParser();
            final Expression exp = parser.parseExpression(expr);
            addAttributeValueMutator(new AttributeValueMutator() {
                @Override
                public String getName() {
                    return attr.name();
                }

                @Override
                public boolean isBinary() {
                    return attr.binary();
                }

                @Override
                public SortBehavior getSortBehavior() {
                    return attr.sortBehavior();
                }

                @Override
                public Collection<String> getStringValues(final Object object) {
                    @SuppressWarnings("unchecked")
                    final Collection<String> values = (Collection<String>) exp.getValue(context, object,
                            Collection.class);
                    return values;
                }

                @Override
                public Collection<byte[]> getBinaryValues(final Object object) {
                    @SuppressWarnings("unchecked")
                    final Collection<byte[]> values = (Collection<byte[]>) exp.getValue(context, object,
                            Collection.class);
                    return values;
                }

                @Override
                public void setStringValues(final Object object, final Collection<String> values) {
                    exp.setValue(context, object, values);
                }

                @Override
                public void setBinaryValues(final Object object, final Collection<byte[]> values) {
                    exp.setValue(context, object, values);
                }
            });
        }
    }
}

From source file:org.springframework.cloud.stream.app.pmml.processor.PmmlProcessorConfiguration.java

@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object evaluate(Message<?> input) {
    Model model = selectModel(input);/*from  w  w  w  .  j  ava 2  s  . c  om*/
    Evaluator evaluator = modelEvaluatorFactory.newModelManager(pmml, model);

    Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();

    List<FieldName> activeFields = evaluator.getActiveFields();
    for (FieldName activeField : activeFields) {
        // The raw (ie. user-supplied) value could be any Java primitive value
        Object rawValue = resolveActiveValue(input, activeField.getValue());

        // The raw value is passed through:
        // 1) outlier treatment,
        // 2) missing value treatment,
        // 3) invalid value treatment
        // and 4) type conversion
        FieldValue activeValue = evaluator.prepare(activeField, rawValue);

        arguments.put(activeField, activeValue);
    }

    Map<FieldName, ?> results = evaluator.evaluate(arguments);

    MutableMessage<?> result = convertToMutable(input);

    for (Map.Entry<FieldName, ?> entry : results.entrySet()) {

        String fieldName = null;
        if (entry.getKey() == null)
            fieldName = DEFAULT_OUTPUT_FIELD;
        else
            fieldName = entry.getKey().getValue();

        Expression expression = properties.getOutputs().get(fieldName);
        if (expression == null) {
            expression = spelExpressionParser.parseExpression("payload." + fieldName);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Setting result field named " + fieldName + " using SpEL[" + expression + " = "
                    + entry.getValue() + "]");
        }
        expression.setValue(evaluationContext, result, entry.getValue());
    }

    return result;

}