Skip to content

Commit

Permalink
[MNG-7344] Copy with working importedFrom tree
Browse files Browse the repository at this point in the history
  • Loading branch information
juulhobert committed Feb 9, 2024
1 parent 20f7cfc commit da5185e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public class InputLocation implements Serializable, InputLocationTracker {
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) {
Expand All @@ -54,13 +56,23 @@ 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) {
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
this.source = source;
this.locations = ImmutableCollections.copy(locations);
this.importedFrom = null;
}

public InputLocation(InputLocation original, InputLocation importedFrom) {
this.lineNumber = original.lineNumber;
this.columnNumber = original.columnNumber;
this.source = original.source;
this.locations = original.locations;
this.importedFrom = importedFrom;
}

public int getLineNumber() {
Expand All @@ -83,6 +95,13 @@ public Map<Object, InputLocation> getLocations() {
return locations;
}

/**
* Gets the input location that caused this model to be read.
*/
public InputLocation getImportedFrom() {
return importedFrom;
}

/**
* Merges the {@code source} location into the {@code target} location.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@

public interface InputLocationTracker {
InputLocation getLocation(Object field);

InputLocation getImportedFrom();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ 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 = modelId;
this.location = location;
this.inputs = null;
this.importedFrom = null;
}

private 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;
}

/**
Expand All @@ -64,6 +74,14 @@ public String getModelId() {
return this.modelId;
}

public InputLocation getImportedFrom() {
return importedFrom;
}

public InputSource importedFrom(InputLocation importedFrom) {
return new InputSource(modelId, location, importedFrom);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,15 @@ private void importDependencyManagement(
importMgmts = new ArrayList<>();
}

importMgmts.add(importMgmt.getDelegate());
if (request.isLocationTracking()) {
// Keep track of why this DependencyManagement was imported.
importMgmts.add(
org.apache.maven.api.model.DependencyManagement.newBuilder(importMgmt.getDelegate(), true)
.importedFrom(dependency.getDelegate().getLocation(""))
.build());
} else {
importMgmts.add(importMgmt.getDelegate());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.*;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblemCollector;

Expand Down Expand Up @@ -62,11 +58,59 @@ public Model importManagement(
for (Dependency dependency : source.getDependencies()) {
String key = dependency.getManagementKey();
dependencies.putIfAbsent(key, dependency);

if (request.isLocationTracking()) {
Dependency updatedDependency = updateWithImportedFrom(dependency, source);
dependencies.put(key, updatedDependency);
}
}
}

return target.withDependencyManagement(depMgmt.withDependencies(dependencies.values()));
}
return target;
}

static Dependency updateWithImportedFrom(Dependency dependency, DependencyManagement bom) {
// We are only interested in the InputSource, so the location of the <dependency> element is sufficient
InputLocation dependencyLocation = dependency.getLocation("");
InputLocation bomLocation = bom.getLocation("");

if (dependencyLocation == null || bomLocation == null) {
return dependency;
}

InputSource dependencySource = dependencyLocation.getSource();
InputSource bomSource = bomLocation.getSource();

// If the dependency and BOM have the same source, it means we found the root where the dependency is declared.
if (dependencySource == null
|| bomSource == null
|| Objects.equals(dependencySource.getModelId(), bomSource.getModelId())) {
return Dependency.newBuilder(dependency, true)
.importedFrom(bomLocation)
.build();
}

// TODO: determine function of the following code
while (dependencySource.getImportedFrom() != null) {
InputLocation importedFrom = dependencySource.getImportedFrom();

// Stop if the BOM is already in the list, no update necessary
if (Objects.equals(importedFrom.getSource().getModelId(), bomSource.getModelId())) {
return dependency;
}

dependencySource = importedFrom.getSource();
}

// We modify the input location that is used for the whole file.
// This is likely correct because the POM hierarchy applies to the whole POM, not just one dependency.
// TODO What to do now?!

// Create copy of bomLocation and set importedFrom with the value of dependency.getimportedFrom()
return Dependency.newBuilder(dependency, true)
.importedFrom(new InputLocation(bomLocation, dependency.getImportedFrom()))
.build();
}
}
27 changes: 24 additions & 3 deletions src/mdo/model.vm
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class ${class.name}
#if ( ! $class.superClass )
/** Locations */
final Map<Object, InputLocation> locations;
final InputLocation importedFrom;
#end
#end

Expand All @@ -151,7 +152,8 @@ public class ${class.name}
$type $field.name${sep}
#end
#if ( $locationTracking )
Map<Object, InputLocation> locations
Map<Object, InputLocation> locations,
InputLocation importedFrom
#end
) {
#if ( $class.superClass )
Expand All @@ -161,7 +163,8 @@ public class ${class.name}
${field.name}${sep}
#end
#if ( $locationTracking )
locations
locations,
importedFrom
#end
);
#end
Expand All @@ -179,6 +182,7 @@ public class ${class.name}
#if ( $locationTracking )
#if ( ! $class.superClass )
this.locations = ImmutableCollections.copy(locations);
this.importedFrom = importedFrom;
#end
#end
}
Expand Down Expand Up @@ -244,6 +248,14 @@ public class ${class.name}
return locations != null ? locations.get(key) : null;
}

/**
* Gets the input location that caused this model to be read.
*/
public InputLocation getImportedFrom()
{
return importedFrom;
}

#end
/**
* Creates a new builder with this object as the basis.
Expand Down Expand Up @@ -374,6 +386,7 @@ public class ${class.name}
#end
#if ( ! $class.superClass && $locationTracking )
Map<Object, InputLocation> locations;
InputLocation importedFrom;
#end

Builder(boolean withDefaults) {
Expand Down Expand Up @@ -408,6 +421,7 @@ public class ${class.name}
#end
#if ( $locationTracking )
this.locations = base.locations;
this.importedFrom = base.importedFrom;
#end
} else {
this.base = base;
Expand Down Expand Up @@ -453,6 +467,12 @@ public class ${class.name}
return this;
}

@Nonnull
public Builder importedFrom(InputLocation importedFrom) {
this.importedFrom = importedFrom;
return this;
}

#end
@Nonnull
public ${class.name} build() {
Expand Down Expand Up @@ -486,7 +506,8 @@ public class ${class.name}
#end
#end
#if ( $locationTracking )
locations
locations,
importedFrom
#end
);
}
Expand Down

0 comments on commit da5185e

Please sign in to comment.