Skip to content

Commit 83186b6

Browse files
committed
Refine CachedIntrospectionResults property introspection
Closes gh-28445
1 parent dc2947c commit 83186b6

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.beans.PropertyDescriptor;
2323
import java.lang.reflect.Method;
2424
import java.lang.reflect.Modifier;
25+
import java.net.URL;
2526
import java.security.ProtectionDomain;
2627
import java.util.Collections;
2728
import java.util.HashSet;
@@ -292,10 +293,12 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
292293
// Only allow all name variants of Class properties
293294
continue;
294295
}
295-
if (pd.getWriteMethod() == null && pd.getPropertyType() != null &&
296-
(ClassLoader.class.isAssignableFrom(pd.getPropertyType()) ||
297-
ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
298-
// Ignore ClassLoader and ProtectionDomain read-only properties - no need to bind to those
296+
if (URL.class == beanClass && "content".equals(pd.getName())) {
297+
// Only allow URL attribute introspection, not content resolution
298+
continue;
299+
}
300+
if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) {
301+
// Ignore read-only properties such as ClassLoader - no need to bind to those
299302
continue;
300303
}
301304
if (logger.isTraceEnabled()) {
@@ -344,10 +347,8 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St
344347
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
345348
// against a declared read method, so we prefer read method descriptors here.
346349
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
347-
if (pd.getWriteMethod() == null && pd.getPropertyType() != null &&
348-
(ClassLoader.class.isAssignableFrom(pd.getPropertyType()) ||
349-
ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
350-
// Ignore ClassLoader and ProtectionDomain read-only properties - no need to bind to those
350+
if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) {
351+
// Ignore read-only properties such as ClassLoader - no need to bind to those
351352
continue;
352353
}
353354
this.propertyDescriptors.put(pd.getName(), pd);
@@ -379,8 +380,7 @@ private boolean isPlainAccessor(Method method) {
379380
if (Modifier.isStatic(method.getModifiers()) ||
380381
method.getDeclaringClass() == Object.class || method.getDeclaringClass() == Class.class ||
381382
method.getParameterCount() > 0 || method.getReturnType() == void.class ||
382-
ClassLoader.class.isAssignableFrom(method.getReturnType()) ||
383-
ProtectionDomain.class.isAssignableFrom(method.getReturnType())) {
383+
isInvalidReadOnlyPropertyType(method.getReturnType())) {
384384
return false;
385385
}
386386
try {
@@ -393,6 +393,12 @@ private boolean isPlainAccessor(Method method) {
393393
}
394394
}
395395

396+
private boolean isInvalidReadOnlyPropertyType(@Nullable Class<?> returnType) {
397+
return (returnType != null && (AutoCloseable.class.isAssignableFrom(returnType) ||
398+
ClassLoader.class.isAssignableFrom(returnType) ||
399+
ProtectionDomain.class.isAssignableFrom(returnType)));
400+
}
401+
396402

397403
BeanInfo getBeanInfo() {
398404
return this.beanInfo;

spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.beans.testfixture.beans.TestBean;
2626
import org.springframework.core.OverridingClassLoader;
2727
import org.springframework.core.io.DefaultResourceLoader;
28+
import org.springframework.core.io.UrlResource;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -153,7 +154,7 @@ void setPropertyTypeMismatch() {
153154
}
154155

155156
@Test
156-
void propertyDescriptors() {
157+
void propertyDescriptors() throws Exception {
157158
TestBean target = new TestBean();
158159
target.setSpouse(new TestBean());
159160
BeanWrapper accessor = createAccessor(target);
@@ -182,11 +183,29 @@ void propertyDescriptors() {
182183
assertThat(accessor.isReadableProperty("class.package")).isFalse();
183184
assertThat(accessor.isReadableProperty("class.module")).isFalse();
184185
assertThat(accessor.isReadableProperty("class.classLoader")).isFalse();
186+
assertThat(accessor.isReadableProperty("class.name")).isTrue();
187+
assertThat(accessor.isReadableProperty("class.simpleName")).isTrue();
185188
assertThat(accessor.isReadableProperty("classLoader")).isTrue();
186189
assertThat(accessor.isWritableProperty("classLoader")).isTrue();
187190
OverridingClassLoader ocl = new OverridingClassLoader(getClass().getClassLoader());
188191
accessor.setPropertyValue("classLoader", ocl);
189192
assertThat(accessor.getPropertyValue("classLoader")).isSameAs(ocl);
193+
194+
accessor = createAccessor(new UrlResource("https://spring.io"));
195+
196+
assertThat(accessor.isReadableProperty("class.package")).isFalse();
197+
assertThat(accessor.isReadableProperty("class.module")).isFalse();
198+
assertThat(accessor.isReadableProperty("class.classLoader")).isFalse();
199+
assertThat(accessor.isReadableProperty("class.name")).isTrue();
200+
assertThat(accessor.isReadableProperty("class.simpleName")).isTrue();
201+
assertThat(accessor.isReadableProperty("URL.protocol")).isTrue();
202+
assertThat(accessor.isReadableProperty("URL.host")).isTrue();
203+
assertThat(accessor.isReadableProperty("URL.port")).isTrue();
204+
assertThat(accessor.isReadableProperty("URL.file")).isTrue();
205+
assertThat(accessor.isReadableProperty("URL.content")).isFalse();
206+
assertThat(accessor.isReadableProperty("inputStream")).isFalse();
207+
assertThat(accessor.isReadableProperty("filename")).isTrue();
208+
assertThat(accessor.isReadableProperty("description")).isTrue();
190209
}
191210

192211
@Test

0 commit comments

Comments
 (0)