Adds VM test that verifies that exceptions are caught

This commit is contained in:
konsoletyper 2014-02-26 17:14:08 +04:00
parent 62686878c6
commit ea3e6adb64
5 changed files with 32 additions and 11 deletions

View File

@ -58,4 +58,13 @@ public class VMTest {
long b = 1836311903;
assertEquals(2971215073L, a + b);
}
@Test
public void catchesException() {
try {
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
// do nothing
}
}
}

View File

@ -53,6 +53,10 @@ class ReadWriteStatsBuilder {
reads[incoming.getValue().getIndex()]++;
}
}
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
writes[tryCatch.getExceptionVariable().getIndex()]++;
reads[tryCatch.getExceptionVariable().getIndex()]++;
}
}
}
}

View File

@ -1264,16 +1264,17 @@ public class Renderer implements ExprVisitor, StatementVisitor {
for (Statement part : protectedBody) {
part.acceptVisitor(this);
}
String var = variableName(statement.getExceptionVariable());
writer.outdent().append("}").ws().append("catch").ws().append("(").append(var).append(")")
writer.outdent().append("}").ws().append("catch").ws().append("($e)")
.ws().append("{").indent().softNewLine();
writer.append("$je").ws().append("=").ws().append("$e.$javaException;").softNewLine();
for (TryCatchStatement catchClause : sequence) {
writer.append("if").ws().append("(").append(var).append(" instanceof ")
.appendClass(catchClause.getExceptionType()).append(")").ws().append("{")
.indent().softNewLine();
writer.append("if").ws().append("($je && $je instanceof ").appendClass(catchClause.getExceptionType())
.append(")").ws().append("{").indent().softNewLine();
if (statement.getExceptionVariable() != catchClause.getExceptionVariable()) {
writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws()
.append(var).append(";").softNewLine();
if (catchClause.getExceptionVariable() != null) {
writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws()
.append("$e;").softNewLine();
}
}
for (Statement part : statement.getHandler()) {
part.acceptVisitor(this);
@ -1281,7 +1282,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
writer.outdent().append("}").ws().append("else ");
}
writer.append("{").indent().softNewLine();
writer.append("throw ").append(var).append(";").softNewLine();
writer.append("throw $e;").softNewLine();
writer.outdent().append("}").softNewLine();
writer.outdent().append("}").softNewLine();
} catch (IOException e) {

View File

@ -224,5 +224,12 @@ class UnusedVariableEliminator implements ExprVisitor, StatementVisitor {
for (Statement part : statement.getHandler()) {
part.acceptVisitor(this);
}
if (statement.getExceptionVariable() != null) {
if (variables[statement.getExceptionVariable()] < 0) {
statement.setExceptionVariable(null);
} else {
statement.setExceptionVariable(renumber(statement.getExceptionVariable()));
}
}
}
}

View File

@ -26,7 +26,7 @@ public class TryCatchStatement extends Statement {
private List<Statement> protectedBody = new ArrayList<>();
private List<Statement> handler = new ArrayList<>();
private String exceptionType;
private int exceptionVariable;
private Integer exceptionVariable;
public List<Statement> getProtectedBody() {
return protectedBody;
@ -44,11 +44,11 @@ public class TryCatchStatement extends Statement {
this.exceptionType = exceptionType;
}
public int getExceptionVariable() {
public Integer getExceptionVariable() {
return exceptionVariable;
}
public void setExceptionVariable(int exceptionVariable) {
public void setExceptionVariable(Integer exceptionVariable) {
this.exceptionVariable = exceptionVariable;
}