Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -638,5 +638,15 @@ public final class Constants {
*/
public static final String MAVEN_LOGGER_LOG_PREFIX = MAVEN_LOGGER_PREFIX + "log.";

/**
* User property key for configuring which object types are pooled by ModelObjectProcessor.
* Value should be a comma-separated list of simple class names (e.g., "Dependency,Plugin,Build").
* Default is "Dependency" for backward compatibility.
*
* @since 4.1.0
*/
@Config(defaultValue = "Dependency")
public static final String MAVEN_MODEL_PROCESSOR_POOLED_TYPES = "maven.model.processor.pooledTypes";

private Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents the location of an element within a model source file.
Expand All @@ -40,11 +40,15 @@ public class InputLocation implements Serializable, InputLocationTracker {
private final Map<Object, InputLocation> locations;
private final InputLocation importedFrom;

private volatile int hashCode = 0; // Cached hashCode for performance

private static final InputLocation EMPTY = new InputLocation(-1, -1);

public InputLocation(InputSource source) {
this.lineNumber = -1;
this.columnNumber = -1;
this.source = source;
this.locations = Collections.singletonMap(0, this);
this.locations = ImmutableCollections.singletonMap(0, this);
this.importedFrom = null;
}

Expand All @@ -60,8 +64,9 @@ public InputLocation(int lineNumber, int columnNumber, InputSource source, Objec
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
this.source = source;
this.locations =
selfLocationKey != null ? Collections.singletonMap(selfLocationKey, this) : Collections.emptyMap();
this.locations = selfLocationKey != null
? ImmutableCollections.singletonMap(selfLocationKey, this)
: ImmutableCollections.emptyMap();
this.importedFrom = null;
}

Expand Down Expand Up @@ -143,7 +148,7 @@ public static InputLocation merge(InputLocation target, InputLocation source, bo
}

return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
} // -- InputLocation merge( InputLocation, InputLocation, boolean )
}

/**
* Merges the {@code source} location into the {@code target} location.
Expand Down Expand Up @@ -182,7 +187,86 @@ public static InputLocation merge(InputLocation target, InputLocation source, Co
}

return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
} // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
InputLocation that = (InputLocation) o;
return lineNumber == that.lineNumber
&& columnNumber == that.columnNumber
&& Objects.equals(source, that.source)
&& safeLocationsEquals(this, locations, that, that.locations)
&& Objects.equals(importedFrom, that.importedFrom);
}

/**
* Safely compares two locations maps, treating self-references as equal.
*/
private static boolean safeLocationsEquals(
InputLocation this1,
Map<Object, InputLocation> map1,
InputLocation this2,
Map<Object, InputLocation> map2) {
if (map1 == map2) {
return true;
}
if (map1 == null || map2 == null) {
return false;
}
if (map1.size() != map2.size()) {
return false;
}

for (Map.Entry<Object, InputLocation> entry1 : map1.entrySet()) {
Object key = entry1.getKey();
InputLocation value1 = entry1.getValue();
InputLocation value2 = map2.get(key);

if (value1 == this1) {
if (value2 == this2) {
continue;
}
return false;
} else {
if (Objects.equals(value1, value2)) {
continue;
}
return false;
}
}

return true;
}

@Override
public int hashCode() {
int result = hashCode;
if (result == 0) {
result = Objects.hash(lineNumber, columnNumber, source, safeHash(locations), importedFrom);
hashCode = result;
}
return result;
}

public int safeHash(Map<Object, InputLocation> locations) {
if (locations == null) {
return 0;
}
int result = 1;
for (Map.Entry<Object, InputLocation> entry : locations.entrySet()) {
result = 31 * result + Objects.hashCode(entry.getKey());
if (entry.getValue() != this) {
result = 31 * result + Objects.hashCode(entry.getValue());
}
}
return result;
}

/**
* Class StringFormatter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class InputSource implements Serializable {
private final List<InputSource> inputs;
private final InputLocation importedFrom;

private volatile int hashCode = 0; // Cached hashCode for performance

public InputSource(String modelId, String location) {
this(modelId, location, null);
}
Expand Down Expand Up @@ -99,12 +101,18 @@ public boolean equals(Object o) {
InputSource that = (InputSource) o;
return Objects.equals(modelId, that.modelId)
&& Objects.equals(location, that.location)
&& Objects.equals(inputs, that.inputs);
&& Objects.equals(inputs, that.inputs)
&& Objects.equals(importedFrom, that.importedFrom);
}

@Override
public int hashCode() {
return Objects.hash(modelId, location, inputs);
int result = hashCode;
if (result == 0) {
result = Objects.hash(modelId, location, inputs, importedFrom);
hashCode = result;
}
return result;
}

Stream<InputSource> sources() {
Expand All @@ -119,7 +127,15 @@ public String toString() {
return getModelId() + " " + getLocation();
}

/**
* Merges two InputSource instances.
*
* @param src1 the first input source
* @param src2 the second input source
* @return a new merged InputSource
*/
public static InputSource merge(InputSource src1, InputSource src2) {
return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
return new InputSource(
Stream.concat(src1.sources(), src2.sources()).distinct().toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.model;

import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicReference;

/**
* A pluggable service for processing model objects during model building.
*
* <p>This service allows implementations to:</p>
* <ul>
* <li>Pool identical objects to reduce memory footprint</li>
* <li>Intern objects for faster equality comparisons</li>
* <li>Apply custom optimization strategies</li>
* <li>Transform or modify objects during building</li>
* </ul>
*
* <p>Implementations are discovered via the Java ServiceLoader mechanism and should
* be registered in {@code META-INF/services/org.apache.maven.api.model.ModelObjectProcessor}.</p>
*
* <p>The service is called during model building for all model objects, allowing
* implementations to decide which objects to process and how to optimize them.</p>
*
* @since 4.0.0
*/
public interface ModelObjectProcessor {

/**
* Process a model object, potentially returning a pooled or optimized version.
*
* <p>This method is called during model building for various model objects.
* Implementations can:</p>
* <ul>
* <li>Return the same object if no processing is desired</li>
* <li>Return a pooled equivalent object to reduce memory usage</li>
* <li>Return a modified or optimized version of the object</li>
* </ul>
*
* <p>The implementation must ensure that the returned object is functionally
* equivalent to the input object from the perspective of the Maven model.</p>
*
* @param <T> the type of the model object
* @param object the model object to process
* @return the processed object (may be the same instance, a pooled instance, or a modified instance)
* @throws IllegalArgumentException if the object cannot be processed
*/
<T> T process(T object);

/**
* Process a model object using the first available processor implementation.
*
* <p>This method discovers processor implementations via ServiceLoader and
* uses the first one found. If no implementations are available, the object
* is returned unchanged. The processor is cached for performance.</p>
*
* @param <T> the type of the model object
* @param object the model object to process
* @return the processed object
*/
static <T> T processObject(T object) {
class ProcessorHolder {
/**
* Cached processor instance for performance.
*/
private static final AtomicReference<ModelObjectProcessor> CACHED_PROCESSOR = new AtomicReference<>();
}

ModelObjectProcessor processor = ProcessorHolder.CACHED_PROCESSOR.get();
if (processor == null) {
processor = loadProcessor();
ProcessorHolder.CACHED_PROCESSOR.compareAndSet(null, processor);
processor = ProcessorHolder.CACHED_PROCESSOR.get();
}
return processor.process(object);
}

/**
* Load the first available processor implementation.
*/
private static ModelObjectProcessor loadProcessor() {
/*
* No-op processor that returns objects unchanged.
*/
class NoOpProcessor implements ModelObjectProcessor {
@Override
public <T> T process(T object) {
return object;
}
}

try {
ServiceLoader<ModelObjectProcessor> loader = ServiceLoader.load(ModelObjectProcessor.class);
for (ModelObjectProcessor processor : loader) {
return processor;
}
} catch (Exception e) {
// If service loading fails, use no-op processor
}
return new NoOpProcessor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ private void traverseObjectWithParents(Class<?> cls, Object target) throws Model

private boolean isQualifiedForInterpolation(Class<?> cls) {
return !cls.getPackage().getName().startsWith("java")
&& !cls.getPackage().getName().startsWith("sun.nio.fs");
&& !cls.getPackage().getName().startsWith("sun.nio.fs")
// org.apache.maven.api.model.InputLocation can be self-referencing
&& !cls.getName().equals("org.apache.maven.api.model.InputLocation");
}

private boolean isQualifiedForInterpolation(Field field, Class<?> fieldType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.project.interpolation;

import java.util.Map;

import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.junit.jupiter.api.Test;

class StringSearchModelInterpolatorTest {

@Test
void interpolate() throws ModelInterpolationException, InitializationException {
Model model = Model.newBuilder()
.groupId("group")
.location("groupId", new InputLocation(new InputSource("model", null)))
.build();
StringSearchModelInterpolator interpolator = new StringSearchModelInterpolator();
interpolator.initialize();
interpolator.interpolate(new org.apache.maven.model.Model(model), Map.of());
}
}
Loading