From 78d22e9f811f2fce04e2d86fedb51151ef5658b7 Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Wed, 10 Oct 2012 14:45:43 +0400 Subject: [PATCH] KT-2823 TypeCastException has no message #KT-2823 Fixed --- .../jet/codegen/ExpressionCodegen.java | 19 ++++++++++++++++--- .../testData/codegen/typeCastException.jet | 14 ++++++++++++++ .../jet/codegen/NamespaceGenTest.java | 6 +++++- runtime/src/jet/TypeCastException.java | 4 ++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/typeCastException.jet diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 981edadf48f..6682c3c4254 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -57,6 +57,7 @@ import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.resolve.DescriptorRenderer; import java.util.*; @@ -3156,7 +3157,9 @@ The "returned" value of try expression with no finally is either the last expres v.dup(); Label nonnull = new Label(); v.ifnonnull(nonnull); - throwNewException(CLASS_TYPE_CAST_EXCEPTION); + throwNewException(CLASS_TYPE_CAST_EXCEPTION, DescriptorRenderer.TEXT.renderType(leftType) + + " cannot be cast to " + + DescriptorRenderer.TEXT.renderType(rightType)); v.mark(nonnull); } } @@ -3376,10 +3379,20 @@ The "returned" value of try expression with no finally is either the last expres return false; } - private void throwNewException(final String className) { + private void throwNewException(@NotNull final String className) { + throwNewException(className, null); + } + + private void throwNewException(@NotNull final String className, @Nullable final String message) { v.anew(Type.getObjectType(className)); v.dup(); - v.invokespecial(className, "", "()V"); + if (message != null) { + v.visitLdcInsn(message); + v.invokespecial(className, "", "(Ljava/lang/String;)V"); + } + else { + v.invokespecial(className, "", "()V"); + } v.athrow(); } diff --git a/compiler/testData/codegen/typeCastException.jet b/compiler/testData/codegen/typeCastException.jet new file mode 100644 index 00000000000..e798dc710d5 --- /dev/null +++ b/compiler/testData/codegen/typeCastException.jet @@ -0,0 +1,14 @@ +// KT-2823 TypeCastException has no message + +fun box(): String { + try { + val a: Any? = null + a as Array + } + catch(e: TypeCastException) { + if (e.getMessage() == "jet.Any? cannot be cast to jet.Array") { + return "OK" + } + } + return "fail" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java index 17e5c201e4b..c7534408427 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java @@ -32,7 +32,7 @@ public class NamespaceGenTest extends CodegenTestCase { @Override protected void setUp() throws Exception { super.setUp(); - createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + createEnvironmentWithMockJdkAndIdeaAnnotations(); } public void testPSVM() throws Exception { @@ -477,6 +477,10 @@ public class NamespaceGenTest extends CodegenTestCase { blackBoxFile("checkCast.jet"); } + public void testTypeCastException() throws Exception { + blackBoxFile("typeCastException.jet"); + } + public void testPutBooleanAsVoid() throws Exception { loadText("class C(val x: Int) { { x > 0 } } fun box() { val c = C(0) } "); final Method main = generateFunction(); diff --git a/runtime/src/jet/TypeCastException.java b/runtime/src/jet/TypeCastException.java index 2b3e93f7a58..7d0a8e0e9fc 100644 --- a/runtime/src/jet/TypeCastException.java +++ b/runtime/src/jet/TypeCastException.java @@ -20,4 +20,8 @@ package jet; * @author yole */ public class TypeCastException extends ClassCastException { + + public TypeCastException(String s) { + super(s); + } }