@Override public void setBeanFactory(BeanFactory beanFactory) { if (!StringUtils.hasText(this.targetBeanName)) { throw new IllegalArgumentException("Property 'targetBeanName' is required"); } if (!StringUtils.hasText(this.methodName)) { throw new IllegalArgumentException("Property 'methodName' is required"); } Class<?> beanClass = beanFactory.getType(this.targetBeanName); if (beanClass == null) { throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); } this.method = BeanUtils.resolveSignature(this.methodName, beanClass); if (this.method == null) { throw new IllegalArgumentException("Unable to locate method [" + this.methodName + "] on bean [" + this.targetBeanName + "]"); } }
private void assertSignatureEquals(Method desiredMethod, String signature) { assertEquals(desiredMethod, BeanUtils.resolveSignature(signature, MethodSignatureBean.class)); }
@Test public void testResolveInvalidSignature() throws Exception { try { BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class); fail("Should not be able to parse with opening but no closing paren."); } catch (IllegalArgumentException ex) { // success } try { BeanUtils.resolveSignature("doSomething)", MethodSignatureBean.class); fail("Should not be able to parse with closing but no opening paren."); } catch (IllegalArgumentException ex) { // success } }
@Test public void testResolveWithAndWithoutArgList() throws Exception { Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", String.class, int.class); assertSignatureEquals(desiredMethod, "doSomethingElse"); assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)); }
@Override public void afterPropertiesSet() throws Exception { this.method = BeanUtils.resolveSignature(this.functionMethodSignature, this.functionClass); if (this.method == null) { throw new BeanDefinitionStoreException(String.format("No declared method '%s' in class '%s'", this.functionMethodSignature, this.functionClass)); } if (!Modifier.isStatic(this.method.getModifiers())) { throw new BeanDefinitionStoreException("SpEL-function method has to be 'static'"); } }
@Override public boolean validate() throws EvaluationException { try { final ExpressionParser parser = new SpelExpressionParser(); final Expression expr = parser.parseExpression(assertion); final StandardEvaluationContext context = new StandardEvaluationContext(); context.registerFunction("jsonPath", BeanUtils.resolveSignature("evaluate", JsonPathFunction.class)); context.setVariables(variables); return expr.getValue(context, boolean.class); } catch (SpelEvaluationException spelex) { throw new EvaluationException("Assertion can not be verified : " + assertion, spelex); } }
public SpelTemplateContext() { context.registerFunction("jsonPath", BeanUtils.resolveSignature("evaluate", JsonPathFunction.class)); }
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!StringUtils.hasText(this.targetBeanName)) { throw new IllegalArgumentException("Property 'targetBeanName' is required"); } if (!StringUtils.hasText(this.methodName)) { throw new IllegalArgumentException("Property 'methodName' is required"); } Class<?> beanClass = beanFactory.getType(this.targetBeanName); if (beanClass == null) { throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); } this.method = BeanUtils.resolveSignature(this.methodName, beanClass); if (this.method == null) { throw new IllegalArgumentException("Unable to locate method [" + this.methodName + "] on bean [" + this.targetBeanName + "]"); } }
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!StringUtils.hasText(this.targetBeanName)) { throw new IllegalArgumentException("Property 'targetBeanName' is required"); } if (!StringUtils.hasText(this.methodName)) { throw new IllegalArgumentException("Property 'methodName' is required"); } Class<?> beanClass = beanFactory.getType(this.targetBeanName); if (beanClass == null) { throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); } this.method = BeanUtils.resolveSignature(this.methodName, beanClass); if (this.method == null) { throw new IllegalArgumentException("Unable to locate method [" + this.methodName + "] on bean [" + this.targetBeanName + "]"); } }
@Override public void afterPropertiesSet() throws Exception { this.method = BeanUtils.resolveSignature(this.functionMethodSignature, this.functionClass); if (this.method == null) { throw new BeanDefinitionStoreException(String.format("No declared method '%s' in class '%s'", this.functionMethodSignature, this.functionClass)); } if (!Modifier.isStatic(this.method.getModifiers())) { throw new BeanDefinitionStoreException("SpEL-function method has to be 'static'"); } }
@Override public boolean validate() throws EvaluationException { try { final ExpressionParser parser = new SpelExpressionParser(); final Expression expr = parser.parseExpression(assertion); final StandardEvaluationContext context = new StandardEvaluationContext(); context.registerFunction("jsonPath", BeanUtils.resolveSignature("evaluate", JsonPathFunction.class)); context.setVariables(variables); return expr.getValue(context, boolean.class); } catch (SpelEvaluationException spelex) { throw new EvaluationException("Assertion can not be verified : " + assertion, spelex); } }