TryStatement added
This commit is contained in:
@@ -85,7 +85,7 @@ public class Converter {
|
||||
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
|
||||
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
||||
final Type type = typeToType(method.getReturnType());
|
||||
final Block body = bodyToBlock(method.getBody(), notEmpty);
|
||||
final Block body = blockToBlock(method.getBody(), notEmpty);
|
||||
final Element params = elementToElement(method.getParameterList());
|
||||
|
||||
if (method.isConstructor())
|
||||
@@ -104,10 +104,10 @@ public class Converter {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Block bodyToBlock(PsiCodeBlock body, boolean notEmpty) {
|
||||
if (body == null)
|
||||
return new Block(new LinkedList<Statement>(), false);
|
||||
return new Block(statementsToStatementList(body.getStatements()), notEmpty);
|
||||
public static Block blockToBlock(PsiCodeBlock block, boolean notEmpty) {
|
||||
if (block == null)
|
||||
return Block.EMPTY_BLOCK;
|
||||
return new Block(statementsToStatementList(block.getStatements()), notEmpty);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -208,6 +208,7 @@ public class Converter {
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Parameter parameterToParameter(PsiParameter parameter) {
|
||||
return new Parameter(
|
||||
new IdentifierImpl(parameter.getName()), // TODO: remove
|
||||
|
||||
@@ -3,15 +3,20 @@ package org.jetbrains.jet.j2k.ast;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class Block extends Statement {
|
||||
private List<Statement> myStatements;
|
||||
public final static Block EMPTY_BLOCK = new Block();
|
||||
private List<Statement> myStatements = new LinkedList<Statement>();
|
||||
private boolean myNotEmpty = false;
|
||||
|
||||
private Block() {
|
||||
}
|
||||
|
||||
public Block(List<Statement> statements) {
|
||||
myStatements = statements;
|
||||
}
|
||||
@@ -21,7 +26,7 @@ public class Block extends Statement {
|
||||
myNotEmpty = notEmpty;
|
||||
}
|
||||
|
||||
private boolean isEmpty() {
|
||||
public boolean isEmpty() {
|
||||
return !myNotEmpty && myStatements.size() == 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class CatchStatement extends Statement {
|
||||
private final Parameter myVariable;
|
||||
private final Block myBlock;
|
||||
|
||||
public CatchStatement(Parameter variable, Block block) {
|
||||
myVariable = variable;
|
||||
myBlock = block;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "catch" + SPACE + "(" + myVariable.toKotlin() + ")" + SPACE + myBlock.toKotlin();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class TryStatement extends Statement {
|
||||
private final Block myBlock;
|
||||
private final List<CatchStatement> myCatches;
|
||||
private final Block myFinallyBlock;
|
||||
|
||||
public TryStatement(Block block, List<CatchStatement> catches, Block finallyBlock) {
|
||||
myBlock = block;
|
||||
myCatches = catches;
|
||||
myFinallyBlock = finallyBlock;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "try" + N +
|
||||
myBlock.toKotlin() + N +
|
||||
AstUtil.joinNodes(myCatches, N) + N +
|
||||
(myFinallyBlock.isEmpty() ? EMPTY : "finally" + N + myFinallyBlock.toKotlin());
|
||||
}
|
||||
}
|
||||
@@ -159,6 +159,20 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
||||
@Override
|
||||
public void visitTryStatement(PsiTryStatement statement) {
|
||||
super.visitTryStatement(statement);
|
||||
|
||||
List<CatchStatement> catches = new LinkedList<CatchStatement>();
|
||||
for (int i = 0; i < statement.getCatchBlocks().length; i++) {
|
||||
catches.add(new CatchStatement(
|
||||
parameterToParameter(statement.getCatchBlockParameters()[i]),
|
||||
blockToBlock(statement.getCatchBlocks()[i], true)
|
||||
));
|
||||
}
|
||||
|
||||
myResult = new TryStatement(
|
||||
blockToBlock(statement.getTryBlock(), true),
|
||||
catches,
|
||||
blockToBlock(statement.getFinallyBlock(), true)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class TryStatementTest extends JetTestCaseBase {
|
||||
public void testEmptyTryWithTwoCatchesWithoutFinally() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin(
|
||||
"try {" +
|
||||
"} catch (Exception e) {" +
|
||||
" println(1);" +
|
||||
"} catch (IOException e) {" +
|
||||
" println(0);" +
|
||||
"}"),
|
||||
"try\n" +
|
||||
"{\n" +
|
||||
"}\n" +
|
||||
"catch (e : Exception?) {\n" +
|
||||
"println(1)\n" +
|
||||
"}\n" +
|
||||
"catch (e : IOException?) {\n" +
|
||||
"println(0)\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEmptyTryWithTwoCatchesWithEmptyFinally() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin(
|
||||
"try {" +
|
||||
"} catch (Exception e) {" +
|
||||
" println(1);" +
|
||||
"} catch (IOException e) {" +
|
||||
" println(0);" +
|
||||
"} finally {}"),
|
||||
"try\n" +
|
||||
"{\n" +
|
||||
"}\n" +
|
||||
"catch (e : Exception?) {\n" +
|
||||
"println(1)\n" +
|
||||
"}\n" +
|
||||
"catch (e : IOException?) {\n" +
|
||||
"println(0)\n" +
|
||||
"}\n" +
|
||||
"finally\n" +
|
||||
"{\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEmptyTryWithTwoCatchesWithFinally() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin(
|
||||
"try {" +
|
||||
"} catch (Exception e) {" +
|
||||
" println(1);" +
|
||||
"} catch (IOException e) {" +
|
||||
" println(0);" +
|
||||
"} finally {" +
|
||||
" println(3);" +
|
||||
"}"),
|
||||
"try\n" +
|
||||
"{\n" +
|
||||
"}\n" +
|
||||
"catch (e : Exception?) {\n" +
|
||||
"println(1)\n" +
|
||||
"}\n" +
|
||||
"catch (e : IOException?) {\n" +
|
||||
"println(0)\n" +
|
||||
"}\n" +
|
||||
"finally\n" +
|
||||
"{\n" +
|
||||
"println(3)\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testCommonCaseForTryStatement() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin(
|
||||
"try {" +
|
||||
" callMethod(params);" +
|
||||
"} catch (Exception e) {" +
|
||||
" println(1);" +
|
||||
"} catch (IOException e) {" +
|
||||
" println(0);" +
|
||||
"} finally {" +
|
||||
" println(3);" +
|
||||
"}"),
|
||||
"try\n" +
|
||||
"{\n" +
|
||||
"callMethod(params)\n" +
|
||||
"}\n" +
|
||||
"catch (e : Exception?) {\n" +
|
||||
"println(1)\n" +
|
||||
"}\n" +
|
||||
"catch (e : IOException?) {\n" +
|
||||
"println(0)\n" +
|
||||
"}\n" +
|
||||
"finally\n" +
|
||||
"{\n" +
|
||||
"println(3)\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user