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

Isolate JavaTemplate issue with generic type parameters #4372

Merged
merged 8 commits into from
Aug 11, 2024

Conversation

timtebeek
Copy link
Contributor

What's changed?

Replicate generic type issue affecting a downstream PR.

diff --git a/Foo.java b/Foo.java
index c811849..2466f30 100644
--- a/Foo.java
+++ b/Foo.java
@@ -1,7 +1,8 @@ 
 import batch.StepBuilder;
 class Foo {
     void test() {
-        new StepBuilder()
+        new StepBuilder()/*__TEMPLATE_STOP__*/
+                ./*__TEMPLATE_STOP__*/<String>method()
             .<String>method();
     }
 }

What's your motivation?

This is holding up

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

Not yet explored the fix; figured log the issue first to document the problem in isolation. Any help welcome.

Anyone you would like to review specifically?

@knutwannheden or @sambsnyd

@timtebeek
Copy link
Contributor Author

For context: the generated template in this case is

import batch.StepBuilder;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
class Foo{void test(){
/*__TEMPLATE__*/new StepBuilder()/*__TEMPLATE_STOP__*/
./*__TEMPLATE_STOP__*/<String>method();}}

@timtebeek timtebeek marked this pull request as ready for review August 11, 2024 13:16
@timtebeek timtebeek self-assigned this Aug 11, 2024
As this trips up the auto format in IntelliJ
Comment on lines +809 to +815
if (method.getTypeParameters() != null) {
// For method chains return `select` if `STOP_COMMENT` is found before `typeParameters`
if (stopCommentExists(mi.getPadding().getTypeParameters().getBefore().getComments())) {
//noinspection ConstantConditions
return mi.getSelect();
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what fixed the unit test added here, for the minimal possible impact change we could do here.

A larger change could reduce remove the __TEMPLATE_STOP__ added twice, but it's unclear what unintended side effects that might have downstream, so I went for this safer option.

Comment on lines -196 to -202
private void template(Cursor cursor, J prior, StringBuilder before, StringBuilder after, J insertionPoint, JavaCoordinates.Mode mode) {
if (contextSensitive) {
contextTemplate(cursor, prior, before, after, insertionPoint, mode);
} else {
contextFreeTemplate(cursor, prior, before, after);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inlined above for simplicity, as this was only used once and gave a confusion indirection with unused arguments.

@@ -174,7 +172,8 @@ private static String printTreeElement(Tree tree) {
return s != null ? s : "";
}

String[] lines = tree.toString().split("\n");
String precedingComments = tree instanceof J ? printComments(((J) tree).getPrefix().getComments()) : "";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discovered while debugging the below; figured helpful for folks to see preceding comments too.

@timtebeek
Copy link
Contributor Author

timtebeek commented Aug 11, 2024

Merging already given how this is guarded to only affect cases that would have previously failed, to continue work on

@timtebeek timtebeek merged commit eeb43a8 into main Aug 11, 2024
2 checks passed
@timtebeek timtebeek deleted the java-template-issue-with-generic-type-parameters branch August 11, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant