Example #1
0
static XP_Bool
and_util_askPassword( XW_UtilCtxt* uc, const XP_UCHAR* name, 
                      XP_UCHAR* buf, XP_U16* len )
{
    XP_Bool result = false;
    UTIL_CBK_HEADER("askPassword", "(Ljava/lang/String;)Ljava/lang/String;" );

    jstring jname = (*env)->NewStringUTF( env, name );
    jstring jstr = (*env)->CallObjectMethod( env, util->jutil, mid, 
                                             jname );
    deleteLocalRef( env, jname );

    if ( NULL != jstr ) {       /* null means user cancelled */
        jsize jsiz = (*env)->GetStringUTFLength( env, jstr );
        if ( jsiz < *len ) {
            const char* chars = (*env)->GetStringUTFChars( env, jstr, NULL );
            XP_MEMCPY( buf, chars, jsiz );
            (*env)->ReleaseStringUTFChars( env, jstr, chars );
            buf[jsiz] = '\0';
            *len = jsiz;
            result = XP_TRUE;
        }
        deleteLocalRef( env, jstr );
    }

    UTIL_CBK_TAIL();
    return result;
}
Example #2
0
std::string toString( jobject obj ) {

    JNIEnv* env = attach();

    jclass objectClass = env->FindClass( "java/lang/Object" );

    if ( ! objectClass ) {
        string msg = "Assert failed: Unable to find the class, java.lang.Object.";
        try
        {
            helper::catchAndThrow();
        }
        catch (JNIException& e)
        {
            msg.append("\ncaused by:\n");
            msg.append(e.what());
        }
        throw JNIException( msg );
    }

    jmethodID toString = env->GetMethodID( objectClass, "toString", "()Ljava/lang/String;" );

    if ( ! toString ) {
        string msg = "Assert failed: Unable to find the method, Object.toString().";
        try
        {
            helper::catchAndThrow();
        }
        catch (JNIException& e)
        {
            msg.append("\ncaused by:\n");
            msg.append(e.what());
        }
        throw JNIException( msg );
    }

    jstring javaStr = static_cast<jstring>( env->CallObjectMethod( obj, toString ) );

    const char* strBuf = env->GetStringUTFChars( javaStr, 0 );
    string value = string( strBuf );

    env->ReleaseStringUTFChars( javaStr, strBuf );

    deleteLocalRef( env, javaStr );
    deleteLocalRef( env, objectClass );

    return value;
}
Example #3
0
static const XP_UCHAR*
and_util_getUserString( XW_UtilCtxt* uc, XP_U16 stringCode )
{
    XP_UCHAR* result = "";
    UTIL_CBK_HEADER("getUserString", "(I)Ljava/lang/String;" );
    int index = stringCode - 1; /* see LocalizedStrIncludes.h */
    XP_ASSERT( index < VSIZE( util->userStrings ) );

    if ( ! util->userStrings[index] ) {
        jstring jresult = (*env)->CallObjectMethod( env, util->jutil, mid, 
                                                    stringCode );
        jsize len = (*env)->GetStringUTFLength( env, jresult );
        XP_UCHAR* buf = XP_MALLOC( util->util.mpool, len + 1 );

        const char* jchars = (*env)->GetStringUTFChars( env, jresult, NULL );
        XP_MEMCPY( buf, jchars, len );
        buf[len] = '\0';
        (*env)->ReleaseStringUTFChars( env, jresult, jchars );
        deleteLocalRef( env, jresult );
        util->userStrings[index] = buf;
    }

    result = util->userStrings[index];
    UTIL_CBK_TAIL();
    return result;
}
Example #4
0
static const XP_UCHAR*
and_util_getDevID( XW_UtilCtxt* uc, DevIDType* typ )
{
    const XP_UCHAR* result = NULL;
    *typ = ID_TYPE_NONE;
    UTIL_CBK_HEADER( "getDevID", "([B)Ljava/lang/String;" );
    jbyteArray jbarr = makeByteArray( env, 1, NULL );
    jstring jresult = (*env)->CallObjectMethod( env, util->jutil, mid, jbarr );
    if ( NULL != jresult ) {
        const char* jchars = (*env)->GetStringUTFChars( env, jresult, NULL );
        jsize len = (*env)->GetStringUTFLength( env, jresult );
        if ( NULL != util->devIDStorage
             && 0 == XP_MEMCMP( util->devIDStorage, jchars, len ) ) {
            XP_LOGF( "%s: already have matching devID", __func__ );
        } else {
            XP_LOGF( "%s: allocating storage for devID", __func__ );
            XP_FREEP( util->util.mpool, &util->devIDStorage );
            util->devIDStorage = XP_MALLOC( util->util.mpool, len + 1 );
            XP_MEMCPY( util->devIDStorage, jchars, len );
            util->devIDStorage[len] = '\0';
        }
        (*env)->ReleaseStringUTFChars( env, jresult, jchars );
        result = (const XP_UCHAR*)util->devIDStorage;

        jbyte* elems = (*env)->GetByteArrayElements( env, jbarr, NULL );
        *typ = (DevIDType)elems[0];
        (*env)->ReleaseByteArrayElements( env, jbarr, elems, 0 );
    }
    deleteLocalRef( env, jbarr );
    UTIL_CBK_TAIL();
    return result;
}
Example #5
0
Common Common::Factory::create(){
  JArguments arguments;
  jobject localRef = newObject(Common::staticGetJavaJniClass(), arguments);
  Common result = Common(localRef);
  JNIEnv* env = attach();
  deleteLocalRef(env, localRef);
  return result;
}
Example #6
0
Throwable Throwable::Factory::create(){
  JArguments arguments;
  jobject localRef = newObject(Throwable::staticGetJavaJniClass(), arguments);
  Throwable result = Throwable(localRef);
  JNIEnv* env = attach();
  deleteLocalRef(env, localRef);
  return result;
}
Example #7
0
Throwable Throwable::Factory::create(::jace::proxy::java::lang::Throwable p0){
  JArguments arguments;
  arguments << p0;
  jobject localRef = newObject(Throwable::staticGetJavaJniClass(), arguments);
  Throwable result = Throwable(localRef);
  JNIEnv* env = attach();
  deleteLocalRef(env, localRef);
  return result;
}
Example #8
0
static void
and_util_showChat( XW_UtilCtxt* uc, const XP_UCHAR const* msg )
{
    UTIL_CBK_HEADER("showChat", "(Ljava/lang/String;)V" );
    jstring jmsg = (*env)->NewStringUTF( env, msg );
    (*env)->CallVoidMethod( env, util->jutil, mid, jmsg );
    deleteLocalRef( env, jmsg );
    UTIL_CBK_TAIL();
}
Example #9
0
Action Action::Factory::create(::jace::proxy::edu::stanford::graphics::parser::Mention p0, ::jace::proxy::edu::stanford::graphics::parser::Mention p1, ::jace::proxy::edu::stanford::graphics::parser::Mention p2){
  JArguments arguments;
  arguments << p0 << p1 << p2;
  jobject localRef = newObject(Action::staticGetJavaJniClass(), arguments);
  Action result = Action(localRef);
  JNIEnv* env = attach();
  deleteLocalRef(env, localRef);
  return result;
}
Example #10
0
Action Action::Factory::create(::jace::proxy::java::lang::String p0, ::jace::proxy::java::lang::String p1, ::jace::proxy::java::lang::String p2){
  JArguments arguments;
  arguments << p0 << p1 << p2;
  jobject localRef = newObject(Action::staticGetJavaJniClass(), arguments);
  Action result = Action(localRef);
  JNIEnv* env = attach();
  deleteLocalRef(env, localRef);
  return result;
}
Example #11
0
static void
and_util_deviceRegistered( XW_UtilCtxt* uc, DevIDType typ, 
                           const XP_UCHAR* idRelay )
{
    UTIL_CBK_HEADER( "deviceRegistered", "(ILjava/lang/String;)V" );
    jstring jstr = (*env)->NewStringUTF( env, idRelay );
    (*env)->CallVoidMethod( env, util->jutil, mid, typ, jstr );
    deleteLocalRef( env, jstr );
    UTIL_CBK_TAIL();
}
Example #12
0
static void
and_util_cellSquareHeld( XW_UtilCtxt* uc, XWStreamCtxt* words )
{
    if ( NULL != words ) {
        UTIL_CBK_HEADER( "cellSquareHeld", "(Ljava/lang/String;)V" );
        jstring jwords = streamToJString( env, words );
        (*env)->CallVoidMethod( env, util->jutil, mid, jwords );
        deleteLocalRef( env, jwords );
        UTIL_CBK_TAIL();
    }
}
Example #13
0
static XP_Bool
and_util_confirmTrade( XW_UtilCtxt* uc, const XP_UCHAR** tiles, XP_U16 nTiles )
{
    XP_Bool result = XP_FALSE;
    UTIL_CBK_HEADER("confirmTrade", "([Ljava/lang/String;)Z" );
    jobjectArray jtiles = makeStringArray( env, nTiles, tiles );
    result = (*env)->CallBooleanMethod( env, util->jutil, mid, jtiles );
    deleteLocalRef( env, jtiles );
    UTIL_CBK_TAIL();
    return result;
}
Example #14
0
static void
and_util_informMissing(XW_UtilCtxt* uc, XP_Bool isServer, 
                       CommsConnType connType, XP_U16 nMissing )
{
    UTIL_CBK_HEADER( "informMissing",
                     "(ZL" PKG_PATH("jni/CommsAddrRec$CommsConnType") ";I)V" );
    jobject jtyp = intToJEnum( env, connType,
                               PKG_PATH("jni/CommsAddrRec$CommsConnType") );
    (*env)->CallVoidMethod( env, util->jutil, mid, isServer, jtyp, nMissing );
    deleteLocalRef( env, jtyp );
    UTIL_CBK_TAIL();
}
Example #15
0
static XP_S16
and_util_userPickTileBlank( XW_UtilCtxt* uc, XP_U16 playerNum, 
                            const XP_UCHAR** tileFaces, XP_U16 nTiles )
{
    XP_S16 result = -1;
    UTIL_CBK_HEADER("userPickTileBlank", "(I[Ljava/lang/String;)I" );

    jobject jtexts = makeStringArray( env, nTiles, tileFaces );

    result = (*env)->CallIntMethod( env, util->jutil, mid, 
                                    playerNum, jtexts );

    deleteLocalRef( env, jtexts );
    UTIL_CBK_TAIL();
    return result;
}
Example #16
0
static XP_Bool
and_util_userQuery( XW_UtilCtxt* uc, UtilQueryID id, XWStreamCtxt* stream )
{
    jboolean result = XP_FALSE;
    XP_ASSERT( id < QUERY_LAST_COMMON );
    UTIL_CBK_HEADER("userQuery", "(ILjava/lang/String;)Z" );

    jstring jstr = NULL;
    if ( NULL != stream ) {
        jstr = streamToJString( env, stream );
    }
    result = (*env)->CallBooleanMethod( env, util->jutil, mid, id, jstr );
    deleteLocalRef( env, jstr );
    UTIL_CBK_TAIL();
    return result;
}
Example #17
0
jobject
and_util_splitFaces( JNIUtilCtxt* jniutil, const XP_U8* bytes, jsize len,
                     XP_Bool isUTF8 )
{
    jobject strarray = NULL;
    JNIEnv* env = *jniutil->envp;
    jmethodID mid
        = getMethodID( env, jniutil->jjniutil, "splitFaces",
                       "([BZ)[[Ljava/lang/String;" );

    jbyteArray jbytes = makeByteArray( env, len, (jbyte*)bytes );
    strarray = 
        (*env)->CallObjectMethod( env, jniutil->jjniutil, mid, jbytes, isUTF8 );
    deleteLocalRef( env, jbytes );

    return strarray;
}
Example #18
0
jobject
and_util_makeJBitmap( JNIUtilCtxt* jniutil, int nCols, int nRows, 
                      const jboolean* colors )
{
    jobject bitmap;
    JNIEnv* env = *jniutil->envp;
    jmethodID mid
        = getMethodID( env, jniutil->jjniutil, "makeBitmap", 
                       "(II[Z)Landroid/graphics/drawable/BitmapDrawable;" );

    jbooleanArray jcolors = makeBooleanArray( env, nCols*nRows, colors );

    bitmap = (*env)->CallObjectMethod( env, jniutil->jjniutil, mid,
                                       nCols, nRows, jcolors );
    deleteLocalRef( env, jcolors );

    return bitmap;
}
Example #19
0
void printClass( jobject obj ) {
    JNIEnv* env = attach();
    jclass objClass = env->GetObjectClass( obj );
    print( objClass );
    deleteLocalRef( env, objClass );
}
Example #20
0
void catchAndThrow() {

    JNIEnv* env = attach();

    if ( ! env->ExceptionCheck() ) {
        return;
    }

    jthrowable jexception = env->ExceptionOccurred();
    env->ExceptionClear();

    /* Find the fully qualified name for the exception type, so
     * we can find a matching C++ proxy exception.
     *
     * In java, this looks like:
     *   String typeName = exception.getClass().getName();
     */
    jclass throwableClass = env->FindClass( "java/lang/Throwable" );

    if ( ! throwableClass ) {
        string msg = "Assert failed: Unable to find the class, java.lang.Throwable.";
        throw JNIException( msg );
    }

    jclass classClass = env->FindClass( "java/lang/Class" );
    if ( ! classClass ) {
        string msg = "Assert failed: Unable to find the class, java.lang.Class.";
        throw JNIException( msg );
    }

    jmethodID throwableGetClass = env->GetMethodID( throwableClass, "getClass", "()Ljava/lang/Class;" );
    if ( ! throwableGetClass ) {
        string msg = "Assert failed: Unable to find the method, Throwable.getClass().";
        throw JNIException( msg );
    }
    deleteLocalRef( env, throwableClass );

    jmethodID classGetName = env->GetMethodID( classClass, "getName", "()Ljava/lang/String;" );
    if ( ! classGetName ) {
        string msg = "Assert failed: Unable to find the method, Class.getName().";
        throw JNIException( msg );
    }

    jmethodID classGetSuperclass = env->GetMethodID( classClass, "getSuperclass", "()Ljava/lang/Class;" );
    if ( ! classGetSuperclass ) {
        string msg = "Assert failed: Unable to find the method, Class.getSuperclass().";
        throw JNIException( msg );
    }
    deleteLocalRef( env, classClass );

    jobject exceptionClass = env->CallObjectMethod( jexception, throwableGetClass );
    if ( env->ExceptionOccurred() ) {
        env->ExceptionDescribe();
        string msg = "helper::catchAndThrow()\n" \
                "An error occurred while trying to call getClass() on the thrown exception.";
        throw JNIException( msg );
    }

    jstring exceptionType = static_cast<jstring>( env->CallObjectMethod( exceptionClass, classGetName ) );
    if ( env->ExceptionOccurred() ) {
        env->ExceptionDescribe();
        string msg = "helper::catchAndThrow()\n" \
                "An error occurred while trying to call getName() on the class of the thrown exception.";
        throw JNIException( msg );
    }

    string exceptionTypeString = asString( env, exceptionType );

    /* Now, find the matching factory for this exception type.
     */
    while ( true ) {

        FactoryMap::iterator it = getFactoryMap()->find( exceptionTypeString );

        /* If we couldn't find a match, try to find the parent exception type.
         */
        if ( it == getFactoryMap()->end() ) {

            jobject superClass = env->CallObjectMethod( exceptionClass, classGetSuperclass );

            if ( env->ExceptionOccurred() ) {
                env->ExceptionDescribe();
                string msg = "helper::catchAndThrow()\n" \
                        "An error occurred while trying to call getSuperclass() on the thrown exception.";
                throw JNIException( msg );
            }

            /* We get NULL if we've already reached java.lang.Object, in which case,
             * we couldn't find any match at all.
             */
            if ( ! superClass ) {
                break;
            }

            deleteLocalRef( env, exceptionClass );
            deleteLocalRef( env, exceptionType );
            exceptionClass = superClass;

            exceptionType = static_cast<jstring>( env->CallObjectMethod( exceptionClass, classGetName ) );

            if ( env->ExceptionOccurred() ) {
                env->ExceptionDescribe();
                string msg = "helper::catchAndThrow()\n" \
                        "An error occurred while trying to call getName() on the superclass " \
                        "of the thrown exception.";
                throw JNIException( msg );
            }

            exceptionTypeString = asString( env, exceptionType );
            if ( exceptionTypeString == "java.lang.Object" ) {
                /*
                 * Couldn't find a matching exception. Abort!
                 */
                break;
            }
            continue;
        }

        /* Ask the factory to throw the exception.
         */
        jvalue value;
        value.l = jexception;
        it->second->throwInstance( value );
    }

    exceptionClass = env->CallObjectMethod( jexception, throwableGetClass );

    if ( env->ExceptionOccurred() ) {
        env->ExceptionDescribe();
        string msg = "helper::catchAndThrow()\n" \
                "An error occurred while trying to call getClass() on the thrown exception.";
        throw JNIException( msg );
    }

    exceptionType = static_cast<jstring>( env->CallObjectMethod( exceptionClass, classGetName ) );

    if ( env->ExceptionOccurred() ) {
        env->ExceptionDescribe();
        string msg = "helper::catchAndThrow()\n" \
                "An error occurred while trying to call getName() on the class of the thrown exception.";
        throw JNIException( msg );
    }

    // Fallback: throw a JNIException with the type name and message of the Throwable.

    exceptionTypeString = asString( env, exceptionType );
    jmethodID getMessage = env->GetMethodID(static_cast<jclass>(exceptionClass), "getMessage", "()Ljava/lang/String;");
    if ( !getMessage) {
        throw JNIException("Could not find Throwable.getMessage() method");
    }

    jstring jmessage = static_cast<jstring>( env->CallObjectMethod(jexception, getMessage));
    if ( !jmessage) {
        // No message specified (or some error in the JVM), so just print the Exception type.
        throw JNIException(string("Exception occurred in JVM: ") + exceptionTypeString);
    }
    const char *msg = env->GetStringUTFChars(jmessage, NULL);
    throw JNIException( exceptionTypeString + ": " + msg );
}
Example #21
0
void createVm( const VmLoader& loader,
               const OptionList& options,
               bool ignoreUnrecognized ) {

    globalLoader = std::unique_ptr<VmLoader>( loader.clone() );
    globalLoader->loadVm();

    JavaVM* vm;
    JNIEnv* env;

    JavaVMInitArgs vm_args;
    JavaVMOption* jniOptions = options.createJniOptions();

    vm_args.version = globalLoader->version();
    vm_args.options = jniOptions;
    vm_args.nOptions = jint( options.size() );

    vm_args.ignoreUnrecognized = ignoreUnrecognized;

    jint rc = globalLoader->createJavaVM( &vm, reinterpret_cast<void**>( &env ), &vm_args );

    options.destroyJniOptions( jniOptions );

    if ( rc != 0 ) {
        string msg = "Unable to create the virtual machine. The error was " + std::to_string( rc );
        throw JNIException( msg );
    }

    jclass runtimeClass = env->FindClass( "java/lang/Runtime" );
    if ( ! runtimeClass ) {
        string msg = "Assert failed: Unable to find the class, java.lang.Runtime.";
        throw JNIException( msg );
    }

    jmethodID runtimeGetRuntime = env->GetStaticMethodID( runtimeClass, "getRuntime", "()Ljava/lang/Runtime;" );
    if ( ! runtimeGetRuntime ) {
        deleteLocalRef( env, runtimeClass );
        string msg = "Assert failed: Unable to find the method, Runtime.getRuntime().";
        throw JNIException( msg );
    }

    jobject runtimeObject = env->CallStaticObjectMethod( runtimeClass, runtimeGetRuntime );
    if ( ! runtimeObject )	{
        deleteLocalRef( env, runtimeClass );
        string msg = "Unable to invoke Runtime.getRuntime()";
        try
        {
            helper::catchAndThrow();
        }
        catch (JNIException& e)
        {
            msg.append("\ncaused by:\n");
            msg.append(e.what());
        }
        throw JNIException( msg );
    }

    jmethodID runtimeAddShutdownHook = env->GetMethodID( runtimeClass, "addShutdownHook", "(Ljava/lang/Thread;)V" );
    if ( ! runtimeAddShutdownHook ) {
        deleteLocalRef( env, runtimeObject );
        deleteLocalRef( env, runtimeClass );
        string msg = "Assert failed: Unable to find the method, Runtime.addShutdownHook().";
        throw JNIException( msg );
    }

    jclass shutdownHookClass = env->FindClass( "jace/util/ShutdownHook" );
    if ( ! shutdownHookClass ) {
        deleteLocalRef( env, runtimeObject );
        deleteLocalRef( env, runtimeClass );
        string msg = "Assert failed: Unable to find the class, jace.util.ShutdownHook. Did you forget to include "
                "jace-runtime.jar in your classpath at runtime?";
        throw JNIException( msg );
    }

    // Register the native method jace.util.ShutdownHook.signalVMShutdown
    JNINativeMethod signalVMShutdown;
    signalVMShutdown.fnPtr     = reinterpret_cast<void*>(Java_jace_util_ShutdownHook_signalVMShutdown);
    signalVMShutdown.name      = const_cast<char*>("signalVMShutdown");
    signalVMShutdown.signature = const_cast<char*>("()V");

    jint result = env->RegisterNatives(shutdownHookClass, &signalVMShutdown, 1);
    if (result != JNI_OK) {
        deleteLocalRef( env, runtimeObject );
        deleteLocalRef( env, runtimeClass );
        deleteLocalRef( env, shutdownHookClass );
        string msg = "Assert failed: Unable to register native method, jace.util.ShutdownHook.signalVMShutdown()";
        throw JNIException( msg );
    }

    jmethodID shutdownHookGetInstance = env->GetStaticMethodID( shutdownHookClass, "getInstance", "()Ljace/util/ShutdownHook;" );
    if ( ! shutdownHookGetInstance ) {
        deleteLocalRef( env, runtimeObject );
        deleteLocalRef( env, runtimeClass );
        deleteLocalRef( env, shutdownHookClass );
        string msg = "Assert failed: Unable to find the method, jace.util.ShutdownHook.getInstance()";
        throw JNIException( msg );
    }

    jobject shutdownHookObject = env->CallStaticObjectMethod( shutdownHookClass, shutdownHookGetInstance );
    if ( ! shutdownHookObject ) {
        deleteLocalRef( env, runtimeObject );
        deleteLocalRef( env, runtimeClass );
        deleteLocalRef( env, shutdownHookClass );
        string msg = "Unable to invoke jace.util.ShutdownHook.getInstance()";
        try
        {
            helper::catchAndThrow();
        }
        catch (JNIException& e)
        {
            msg.append("\ncaused by:\n");
            msg.append(e.what());
        }
        throw JNIException( msg );
    }

    env->CallVoidMethod( runtimeObject, runtimeAddShutdownHook, shutdownHookObject );
    try
    {
        helper::catchAndThrow();
    }
    catch (JNIException& e)
    {
        string msg = "Exception thrown invoking Runtime.addShutdownHook(shutdownHook)";
        msg.append("\ncaused by:\n");
        msg.append(e.what());
        throw JNIException( msg );
    }
    deleteLocalRef( env, runtimeObject );
    deleteLocalRef( env, runtimeClass );
    deleteLocalRef( env, shutdownHookClass );
}