Skip to content

Commit b5d6e53

Browse files
FlorianKirmaiersbrannen
authored andcommitted
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
1 parent 8659694 commit b5d6e53

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -44,8 +44,6 @@ public class PropertyComparator<T> implements Comparator<T> {
4444

4545
private final SortDefinition sortDefinition;
4646

47-
private final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false);
48-
4947

5048
/**
5149
* Create a new PropertyComparator for the given SortDefinition.
@@ -115,8 +113,9 @@ private Object getPropertyValue(Object obj) {
115113
// (similar to JSTL EL). If the property doesn't exist in the
116114
// first place, let the exception through.
117115
try {
118-
this.beanWrapper.setWrappedInstance(obj);
119-
return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty());
116+
BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false);
117+
beanWrapper.setWrappedInstance(obj);
118+
return beanWrapper.getPropertyValue(this.sortDefinition.getProperty());
120119
}
121120
catch (BeansException ex) {
122121
logger.debug("PropertyComparator could not access property - treating as null for sorting", ex);

0 commit comments

Comments
 (0)