Skip to content

Commit e7df445

Browse files
committed
Merge pull reqest #24617 from dreis2211/avoid-unnecessary-sorting
Closes gh-24617
2 parents 87230c4 + 2093e35 commit e7df445

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java

Lines changed: 4 additions & 2 deletions
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-2020 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.
@@ -154,7 +154,9 @@ private List<Method> getAdvisorMethods(Class<?> aspectClass) {
154154
methods.add(method);
155155
}
156156
}, ReflectionUtils.USER_DECLARED_METHODS);
157-
methods.sort(METHOD_COMPARATOR);
157+
if (methods.size() > 1) {
158+
methods.sort(METHOD_COMPARATOR);
159+
}
158160
return methods;
159161
}
160162

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 9 additions & 4 deletions
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-2020 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.
@@ -398,6 +398,9 @@ public Stream<T> stream() {
398398
@Override
399399
public Stream<T> orderedStream() {
400400
String[] beanNames = getBeanNamesForTypedStream(requiredType);
401+
if (beanNames.length == 0) {
402+
return Stream.empty();
403+
}
401404
Map<String, T> matchingBeans = new LinkedHashMap<>(beanNames.length);
402405
for (String beanName : beanNames) {
403406
Object beanInstance = getBean(beanName);
@@ -1366,9 +1369,11 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
13661369
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
13671370
Object result = converter.convertIfNecessary(matchingBeans.values(), type);
13681371
if (result instanceof List) {
1369-
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
1370-
if (comparator != null) {
1371-
((List<?>) result).sort(comparator);
1372+
if (((List<?>) result).size() > 1) {
1373+
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
1374+
if (comparator != null) {
1375+
((List<?>) result).sort(comparator);
1376+
}
13721377
}
13731378
}
13741379
return result;

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ConsumesRequestCondition.java

Lines changed: 4 additions & 2 deletions
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-2020 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.
@@ -76,7 +76,9 @@ public ConsumesRequestCondition(String... consumes) {
7676
*/
7777
public ConsumesRequestCondition(String[] consumes, String[] headers) {
7878
this.expressions = new ArrayList<>(parseExpressions(consumes, headers));
79-
Collections.sort(this.expressions);
79+
if (this.expressions.size() > 1) {
80+
Collections.sort(this.expressions);
81+
}
8082
}
8183

8284
/**

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java

Lines changed: 4 additions & 2 deletions
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-2020 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.
@@ -93,7 +93,9 @@ public ProducesRequestCondition(String[] produces, String[] headers) {
9393
*/
9494
public ProducesRequestCondition(String[] produces, String[] headers, RequestedContentTypeResolver resolver) {
9595
this.expressions = new ArrayList<>(parseExpressions(produces, headers));
96-
Collections.sort(this.expressions);
96+
if (this.expressions.size() > 1) {
97+
Collections.sort(this.expressions);
98+
}
9799
this.contentTypeResolver = resolver != null ? resolver : DEFAULT_CONTENT_TYPE_RESOLVER;
98100
}
99101

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java

Lines changed: 4 additions & 2 deletions
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-2020 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.
@@ -77,7 +77,9 @@ public ConsumesRequestCondition(String... consumes) {
7777
*/
7878
public ConsumesRequestCondition(String[] consumes, @Nullable String[] headers) {
7979
this.expressions = new ArrayList<>(parseExpressions(consumes, headers));
80-
Collections.sort(this.expressions);
80+
if (this.expressions.size() > 1) {
81+
Collections.sort(this.expressions);
82+
}
8183
}
8284

8385
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java

Lines changed: 4 additions & 2 deletions
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-2020 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.
@@ -97,7 +97,9 @@ public ProducesRequestCondition(String[] produces, @Nullable String[] headers,
9797
@Nullable ContentNegotiationManager manager) {
9898

9999
this.expressions = new ArrayList<>(parseExpressions(produces, headers));
100-
Collections.sort(this.expressions);
100+
if (this.expressions.size() > 1) {
101+
Collections.sort(this.expressions);
102+
}
101103
this.contentNegotiationManager = manager != null ? manager : DEFAULT_CONTENT_NEGOTIATION_MANAGER;
102104
}
103105

0 commit comments

Comments
 (0)