All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bugvm.apple.foundation.NSException Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2013-2015 RoboVM AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bugvm.apple.foundation;

/**/
import java.io.*;
import java.nio.*;
import java.util.*;

import com.bugvm.objc.*;
import com.bugvm.objc.annotation.*;
import com.bugvm.objc.block.*;
import com.bugvm.rt.*;
import com.bugvm.rt.bro.*;
import com.bugvm.rt.bro.annotation.*;
import com.bugvm.rt.bro.ptr.*;
import com.bugvm.apple.corefoundation.*;
import com.bugvm.apple.uikit.*;
import com.bugvm.apple.coreanimation.*;
import com.bugvm.apple.coregraphics.*;
import com.bugvm.apple.coremedia.*;
import com.bugvm.apple.security.*;

/**/

/**/
/**/@Library("Foundation") @NativeClass/**/
/**/public/**/ class /**/NSException/**/ 
    extends /**/NSObject/**/ 
    /**/implements NSCoding/**/ {

    /**/public static class NSExceptionPtr extends Ptr {}/**/
    /**/static { ObjCRuntime.bind(NSException.class); }/**/
    /**//**/
    /**/
    protected NSException(SkipInit skipInit) { super(skipInit); }
    public NSException(String aName, String aReason, NSDictionary aUserInfo) { super((SkipInit) null); initObject(init(aName, aReason, aUserInfo)); }
    public NSException(NSCoder aDecoder) { super((SkipInit) null); initObject(init(aDecoder)); }
    /**/
    /**/
    @Property(selector = "name")
    public native String getName();
    @Property(selector = "reason")
    public native String getReason();
    @Property(selector = "userInfo")
    public native NSDictionary getUserInfo();
    /**
     * @since Available in iOS 2.0 and later.
     */
    @Property(selector = "callStackReturnAddresses")
    public native NSArray getCallStackReturnAddresses();
    /**
     * @since Available in iOS 4.0 and later.
     */
    @Property(selector = "callStackSymbols")
    public native NSArray getCallStackSymbols();
    /**/
    /**//**/
    
    private static VoidBlock1 handler = null;
    
    @Callback
    private static void handler(NSException e) {
        handler.invoke(e);
    }
    
    public static void setUncaughtExceptionHandler(VoidBlock1 handler) {
        if (handler == null) {
            throw new NullPointerException("handler");
        }
        NSException.handler = handler;
        try {
            setUncaughtExceptionHandler(new FunctionPtr(NSException.class.getDeclaredMethod("handler", NSException.class)));
        } catch (Throwable t) {
            throw new Error(t);
        }
    }
    
    /**
     * Registers a default java uncaught exception handler that forwards exceptions to RoboVM's signal handlers.
     * Use this if you want Java exceptions to be logged by crash reporters.
     */
    public static void registerDefaultJavaUncaughtExceptionHandler() {
        Thread.setDefaultUncaughtExceptionHandler(new java.lang.Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException (Thread thread, Throwable ex) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                ex.printStackTrace(pw);
                pw.flush();
                Foundation.log(sw.toString());
                NSException exception = new NSException(ex.getClass().getName(), sw.toString(), new NSDictionary<>());
                if (NSThread.getCurrentThread().isMainThread()) {
                    exception.raise();
                } else {
                    long handler = getUncaughtExceptionHandler();
                    callUncaughtExceptionHandler(handler, exception);
                    // We should never get to this line!
                }
            }
        });
    }
    
    @Bridge(symbol = "NSGetUncaughtExceptionHandler", optional = true)
    private static native @Pointer long getUncaughtExceptionHandler ();

    @Bridge(dynamic = true)
    private static native void callUncaughtExceptionHandler (@Pointer long fn, NSException ex);
    /**/
    @Bridge(symbol="NSSetUncaughtExceptionHandler", optional=true)
    private static native void setUncaughtExceptionHandler(FunctionPtr p0);
    
    @Method(selector = "initWithName:reason:userInfo:")
    protected native @Pointer long init(String aName, String aReason, NSDictionary aUserInfo);
    @Method(selector = "raise")
    public native void raise();
    @Method(selector = "encodeWithCoder:")
    public native void encode(NSCoder coder);
    @Method(selector = "initWithCoder:")
    protected native @Pointer long init(NSCoder aDecoder);
    /**/
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy