Example usage for org.springframework.expression.spel.support StandardEvaluationContext setMethodResolvers

List of usage examples for org.springframework.expression.spel.support StandardEvaluationContext setMethodResolvers

Introduction

In this page you can find the example usage for org.springframework.expression.spel.support StandardEvaluationContext setMethodResolvers.

Prototype

public void setMethodResolvers(List<MethodResolver> methodResolvers) 

Source Link

Usage

From source file:reactor.spring.context.ConsumerBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
        @Override/*from w  w w.  ja  va 2 s.  co m*/
        public void doWith(final Method method) throws IllegalArgumentException, IllegalAccessException {
            Annotation anno = AnnotationUtils.findAnnotation(method, On.class);
            if (null == anno) {
                return;
            }

            StandardEvaluationContext evalCtx = new StandardEvaluationContext();
            evalCtx.setRootObject(bean);
            evalCtx.setBeanResolver(beanResolver);
            evalCtx.setMethodResolvers(METHOD_RESOLVERS);

            On onAnno = (On) anno;

            Object reactorObj = null;
            if (StringUtils.hasText(onAnno.reactor())) {
                Expression reactorExpr = expressionParser.parseExpression(onAnno.reactor());
                reactorObj = reactorExpr.getValue(evalCtx);
            }

            Object selObj;
            if (StringUtils.hasText(onAnno.selector())) {
                try {
                    Expression selectorExpr = expressionParser.parseExpression(onAnno.selector());
                    selObj = selectorExpr.getValue(evalCtx);
                } catch (EvaluationException e) {
                    selObj = Fn.$(onAnno.selector());
                }
            } else {
                selObj = Fn.$(method.getName());
            }

            Consumer<Event<Object>> handler = new Consumer<Event<Object>>() {
                Class<?>[] argTypes = method.getParameterTypes();

                @Override
                public void accept(Event<Object> ev) {
                    if (argTypes.length == 0) {
                        ReflectionUtils.invokeMethod(method, bean);
                        return;
                    }

                    if (!argTypes[0].isAssignableFrom(ev.getClass())
                            && conversionService.canConvert(ev.getClass(), argTypes[0])) {
                        ReflectionUtils.invokeMethod(method, bean, conversionService.convert(ev, argTypes[0]));
                    } else {
                        ReflectionUtils.invokeMethod(method, bean, ev);
                        return;
                    }

                    if (null == ev.getData() || argTypes[0].isAssignableFrom(ev.getData().getClass())) {
                        ReflectionUtils.invokeMethod(method, bean, ev.getData());
                        return;
                    }

                    if (conversionService.canConvert(ev.getData().getClass(), argTypes[0])) {
                        ReflectionUtils.invokeMethod(method, bean,
                                conversionService.convert(ev.getData(), argTypes[0]));
                        return;
                    }

                    throw new IllegalArgumentException(
                            "Cannot invoke method " + method + " passing parameter " + ev.getData());
                }
            };

            if (!(selObj instanceof Selector)) {
                throw new IllegalArgumentException(selObj + ", referred to by the expression '"
                        + onAnno.selector() + "', is not a Selector");
            }
            if (null == reactorObj) {
                throw new IllegalStateException("Cannot register handler with null Reactor");
            } else {
                ((Reactor) reactorObj).on((Selector) selObj, handler);
            }
        }
    });
    return bean;
}

From source file:guru.qas.martini.DefaultMixologist.java

protected Collection<Martini> getMartinis(Expression expression) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    List<MethodResolver> methodResolvers = context.getMethodResolvers();
    ArrayList<MethodResolver> modifiedList = Lists.newArrayList(methodResolvers);
    modifiedList.add(new TagResolver(this.context, categories));
    context.setMethodResolvers(modifiedList);

    ImmutableList<Martini> martinis = getMartinis();
    List<Martini> matches = Lists.newArrayListWithCapacity(martinis.size());
    for (Martini martini : martinis) {
        Boolean match = expression.getValue(context, martini, Boolean.class);
        if (null != match && match) {
            matches.add(martini);//from w w  w .j av  a2  s  .c o  m
        }
    }
    return matches;
}