Skip to content

Commit

Permalink
[MNG-8524] DefaultInterpolator should be used by injection
Browse files Browse the repository at this point in the history
Signed-off-by: crazyhzm <[email protected]>
  • Loading branch information
CrazyHZM committed Jan 16, 2025
1 parent aeec37f commit 5eb89c8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,25 @@ public class BootstrapCoreExtensionManager {

private final RepositorySystem repoSystem;

private final Interpolator interpolator;

@Inject
public BootstrapCoreExtensionManager(
DefaultPluginDependenciesResolver pluginDependenciesResolver,
RepositorySystemSessionFactory repositorySystemSessionFactory,
CoreExports coreExports,
PlexusContainer container,
@Nullable @Named("ide") WorkspaceReader ideWorkspaceReader,
RepositorySystem repoSystem) {
RepositorySystem repoSystem,
Interpolator interpolator) {
this.pluginDependenciesResolver = pluginDependenciesResolver;
this.repositorySystemSessionFactory = repositorySystemSessionFactory;
this.coreExports = coreExports;
this.classWorld = ((DefaultPlexusContainer) container).getClassWorld();
this.parentRealm = container.getContainerRealm();
this.ideWorkspaceReader = ideWorkspaceReader;
this.repoSystem = repoSystem;
this.interpolator = interpolator;
}

public List<CoreExtensionEntry> loadCoreExtensions(
Expand Down Expand Up @@ -230,8 +234,7 @@ private List<Artifact> resolveExtension(
}
}

private static UnaryOperator<String> createInterpolator(MavenExecutionRequest request) {
Interpolator interpolator = new DefaultInterpolator();
private UnaryOperator<String> createInterpolator(MavenExecutionRequest request) {
UnaryOperator<String> callback = v -> {
String r = request.getUserProperties().getProperty(v);
if (r == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ExtensionConfigurationModule implements Module {

private final CoreExtensionEntry extension;
private final UnaryOperator<String> callback;

private final DefaultInterpolator interpolator = new DefaultInterpolator();

public ExtensionConfigurationModule(CoreExtensionEntry extension, UnaryOperator<String> callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.Profile;
import org.apache.maven.api.services.BuilderProblem.Severity;
import org.apache.maven.api.services.Interpolator;
import org.apache.maven.api.services.ModelProblem.Version;
import org.apache.maven.api.services.ModelProblemCollector;
import org.apache.maven.api.services.VersionParser;
import org.apache.maven.api.services.model.ProfileActivationContext;
import org.apache.maven.api.services.model.ProfileActivator;
import org.apache.maven.internal.impl.model.DefaultInterpolator;

import static org.apache.maven.internal.impl.model.profile.ConditionParser.toBoolean;

Expand All @@ -47,14 +47,18 @@ public class ConditionProfileActivator implements ProfileActivator {

private final VersionParser versionParser;

private final Interpolator interpolator;

/**
* Constructs a new ConditionProfileActivator with the necessary dependencies.
*
* @param versionParser The parser for handling version comparisons
* @param interpolator The interpolator for interpolating the values in the given map using the provided callback function
*/
@Inject
public ConditionProfileActivator(VersionParser versionParser) {
public ConditionProfileActivator(VersionParser versionParser, Interpolator interpolator) {
this.versionParser = versionParser;
this.interpolator = interpolator;
}

/**
Expand Down Expand Up @@ -106,7 +110,7 @@ public boolean presentInConfig(Profile profile, ProfileActivationContext context
* @param versionParser The parser for handling version comparisons
* @return A map of function names to their implementations
*/
public static Map<String, ConditionParser.ExpressionFunction> registerFunctions(
public Map<String, ConditionParser.ExpressionFunction> registerFunctions(
ProfileActivationContext context, VersionParser versionParser) {
Map<String, ConditionParser.ExpressionFunction> functions = new HashMap<>();

Expand Down Expand Up @@ -156,9 +160,9 @@ public static Map<String, ConditionParser.ExpressionFunction> registerFunctions(
* @return The value of the property, or null if not found
* @throws IllegalArgumentException if the number of arguments is not exactly one
*/
static String property(ProfileActivationContext context, String name) {
String property(ProfileActivationContext context, String name) {
String value = doGetProperty(context, name);
return new DefaultInterpolator().interpolate(value, s -> doGetProperty(context, s));
return interpolator.interpolate(value, s -> doGetProperty(context, s));
}

static String doGetProperty(ProfileActivationContext context, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ void setUp() {
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
DefaultRootLocator rootLocator = new DefaultRootLocator();

functions = ConditionProfileActivator.registerFunctions(context, versionParser);
propertyResolver = s -> ConditionProfileActivator.property(context, s);
ConditionProfileActivator activator = new ConditionProfileActivator(versionParser, new DefaultInterpolator());
functions = activator.registerFunctions(context, versionParser);
propertyResolver = s -> activator.property(context, s);
parser = new ConditionParser(functions, propertyResolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class ConditionProfileActivatorTest extends AbstractProfileActivatorTest<
@Override
void setUp() throws Exception {
activator = new ConditionProfileActivator(
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme())));
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme())),
new DefaultInterpolator());

Path file = tempDir.resolve("file.txt");
Files.createFile(file);
Expand Down

0 comments on commit 5eb89c8

Please sign in to comment.