-
Notifications
You must be signed in to change notification settings - Fork 357
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
Changes from 6 commits
45ca436
b3b9af9
b7136c3
0e99dd8
2465404
6b36e7b
e1cadab
dc54e9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,9 +67,17 @@ public String template(Cursor cursor, String template, Space.Location location, | |
after.append('}'); | ||
} | ||
|
||
template(next(cursor), cursor.getValue(), before, after, cursor.getValue(), mode); | ||
if (contextSensitive) { | ||
contextTemplate(next(cursor), cursor.getValue(), before, after, cursor.getValue(), mode); | ||
} else { | ||
contextFreeTemplate(next(cursor), cursor.getValue(), before, after); | ||
} | ||
|
||
return before.toString().trim() + "\n/*" + TEMPLATE_COMMENT + "*/" + template + "/*" + STOP_COMMENT + "*/" + "\n" + after.toString().trim(); | ||
return before.toString().trim() + | ||
"\n/*" + TEMPLATE_COMMENT + "*/" + | ||
template + | ||
"/*" + STOP_COMMENT + "*/\n" + | ||
after.toString().trim(); | ||
}); | ||
} | ||
|
||
|
@@ -84,7 +92,8 @@ public <J2 extends J> List<J2> listTemplatedTrees(JavaSourceFile cu, Class<J2> e | |
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Integer integer) { | ||
if (getCursor().getParentTreeCursor().getValue() instanceof SourceFile && (classDecl.getSimpleName().equals("__P__") || classDecl.getSimpleName().equals("__M__"))) { | ||
if (getCursor().getParentTreeCursor().getValue() instanceof SourceFile && | ||
(classDecl.getSimpleName().equals("__P__") || classDecl.getSimpleName().equals("__M__"))) { | ||
// don't visit the __P__ and __M__ classes declaring stubs | ||
return classDecl; | ||
} | ||
|
@@ -193,14 +202,6 @@ private boolean isTemplateStopComment(Comment comment) { | |
return js; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
Comment on lines
-196
to
-202
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
@SuppressWarnings("DataFlowIssue") | ||
protected void contextFreeTemplate(Cursor cursor, J j, StringBuilder before, StringBuilder after) { | ||
if (j instanceof J.Lambda) { | ||
|
@@ -774,11 +775,13 @@ private static class RemoveTreeMarker implements Marker { | |
|
||
private static class TemplatedTreeTrimmerVisitor extends JavaVisitor<Integer> { | ||
private boolean stopCommentExists(@Nullable J j) { | ||
if (j != null) { | ||
for (Comment comment : j.getComments()) { | ||
if (comment instanceof TextComment && ((TextComment) comment).getText().equals(STOP_COMMENT)) { | ||
return true; | ||
} | ||
return j != null && stopCommentExists(j.getComments()); | ||
} | ||
|
||
private static boolean stopCommentExists(List<Comment> comments) { | ||
for (Comment comment : comments) { | ||
if (comment instanceof TextComment && ((TextComment) comment).getText().equals(STOP_COMMENT)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
|
@@ -803,6 +806,13 @@ public J visitMethodInvocation(J.MethodInvocation method, Integer integer) { | |
//noinspection ConstantConditions | ||
return mi.getSelect(); | ||
} | ||
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(); | ||
} | ||
} | ||
Comment on lines
+809
to
+815
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return mi; | ||
} | ||
} | ||
|
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.
Discovered while debugging the below; figured helpful for folks to see preceding comments too.