Browse Source

Avoid memory leak when PropertyComparator is reused

This commit fixes a potential memory leak, since PropertyComparator
previously kept an indirect reference to the value it last compared.

Closes gh-26869
pull/26905/head
Florian Kirmaier 5 years ago committed by Sam Brannen
parent
commit
b5d6e53e50
  1. 9
      spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java

9
spring-beans/src/main/java/org/springframework/beans/support/PropertyComparator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,8 +44,6 @@ public class PropertyComparator<T> implements Comparator<T> { @@ -44,8 +44,6 @@ public class PropertyComparator<T> implements Comparator<T> {
private final SortDefinition sortDefinition;
private final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false);
/**
* Create a new PropertyComparator for the given SortDefinition.
@ -115,8 +113,9 @@ public class PropertyComparator<T> implements Comparator<T> { @@ -115,8 +113,9 @@ public class PropertyComparator<T> implements Comparator<T> {
// (similar to JSTL EL). If the property doesn't exist in the
// first place, let the exception through.
try {
this.beanWrapper.setWrappedInstance(obj);
return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty());
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false);
beanWrapper.setWrappedInstance(obj);
return beanWrapper.getPropertyValue(this.sortDefinition.getProperty());
}
catch (BeansException ex) {
logger.debug("PropertyComparator could not access property - treating as null for sorting", ex);

Loading…
Cancel
Save