Make sure class cast exception contains the same message as produced by JDK
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 19 Apr 2016 07:24:30 +0200
changeset 19309ca946fc4f01
parent 1929 dd63b6731507
child 1931 685193e0d793
Make sure class cast exception contains the same message as produced by JDK
rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java
rt/emul/mini/src/main/java/java/lang/Class.java
rt/emul/mini/src/main/java/java/lang/Throwable.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
     1.1 --- a/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java	Tue Apr 19 05:31:17 2016 +0200
     1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java	Tue Apr 19 07:24:30 2016 +0200
     1.3 @@ -54,6 +54,17 @@
     1.4          int newLine = s.indexOf('\n');
     1.5          return s.substring(0, newLine);
     1.6      }
     1.7 +
     1.8 +    @Compare
     1.9 +    public String classCastMessage() throws Exception {
    1.10 +        Object obj = 10;
    1.11 +        try {
    1.12 +            return (String) obj;
    1.13 +        } catch (ClassCastException ex) {
    1.14 +            assert ex.getMessage().contains("cannot be cast to") : "Contains the right text: " + ex.getMessage();
    1.15 +            throw ex;
    1.16 +        }
    1.17 +    }
    1.18      
    1.19      static class MyException extends Exception {
    1.20          public MyException(String message) {
     2.1 --- a/rt/emul/mini/src/main/java/java/lang/Class.java	Tue Apr 19 05:31:17 2016 +0200
     2.2 +++ b/rt/emul/mini/src/main/java/java/lang/Class.java	Tue Apr 19 07:24:30 2016 +0200
     2.3 @@ -1915,7 +1915,16 @@
     2.4      }
     2.5  
     2.6      @Exported
     2.7 -    @JavaScriptOnly(name = "castEx", value = "function() { throw vm.java_lang_ClassCastException(true); }")
     2.8 +    @JavaScriptOnly(name = "castEx", value = ""
     2.9 +        + "function(obj, type) {\n"
    2.10 +        + "  var realType = obj.getClass__Ljava_lang_Class_2().getName__Ljava_lang_String_2();\n"
    2.11 +        + "  var msg = realType + ' cannot be cast to ' + type;\n"
    2.12 +        + "  var ex = vm.java_lang_ClassCastException(true);\n"
    2.13 +        + "  ex.constructor.cons__VLjava_lang_String_2.call(ex, msg);;\n"
    2.14 +        + "  throw ex;\n"
    2.15 +        + "}\n"
    2.16 +        + ""
    2.17 +    )
    2.18      private static void castEx() {
    2.19      }
    2.20      
     3.1 --- a/rt/emul/mini/src/main/java/java/lang/Throwable.java	Tue Apr 19 05:31:17 2016 +0200
     3.2 +++ b/rt/emul/mini/src/main/java/java/lang/Throwable.java	Tue Apr 19 07:24:30 2016 +0200
     3.3 @@ -762,19 +762,8 @@
     3.4          return getOurStackTrace().clone();
     3.5      }
     3.6  
     3.7 -    private synchronized StackTraceElement[] getOurStackTrace() {
     3.8 -        // Initialize stack trace field with information from
     3.9 -        // backtrace if this is the first call to this method
    3.10 -        if (stackTrace == UNASSIGNED_STACK ||
    3.11 -            (stackTrace == null && backtrace != null) /* Out of protocol state */) {
    3.12 -            int depth = getStackTraceDepth();
    3.13 -            stackTrace = new StackTraceElement[depth];
    3.14 -            for (int i=0; i < depth; i++)
    3.15 -                stackTrace[i] = getStackTraceElement(i);
    3.16 -        } else if (stackTrace == null) {
    3.17 -            return UNASSIGNED_STACK;
    3.18 -        }
    3.19 -        return stackTrace;
    3.20 +    private StackTraceElement[] getOurStackTrace() {
    3.21 +        return new StackTraceElement[0];
    3.22      }
    3.23  
    3.24      /**
     4.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Apr 19 05:31:17 2016 +0200
     4.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Apr 19 07:24:30 2016 +0200
     4.3 @@ -2420,8 +2420,8 @@
     4.4          String type = jc.getClassName(indx);
     4.5          if (!type.startsWith("[")) {
     4.6              emitNoFlush(smapper, 
     4.7 -                 "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx();",
     4.8 -                 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type));
     4.9 +                 "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx(@1, '@3');",
    4.10 +                 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type), type.replace('/', '.'));
    4.11          } else {
    4.12              int cnt = 0;
    4.13              while (type.charAt(cnt) == '[') {