-
Notifications
You must be signed in to change notification settings - Fork 424
Add expression type in JavaTemplate #5384
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
Conversation
rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateGenericsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateGenericsTest.java
Outdated
Show resolved
Hide resolved
Thanks again! Looking at the overlap in changes I think this PR should go first, right? |
Yes, that should go first. It adds some of the functionality removed in #5394. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeUtilsTest.java
- lines 18-18
rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeUtilsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeUtilsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-tck/src/main/java/org/openrewrite/java/tree/TypeUtilsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateMatchTest.java
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/JavaTemplate.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateGenericsTest.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
@Test | ||
@ExpectedToFail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one it's more to showcase a scenario we are not handling, even if the apply had arguments, T is not really a parameter, and #{type:any(T)}
would not match a JavaType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have some other kind of argument. #{type(T)}
where instead of using __P__.<T>any()
we could add the TypeUtils.toString()
that would also make the test above pass, being an argument the containing would match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've considered doing exactly this before, but I didn't yet see a use case for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have try this templates in error-prone refaster and they work. But I haven't found any use on the Picnic tests.
static final class RefToLambda<T> {
@BeforeTemplate
Function<T, String> before() {
return T::toString;
}
@AfterTemplate
Function<T, String> after() {
return e -> e.toString();
}
}
static final class LambdaToRef<T> {
@BeforeTemplate
Function<T, String> before() {
return e -> e.toString();
}
@AfterTemplate
Function<T, String> after() {
return T::toString;
}
}
---
ImmutableSet<Function<Object, String>> testRefToLambda() {
return ImmutableSet.of(
Object::toString);
}
ImmutableSet<Function<Object, String>> testLambdaToRef() {
return ImmutableSet.of(
e -> e.toString());
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember the use case now. Having a #{type()}
construct could indeed help here, but I don't yet see how we would be using that from a Refaster recipe. The problem as I see it is that there wouldn't be any corresponding parameter in the before template, so how do we correctly generate the recipe to set that parameter?
rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateGenericsTest.java
Outdated
Show resolved
Hide resolved
…ateGenericsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Thanks again @fmodesto ! We'll keep an eye on any effects downstream, but I know you've already checked that as well. 🙏🏻 |
What's changed?
Added support for explicit setting
expressionType
inJavaTemplate
Have you considered any alternatives or workarounds?
It is possible to write code that doesn't require setting the expression type.
However, this approach presents challenges:
Checklist