Skip to content

Commit c31e2cb

Browse files
committed
DATACMNS-1324 - Introduced extensible proxy detection infrastructure.
Introduced ProxyUtils.getUserClass(…) that by default is basically a facade for Spring's ClassUtils.getUserClass(…) but allows the registration of ProxyDetector implementations via Spring's SpringFactoriesLoader mechanism. Moved all existing usages of ClassUtils.getUserClass(…) to ProxyUtils.
1 parent 7043707 commit c31e2cb

File tree

7 files changed

+192
-10
lines changed

7 files changed

+192
-10
lines changed

src/main/java/org/springframework/data/domain/Example.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import lombok.RequiredArgsConstructor;
2222
import lombok.ToString;
2323

24-
import org.springframework.util.ClassUtils;
24+
import org.springframework.data.util.ProxyUtils;
2525

2626
/**
2727
* Support for query by example (QBE). An {@link Example} takes a {@code probe} to define the example. Matching options
@@ -85,10 +85,10 @@ public ExampleMatcher getMatcher() {
8585
* CGLIB-generated subclass.
8686
*
8787
* @return
88-
* @see ClassUtils#getUserClass(Class)
88+
* @see ProxyUtils#getUserClass(Class)
8989
*/
9090
@SuppressWarnings("unchecked")
9191
public Class<T> getProbeType() {
92-
return (Class<T>) ClassUtils.getUserClass(probe.getClass());
92+
return (Class<T>) ProxyUtils.getUserClass(probe.getClass());
9393
}
9494
}

src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryProxyPostProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.BridgeMethodResolver;
3434
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
3535
import org.springframework.data.repository.core.RepositoryInformation;
36+
import org.springframework.data.util.ProxyUtils;
3637
import org.springframework.transaction.annotation.Ejb3TransactionAnnotationParser;
3738
import org.springframework.transaction.annotation.JtaTransactionAnnotationParser;
3839
import org.springframework.transaction.annotation.SpringTransactionAnnotationParser;
@@ -383,7 +384,7 @@ private TransactionAttribute computeTransactionAttribute(Method method, Class<?>
383384
}
384385

385386
// Ignore CGLIB subclasses - introspect the actual user class.
386-
Class<?> userClass = ClassUtils.getUserClass(targetClass);
387+
Class<?> userClass = ProxyUtils.getUserClass(targetClass);
387388
// The method may be on an interface, but we need attributes from the target class.
388389
// If the target class is null, the method will be unchanged.
389390
Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);

src/main/java/org/springframework/data/repository/support/Repositories.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.repository.core.RepositoryInformation;
3434
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
3535
import org.springframework.data.repository.query.QueryMethod;
36+
import org.springframework.data.util.ProxyUtils;
3637
import org.springframework.util.Assert;
3738
import org.springframework.util.ClassUtils;
3839

@@ -121,7 +122,7 @@ public boolean hasRepositoryFor(Class<?> domainClass) {
121122

122123
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
123124

124-
Class<?> userClass = ClassUtils.getUserClass(domainClass);
125+
Class<?> userClass = ProxyUtils.getUserClass(domainClass);
125126

126127
return repositoryFactoryInfos.containsKey(userClass);
127128
}
@@ -136,16 +137,16 @@ public Object getRepositoryFor(Class<?> domainClass) {
136137

137138
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
138139

139-
Class<?> userClass = ClassUtils.getUserClass(domainClass);
140+
Class<?> userClass = ProxyUtils.getUserClass(domainClass);
140141
String repositoryBeanName = repositoryBeanNames.get(userClass);
141142

142143
return repositoryBeanName == null || beanFactory == null ? null : beanFactory.getBean(repositoryBeanName);
143144
}
144145

145146
/**
146147
* Returns the {@link RepositoryFactoryInformation} for the given domain class. The given <code>code</code> is
147-
* converted to the actual user class if necessary, @see ClassUtils#getUserClass.
148-
*
148+
* converted to the actual user class if necessary, @see ProxyUtils#getUserClass.
149+
*
149150
* @param domainClass must not be {@literal null}.
150151
* @return the {@link RepositoryFactoryInformation} for the given domain class or {@literal null} if no repository
151152
* registered for this domain class.
@@ -154,7 +155,7 @@ private RepositoryFactoryInformation<Object, Serializable> getRepositoryFactoryI
154155

155156
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
156157

157-
Class<?> userType = ClassUtils.getUserClass(domainClass);
158+
Class<?> userType = ProxyUtils.getUserClass(domainClass);
158159
RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos.get(userType);
159160

160161
if (repositoryInfo != null) {

src/main/java/org/springframework/data/util/ClassTypeInformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static <S> TypeInformation<S> fromReturnTypeOf(Method method) {
102102
* @param type
103103
*/
104104
ClassTypeInformation(Class<S> type) {
105-
super(ClassUtils.getUserClass(type), getTypeVariableMap(type));
105+
super(ProxyUtils.getUserClass(type), getTypeVariableMap(type));
106106
this.type = type;
107107
}
108108

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.util;
17+
18+
import lombok.experimental.UtilityClass;
19+
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.springframework.core.io.support.SpringFactoriesLoader;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.ClassUtils;
26+
import org.springframework.util.ConcurrentReferenceHashMap;
27+
28+
/**
29+
* Proxy type detection utilities, extensible via {@link ProxyDetector} registered via Spring factories.
30+
*
31+
* @author Oliver Gierke
32+
* @soundtrack Victor Wooten - Cruising Altitude (Trypnotix)
33+
*/
34+
@UtilityClass
35+
public class ProxyUtils {
36+
37+
private static Map<Class<?>, Class<?>> USER_TYPES = new ConcurrentReferenceHashMap<Class<?>, Class<?>>();
38+
39+
private static final List<ProxyDetector> DETECTORS = SpringFactoriesLoader.loadFactories(ProxyDetector.class,
40+
ProxyUtils.class.getClassLoader());
41+
42+
static {
43+
44+
DETECTORS.add(new ProxyDetector() {
45+
46+
@Override
47+
public Class<?> getUserType(Class<?> type) {
48+
return ClassUtils.getUserClass(type);
49+
}
50+
});
51+
}
52+
53+
/**
54+
* Returns the user class for the given type.
55+
*
56+
* @param type must not be {@literal null}.
57+
* @return
58+
*/
59+
public static Class<?> getUserClass(Class<?> type) {
60+
61+
Assert.notNull(type, "Type must not be null!");
62+
63+
Class<?> result = USER_TYPES.get(type);
64+
65+
if (result != null) {
66+
return result;
67+
}
68+
69+
result = type;
70+
71+
for (ProxyDetector proxyDetector : DETECTORS) {
72+
result = proxyDetector.getUserType(result);
73+
}
74+
75+
USER_TYPES.put(type, result);
76+
77+
return result;
78+
}
79+
80+
/**
81+
* Returns the user class for the given source object.
82+
*
83+
* @param source must not be {@literal null}.
84+
* @return
85+
*/
86+
public static Class<?> getUserClass(Object source) {
87+
88+
Assert.notNull(source, "Source object must not be null!");
89+
90+
return getUserClass(source.getClass());
91+
}
92+
93+
/**
94+
* SPI to extend Spring's default proxy detection capabilities.
95+
*
96+
* @author Oliver Gierke
97+
*/
98+
public interface ProxyDetector {
99+
100+
/**
101+
* Returns the user class for the given type.
102+
*
103+
* @param type will never be {@literal null}.
104+
* @return
105+
*/
106+
Class<?> getUserType(Class<?> type);
107+
}
108+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.util;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.aop.framework.ProxyFactory;
23+
import org.springframework.data.util.ProxyUtils.ProxyDetector;
24+
25+
/**
26+
* Unit tests for {@link ProxyUtils}.
27+
*
28+
* @author Oliver Gierke
29+
* @soundtrack Victor Wooten - The 13th Floor (Trypnotix)
30+
*/
31+
public class ProxyUtilsUnitTests {
32+
33+
@Test // DATACMNS-1324
34+
public void detectsStandardProxy() {
35+
36+
ProxyFactory factory = new ProxyFactory();
37+
factory.setTarget(new Sample());
38+
Object proxy = factory.getProxy();
39+
40+
assertThat(proxy.getClass(), is(not((Class) Sample.class)));
41+
assertThat(ProxyUtils.getUserClass(proxy), is(equalTo((Class) Sample.class)));
42+
}
43+
44+
@Test // DATACMNS-1324
45+
public void usesCustomProxyDetector() {
46+
47+
ProxyFactory factory = new ProxyFactory();
48+
factory.setTarget(new AnotherSample());
49+
Object proxy = factory.getProxy();
50+
51+
assertThat(ProxyUtils.getUserClass(proxy), is(equalTo((Class) UserType.class)));
52+
}
53+
54+
static class SampleProxyDetector implements ProxyDetector {
55+
56+
/*
57+
* (non-Javadoc)
58+
* @see org.springframework.data.util.ProxyUtils.ProxyDetector#getUserType(java.lang.Class)
59+
*/
60+
@Override
61+
public Class<?> getUserType(Class<?> type) {
62+
return AnotherSample.class.isAssignableFrom(type) ? UserType.class : type;
63+
}
64+
}
65+
66+
static class Sample {}
67+
68+
static class UserType {}
69+
70+
static class AnotherSample {}
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
org.springframework.data.web.config.SpringDataJacksonModules=org.springframework.data.web.config.SampleMixin
2+
org.springframework.data.util.ProxyUtils$ProxyDetector=org.springframework.data.util.ProxyUtilsUnitTests$SampleProxyDetector

0 commit comments

Comments
 (0)