TryStatement added
This commit is contained in:
@@ -85,7 +85,7 @@ public class Converter {
|
|||||||
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
|
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
|
||||||
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
||||||
final Type type = typeToType(method.getReturnType());
|
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());
|
final Element params = elementToElement(method.getParameterList());
|
||||||
|
|
||||||
if (method.isConstructor())
|
if (method.isConstructor())
|
||||||
@@ -104,10 +104,10 @@ public class Converter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Block bodyToBlock(PsiCodeBlock body, boolean notEmpty) {
|
public static Block blockToBlock(PsiCodeBlock block, boolean notEmpty) {
|
||||||
if (body == null)
|
if (block == null)
|
||||||
return new Block(new LinkedList<Statement>(), false);
|
return Block.EMPTY_BLOCK;
|
||||||
return new Block(statementsToStatementList(body.getStatements()), notEmpty);
|
return new Block(statementsToStatementList(block.getStatements()), notEmpty);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -208,6 +208,7 @@ public class Converter {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static Parameter parameterToParameter(PsiParameter parameter) {
|
public static Parameter parameterToParameter(PsiParameter parameter) {
|
||||||
return new Parameter(
|
return new Parameter(
|
||||||
new IdentifierImpl(parameter.getName()), // TODO: remove
|
new IdentifierImpl(parameter.getName()), // TODO: remove
|
||||||
|
|||||||
@@ -3,15 +3,20 @@ package org.jetbrains.jet.j2k.ast;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class Block extends Statement {
|
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 boolean myNotEmpty = false;
|
||||||
|
|
||||||
|
private Block() {
|
||||||
|
}
|
||||||
|
|
||||||
public Block(List<Statement> statements) {
|
public Block(List<Statement> statements) {
|
||||||
myStatements = statements;
|
myStatements = statements;
|
||||||
}
|
}
|
||||||
@@ -21,7 +26,7 @@ public class Block extends Statement {
|
|||||||
myNotEmpty = notEmpty;
|
myNotEmpty = notEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return !myNotEmpty && myStatements.size() == 0;
|
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
|
@Override
|
||||||
public void visitTryStatement(PsiTryStatement statement) {
|
public void visitTryStatement(PsiTryStatement statement) {
|
||||||
super.visitTryStatement(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
|
@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