Skip to content

Commit

Permalink
Exclude Maven built-in property from circular placeholder reference c…
Browse files Browse the repository at this point in the history
…heck.
  • Loading branch information
hoanamzn committed Jul 16, 2024
1 parent 7d056fd commit 86ae0f1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.internal;

import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.internal.lang.Nullable;

import java.util.*;
Expand All @@ -27,12 +28,21 @@
* @author Rob Harrop
*/
public class PropertyPlaceholderHelper {

private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<>(4);

private static final List<String> MAVEN_BUILT_IN_PROPERTY_PREFIXES = new ArrayList<>();

static {
wellKnownSimplePrefixes.put("}", "{");
wellKnownSimplePrefixes.put("]", "[");
wellKnownSimplePrefixes.put(")", "(");

MAVEN_BUILT_IN_PROPERTY_PREFIXES.add("project.");
MAVEN_BUILT_IN_PROPERTY_PREFIXES.add("pom.");
MAVEN_BUILT_IN_PROPERTY_PREFIXES.add("maven.");
MAVEN_BUILT_IN_PROPERTY_PREFIXES.add("env.");
MAVEN_BUILT_IN_PROPERTY_PREFIXES.add("settings.");
}

private final String placeholderPrefix;
Expand Down Expand Up @@ -88,7 +98,7 @@ protected String parseStringValue(String value, Function<String, String> placeho
if (visitedPlaceholders == null) {
visitedPlaceholders = new HashSet<>(4);
}
if (!visitedPlaceholders.add(originalPlaceholder)) {
if (!visitedPlaceholders.add(originalPlaceholder) && !isMavenBuiltInProperty(originalPlaceholder)) {
throw new IllegalArgumentException(
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
}
Expand Down Expand Up @@ -160,4 +170,13 @@ private static boolean substringMatch(CharSequence str, int index, CharSequence
}
return true;
}

private static boolean isMavenBuiltInProperty(@NonNull final String originalPlaceholder) {
for (final String prefix : MAVEN_BUILT_IN_PROPERTY_PREFIXES) {
if (originalPlaceholder.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;

class PropertyPlaceholderHelperTest {
Expand All @@ -31,4 +35,25 @@ void dashed() {
});
assertThat(s).isEqualTo("hi jon");
}

@Test
void testMavenBuiltInProperty() {
var helper = new PropertyPlaceholderHelper("${", "}", null);
final Set<String> visitedPlaceholders = new HashSet<>();
visitedPlaceholders.add("project.version");
visitedPlaceholders.add("pom.basedir");
visitedPlaceholders.add("maven.home");
visitedPlaceholders.add("env.PATH");
visitedPlaceholders.add("settings.localRepository");
assertThat(helper.parseStringValue("${project.version}", Function.identity(), visitedPlaceholders))
.isEqualTo("project.version");
assertThat(helper.parseStringValue("${pom.basedir}", Function.identity(), visitedPlaceholders))
.isEqualTo("pom.basedir");
assertThat(helper.parseStringValue("${maven.home}", Function.identity(), visitedPlaceholders))
.isEqualTo("maven.home");
assertThat(helper.parseStringValue("${env.PATH}", Function.identity(), visitedPlaceholders))
.isEqualTo("env.PATH");
assertThat(helper.parseStringValue("${settings.localRepository}", Function.identity(), visitedPlaceholders))
.isEqualTo("settings.localRepository");
}
}

0 comments on commit 86ae0f1

Please sign in to comment.