Skip to content

Commit

Permalink
Allow AutoCloseable dereferences on original AutoCloseable beans
Browse files Browse the repository at this point in the history
Closes gh-29480
  • Loading branch information
jhoeller committed Nov 14, 2022
1 parent 49ee4a4 commit ec3f59e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Expand Up @@ -295,7 +295,7 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
// Only allow URL attribute introspection, not content resolution
continue;
}
if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) {
if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType(), beanClass)) {
// Ignore read-only properties such as ClassLoader - no need to bind to those
continue;
}
Expand Down Expand Up @@ -345,7 +345,8 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass, Set<St
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
// against a declared read method, so we prefer read method descriptors here.
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) {
if (pd.getWriteMethod() == null &&
isInvalidReadOnlyPropertyType(pd.getPropertyType(), beanClass)) {
// Ignore read-only properties such as ClassLoader - no need to bind to those
continue;
}
Expand Down Expand Up @@ -378,7 +379,7 @@ private boolean isPlainAccessor(Method method) {
if (Modifier.isStatic(method.getModifiers()) ||
method.getDeclaringClass() == Object.class || method.getDeclaringClass() == Class.class ||
method.getParameterCount() > 0 || method.getReturnType() == void.class ||
isInvalidReadOnlyPropertyType(method.getReturnType())) {
isInvalidReadOnlyPropertyType(method.getReturnType(), method.getDeclaringClass())) {
return false;
}
try {
Expand All @@ -391,10 +392,11 @@ private boolean isPlainAccessor(Method method) {
}
}

private boolean isInvalidReadOnlyPropertyType(@Nullable Class<?> returnType) {
return (returnType != null && (AutoCloseable.class.isAssignableFrom(returnType) ||
ClassLoader.class.isAssignableFrom(returnType) ||
ProtectionDomain.class.isAssignableFrom(returnType)));
private boolean isInvalidReadOnlyPropertyType(@Nullable Class<?> returnType, Class<?> beanClass) {
return (returnType != null && (ClassLoader.class.isAssignableFrom(returnType) ||
ProtectionDomain.class.isAssignableFrom(returnType) ||
(AutoCloseable.class.isAssignableFrom(returnType) &&
!AutoCloseable.class.isAssignableFrom(beanClass))));
}


Expand Down
Expand Up @@ -216,6 +216,10 @@ void propertyDescriptors() throws Exception {
assertThat(accessor.isReadableProperty("inputStream")).isFalse();
assertThat(accessor.isReadableProperty("filename")).isTrue();
assertThat(accessor.isReadableProperty("description")).isTrue();

accessor = createAccessor(new ActiveResource());

assertThat(accessor.isReadableProperty("resource")).isTrue();
}

@Test
Expand Down Expand Up @@ -376,6 +380,18 @@ public String getObject() {
}


public static class ActiveResource implements AutoCloseable {

public ActiveResource getResource() {
return this;
}

@Override
public void close() throws Exception {
}
}


public static class GetterWithOptional {

public TestBean value;
Expand Down

0 comments on commit ec3f59e

Please sign in to comment.