Class GradleDependency

java.lang.Object
org.openrewrite.gradle.trait.GradleDependency
All Implemented Interfaces:
org.openrewrite.trait.Trait<org.openrewrite.java.tree.J.MethodInvocation>

public class GradleDependency extends Object implements org.openrewrite.trait.Trait<org.openrewrite.java.tree.J.MethodInvocation>
  • Constructor Details

    • GradleDependency

      public GradleDependency()
  • Method Details

    • isDependencyDeclaration

      public static boolean isDependencyDeclaration(org.openrewrite.Cursor cursor)
    • getGroupId

      public String getGroupId()
      Gets the resolved group ID of the dependency after Gradle's dependency resolution.
      Returns:
      The resolved group ID (e.g., "com.google.guava" from "com.google.guava:guava:VERSION")
    • getArtifactId

      public String getArtifactId()
      Gets the resolved artifact ID of the dependency after Gradle's dependency resolution.
      Returns:
      The resolved artifact ID (e.g., "guava" from "com.google.guava:guava:VERSION")
    • getVersion

      public String getVersion()
      Gets the resolved version of the dependency
      Returns:
      the resolved version of the dependency, after any variable substitutions have taken place
    • getGav

      public org.openrewrite.maven.tree.GroupArtifactVersion getGav()
    • getResolvedGav

      public org.openrewrite.maven.tree.ResolvedGroupArtifactVersion getResolvedGav()
    • getConfigurationName

      public String getConfigurationName()
      Gets the configuration name for this dependency. For example, "implementation", "testImplementation", "api", etc. For platform dependencies wrapped in platform() or enforcedPlatform(), returns the configuration name of the outer method invocation.
      Returns:
      The configuration name
    • getDeclaredGroupId

      public @Nullable String getDeclaredGroupId()
      Gets the declared group ID from the dependency declaration. This may be a literal value or a variable name/expression. For platform dependencies, automatically looks at the inner dependency.
      Returns:
      The declared group ID string, or null if it cannot be determined
    • getDeclaredArtifactId

      public @Nullable String getDeclaredArtifactId()
      Gets the declared artifact ID from the dependency declaration. This may be a literal value or a variable name/expression. For platform dependencies, automatically looks at the inner dependency.
      Returns:
      The declared artifact ID string, or null if it cannot be determined
    • getDeclaredVersion

      public @Nullable String getDeclaredVersion()
      Gets the declared version from the dependency declaration. This may be a literal value or a variable name/expression. For platform dependencies, automatically looks at the inner dependency.
      Returns:
      The declared version string (literal or variable name), or null if it cannot be determined
    • isPlatform

      public boolean isPlatform()
      Checks if this dependency declaration uses platform() or enforcedPlatform().
      Returns:
      true if the dependency is wrapped in platform() or enforcedPlatform()
    • getPlatformDependency

      public @Nullable GradleDependency getPlatformDependency()
      Gets the inner dependency if this dependency uses platform() or enforcedPlatform(). For example, for implementation platform("com.example:bom:1.0"), this returns the GradleDependency representing platform("com.example:bom:1.0").
      Returns:
      The inner GradleDependency if this is a platform dependency, or null otherwise
    • getVersionVariable

      public @Nullable String getVersionVariable()
      Gets the version variable name if the dependency's version is specified via a variable reference rather than a literal value. Handles various dependency notation formats. If this dependency uses platform() or enforcedPlatform(), this method automatically looks at the inner dependency declaration.
      Returns:
      The variable name used for the version, or null if the version is a literal or cannot be determined
    • matches

      public boolean matches(org.openrewrite.semver.DependencyMatcher matcher)
      Checks if this dependency matches the given DependencyMatcher. This is a convenience method that extracts the declared group and artifact IDs and passes them to the matcher.
      Parameters:
      matcher - The DependencyMatcher to check against
      Returns:
      true if this dependency matches the matcher's patterns
    • removeVersion

      public GradleDependency removeVersion()
      Removes the version from a dependency declaration. This method handles various dependency notation formats including string literals, map literals, and map entries. If this dependency uses platform() or enforcedPlatform(), this method automatically operates on the inner dependency declaration.
      Returns:
      A new GradleDependency with the version removed from the cursor's method invocation, or the original GradleDependency if the version cannot be removed
    • withDeclaredGroupId

      public GradleDependency withDeclaredGroupId(String newGroupId)
      Updates the declared group ID of a dependency. This method handles various dependency notation formats including string literals, GStrings, map entries, map literals, assignments, and Kotlin string templates. If this dependency uses platform() or enforcedPlatform(), this method automatically operates on the inner dependency declaration.
      Parameters:
      newGroupId - The new group ID to set
      Returns:
      A new GradleDependency with the updated group ID, or the original if no change was made
    • withDeclaredArtifactId

      public GradleDependency withDeclaredArtifactId(String newArtifactId)
      Updates the declared artifact ID of a dependency. This method handles various dependency notation formats including string literals, GStrings, map entries, map literals, assignments, and Kotlin string templates. If this dependency uses platform() or enforcedPlatform(), this method automatically operates on the inner dependency declaration.
      Parameters:
      newArtifactId - The new artifact ID to set
      Returns:
      A new GradleDependency with the updated artifact ID, or the original if no change was made
    • withDeclaredVersion

      public GradleDependency withDeclaredVersion(@Nullable String newVersion)
      Updates the declared version of a dependency. This method handles various dependency notation formats including string literals, GStrings, map entries, map literals, assignments, and Kotlin string templates. If this dependency uses platform() or enforcedPlatform(), this method automatically operates on the inner dependency declaration.
      Parameters:
      newVersion - The new version to set (can be null to remove version)
      Returns:
      A new GradleDependency with the updated version, or the original if no change was made