From aa9cfc60ef419cc447cfdcbc28029406ce7ba3d5 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 117a4dd46..7e7f9081c 100644 --- a/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java +++ b/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java @@ -82,8 +82,8 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware, 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 2c683c8ca..4d1905ceb 100644 --- a/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java @@ -264,7 +264,17 @@ public class ProxyProjectionFactoryUnitTests { assertThat(data.get("key"), is(nullValue())); } - static class Customer { + @Test // DATACMNS-1121 + public void doesNotCreateWrappingProxyIfTargetImplementsProjectionInterface() { + + Customer customer = new Customer(); + + assertThat(factory.createProjection(Contact.class, customer) == customer, is(true)); + } + + interface Contact {} + + static class Customer implements Contact { public Long id; public String firstname, lastname;