Skip to content

Commit 69c7eb9

Browse files
committed
Restore ability to configure setClassLoader methods
Closes gh-28269 (cherry picked from commit 9f91168)
1 parent fb763dd commit 69c7eb9

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,15 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
282282
// This call is slow so we do it once.
283283
PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors();
284284
for (PropertyDescriptor pd : pds) {
285-
if (Class.class == beanClass && (!"name".equals(pd.getName()) && !pd.getName().endsWith("Name"))) {
285+
if (Class.class == beanClass && !("name".equals(pd.getName()) ||
286+
(pd.getName().endsWith("Name") && String.class == pd.getPropertyType()))) {
286287
// Only allow all name variants of Class properties
287288
continue;
288289
}
289-
if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
290-
|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
291-
// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
290+
if (pd.getWriteMethod() == null && pd.getPropertyType() != null &&
291+
(ClassLoader.class.isAssignableFrom(pd.getPropertyType()) ||
292+
ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
293+
// Ignore ClassLoader and ProtectionDomain read-only properties - no need to bind to those
292294
continue;
293295
}
294296
if (logger.isTraceEnabled()) {
@@ -326,9 +328,10 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass) throws
326328
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
327329
// against a declared read method, so we prefer read method descriptors here.
328330
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
329-
if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
330-
|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
331-
// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
331+
if (pd.getWriteMethod() == null && pd.getPropertyType() != null &&
332+
(ClassLoader.class.isAssignableFrom(pd.getPropertyType()) ||
333+
ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
334+
// Ignore ClassLoader and ProtectionDomain read-only properties - no need to bind to those
332335
continue;
333336
}
334337
this.propertyDescriptors.put(pd.getName(), pd);

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.beans.testfixture.beans.TestBean;
26+
import org.springframework.core.OverridingClassLoader;
27+
import org.springframework.core.io.DefaultResourceLoader;
2628

2729
import static org.assertj.core.api.Assertions.assertThat;
2830
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -104,7 +106,7 @@ public void checkNotWritablePropertyHoldPossibleMatches() {
104106
.satisfies(ex -> assertThat(ex.getPossibleMatches()).containsExactly("age"));
105107
}
106108

107-
@Test // Can't be shared; there is no such thing as a read-only field
109+
@Test // Can't be shared; there is no such thing as a read-only field
108110
public void setReadOnlyMapProperty() {
109111
TypedReadOnlyMap map = new TypedReadOnlyMap(Collections.singletonMap("key", new TestBean()));
110112
TypedReadOnlyMapClient target = new TypedReadOnlyMapClient();
@@ -156,12 +158,34 @@ public void propertyDescriptors() {
156158
BeanWrapper accessor = createAccessor(target);
157159
accessor.setPropertyValue("name", "a");
158160
accessor.setPropertyValue("spouse.name", "b");
161+
159162
assertThat(target.getName()).isEqualTo("a");
160163
assertThat(target.getSpouse().getName()).isEqualTo("b");
161164
assertThat(accessor.getPropertyValue("name")).isEqualTo("a");
162165
assertThat(accessor.getPropertyValue("spouse.name")).isEqualTo("b");
163166
assertThat(accessor.getPropertyDescriptor("name").getPropertyType()).isEqualTo(String.class);
164167
assertThat(accessor.getPropertyDescriptor("spouse.name").getPropertyType()).isEqualTo(String.class);
168+
169+
assertThat(accessor.isReadableProperty("class.package")).isFalse();
170+
assertThat(accessor.isReadableProperty("class.module")).isFalse();
171+
assertThat(accessor.isReadableProperty("class.classLoader")).isFalse();
172+
assertThat(accessor.isReadableProperty("class.name")).isTrue();
173+
assertThat(accessor.isReadableProperty("class.simpleName")).isTrue();
174+
assertThat(accessor.getPropertyValue("class.name")).isEqualTo(TestBean.class.getName());
175+
assertThat(accessor.getPropertyValue("class.simpleName")).isEqualTo(TestBean.class.getSimpleName());
176+
assertThat(accessor.getPropertyDescriptor("class.name").getPropertyType()).isEqualTo(String.class);
177+
assertThat(accessor.getPropertyDescriptor("class.simpleName").getPropertyType()).isEqualTo(String.class);
178+
179+
accessor = createAccessor(new DefaultResourceLoader());
180+
181+
assertThat(accessor.isReadableProperty("class.package")).isFalse();
182+
assertThat(accessor.isReadableProperty("class.module")).isFalse();
183+
assertThat(accessor.isReadableProperty("class.classLoader")).isFalse();
184+
assertThat(accessor.isReadableProperty("classLoader")).isTrue();
185+
assertThat(accessor.isWritableProperty("classLoader")).isTrue();
186+
OverridingClassLoader ocl = new OverridingClassLoader(getClass().getClassLoader());
187+
accessor.setPropertyValue("classLoader", ocl);
188+
assertThat(accessor.getPropertyValue("classLoader")).isSameAs(ocl);
165189
}
166190

167191
@Test

0 commit comments

Comments
 (0)