Skip to content

Commit d15df34

Browse files
committed
Avoid proxy replacement for generic return type signatures
Issue: SPR-15010 (cherry picked from commit 6d1cae2)
1 parent f3cc4ab commit d15df34

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
351351
*/
352352
private static Object processReturnType(Object proxy, Object target, Method method, Object retVal) {
353353
// Massage return value if necessary
354-
if (retVal != null && retVal == target && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
354+
if (retVal != null && retVal == target &&
355+
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
355356
// Special case: it returned "this". Note that we can't help
356357
// if the target sets a reference to itself in another returned object.
357358
retVal = proxy;

spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
215215

216216
// Massage return value if necessary.
217217
Class<?> returnType = method.getReturnType();
218-
if (retVal != null && retVal == target && returnType.isInstance(proxy) &&
218+
if (retVal != null && retVal == target &&
219+
returnType != Object.class && returnType.isInstance(proxy) &&
219220
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
220221
// Special case: it returned "this" and the return type of the method
221222
// is type-compatible. Note that we can't help if the target sets

spring-orm/src/test/java/org/springframework/orm/jpa/hibernate/HibernateEntityManagerFactoryIntegrationTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
1616

1717
package org.springframework.orm.jpa.hibernate;
1818

19+
import javax.persistence.EntityManager;
20+
1921
import org.hibernate.ejb.HibernateEntityManager;
2022
import org.hibernate.ejb.HibernateEntityManagerFactory;
2123

24+
import org.springframework.aop.framework.ProxyFactory;
25+
import org.springframework.aop.target.SingletonTargetSource;
2226
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
2327
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
2428

@@ -48,4 +52,14 @@ public void testCanCastSharedEntityManagerProxyToHibernateEntityManager() {
4852
assertNotNull(hibernateEntityManager.getSession());
4953
}
5054

55+
public void testCanUnwrapAopProxy() {
56+
EntityManager em = entityManagerFactory.createEntityManager();
57+
EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em));
58+
assertTrue(em instanceof HibernateEntityManager);
59+
assertFalse(proxy instanceof HibernateEntityManager);
60+
assertTrue(proxy.unwrap(HibernateEntityManager.class) instanceof HibernateEntityManager);
61+
assertSame(em, proxy.unwrap(HibernateEntityManager.class));
62+
assertSame(em.getDelegate(), proxy.getDelegate());
63+
}
64+
5165
}

0 commit comments

Comments
 (0)