Browse Source

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.
pull/347/head
Oliver Gierke 9 years ago
parent
commit
aa9cfc60ef
  1. 4
      src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java
  2. 12
      src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

4
src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java

@ -82,8 +82,8 @@ class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware, @@ -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();

12
src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

@ -264,7 +264,17 @@ public class ProxyProjectionFactoryUnitTests { @@ -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;

Loading…
Cancel
Save