Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NamingService with Java implementation #4725

Merged
merged 6 commits into from
Dec 3, 2024
Merged

Conversation

Laurens-W
Copy link
Contributor

What's changed?

Added a NamingService interface including a Java implementation

What's your motivation?

Working on

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

@knutwannheden @OlegDokuka @timtebeek

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

…mingService is requested from a JavaSourceFile
@Laurens-W Laurens-W added the enhancement New feature or request label Nov 27, 2024
@Laurens-W Laurens-W self-assigned this Nov 27, 2024
Laurens-W and others added 2 commits November 27, 2024 16:20
…ice.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@Laurens-W Laurens-W marked this pull request as ready for review November 28, 2024 15:17
@timtebeek
Copy link
Contributor

I figured explore what usage would look like through a test, and figured point out a few things that might influence the design:

Usage in visitor

Here's the bare bones visitor I came up with; mostly call the service to potentially get a new name (no guarantees it's actually different), and then delegate the actual work. API seems to mostly work, provided with expect folks to delegate the work of actually renaming, as otherwise you end up having the change the name.simpleName & name.type & methodType, which gets awkward.

@Override
public void defaults(RecipeSpec spec) {
    spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
        @Override
        public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
            J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
            String oldName = md.getName().getSimpleName();
            String newName = service(NamingService.class).standardizeMethodName(oldName);
            if (!oldName.equals(newName)) {
                doAfterVisit(new ChangeMethodName(MethodMatcher.methodPattern(md), newName, true, false).getVisitor());
            }
            return md;
        }
    }));
}

Cases I'd expect to change

Here's a couple quick examples that I'd then expect to be renamed predictably; we can argue what their new names should be, but it's perhaps to capture that in tests for any changes we might make.

@ParameterizedTest
@CsvSource(textBlock = """
  foo_bar,fooBar
  foo$bar,fooBar
  foo_bar$,fooBar
  foo$bar$,fooBar
  """)
void changeMethodName(String before, String after) {
    String template = """
      class A {
          void %1$s() {}
      }
      class B extends A {
          @Override
          void %1$s() {}
      }
      class C {
          void use() {
              new A().%1$s();
              new B().%1$s();
          }
      }
      """;
    rewriteRun(
      java(
        template.formatted(before),
        template.formatted(after)
      )
    );
}

In reality only the first is actually changes as expected, with some odd choices of changes for the others
image

I think it would be good to include a test like the above to capture the expected behaviour, and guard against downstream test failures if there's any updates.

@Laurens-W
Copy link
Contributor Author

Laurens-W commented Dec 2, 2024

@timtebeek I guess the initial logic of the MethodNameCasing recipe didn't take this into account but I agree that if we want to provide this NamingService generically it should be able to handle these things, I'll work on adding the test you proposed!

edit: It tried to take it into account but not properly

Laurens-W and others added 2 commits December 2, 2024 11:32
…mingServiceTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for adding this and the proposed fixes as well.

@Laurens-W Laurens-W merged commit f0b1d51 into main Dec 3, 2024
2 checks passed
@Laurens-W Laurens-W deleted the introduce-naming-service branch December 3, 2024 16:39
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
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants