You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2025/09/16 12:40:05 UTC

(maven) branch master updated: Move InputLocation/InputLocationTracker/InputSource to velocity templates (#10931)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f87ad4dcf Move InputLocation/InputLocationTracker/InputSource to velocity templates (#10931)
9f87ad4dcf is described below

commit 9f87ad4dcf1e337ccda4143b5d7685329255d8ce
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue Sep 16 14:39:59 2025 +0200

    Move InputLocation/InputLocationTracker/InputSource to velocity templates (#10931)
    
    This PR moves the InputLocation, InputLocationTracker, and InputSource
    classes from maven-api-model to be generated using the existing velocity
    templates in src/mdo/java/.
    
    Changes Made:
    - Updated Velocity Templates with Maven-specific features controlled by isMavenModel parameter
    - Added locationTracking=true and generateLocationClasses=true parameters to maven-api-model configuration
    - Removed manually written InputLocation classes from api/maven-api-model/src/main/java/org/apache/maven/api/model/
    
    Benefits:
    - Consistency: InputLocation classes are now generated consistently across all Maven modules
    - Maintainability: Changes can be made in one place (the templates) rather than maintaining separate implementations
    - Feature Parity: All Maven-specific features are preserved through the isMavenModel boolean parameter
    - Code Reuse: The same templates can generate both simple and Maven-enhanced versions
---
 api/maven-api-model/pom.xml                        |   2 +
 .../org/apache/maven/api/model/InputLocation.java  | 208 ---------------------
 .../maven/api/model/InputLocationTracker.java      |  32 ----
 .../org/apache/maven/api/model/InputSource.java    | 125 -------------
 src/mdo/java/InputLocation.java                    |  41 +++-
 src/mdo/java/InputLocationTracker.java             |   9 +
 src/mdo/java/InputSource.java                      | 112 ++++++++++-
 7 files changed, 161 insertions(+), 368 deletions(-)

diff --git a/api/maven-api-model/pom.xml b/api/maven-api-model/pom.xml
index 25a7648278..8294a3e887 100644
--- a/api/maven-api-model/pom.xml
+++ b/api/maven-api-model/pom.xml
@@ -59,6 +59,8 @@ under the License.
           <params>
             <param>packageModelV4=org.apache.maven.api.model</param>
             <param>isMavenModel=true</param>
+            <param>locationTracking=true</param>
+            <param>generateLocationClasses=true</param>
           </params>
         </configuration>
         <executions>
diff --git a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocation.java b/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocation.java
deleted file mode 100644
index 2e65dea793..0000000000
--- a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocation.java
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * 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.io.Serializable;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Represents the location of an element within a model source file.
- * <p>
- * This class tracks the line and column numbers of elements in source files like POM files.
- * It's used for error reporting and debugging to help identify where specific model elements
- * are defined in the source files.
- *
- * @since 4.0.0
- */
-public class InputLocation implements Serializable, InputLocationTracker {
-    private final int lineNumber;
-    private final int columnNumber;
-    private final InputSource source;
-    private final Map<Object, InputLocation> locations;
-    private final InputLocation importedFrom;
-
-    public InputLocation(InputSource source) {
-        this.lineNumber = -1;
-        this.columnNumber = -1;
-        this.source = source;
-        this.locations = Collections.singletonMap(0, this);
-        this.importedFrom = null;
-    }
-
-    public InputLocation(int lineNumber, int columnNumber) {
-        this(lineNumber, columnNumber, null, null);
-    }
-
-    public InputLocation(int lineNumber, int columnNumber, InputSource source) {
-        this(lineNumber, columnNumber, source, null);
-    }
-
-    public InputLocation(int lineNumber, int columnNumber, InputSource source, Object selfLocationKey) {
-        this.lineNumber = lineNumber;
-        this.columnNumber = columnNumber;
-        this.source = source;
-        this.locations =
-                selfLocationKey != null ? Collections.singletonMap(selfLocationKey, this) : Collections.emptyMap();
-        this.importedFrom = null;
-    }
-
-    public InputLocation(int lineNumber, int columnNumber, InputSource source, Map<Object, InputLocation> locations) {
-        this.lineNumber = lineNumber;
-        this.columnNumber = columnNumber;
-        this.source = source;
-        this.locations = ImmutableCollections.copy(locations);
-        this.importedFrom = null;
-    }
-
-    public InputLocation(InputLocation original) {
-        this.lineNumber = original.lineNumber;
-        this.columnNumber = original.columnNumber;
-        this.source = original.source;
-        this.locations = original.locations;
-        this.importedFrom = original.importedFrom;
-    }
-
-    public int getLineNumber() {
-        return lineNumber;
-    }
-
-    public int getColumnNumber() {
-        return columnNumber;
-    }
-
-    public InputSource getSource() {
-        return source;
-    }
-
-    @Override
-    public InputLocation getLocation(Object key) {
-        return locations != null ? locations.get(key) : null;
-    }
-
-    public Map<Object, InputLocation> getLocations() {
-        return locations;
-    }
-
-    /**
-     * Gets the parent InputLocation where this InputLocation may have been imported from.
-     * Can return {@code null}.
-     *
-     * @return InputLocation
-     * @since 4.0.0
-     */
-    @Override
-    public InputLocation getImportedFrom() {
-        return importedFrom;
-    }
-
-    /**
-     * Merges the {@code source} location into the {@code target} location.
-     *
-     * @param target the target location
-     * @param source the source location
-     * @param sourceDominant the boolean indicating of {@code source} is dominant compared to {@code target}
-     * @return the merged location
-     */
-    public static InputLocation merge(InputLocation target, InputLocation source, boolean sourceDominant) {
-        if (source == null) {
-            return target;
-        } else if (target == null) {
-            return source;
-        }
-
-        Map<Object, InputLocation> locations;
-        Map<Object, InputLocation> sourceLocations = source.locations;
-        Map<Object, InputLocation> targetLocations = target.locations;
-        if (sourceLocations == null) {
-            locations = targetLocations;
-        } else if (targetLocations == null) {
-            locations = sourceLocations;
-        } else {
-            locations = new LinkedHashMap<>();
-            locations.putAll(sourceDominant ? targetLocations : sourceLocations);
-            locations.putAll(sourceDominant ? sourceLocations : targetLocations);
-        }
-
-        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.
-     * This method is used when the locations refer to lists and also merges the indices.
-     *
-     * @param target the target location
-     * @param source the source location
-     * @param indices the list of integers for the indices
-     * @return the merged location
-     */
-    public static InputLocation merge(InputLocation target, InputLocation source, Collection<Integer> indices) {
-        if (source == null) {
-            return target;
-        } else if (target == null) {
-            return source;
-        }
-
-        Map<Object, InputLocation> locations;
-        Map<Object, InputLocation> sourceLocations = source.locations;
-        Map<Object, InputLocation> targetLocations = target.locations;
-        if (sourceLocations == null) {
-            locations = targetLocations;
-        } else if (targetLocations == null) {
-            locations = sourceLocations;
-        } else {
-            locations = new LinkedHashMap<>();
-            for (int index : indices) {
-                InputLocation location;
-                if (index < 0) {
-                    location = sourceLocations.get(~index);
-                } else {
-                    location = targetLocations.get(index);
-                }
-                locations.put(locations.size(), location);
-            }
-        }
-
-        return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
-    } // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )
-
-    /**
-     * Class StringFormatter.
-     *
-     * @version $Revision$ $Date$
-     */
-    public interface StringFormatter {
-
-        // -----------/
-        // - Methods -/
-        // -----------/
-
-        /**
-         * Method toString.
-         */
-        String toString(InputLocation location);
-    }
-
-    @Override
-    public String toString() {
-        return String.format("%s @ %d:%d", source != null ? source.getLocation() : "n/a", lineNumber, columnNumber);
-    }
-}
diff --git a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocationTracker.java b/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocationTracker.java
deleted file mode 100644
index 8b2958a35c..0000000000
--- a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputLocationTracker.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * 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;
-
-public interface InputLocationTracker {
-    InputLocation getLocation(Object field);
-
-    /**
-     * Gets the parent InputLocation where this InputLocation may have been imported from.
-     * Can return {@code null}.
-     *
-     * @return InputLocation
-     * @since 4.0.0
-     */
-    InputLocation getImportedFrom();
-}
diff --git a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputSource.java b/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputSource.java
deleted file mode 100644
index f4d5e7fc67..0000000000
--- a/api/maven-api-model/src/main/java/org/apache/maven/api/model/InputSource.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * 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.io.Serializable;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Represents the source of a model input, such as a POM file.
- * <p>
- * This class tracks the origin of model elements, including their location in source files
- * and relationships between imported models. It's used for error reporting and debugging
- * to help identify where specific model elements came from.
- *
- * @since 4.0.0
- */
-public class InputSource implements Serializable {
-
-    private final String modelId;
-    private final String location;
-    private final List<InputSource> inputs;
-    private final InputLocation importedFrom;
-
-    public InputSource(String modelId, String location) {
-        this(modelId, location, null);
-    }
-
-    public InputSource(String modelId, String location, InputLocation importedFrom) {
-        this.modelId = modelId;
-        this.location = location;
-        this.inputs = null;
-        this.importedFrom = importedFrom;
-    }
-
-    public InputSource(Collection<InputSource> inputs) {
-        this.modelId = null;
-        this.location = null;
-        this.inputs = ImmutableCollections.copy(inputs);
-        this.importedFrom = null;
-    }
-
-    /**
-     * Get the path/URL of the POM or {@code null} if unknown.
-     *
-     * @return the location
-     */
-    public String getLocation() {
-        return this.location;
-    }
-
-    /**
-     * Get the identifier of the POM in the format {@code <groupId>:<artifactId>:<version>}.
-     *
-     * @return the model id
-     */
-    public String getModelId() {
-        return this.modelId;
-    }
-
-    /**
-     * Gets the parent InputLocation where this InputLocation may have been imported from.
-     * Can return {@code null}.
-     *
-     * @return InputLocation
-     * @since 4.0.0
-     */
-    public InputLocation getImportedFrom() {
-        return importedFrom;
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) {
-            return true;
-        }
-        if (o == null || getClass() != o.getClass()) {
-            return false;
-        }
-        InputSource that = (InputSource) o;
-        return Objects.equals(modelId, that.modelId)
-                && Objects.equals(location, that.location)
-                && Objects.equals(inputs, that.inputs);
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(modelId, location, inputs);
-    }
-
-    Stream<InputSource> sources() {
-        return inputs != null ? inputs.stream() : Stream.of(this);
-    }
-
-    @Override
-    public String toString() {
-        if (inputs != null) {
-            return inputs.stream().map(InputSource::toString).collect(Collectors.joining(", ", "merged[", "]"));
-        }
-        return getModelId() + " " + getLocation();
-    }
-
-    public static InputSource merge(InputSource src1, InputSource src2) {
-        return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
-    }
-}
diff --git a/src/mdo/java/InputLocation.java b/src/mdo/java/InputLocation.java
index 3345a1d1d0..7cd5677403 100644
--- a/src/mdo/java/InputLocation.java
+++ b/src/mdo/java/InputLocation.java
@@ -22,22 +22,34 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
- * Class InputLocation.
+ * Represents the location of an element within a model source file.
+ * <p>
+ * This class tracks the line and column numbers of elements in source files like POM files.
+ * It's used for error reporting and debugging to help identify where specific model elements
+ * are defined in the source files.
+ *
+ * @since 4.0.0
  */
 public class InputLocation implements Serializable, InputLocationTracker {
     private final int lineNumber;
     private final int columnNumber;
     private final InputSource source;
     private final Map<Object, InputLocation> locations;
+    private final InputLocation importedFrom;
 
     public InputLocation(InputSource source) {
         this.lineNumber = -1;
         this.columnNumber = -1;
         this.source = source;
         this.locations = Collections.singletonMap(0, this);
+        this.importedFrom = null;
     }
 
     public InputLocation(int lineNumber, int columnNumber) {
@@ -54,6 +66,7 @@ public InputLocation(int lineNumber, int columnNumber, InputSource source, Objec
         this.source = source;
         this.locations =
                 selfLocationKey != null ? Collections.singletonMap(selfLocationKey, this) : Collections.emptyMap();
+        this.importedFrom = null;
     }
 
     public InputLocation(int lineNumber, int columnNumber, InputSource source, Map<Object, InputLocation> locations) {
@@ -61,6 +74,15 @@ public InputLocation(int lineNumber, int columnNumber, InputSource source, Map<O
         this.columnNumber = columnNumber;
         this.source = source;
         this.locations = ImmutableCollections.copy(locations);
+        this.importedFrom = null;
+    }
+
+    public InputLocation(InputLocation original) {
+        this.lineNumber = original.lineNumber;
+        this.columnNumber = original.columnNumber;
+        this.source = original.source;
+        this.locations = original.locations;
+        this.importedFrom = original.importedFrom;
     }
 
     public int getLineNumber() {
@@ -84,6 +106,18 @@ public Map<Object, InputLocation> getLocations() {
         return locations;
     }
 
+    /**
+     * Gets the parent InputLocation where this InputLocation may have been imported from.
+     * Can return {@code null}.
+     *
+     * @return InputLocation
+     * @since 4.0.0
+     */
+    @Override
+    public InputLocation getImportedFrom() {
+        return importedFrom;
+    }
+
     /**
      * Merges the {@code source} location into the {@code target} location.
      *
@@ -170,4 +204,9 @@ public interface StringFormatter {
          */
         String toString(InputLocation location);
     }
+
+    @Override
+    public String toString() {
+        return String.format("%s @ %d:%d", source != null ? source.getLocation() : "n/a", lineNumber, columnNumber);
+    }
 }
diff --git a/src/mdo/java/InputLocationTracker.java b/src/mdo/java/InputLocationTracker.java
index 9d28ad2494..365cf7f97e 100644
--- a/src/mdo/java/InputLocationTracker.java
+++ b/src/mdo/java/InputLocationTracker.java
@@ -20,4 +20,13 @@
 
 public interface InputLocationTracker {
     InputLocation getLocation(Object field);
+
+    /**
+     * Gets the parent InputLocation where this InputLocation may have been imported from.
+     * Can return {@code null}.
+     *
+     * @return InputLocation
+     * @since 4.0.0
+     */
+    InputLocation getImportedFrom();
 }
diff --git a/src/mdo/java/InputSource.java b/src/mdo/java/InputSource.java
index aeb405f976..cd030bdf65 100644
--- a/src/mdo/java/InputSource.java
+++ b/src/mdo/java/InputSource.java
@@ -19,20 +19,63 @@
 package ${package};
 
 import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
- * Class InputSource.
+ * Represents the source of a model input, such as a POM file.
+ * <p>
+ * This class tracks the origin of model elements, including their location in source files
+ * and relationships between imported models. It's used for error reporting and debugging
+ * to help identify where specific model elements came from.
+ *
+ * @since 4.0.0
  */
 public class InputSource implements Serializable {
 
+#if ( $isMavenModel )
+    private final String modelId;
+#end
     private final String location;
+    private final List<InputSource> inputs;
+    private final InputLocation importedFrom;
+
+#if ( $isMavenModel )
+    public InputSource(String modelId, String location) {
+        this(modelId, location, null);
+    }
+
+    public InputSource(String modelId, String location, InputLocation importedFrom) {
+        this.modelId = modelId;
+        this.location = location;
+        this.inputs = null;
+        this.importedFrom = importedFrom;
+    }
+#end
 
     public InputSource(String location) {
+#if ( $isMavenModel )
+        this.modelId = null;
+#end
         this.location = location;
+        this.inputs = null;
+        this.importedFrom = null;
+    }
+
+    public InputSource(Collection<InputSource> inputs) {
+#if ( $isMavenModel )
+        this.modelId = null;
+#end
+        this.location = null;
+        this.inputs = ImmutableCollections.copy(inputs);
+        this.importedFrom = null;
     }
 
     /**
-     * Get the path/URL of the settings definition or {@code null} if unknown.
+     * Get the path/URL of the POM or {@code null} if unknown.
      *
      * @return the location
      */
@@ -40,8 +83,73 @@ public String getLocation() {
         return this.location;
     }
 
+#if ( $isMavenModel )
+    /**
+     * Get the identifier of the POM in the format {@code <groupId>:<artifactId>:<version>}.
+     *
+     * @return the model id
+     */
+    public String getModelId() {
+        return this.modelId;
+    }
+#end
+
+    /**
+     * Gets the parent InputLocation where this InputLocation may have been imported from.
+     * Can return {@code null}.
+     *
+     * @return InputLocation
+     * @since 4.0.0
+     */
+    public InputLocation getImportedFrom() {
+        return importedFrom;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        InputSource that = (InputSource) o;
+#if ( $isMavenModel )
+        return Objects.equals(modelId, that.modelId)
+                && Objects.equals(location, that.location)
+                && Objects.equals(inputs, that.inputs);
+#else
+        return Objects.equals(location, that.location)
+                && Objects.equals(inputs, that.inputs);
+#end
+    }
+
+    @Override
+    public int hashCode() {
+#if ( $isMavenModel )
+        return Objects.hash(modelId, location, inputs);
+#else
+        return Objects.hash(location, inputs);
+#end
+    }
+
+    Stream<InputSource> sources() {
+        return inputs != null ? inputs.stream() : Stream.of(this);
+    }
+
     @Override
     public String toString() {
+        if (inputs != null) {
+            return inputs.stream().map(InputSource::toString).collect(Collectors.joining(", ", "merged[", "]"));
+        }
+#if ( $isMavenModel )
+        return getModelId() != null ? getModelId() + " " + getLocation() : getLocation();
+#else
         return getLocation();
+#end
+    }
+
+    public static InputSource merge(InputSource src1, InputSource src2) {
+        return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
     }
 }