Skip to content

Conversation

MBoegers
Copy link
Contributor

@MBoegers MBoegers commented Jun 12, 2025

What's changed?

Added a recipe to remove Traits static method usage in favor of the actual constructor calls.

What's your motivation?

The usage of central Traits classes for every language makes it easier to discover traits, but if more than one type of trait is used, name clashes are the result.
It indicates that the introduction of Traits as central factory was to early and therefore shold be rolled back now as long as the adoption is not that high.
This recipe provided the migration to adapt to this rollback.

Example of a clash for example in Dependency migrations

org.openrewrite.maven.trait.Traits.mavenDependency()
org.openrewrite.gradle.trait.Traits.gradleDependency()
// alternative with normal imports
new MavenDependency.Matcher()
new GradleDependency.Matcher()

Any additional context

removal sugested by @jkschneider

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@MBoegers MBoegers self-assigned this Jun 12, 2025
@MBoegers MBoegers added the enhancement New feature or request label Jun 12, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jun 12, 2025
@MBoegers MBoegers moved this from In Progress to Ready to Review in OpenRewrite Jun 12, 2025
@timtebeek
Copy link
Member

Let's add RemoveTraitsUsageRecipes to the best practices we enforce on our PRs as well:

name: org.openrewrite.java.recipes.JavaRecipeBestPractices
displayName: Java Recipe best practices
description: Best practices for Java recipe development.
preconditions:
- org.openrewrite.java.search.FindTypes:
fullyQualifiedTypeName: org.openrewrite.Recipe
checkAssignability: true
recipeList:

@MBoegers MBoegers requested review from Copilot and timtebeek June 13, 2025 09:07
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request removes the use of factory methods in centralized Traits classes in favor of using direct constructor calls, addressing name clash issues and streamlining the API usage.

  • Added a migration recipe for Java Traits usage.
  • Updated best practice recipe configuration to include the new migration recipe.
  • Added unit tests for validating the migration behavior.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/test/java/org/openrewrite/java/recipes/migrate/RemoveTraitsUsageTest.java Added tests to validate migration from Traits factory usage to constructor calls.
src/main/resources/META-INF/rewrite/recipebestpractice.yml Extended best practice recipe list with the new RemoveTraitsUsage recipe.
src/main/java/org/openrewrite/java/recipes/migrate/RemoveTraitsUsage.java Implemented recipes to replace static Traits calls with corresponding constructor calls for Java, Maven, and Gradle.
Comments suppressed due to low confidence (1)

src/main/java/org/openrewrite/java/recipes/migrate/RemoveTraitsUsage.java:199

  • The description references 'org.openrewrite.java.gradle.Traits' while the code calls 'org.openrewrite.gradle.trait.Traits'. Please update the description to match the actual package used.
description = "Removes the usage of static `org.openrewrite.java.gradle.Traits` class and replace with corresponding constructor calls."

@timtebeek timtebeek merged commit 1f1f8c5 into main Jun 13, 2025
2 checks passed
@timtebeek timtebeek deleted the remove-traits-class branch June 13, 2025 10:23
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants