From 5487395397eb6fec787e430606b0b67466c3d07e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 19 Mar 2015 18:52:37 +0100 Subject: [PATCH] DATACMNS-661 - Polishing. Removed the ability to configure a custom SpElExpressionParser for now as it's not really part of the performance optimization. Polished assertions in SpelEvaluatingMethodInterceptor. Original pull request: #118. --- .../SpelAwareProxyProjectionFactory.java | 15 +------------ .../SpelEvaluatingMethodInterceptor.java | 22 +++++++++---------- ...lAwareProxyProjectionFactoryUnitTests.java | 17 -------------- 3 files changed, 12 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/springframework/data/projection/SpelAwareProxyProjectionFactory.java b/src/main/java/org/springframework/data/projection/SpelAwareProxyProjectionFactory.java index fbdb569d2..b3f698ff9 100644 --- a/src/main/java/org/springframework/data/projection/SpelAwareProxyProjectionFactory.java +++ b/src/main/java/org/springframework/data/projection/SpelAwareProxyProjectionFactory.java @@ -28,7 +28,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.util.AnnotationDetectionMethodCallback; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** @@ -42,9 +41,9 @@ import org.springframework.util.ReflectionUtils; public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory implements BeanFactoryAware { private final Map, Boolean> typeCache = new HashMap, Boolean>(); + private final SpelExpressionParser parser = new SpelExpressionParser(); private BeanFactory beanFactory; - private SpelExpressionParser parser = new SpelExpressionParser(); /* * (non-Javadoc) @@ -55,18 +54,6 @@ public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory impl this.beanFactory = beanFactory; } - /** - * Set the {@link SpelExpressionParser} to use. - * - * @param parser must not be {@literal null} - */ - public void setParser(SpelExpressionParser parser) { - - Assert.notNull(parser, "Parser must not be null!"); - - this.parser = parser; - } - /** * Inspects the given target type for methods with {@link Value} annotations and caches the result. Will create a * {@link SpelEvaluatingMethodInterceptor} if an annotation was found or return the delegate as is if not. diff --git a/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java index ff6e92afd..74220c7f2 100644 --- a/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java @@ -46,6 +46,7 @@ import org.springframework.util.StringUtils; class SpelEvaluatingMethodInterceptor implements MethodInterceptor { private static final ParserContext PARSER_CONTEXT = new TemplateParserContext(); + private final EvaluationContext evaluationContext; private final MethodInterceptor delegate; private final Map expressions; @@ -65,9 +66,9 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor { SpelExpressionParser parser, Class targetInterface) { Assert.notNull(delegate, "Delegate MethodInterceptor must not be null!"); - Assert.notNull(target, "TargetObject must not be null!"); - Assert.notNull(parser, "TargetObject must not be null!"); - Assert.notNull(targetInterface, "TargetInterface must not be null!"); + Assert.notNull(target, "Target object must not be null!"); + Assert.notNull(parser, "SpelExpressionParser must not be null!"); + Assert.notNull(targetInterface, "Target interface must not be null!"); StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new TargetWrapper(target)); @@ -90,12 +91,12 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor { * {@code method.hashCode()} as key and the parsed {@link Expression} or an {@link Collections#emptyMap()} if no * {@code Expressions} were found. * - * @param parser - * @param targetInterface + * @param parser must not be {@literal null}. + * @param targetInterface must not be {@literal null}. * @return */ - private Map potentiallyCreateExpressionsForMethodsOnTargetInterface(SpelExpressionParser parser, - Class targetInterface) { + private static Map potentiallyCreateExpressionsForMethodsOnTargetInterface( + SpelExpressionParser parser, Class targetInterface) { Map expressions = new HashMap(); @@ -106,16 +107,15 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor { } Value value = method.getAnnotation(Value.class); + if (!StringUtils.hasText(value.value())) { throw new IllegalStateException(String.format("@Value annotation on %s contains empty expression!", method)); } - Expression expression = parser.parseExpression(value.value(), PARSER_CONTEXT); - - expressions.put(method.hashCode(), expression); + expressions.put(method.hashCode(), parser.parseExpression(value.value(), PARSER_CONTEXT)); } - return expressions.isEmpty() ? Collections. emptyMap() : expressions; + return Collections.unmodifiableMap(expressions); } /* diff --git a/src/test/java/org/springframework/data/projection/SpelAwareProxyProjectionFactoryUnitTests.java b/src/test/java/org/springframework/data/projection/SpelAwareProxyProjectionFactoryUnitTests.java index fe0e75588..8d5de435f 100644 --- a/src/test/java/org/springframework/data/projection/SpelAwareProxyProjectionFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/projection/SpelAwareProxyProjectionFactoryUnitTests.java @@ -23,7 +23,6 @@ import java.util.List; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; -import org.springframework.expression.spel.standard.SpelExpressionParser; /** * Unit tests for {@link SpelAwareProxyProjectionFactory}. @@ -66,22 +65,6 @@ public class SpelAwareProxyProjectionFactoryUnitTests { assertThat(properties, hasItem("firstname")); } - /** - * @see DATACMNS-661 - */ - @Test - public void shouldSupportConfiguringFactoryWithCustomSpelParser() { - - Customer customer = new Customer(); - customer.firstname = "Dave"; - customer.lastname = "Matthews"; - - factory.setParser(new SpelExpressionParser()); - CustomerExcerpt excerpt = factory.createProjection(CustomerExcerpt.class, customer); - - assertThat(excerpt.getFullName(), is("Dave Matthews")); - } - static class Customer { public String firstname, lastname;