Skip to content

Conversation

timtebeek
Copy link
Member

@timtebeek timtebeek commented Aug 22, 2025

What's changed?

Add an annotation and associated recipe, which supports any recipe with the same structure: our own, Google's or a library specific one, much like @Nullable can come from a variety of libraries

What's your motivation?

Allow folks to easily mark methods for replacement with an argument template. We have these use cases internally for our libraries any moment we expand a constructor with a new nullable argument, or when deprecating a method with a clear one to one replacement using (some of) the same arguments.

It's already used in libraries like Guava, where we can now do such replacements just from having the library on the classpath.

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

Would we want to support and promote this going forward?

Have you considered any alternatives or workarounds?

  • Instead of @InlineMe I've considered @ReplaceWith, to more clearly state that it's not just for inlining. We can optionally support both.

Any additional context

@timtebeek timtebeek self-assigned this Aug 22, 2025
.contextSensitive()
.imports(values.getImports().toArray(new String[0]))
.staticImports(values.getStaticImports().toArray(new String[0]))
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
Copy link
Member Author

Choose a reason for hiding this comment

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

This runtimeClasspath is the only part I'm not quite pleased with, as what's on the runtime classpath might differ in certain environments. Still I wouldn't immediately know of a good alternative here, and we've already seen the recipe work in practice on some projects.

Copy link
Member

Choose a reason for hiding this comment

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

I think for now runtimeClasspath() is as good as you're going to get

@timtebeek timtebeek added the recipe Requested Recipe label Aug 22, 2025
@timtebeek timtebeek marked this pull request as ready for review August 23, 2025 07:55
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Aug 23, 2025
}
List<String> parameterNames = methodType.getParameterNames();
if (!parameterNames.isEmpty() && "arg0".equals(parameterNames.get(0))) {
return null; // We need `-parameters` before we're able to substitute parameters in the template
Copy link
Member

Choose a reason for hiding this comment

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

This feels like something we could always enable in JavaParser if it isn't by default

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's here mostly as a safeguard; it should be set in each of the Java parsers we have:

Options.instance(context).put("-parameters", "true");

@timtebeek timtebeek merged commit 4c84462 into main Aug 26, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Aug 26, 2025
@timtebeek timtebeek deleted the add-inlineme-annotation-and-recipe branch August 26, 2025 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Requested Recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants