Skip to content

Commit 9f91168

Browse files
committed
Restore ability to configure setClassLoader methods
Closes spring-projectsgh-28269
1 parent 7aed627 commit 9f91168

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
@@ -287,13 +287,15 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
287287
// This call is slow so we do it once.
288288
PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors();
289289
for (PropertyDescriptor pd : pds) {
290-
if (Class.class == beanClass && (!"name".equals(pd.getName()) && !pd.getName().endsWith("Name"))) {
290+
if (Class.class == beanClass && !("name".equals(pd.getName()) ||
291+
(pd.getName().endsWith("Name") && String.class == pd.getPropertyType()))) {
291292
// Only allow all name variants of Class properties
292293
continue;
293294
}
294-
if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
295-
|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
296-
// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
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
297299
continue;
298300
}
299301
if (logger.isTraceEnabled()) {
@@ -342,9 +344,10 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St
342344
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
343345
// against a declared read method, so we prefer read method descriptors here.
344346
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
345-
if (pd.getPropertyType() != null && (ClassLoader.class.isAssignableFrom(pd.getPropertyType())
346-
|| ProtectionDomain.class.isAssignableFrom(pd.getPropertyType()))) {
347-
// Ignore ClassLoader and ProtectionDomain types - nobody needs to bind to those
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
348351
continue;
349352
}
350353
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-2021 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;
@@ -105,7 +107,7 @@ void checkNotWritablePropertyHoldPossibleMatches() {
105107
.satisfies(ex -> assertThat(ex.getPossibleMatches()).containsExactly("age"));
106108
}
107109

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

168192
@Test

0 commit comments

Comments
 (0)