Skip to content

Commit 0d31ab6

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 6bf952d commit 0d31ab6

File tree

8 files changed

+181
-11
lines changed

8 files changed

+181
-11
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
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.domain;
1717

18-
import org.springframework.util.ClassUtils;
18+
import org.springframework.data.util.ProxyUtils;
1919

2020
/**
2121
* Support for query by example (QBE). An {@link Example} takes a {@code probe} to define the example. Matching options
@@ -69,10 +69,10 @@ static <T> Example<T> of(T probe, ExampleMatcher matcher) {
6969
* CGLIB-generated subclass.
7070
*
7171
* @return
72-
* @see ClassUtils#getUserClass(Class)
72+
* @see ProxyUtils#getUserClass(Class)
7373
*/
7474
@SuppressWarnings("unchecked")
7575
default Class<T> getProbeType() {
76-
return (Class<T>) ClassUtils.getUserClass(getProbe().getClass());
76+
return (Class<T>) ProxyUtils.getUserClass(getProbe().getClass());
7777
}
7878
}

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;
@@ -386,7 +387,7 @@ private TransactionAttribute computeTransactionAttribute(Method method, Class<?>
386387
}
387388

388389
// Ignore CGLIB subclasses - introspect the actual user class.
389-
Class<?> userClass = ClassUtils.getUserClass(targetClass);
390+
Class<?> userClass = ProxyUtils.getUserClass(targetClass);
390391
// The method may be on an interface, but we need attributes from the target class.
391392
// If the target class is null, the method will be unchanged.
392393
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

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

123124
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
124125

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

127128
return repositoryFactoryInfos.containsKey(userClass);
128129
}
@@ -137,16 +138,16 @@ public Optional<Object> getRepositoryFor(Class<?> domainClass) {
137138

138139
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
139140

140-
Class<?> userClass = ClassUtils.getUserClass(domainClass);
141+
Class<?> userClass = ProxyUtils.getUserClass(domainClass);
141142
Optional<String> repositoryBeanName = Optional.ofNullable(repositoryBeanNames.get(userClass));
142143

143144
return beanFactory.flatMap(it -> repositoryBeanName.map(it::getBean));
144145
}
145146

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

156157
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
157158

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

161162
if (repositoryInfo != null) {

src/main/java/org/springframework/data/repository/util/ReactiveWrappers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import org.reactivestreams.Publisher;
3333
import org.springframework.core.ReactiveTypeDescriptor;
34+
import org.springframework.data.util.ProxyUtils;
3435
import org.springframework.data.util.ReflectionUtils;
3536
import org.springframework.util.Assert;
3637
import org.springframework.util.ClassUtils;
@@ -154,7 +155,7 @@ public static boolean isAvailable(ReactiveLibrary reactiveLibrary) {
154155
* @return {@literal true} if the {@code type} is a supported reactive wrapper type.
155156
*/
156157
public static boolean supports(Class<?> type) {
157-
return isWrapper(ClassUtils.getUserClass(type));
158+
return isWrapper(ProxyUtils.getUserClass(type));
158159
}
159160

160161
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static <S> TypeInformation<S> fromReturnTypeOf(Method method) {
8989
* @param type
9090
*/
9191
ClassTypeInformation(Class<S> type) {
92-
super(ClassUtils.getUserClass(type), getTypeVariableMap(type));
92+
super(ProxyUtils.getUserClass(type), getTypeVariableMap(type));
9393
this.type = type;
9494
}
9595

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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<>();
38+
39+
private static final List<ProxyDetector> DETECTORS = SpringFactoriesLoader.loadFactories(ProxyDetector.class,
40+
ProxyUtils.class.getClassLoader());
41+
42+
static {
43+
DETECTORS.add(ClassUtils::getUserClass);
44+
}
45+
46+
/**
47+
* Returns the user class for the given type.
48+
*
49+
* @param type must not be {@literal null}.
50+
* @return
51+
*/
52+
public static Class<?> getUserClass(Class<?> type) {
53+
54+
Assert.notNull(type, "Type must not be null!");
55+
56+
return USER_TYPES.computeIfAbsent(type, it -> {
57+
58+
Class<?> result = it;
59+
60+
for (ProxyDetector proxyDetector : DETECTORS) {
61+
result = proxyDetector.getUserType(result);
62+
}
63+
64+
return result;
65+
});
66+
}
67+
68+
/**
69+
* Returns the user class for the given source object.
70+
*
71+
* @param source must not be {@literal null}.
72+
* @return
73+
*/
74+
public static Class<?> getUserClass(Object source) {
75+
76+
Assert.notNull(source, "Source object must not be null!");
77+
78+
return getUserClass(source.getClass());
79+
}
80+
81+
/**
82+
* SPI to extend Spring's default proxy detection capabilities.
83+
*
84+
* @author Oliver Gierke
85+
*/
86+
public interface ProxyDetector {
87+
88+
/**
89+
* Returns the user class for the given type.
90+
*
91+
* @param type will never be {@literal null}.
92+
* @return
93+
*/
94+
Class<?> getUserType(Class<?> type);
95+
}
96+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.aop.framework.ProxyFactory;
22+
import org.springframework.data.util.ProxyUtils.ProxyDetector;
23+
24+
/**
25+
* Unit tests for {@link ProxyUtils}.
26+
*
27+
* @author Oliver Gierke
28+
* @soundtrack Victor Wooten - The 13th Floor (Trypnotix)
29+
*/
30+
public class ProxyUtilsUnitTests {
31+
32+
@Test // DATACMNS-1324
33+
public void detectsStandardProxy() {
34+
35+
ProxyFactory factory = new ProxyFactory();
36+
factory.setTarget(new Sample());
37+
Object proxy = factory.getProxy();
38+
39+
assertThat(proxy.getClass()).isNotEqualTo(Sample.class);
40+
assertThat(ProxyUtils.getUserClass(proxy)).isEqualTo(Sample.class);
41+
}
42+
43+
@Test // DATACMNS-1324
44+
public void usesCustomProxyDetector() {
45+
46+
ProxyFactory factory = new ProxyFactory();
47+
factory.setTarget(new AnotherSample());
48+
Object proxy = factory.getProxy();
49+
50+
assertThat(ProxyUtils.getUserClass(proxy)).isEqualTo(UserType.class);
51+
}
52+
53+
static class SampleProxyDetector implements ProxyDetector {
54+
55+
/*
56+
* (non-Javadoc)
57+
* @see org.springframework.data.util.ProxyUtils.ProxyDetector#getUserType(java.lang.Class)
58+
*/
59+
@Override
60+
public Class<?> getUserType(Class<?> type) {
61+
return AnotherSample.class.isAssignableFrom(type) ? UserType.class : type;
62+
}
63+
}
64+
65+
static class Sample {}
66+
67+
static class UserType {}
68+
69+
static class AnotherSample {}
70+
}
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)