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

Generate uses type and uses method #24

Merged
merged 12 commits into from
Aug 18, 2023
Merged

Conversation

timtebeek
Copy link
Contributor

What's changed?

Generate Preconditions based on imports and static imports used in before templates.

What's your motivation?

Speed up recipe execution.
Fixes #23

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

Would we want to detect additional methods now already?
Figured this was good enough to start to limit recipe execution.

Have you considered any alternatives or workarounds?

We could add a new detector specific for methods used in the before templates, but that's perhaps more work we can do later.

Any additional context

@timtebeek timtebeek self-assigned this Aug 17, 2023
@timtebeek timtebeek added the enhancement New feature or request label Aug 17, 2023
@@ -100,7 +106,47 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
}

};
return Preconditions.check(
new UsesType<>("java.util.Objects", false),
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity: Why is there no UsesMethod for Objects#equals() here? I would have expected the precondition to check for either Objects#equals() or Integer#compare().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently those would only be generated if we use a static import of those methods; we can expand that of course, but figured this first implementation already limits the execution of the recipes to recipes using the type, which at scale already trims it down quite a bit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation. Yes, also covering the others would indeed make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

};
return Preconditions.check(
Preconditions.or(
Preconditions.and(new UsesType<>("java.util.HashMap", false), new UsesType<>("java.util.Map", false)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Since Map is not necessarily directly referenced by the input source, I think we should either leave it out or use the overload with the includeImplicit parameter. Otherwise the precondition may be too strict. For example:

Collection m = new HashMap();

Copy link
Contributor

Choose a reason for hiding this comment

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

That being said, I am not 100% sure if Refaster would match this. But I think it would.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm; we don't differentiate between in and output types when we detect imports right now. Would passing true to usesType e7d0883 be enough to reduce your concern here?

};
return Preconditions.check(
Preconditions.or(
Preconditions.and(new UsesType<>("java.util.HashMap", false), new UsesType<>("java.util.Map", false)),
Copy link
Contributor

Choose a reason for hiding this comment

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

That being said, I am not 100% sure if Refaster would match this. But I think it would.

};
return Preconditions.check(
Preconditions.or(
Preconditions.and(new UsesType<>("java.util.HashMap", true), new UsesType<>("java.util.Map", true)),
Copy link
Contributor

Choose a reason for hiding this comment

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

For HashMap we could actually pass the second argument as true, because it must be directly referenced. Map is different, because you got that from the template's return type and not the template body. But we can still fix this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Captures as a note here: #26 (comment)

@timtebeek timtebeek merged commit 4f89462 into main Aug 18, 2023
@timtebeek timtebeek deleted the generate_UsesType_and_UsesMethod branch August 18, 2023 10:28
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.

Generate UsesType and UsesMethod to limit recipe execution and improve performance
2 participants