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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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.

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: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants