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

Handle method call misinterpreted as yield statement #6915

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.AnnotationMemberDeclaration;
Expand Down Expand Up @@ -750,13 +751,58 @@ public Void visitExpressionStatement(ExpressionStatementTree javacTree, Node jav
// surround explicit constructor invocations in an expression statement, we match
// javaParserNode to the javac expression rather than the javac expression statement.
javacTree.getExpression().accept(this, javaParserNode);
} else if (isYieldAndYield(javacTree, javaParserNode)) {
// There is nothing to do
} else {
throwUnexpectedNodeType(javacTree, javaParserNode);
}

return null;
}

/**
* Returns true if {@code javacTree} is a {@code yield()} method call and {@code javaParserNode}
* is a {@code yield()} statement.
*
* <p>There are methods named {@code yield()}, such as one in {@code Thread}. JavaParser parses
* every occurrence of {@code yield} as a yield statement. For example, it considers {@code
* yield();} to be {@code yield ();} which is shorthand for {@code yield ()->{};}. See
* https://github.com/javaparser/javaparser/issues/2332 .
*
* @param javacTree a javac tree
* @param javaParserNode a JavaParser node
* @return true if {@code javacTree} is a {@code yield()} method call and {@code javaParserNode}
* is a {@code yield()} statement
*/
private boolean isYieldAndYield(ExpressionStatementTree javacTree, Node javaParserNode) {
if (javacTree.getExpression().getKind() == Tree.Kind.METHOD_INVOCATION
&& javaParserNode instanceof YieldStmt) {
MethodInvocationTree javacInvok = (MethodInvocationTree) javacTree.getExpression();
ExpressionTree javacInvokMethod = javacInvok.getMethodSelect();
List<? extends ExpressionTree> javacInvokArgs = javacInvok.getArguments();
List<? extends Tree> javacInvokTypeArgs = javacInvok.getTypeArguments();
if ((javacInvokArgs.isEmpty()
&& javacInvokTypeArgs.isEmpty()
&& javacInvokMethod.getKind() == Tree.Kind.IDENTIFIER)
&& ((IdentifierTree) javacInvokMethod).getName().toString().equals("yield")) {

YieldStmt javaParserYieldStmt = (YieldStmt) javaParserNode;
Expression javaParserYieldExpression = javaParserYieldStmt.getExpression();
if (javaParserYieldExpression instanceof LambdaExpr) {
LambdaExpr javaParserLambda = (LambdaExpr) javaParserYieldExpression;
NodeList<Parameter> jpLambdaParams = javaParserLambda.getParameters();
Statement jpLambdaBody = javaParserLambda.getBody();
if (jpLambdaParams.isEmpty()
&& jpLambdaBody.isBlockStmt()
&& jpLambdaBody.asBlockStmt().getStatements().isEmpty()) {
return true;
}
}
}
}
return false;
}

@Override
public Void visitForLoop(ForLoopTree javacTree, Node javaParserNode) {
ForStmt node = castNode(ForStmt.class, javaParserNode, javacTree);
Expand Down