From 9674ce490670b3d80018c8717eced2babb284e0d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 4 May 2019 12:00:22 +0200 Subject: [PATCH 1/2] Avoid HashSet/StringBuilder allocation for non-placeholder values Closes gh-22870 --- .../util/PropertyPlaceholderHelper.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java b/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java index b80ded1bb6..b17d6f85fd 100644 --- a/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java +++ b/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -121,20 +121,26 @@ public class PropertyPlaceholderHelper { */ public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) { Assert.notNull(value, "'value' must not be null"); - return parseStringValue(value, placeholderResolver, new HashSet<>()); + return parseStringValue(value, placeholderResolver, null); } protected String parseStringValue( - String value, PlaceholderResolver placeholderResolver, Set visitedPlaceholders) { - - StringBuilder result = new StringBuilder(value); + String value, PlaceholderResolver placeholderResolver, @Nullable Set visitedPlaceholders) { int startIndex = value.indexOf(this.placeholderPrefix); + if (startIndex == -1) { + return value; + } + + StringBuilder result = new StringBuilder(value); while (startIndex != -1) { int endIndex = findPlaceholderEndIndex(result, startIndex); if (endIndex != -1) { String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex); String originalPlaceholder = placeholder; + if (visitedPlaceholders == null) { + visitedPlaceholders = new HashSet<>(4); + } if (!visitedPlaceholders.add(originalPlaceholder)) { throw new IllegalArgumentException( "Circular placeholder reference '" + originalPlaceholder + "' in property definitions"); @@ -178,7 +184,6 @@ public class PropertyPlaceholderHelper { startIndex = -1; } } - return result.toString(); } From 293188c797c82a72cb5836acde223ac5f57e3080 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 4 May 2019 12:01:54 +0200 Subject: [PATCH 2/2] Consistent non-use of firstIndex 0 in PatternMatchUtils Closes gh-22837 --- .../java/org/springframework/util/PatternMatchUtils.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java b/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java index 99c204c943..0430128489 100644 --- a/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java +++ b/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -39,15 +39,17 @@ public abstract class PatternMatchUtils { if (pattern == null || str == null) { return false; } + int firstIndex = pattern.indexOf('*'); if (firstIndex == -1) { return pattern.equals(str); } + if (firstIndex == 0) { if (pattern.length() == 1) { return true; } - int nextIndex = pattern.indexOf('*', firstIndex + 1); + int nextIndex = pattern.indexOf('*', 1); if (nextIndex == -1) { return str.endsWith(pattern.substring(1)); } @@ -64,6 +66,7 @@ public abstract class PatternMatchUtils { } return false; } + return (str.length() >= firstIndex && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) && simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));