io.codemodder.plugins.maven.DefaultArtifactInjectionPositionFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codemodder-plugin-maven Show documentation
Show all versions of codemodder-plugin-maven Show documentation
Plugin for providing Maven dependency management functions to codemods.
package io.codemodder.plugins.maven;
import com.github.difflib.patch.AbstractDelta;
import java.util.List;
final class DefaultArtifactInjectionPositionFinder implements ArtifactInjectionPositionFinder {
@Override
public int find(final List> deltas, final String artifactId) {
String expectedArtifactToken = "" + artifactId + " ";
Integer backupPosition = null;
for (AbstractDelta delta : deltas) {
List newLines = delta.getTarget().getLines();
boolean hasArtifactId =
newLines.stream().anyMatch(line -> line.contains(expectedArtifactToken));
boolean hasVersion = newLines.stream().anyMatch(line -> line.contains(""));
if (hasArtifactId && hasVersion) {
// if it has the artifact and version, it could be the section, which
// is 2nd preference
backupPosition = 1 + delta.getTarget().getPosition();
} else if (hasArtifactId) {
// if it has the artifact but not the version, it could be the section, which
// is preferred
return 1 + delta.getTarget().getPosition();
}
}
if (backupPosition != null) {
return backupPosition;
}
// the fallback is to just use the first change to the file
return 1 + deltas.get(0).getTarget().getPosition();
}
}