rt/vm/src/test/java/org/apidesign/vm4brwsr/Exceptions.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 423 vm/src/test/java/org/apidesign/vm4brwsr/Exceptions.java@9b5868bf56ec
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
tzezula@287
     1
/**
tzezula@287
     2
 * Back 2 Browser Bytecode Translator
tzezula@287
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
tzezula@287
     4
 *
tzezula@287
     5
 * This program is free software: you can redistribute it and/or modify
tzezula@287
     6
 * it under the terms of the GNU General Public License as published by
tzezula@287
     7
 * the Free Software Foundation, version 2 of the License.
tzezula@287
     8
 *
tzezula@287
     9
 * This program is distributed in the hope that it will be useful,
tzezula@287
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tzezula@287
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
tzezula@287
    12
 * GNU General Public License for more details.
tzezula@287
    13
 *
tzezula@287
    14
 * You should have received a copy of the GNU General Public License
tzezula@287
    15
 * along with this program. Look for COPYING file in the top folder.
tzezula@287
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
tzezula@285
    17
 */
tzezula@285
    18
package org.apidesign.vm4brwsr;
tzezula@285
    19
jaroslav@423
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@423
    21
tzezula@285
    22
/**
tzezula@285
    23
 *
tzezula@285
    24
 * @author tom
tzezula@285
    25
 */
tzezula@285
    26
public class Exceptions {
jaroslav@400
    27
    private Exceptions() {
jaroslav@400
    28
    }
tzezula@285
    29
tzezula@285
    30
    public static int methodWithTryCatchNoThrow() {
tzezula@285
    31
        int res = 0;
tzezula@285
    32
        try {
tzezula@285
    33
            res = 1;
tzezula@285
    34
        } catch (IllegalArgumentException e) {
tzezula@285
    35
            res = 2;
tzezula@285
    36
        }
tzezula@285
    37
        //join point
tzezula@285
    38
        return res;
tzezula@285
    39
    }
tzezula@285
    40
tzezula@285
    41
    public static int methodWithTryCatchThrow() {
tzezula@285
    42
        int res = 0;
tzezula@285
    43
        try {
tzezula@285
    44
            res = 1;
tzezula@285
    45
            throw new IllegalArgumentException();
tzezula@285
    46
        } catch (IllegalArgumentException e) {
tzezula@285
    47
            res = 2;
tzezula@285
    48
        }
tzezula@285
    49
        //join point
tzezula@285
    50
        return res;
tzezula@285
    51
    }
jaroslav@423
    52
    
jaroslav@423
    53
    @JavaScriptBody(args = "msg", body = "throw msg;")
jaroslav@423
    54
    public static void thrw(String msg) {}
jaroslav@423
    55
    
jaroslav@423
    56
    public static String catchThrowableCatchesAll() {
jaroslav@423
    57
        try {
jaroslav@423
    58
            thrw("Hello!");
jaroslav@423
    59
            return "Not here!";
jaroslav@423
    60
        } catch (Throwable ex) {
jaroslav@423
    61
            return ex.getMessage();
jaroslav@423
    62
        }
jaroslav@423
    63
    }
tzezula@285
    64
jaroslav@400
    65
    public static String newInstance(String n) {
jaroslav@400
    66
        try {
jaroslav@400
    67
            Class c;
jaroslav@400
    68
            try {
jaroslav@400
    69
                c = Class.forName(n);
jaroslav@400
    70
            } catch (ClassNotFoundException ex) {
jaroslav@400
    71
                return ("CNFE:" + ex.getMessage()).toString();
jaroslav@400
    72
            }
jaroslav@400
    73
            return c.newInstance().getClass().getName();
jaroslav@400
    74
        } catch (InstantiationException | IllegalAccessException ex) {
jaroslav@400
    75
            return ex.getMessage();
jaroslav@400
    76
        }
jaroslav@400
    77
    }
jaroslav@401
    78
    
jaroslav@401
    79
    private static int counter;
jaroslav@401
    80
    public static int readCounter(String n) throws ClassNotFoundException {
jaroslav@401
    81
        try {
jaroslav@401
    82
            Class.forName(n);
jaroslav@401
    83
        } finally {
jaroslav@401
    84
            counter++;
jaroslav@401
    85
        }
jaroslav@401
    86
        return counter;
jaroslav@401
    87
    }
tzezula@285
    88
}