Skip to content

Commit

Permalink
DependencyStringNotationConverter to static utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Apr 24, 2023
1 parent 6886dee commit b712e02
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (dependencyDslMatcher.matches(m) && (StringUtils.isBlank(configuration) || configuration.equals(m.getSimpleName()))) {
if (m.getArguments().get(0) instanceof J.Literal) {
//noinspection ConstantConditions
Dependency dependency = new DependencyStringNotationConverter().parse((String) ((J.Literal) m.getArguments().get(0)).getValue());
Dependency dependency = DependencyStringNotationConverter.parse((String) ((J.Literal) m.getArguments().get(0)).getValue());
if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) {
getCursor().putMessageOnFirstEnclosing(G.CompilationUnit.class, DEPENDENCY_PRESENT, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext conte
if (depArgs.get(0) instanceof J.Literal) {
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency dependency = new DependencyStringNotationConverter().parse(gav);
Dependency dependency = DependencyStringNotationConverter.parse(gav);
if (!newArtifactId.equals(dependency.getArtifactId()) &&
((dependency.getVersion() == null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) ||
(dependency.getVersion() != null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion())))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext conte
if (depArgs.get(0) instanceof J.Literal) {
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency dependency = new DependencyStringNotationConverter().parse(gav);
Dependency dependency = DependencyStringNotationConverter.parse(gav);
if (dependency.getVersion() != null && dependency.getClassifier() != null && !newClassifier.equals(dependency.getClassifier()) &&
depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion())) {
Dependency newDependency = dependency.withClassifier(newClassifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext conte
if (depArgs.get(0) instanceof J.Literal) {
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency dependency = new DependencyStringNotationConverter().parse(gav);
Dependency dependency = DependencyStringNotationConverter.parse(gav);
if (!newExtension.equals(dependency.getExt()) &&
((dependency.getVersion() == null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) ||
(dependency.getVersion() != null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion())))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext conte
if (depArgs.get(0) instanceof J.Literal) {
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency dependency = new DependencyStringNotationConverter().parse(gav);
Dependency dependency = DependencyStringNotationConverter.parse(gav);
if (!newGroupId.equals(dependency.getGroupId()) &&
((dependency.getVersion() == null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) ||
(dependency.getVersion() != null && depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion())))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext conte
if (depArgs.get(0) instanceof J.Literal) {
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency dependency = new DependencyStringNotationConverter().parse(gav);
Dependency dependency = DependencyStringNotationConverter.parse(gav);
if (dependency.getVersion() != null && !newVersion.equals(dependency.getVersion()) &&
depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion())) {
Dependency newDependency = dependency.withVersion(newVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private J.MethodInvocation forBasicString(J.MethodInvocation m) {
if (dependencyString == null) {
return m;
}
Dependency dependency = new DependencyStringNotationConverter().parse(dependencyString);
Dependency dependency = DependencyStringNotationConverter.parse(dependencyString);
List<Expression> arguments = new ArrayList<>();
arguments.add(mapEntry("group", dependency.getGroupId())
.withMarkers(arg.getMarkers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
J.Literal stringLiteralArgument = (J.Literal) declArguments.get(0);
String argumentValue = (String) stringLiteralArgument.getValue();

Dependency dependency = new DependencyStringNotationConverter().parse(argumentValue);
Dependency dependency = DependencyStringNotationConverter.parse(argumentValue);

if (depMatcher.matches(dependency.getGroupId(), dependency.getArtifactId())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Statement getBeforeDependency() {

private static Optional<String> getEntry(String entry, J.MethodInvocation invocation) {
if (invocation.getArguments().get(0) instanceof J.Literal) {
Dependency dependency = new DependencyStringNotationConverter().parse((String) ((J.Literal) invocation.getArguments().get(0)).getValue());
Dependency dependency = DependencyStringNotationConverter.parse((String) ((J.Literal) invocation.getArguments().get(0)).getValue());
switch (entry) {
case "group":
return Optional.ofNullable(dependency.getGroupId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.With;
import org.openrewrite.internal.lang.Nullable;

@Value
@With
@EqualsAndHashCode
public class Dependency {
String groupId;
String artifactId;

@Nullable
String version;

@Nullable
String classifier;

@Nullable
String ext;

public String toStringNotation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@
*/
package org.openrewrite.gradle.util;

import org.openrewrite.internal.lang.Nullable;

public class DependencyStringNotationConverter {
public Dependency parse(String notation) {

/**
* Static utility class, no need to invoke this constructor. Temporarily maintaining for backwards compatibility.
*/
@Deprecated
public DependencyStringNotationConverter() {
}

public static Dependency parse(String notation) {
int idx = notation.lastIndexOf('@');
if (idx == -1) {
return parse(notation, null);
Expand All @@ -30,7 +40,7 @@ public Dependency parse(String notation) {
return parse(notation, null);
}

private Dependency parse(String notation, String ext) {
private static Dependency parse(String notation, @Nullable String ext) {
Dependency dependency = new Dependency(null, null, null, null, ext);

int count = 0;
Expand All @@ -54,7 +64,7 @@ private Dependency parse(String notation, String ext) {
return dependency;
}

private Dependency assignValue(Dependency dependency, int count, String fragment) {
private static Dependency assignValue(Dependency dependency, int count, String fragment) {
switch (count) {
case 0:
return dependency.withGroupId(fragment);
Expand Down

0 comments on commit b712e02

Please sign in to comment.