基于Linux的JNI动态函数注册
全栈程序员阿志
编辑于 2020年11月25日 18:06

cmake_minimum_required(VERSION 3.2)

project(AlgorithmLibrary)

set(ProjectDir ${CMAKE_CURRENT_SOURCE_DIR})

set(LIBRARY_OUTPUT_PATH ${ProjectDir}/lib)

set(DynamicRegisterDir ${CMAKE_CURRENT_SOURCE_DIR}/DynamicRegister/)

include_directories(

    ./include /opt/jdk1.8.0_201/include/ 

    ${DynamicRegisterDir}

    TimeComplexity/

    Test/

)

add_library(

    

    AlgorithmLibrary SHARED 

    ${DynamicRegisterDir}/DynamicRegisterMethod.cpp

    TimeComplexity/InstrucNumTime.cpp

    Test/Test.cpp

)

动态函数注册核心代码

#ifndef DynamicRegisterMethod

#define DynamicRegisterMethod

#include <Common.h>

#include <InstrucNumTime.h>

#include <Test.h>

#endif

#include <DynamicRegisterMethod.h>

using namespace std;

char* javaClass []  = {

    (char*)"com/ossit/demo01/App&#​34;,

};

static JNINativeMethod getMethod[1][2] = {

    {

        {(char*)"printInstructionTime&#​34;,(char*)"()V&#​34;,(void*)printInstructionTime},

        {(char*)"test&#​34;,(char*)"()V&#​34;,(void*)test}

       

    }

    

};

int registerNativeMethods(JNIEnv* env,const char* name,

    JNINativeMethod* methods,jint nmethods){

    jclass jcls;

    

    jcls = env->FindClass(name);

    if(jcls == NULL) {

        return JNI_FALSE;

    }

    if(env->RegisterNatives(jcls,methods,nmethods) < 0){

        return JNI_FALSE;

    }

    

    return JNI_TRUE;

}

JNIEXPORT int JNICALL JNI_OnLoad(JavaVM* vm,void* reserved){

    

    JNIEnv* env;

    if(vm->GetEnv(reinterpret_cast<void**>(&env),JNI_VERSION_1_6) != JNI_OK){

        return JNI_FALSE;

    }

    int count=0;

    for(int i=0;i < sizeof(javaClass)/sizeof(javaClass[0]);i++){

        

        for(int j=0;j< sizeof(getMethod[i])/sizeof(getMethod[i][0]);j++){

            

            if(getMethod[i][j].fnPtr != NULL){

                count++;

                

            }

        }

        registerNativeMethods(env,javaClass[i],&getMethod[i][0],count);

        count = 0;

    }

    cout << "LoadAlgorithmLibrary&#​34; << endl;

    

    return JNI_VERSION_1_6;

}

#ifndef Common

#define Common

#include <jni.h>

#include <iostream>

#include <stdio.h>

#endif

#ifndef Test

#define Test

#include <Common.h>

void test();

#endif

#include <Test.h>

void test(){

    int l;

    short s;

    char c;

    l =  0xabcddcba;

    s = l;

    c = l;

    printf("宽度溢出\n&#​34;);

    printf("l = 0x%x (%d bits)\n&#​34;, l, sizeof(l) * 8);

    printf("s = 0x%x (%d bits)\n&#​34;, s, sizeof(s) * 8);

    printf("c = 0x%x (%d bits)\n&#​34;, c, sizeof(c) * 8);

    printf("整型提升\n&#​34;);

    printf("s + c = 0x%x (%d bits)\n&#​34;, s+c, sizeof(s+c) * 8);

}

#include <InstrucNumTime.h>

#include <cmath>

#include <ctime>

using namespace std;

/**

 * 

 * 计算机处理指令数的时间

 * 

**/

void printInstructionTime(){

    

  

    for(jint i=1;i<=9;i++){

        

        jint number = pow(10,i);

        clock_t startTime = clock();

        jint sum = 0;

        for(jint j=0;j<number;j++){

            sum += j;

        }

   

        clock_t endTime = clock();

        cout << "10^&#​34; << i << " : &#​34; << (jdouble)(endTime-startTime)/CLOCKS_PER_SEC << "s&#​34; << endl;

      

    }

}

#ifndef InstrucNumTime

#define InstrucNumTime

#include <Common.h>

void printInstructionTime();

#endif