Skip to content

Commit

Permalink
Maybe fixes to eclipse-jdt#141, worth a shot!
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
Rob Stryker committed Mar 14, 2024
1 parent 51e887e commit d241ac8
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblem;
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;

Expand Down Expand Up @@ -917,7 +913,7 @@ private Expression convertExpression(JCExpression javac) {
.forEach(res.parameters()::add);
res.setBody(
jcLambda.getBody() instanceof JCExpression expr ? convertExpression(expr) :
jcLambda.getBody() instanceof JCStatement stmt ? convertStatement(stmt) :
jcLambda.getBody() instanceof JCStatement stmt ? convertStatement(stmt, res) :
null);
// TODO set parenthesis looking at the next non-whitespace char after the last parameter
return res;
Expand Down Expand Up @@ -1006,10 +1002,6 @@ private Expression convertLiteral(JCLiteral literal) {
throw new UnsupportedOperationException("Not supported yet " + literal + "\n of type" + literal.getClass().getName());
}

private Statement convertStatement(JCStatement javac) {
return convertStatement(javac, null);
}

private Statement convertStatement(JCStatement javac, ASTNode parent) {
if (javac instanceof JCReturn returnStatement) {
ReturnStatement res = this.ast.newReturnStatement();
Expand All @@ -1036,7 +1028,7 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
if (jcError.getErrorTrees().size() == 1) {
JCTree tree = jcError.getErrorTrees().get(0);
if (tree instanceof JCStatement nestedStmt) {
return convertStatement(nestedStmt);
return convertStatement(nestedStmt, parent);
}
} else {
Block substitute = this.ast.newBlock();
Expand Down Expand Up @@ -1082,18 +1074,25 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
if (javac instanceof JCForLoop jcForLoop) {
ForStatement res = this.ast.newForStatement();
commonSettings(res, javac);
res.setBody(convertStatement(jcForLoop.getStatement()));
jcForLoop.getInitializer().stream().map(this::convertStatementToExpression).forEach(res.initializers()::add);
res.setBody(convertStatement(jcForLoop.getStatement(), res));
Iterator initializerIt = jcForLoop.getInitializer().iterator();
while(initializerIt.hasNext()) {
res.initializers().add(convertStatementToExpression((JCStatement)initializerIt.next(), res));
}
res.setExpression(convertExpression(jcForLoop.getCondition()));
jcForLoop.getUpdate().stream().map(this::convertStatementToExpression).forEach(res.updaters()::add);

Iterator updateIt = jcForLoop.getUpdate().iterator();
while(updateIt.hasNext()) {
res.updaters().add(convertStatementToExpression((JCStatement)updateIt.next(), res));
}
return res;
}
if (javac instanceof JCEnhancedForLoop jcEnhancedForLoop) {
EnhancedForStatement res = this.ast.newEnhancedForStatement();
commonSettings(res, javac);
res.setParameter((SingleVariableDeclaration)convertVariableDeclaration(jcEnhancedForLoop.getVariable()));
res.setExpression(convertExpression(jcEnhancedForLoop.getExpression()));
res.setBody(convertStatement(jcEnhancedForLoop.getStatement()));
res.setBody(convertStatement(jcEnhancedForLoop.getStatement(), res));
return res;
}
if (javac instanceof JCBreak jcBreak) {
Expand All @@ -1114,7 +1113,7 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
stmts.add(switchCase);
stmts.addAll(switchCase.getStatements());
return stmts.stream();
}).map(this::convertStatement)
}).map(x -> convertStatement(x, res))
.forEach(res.statements()::add);
return res;
}
Expand All @@ -1130,14 +1129,14 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
WhileStatement res = this.ast.newWhileStatement();
commonSettings(res, javac);
res.setExpression(convertExpression(jcWhile.getCondition()));
res.setBody(convertStatement(jcWhile.getStatement()));
res.setBody(convertStatement(jcWhile.getStatement(), res));
return res;
}
if (javac instanceof JCDoWhileLoop jcDoWhile) {
DoStatement res = this.ast.newDoStatement();
commonSettings(res, javac);
res.setExpression(convertExpression(jcDoWhile.getCondition()));
res.setBody(convertStatement(jcDoWhile.getStatement()));
res.setBody(convertStatement(jcDoWhile.getStatement(), res));
return res;
}
if (javac instanceof JCYield jcYield) {
Expand Down Expand Up @@ -1174,11 +1173,11 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) {
throw new UnsupportedOperationException("Missing support to convert " + javac + "of type " + javac.getClass().getName());
}

private Expression convertStatementToExpression(JCStatement javac) {
private Expression convertStatementToExpression(JCStatement javac, ASTNode parent) {
if (javac instanceof JCExpressionStatement jcExpressionStatement) {
return convertExpression(jcExpressionStatement.getExpression());
}
Statement javacStatement = convertStatement(javac);
Statement javacStatement = convertStatement(javac, parent);
if (javacStatement instanceof VariableDeclarationStatement decl && decl.fragments().size() == 1) {
javacStatement.delete();
VariableDeclarationFragment fragment = (VariableDeclarationFragment)decl.fragments().get(0);
Expand All @@ -1195,7 +1194,7 @@ private Block convertBlock(JCBlock javac) {
commonSettings(res, javac);
if (javac.getStatements() != null) {
for( Iterator i = javac.getStatements().iterator(); i.hasNext();) {
Statement s = convertStatement((JCStatement)i.next());
Statement s = convertStatement((JCStatement)i.next(), res);
if( s != null ) {
res.statements().add(s);
}
Expand Down Expand Up @@ -1238,10 +1237,10 @@ private IfStatement convertIfStatement(JCIf javac) {
res.setExpression(convertExpression(javac.getCondition()));
}
if (javac.getThenStatement() != null) {
res.setThenStatement(convertStatement(javac.getThenStatement()));
res.setThenStatement(convertStatement(javac.getThenStatement(), res));
}
if (javac.getElseStatement() != null) {
res.setElseStatement(convertStatement(javac.getElseStatement()));
res.setElseStatement(convertStatement(javac.getElseStatement(), res));
}
return res;
}
Expand Down

0 comments on commit d241ac8

Please sign in to comment.