From 51f099306d8ba9b422863387964e115ef2e2498b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 20 Jul 2017 14:29:54 +0200 Subject: [PATCH] DATACMNS-1121 - ProxyProjectionFactory now returns instance as is if it implements the target. If you previously asked ProxyProjectionFactory for a proxy of an interface which the target instance already implements, it created a proxy although it could've returned the instance as is. It's now actually doing that avoiding superfluous proxy creation. --- .../data/projection/ProxyProjectionFactory.java | 4 ++-- .../projection/ProxyProjectionFactoryUnitTests.java | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java b/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java index 5659677c3..4dac7cbd3 100644 --- a/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java +++ b/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java @@ -96,8 +96,8 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware Assert.notNull(projectionType, "Projection type must not be null!"); Assert.isTrue(projectionType.isInterface(), "Projection type must be an interface!"); - if (source == null) { - return null; + if (source == null || projectionType.isInstance(source)) { + return (T) source; } ProxyFactory factory = new ProxyFactory(); diff --git a/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java b/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java index 0069be1b8..7383e2bd1 100755 --- a/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java @@ -206,7 +206,17 @@ public class ProxyProjectionFactoryUnitTests { assertThat(data.get("key")).isNull(); } - static class Customer { + @Test // DATACMNS-1121 + public void doesNotCreateWrappingProxyIfTargetImplementsProjectionInterface() { + + Customer customer = new Customer(); + + assertThat(factory.createProjection(Contact.class, customer)).isSameAs(customer); + } + + interface Contact {} + + static class Customer implements Contact { public Long id; public String firstname, lastname;