eclipse-jdt / eclipse-jdt/eclipse.jdt.core
ASTParser cannot keep elements/nodes after a lambda expression (without a block)
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
The issue is best to explained with an running code sample here:
```java
String source = """
package whatever;
import java.util.function.*;
public class C {
public void execute() {
int j = 1;
// Function f1 = s1 -> {return "good";}; // works
Function f1 = s1 -> "good"; // not working, combined with issue point blow
int i1 = 0;
i; // issue point
}
}
""";
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_11, options);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray());
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setResolveBindings(true);
parser.setUnitName("C.java");
parser.setEnvironment(new String[]{}, new String[]{}, new String[]{}, true);
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
System.out.println(compilationUnit);
compilationUnit.accept(
new ASTVisitor() {
@Override
public boolean visit(VariableDeclarationFragment node) {
IVariableBinding iVariableBinding = node.resolveBinding();
if (iVariableBinding != null) {
ITypeBinding type = iVariableBinding.getType();
System.out.println(iVariableBinding.getName() + ", type: " + type.getQualifiedName());
}
return super.visit(node);
}
}
);
```
The output is:
```
package whatever;
import java.util.function.*;
public class C {
public void execute(){
int j=1;
Function f1=s1 -> "good";
}
}
j, type: int
f1, type: java.util.function.Function
s1, type: java.lang.String
```
**Note that** the expected element of `i1` in the compilationUnit print or visit print is missing.
### Expected output should be like:
```
package whatever;
import java.util.function.*;
public class C {
public void execute(){
int j=1;
Function f1=s1 -> "good";
int i1=0;
i;
}
}
j, type: int
f1, type: java.util.function.Function
s1, type: java.lang.String
i1, type: int
```
**Note that** this expected output can be seem by simply switching the line of
```java
Function f1 = s1 -> "good";
```
to
```java
Function f1 = s1 -> {return "good";};
```
I would assume that it should not make any differences for the `ASTParser` when the lambda expression is written as `s1 -> "good"` or `s1 -> {return "good";}`?
Thank you.
Contributor guide
Assessment
This issue has not been assessed yet.