Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give explicit property definitions higher precedence than Maven's implicit ones. #4334

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already annotated the package; that means we likely don't have to repeat ourselves here.

@NonNullApi
package org.openrewrite.internal;
import org.openrewrite.internal.lang.NonNullApi;

Suggested change
private static boolean isMavenBuiltInProperty(@NonNull final String originalPlaceholder) {
private static boolean isMavenBuiltInProperty(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");
}
}