diff options
| author | LandrelYoung <landerlyoung@gmail.com> | 2019-10-09 15:10:14 +0800 |
|---|---|---|
| committer | LandrelYoung <landerlyoung@gmail.com> | 2019-10-09 15:10:14 +0800 |
| commit | 41ff638de71bb5db27b6898f93fd62d94101bccd (patch) | |
| tree | 9298c5dfd470f14b69ec0462a4bbb30de809a535 /sample-java/src | |
| parent | 62f09fa64e7caaf2af16bdabeea3413d521782ca (diff) | |
rename samples
Diffstat (limited to 'sample-java/src')
10 files changed, 620 insertions, 0 deletions
diff --git a/sample-java/src/main/cpp/ComputeInNative.cpp b/sample-java/src/main/cpp/ComputeInNative.cpp new file mode 100644 index 0000000..a13cb8c --- /dev/null +++ b/sample-java/src/main/cpp/ComputeInNative.cpp @@ -0,0 +1,59 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ +#include "ComputeInNative.h" + + + + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public boolean init() + * Signature: ()Z + */ +jboolean ComputeInNative::init(JNIEnv *env, jobject thiz) { + return JNI_FALSE; +} + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public void release() + * Signature: ()V + */ +void ComputeInNative::release(JNIEnv *env, jobject thiz) { + return; +} + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public void setParam(java.util.Map<java.lang.String,java.lang.String> globalHttpParam) + * Signature: (Ljava/util/Map;)V + */ +void ComputeInNative::setParam(JNIEnv *env, jobject thiz, jobject globalHttpParam) { + return; +} + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public java.util.Map<java.lang.String,java.lang.String> getGlobalParam() + * Signature: ()Ljava/util/Map; + */ +jobject ComputeInNative::getGlobalParam(JNIEnv *env, jobject thiz) { + return nullptr; +} + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public boolean request(java.lang.String json, io.github.landerlyoung.jennysample.RequestListener listener) + * Signature: (Ljava/lang/String;Lio/github/landerlyoung/jennysample/RequestListener;)Z + */ +jboolean ComputeInNative::request(JNIEnv *env, jobject thiz, jstring json, jobject listener) { + return JNI_FALSE; +} + + + + diff --git a/sample-java/src/main/cpp/ComputeInNative.h b/sample-java/src/main/cpp/ComputeInNative.h new file mode 100644 index 0000000..3edc1cc --- /dev/null +++ b/sample-java/src/main/cpp/ComputeInNative.h @@ -0,0 +1,103 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * DO NOT EDIT THIS FILE. + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ + +/* C++ header file for class io/github/landerlyoung/jennysample/ComputeInNative */ +#pragma once + +#include <jni.h> + + +namespace ComputeInNative { + +// DO NOT modify +static constexpr auto FULL_CLASS_NAME = "io/github/landerlyoung/jennysample/ComputeInNative"; + + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public boolean init() + * Signature: ()Z + */ +jboolean JNICALL init(JNIEnv *env, jobject thiz); + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public void release() + * Signature: ()V + */ +void JNICALL release(JNIEnv *env, jobject thiz); + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public void setParam(java.util.Map<java.lang.String,java.lang.String> globalHttpParam) + * Signature: (Ljava/util/Map;)V + */ +void JNICALL setParam(JNIEnv *env, jobject thiz, jobject globalHttpParam); + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public java.util.Map<java.lang.String,java.lang.String> getGlobalParam() + * Signature: ()Ljava/util/Map; + */ +jobject JNICALL getGlobalParam(JNIEnv *env, jobject thiz); + +/* + * Class: io_github_landerlyoung_jennysample_ComputeInNative + * Method: public boolean request(java.lang.String json, io.github.landerlyoung.jennysample.RequestListener listener) + * Signature: (Ljava/lang/String;Lio/github/landerlyoung/jennysample/RequestListener;)Z + */ +jboolean JNICALL request(JNIEnv *env, jobject thiz, jstring json, jobject listener); + +/** +* register Native functions +* @returns success or not +*/ +inline bool registerNativeFunctions(JNIEnv *env) { + const JNINativeMethod gsNativeMethods[] = { + { + /* method name */ const_cast<char *>("init"), + /* method signature */ const_cast<char *>("()Z"), + /* function pointer */ reinterpret_cast<void *>(init) + }, + { + /* method name */ const_cast<char *>("release"), + /* method signature */ const_cast<char *>("()V"), + /* function pointer */ reinterpret_cast<void *>(release) + }, + { + /* method name */ const_cast<char *>("setParam"), + /* method signature */ const_cast<char *>("(Ljava/util/Map;)V"), + /* function pointer */ reinterpret_cast<void *>(setParam) + }, + { + /* method name */ const_cast<char *>("getGlobalParam"), + /* method signature */ const_cast<char *>("()Ljava/util/Map;"), + /* function pointer */ reinterpret_cast<void *>(getGlobalParam) + }, + { + /* method name */ const_cast<char *>("request"), + /* method signature */ const_cast<char *>("(Ljava/lang/String;Lio/github/landerlyoung/jennysample/RequestListener;)Z"), + /* function pointer */ reinterpret_cast<void *>(request) + } + }; + const int gsMethodCount = + sizeof(gsNativeMethods) / sizeof(JNINativeMethod); + + bool success = false; + jclass clazz = env->FindClass(FULL_CLASS_NAME); + if (clazz != nullptr) { + success = 0 == env->RegisterNatives(clazz, gsNativeMethods, gsMethodCount); + env->DeleteLocalRef(clazz); + } + return success; +} + +} // endof namespace ComputeInNative + + diff --git a/sample-java/src/main/cpp/ComputeInNativeProxy.cpp b/sample-java/src/main/cpp/ComputeInNativeProxy.cpp new file mode 100644 index 0000000..9f71224 --- /dev/null +++ b/sample-java/src/main/cpp/ComputeInNativeProxy.cpp @@ -0,0 +1,62 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * DO NOT EDIT THIS FILE. + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ +#include "ComputeInNativeProxy.h" + + +jclass ComputeInNativeProxy::sClazz = nullptr; + +// thread safe init +std::mutex ComputeInNativeProxy::sInitLock; +std::atomic_bool ComputeInNativeProxy::sInited; + +/*static*/ bool ComputeInNativeProxy::initClazz(JNIEnv *env) { +#define JENNY_CHECK_NULL(val) \ + do { \ + if ((val) == nullptr) { \ + return false; \ + } \ + } while(false) + + if (!sInited) { + std::lock_guard<std::mutex> lg(sInitLock); + if (!sInited) { + auto clazz = env->FindClass(FULL_CLASS_NAME); + JENNY_CHECK_NULL(clazz); + sClazz = reinterpret_cast<jclass>(env->NewGlobalRef(clazz)); + env->DeleteLocalRef(clazz); + JENNY_CHECK_NULL(sClazz); + + + + sField_nativeContext_0 = env->GetFieldID(sClazz, "nativeContext", "J"); + JENNY_CHECK_NULL(sField_nativeContext_0); + + + sInited = true; + } + } +#undef JENNY_CHECK_NULL + return true; +} + +/*static*/ void ComputeInNativeProxy::releaseClazz(JNIEnv *env) { + if (sInited) { + std::lock_guard<std::mutex> lg(sInitLock); + if (sInited) { + env->DeleteLocalRef(sClazz); + sInited = false; + } + } +} + + + +jfieldID ComputeInNativeProxy::sField_nativeContext_0; + + diff --git a/sample-java/src/main/cpp/ComputeInNativeProxy.h b/sample-java/src/main/cpp/ComputeInNativeProxy.h new file mode 100644 index 0000000..55259cc --- /dev/null +++ b/sample-java/src/main/cpp/ComputeInNativeProxy.h @@ -0,0 +1,97 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * DO NOT EDIT THIS FILE. + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ +#pragma once + +#include <jni.h> +#include <assert.h> +#include <atomic> +#include <mutex> + + +class ComputeInNativeProxy { + +public: + static constexpr auto FULL_CLASS_NAME = "io/github/landerlyoung/jennysample/ComputeInNative"; + + + +private: + // thread safe init + static std::atomic_bool sInited; + static std::mutex sInitLock; + + JNIEnv* mJniEnv; + jobject mJavaObjectReference; + +public: + + static bool initClazz(JNIEnv *env); + + static void releaseClazz(JNIEnv *env); + + static void assertInited(JNIEnv *env) { + assert(initClazz(env)); + } + + ComputeInNativeProxy(JNIEnv *env, jobject javaObj) + : mJniEnv(env), mJavaObjectReference(javaObj) { + assertInited(env); + } + + ComputeInNativeProxy(const ComputeInNativeProxy &from) = default; + ComputeInNativeProxy &operator=(const ComputeInNativeProxy &) = default; + + ComputeInNativeProxy(ComputeInNativeProxy &&from) + : mJniEnv(from.mJniEnv), mJavaObjectReference(from.mJavaObjectReference) { + from.mJavaObjectReference = nullptr; + } + + ~ComputeInNativeProxy() = default; + + // helper method to get underlay jobject reference + jobject operator*() { + return mJavaObjectReference; + } + + // helper method to delete JNI local ref. + // use only when you really understand JNIEnv::DeleteLocalRef. + void deleteLocalRef() { + if (mJavaObjectReference) { + mJniEnv->DeleteLocalRef(mJavaObjectReference); + mJavaObjectReference = nullptr; + } + } + + // === java methods below === + + + + // field: private long nativeContext + jlong getNativeContext() const { + + return mJniEnv->GetLongField(mJavaObjectReference, sField_nativeContext_0); + + } + + // field: private long nativeContext + void setNativeContext(jlong nativeContext) const { + + mJniEnv->SetLongField(mJavaObjectReference, sField_nativeContext_0, nativeContext); + } + + + +private: + static jclass sClazz; + + + static jfieldID sField_nativeContext_0; + +}; + diff --git a/sample-java/src/main/cpp/RequestListenerProxy.cpp b/sample-java/src/main/cpp/RequestListenerProxy.cpp new file mode 100644 index 0000000..7ec24ba --- /dev/null +++ b/sample-java/src/main/cpp/RequestListenerProxy.cpp @@ -0,0 +1,62 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * DO NOT EDIT THIS FILE. + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ +#include "RequestListenerProxy.h" + + +jclass RequestListenerProxy::sClazz = nullptr; + +// thread safe init +std::mutex RequestListenerProxy::sInitLock; +std::atomic_bool RequestListenerProxy::sInited; + +/*static*/ bool RequestListenerProxy::initClazz(JNIEnv *env) { +#define JENNY_CHECK_NULL(val) \ + do { \ + if ((val) == nullptr) { \ + return false; \ + } \ + } while(false) + + if (!sInited) { + std::lock_guard<std::mutex> lg(sInitLock); + if (!sInited) { + auto clazz = env->FindClass(FULL_CLASS_NAME); + JENNY_CHECK_NULL(clazz); + sClazz = reinterpret_cast<jclass>(env->NewGlobalRef(clazz)); + env->DeleteLocalRef(clazz); + JENNY_CHECK_NULL(sClazz); + + + sMethod_onResponse_0 = env->GetMethodID(sClazz, "onResponse", "(ZLjava/lang/String;)V"); + JENNY_CHECK_NULL(sMethod_onResponse_0); + + + + sInited = true; + } + } +#undef JENNY_CHECK_NULL + return true; +} + +/*static*/ void RequestListenerProxy::releaseClazz(JNIEnv *env) { + if (sInited) { + std::lock_guard<std::mutex> lg(sInitLock); + if (sInited) { + env->DeleteLocalRef(sClazz); + sInited = false; + } + } +} + + +jmethodID RequestListenerProxy::sMethod_onResponse_0; + + + diff --git a/sample-java/src/main/cpp/RequestListenerProxy.h b/sample-java/src/main/cpp/RequestListenerProxy.h new file mode 100644 index 0000000..a34c2bf --- /dev/null +++ b/sample-java/src/main/cpp/RequestListenerProxy.h @@ -0,0 +1,88 @@ +/** + * File generated by Jenny -- https://github.com/LanderlYoung/Jenny + * + * DO NOT EDIT THIS FILE. + * + * For bug report, please refer to github issue tracker https://github.com/LanderlYoung/Jenny/issues, + * or contact author landerlyoung@gmail.com. + */ +#pragma once + +#include <jni.h> +#include <assert.h> +#include <atomic> +#include <mutex> + + +class RequestListenerProxy { + +public: + static constexpr auto FULL_CLASS_NAME = "io/github/landerlyoung/jennysample/RequestListener"; + + + +private: + // thread safe init + static std::atomic_bool sInited; + static std::mutex sInitLock; + + JNIEnv* mJniEnv; + jobject mJavaObjectReference; + +public: + + static bool initClazz(JNIEnv *env); + + static void releaseClazz(JNIEnv *env); + + static void assertInited(JNIEnv *env) { + assert(initClazz(env)); + } + + RequestListenerProxy(JNIEnv *env, jobject javaObj) + : mJniEnv(env), mJavaObjectReference(javaObj) { + assertInited(env); + } + + RequestListenerProxy(const RequestListenerProxy &from) = default; + RequestListenerProxy &operator=(const RequestListenerProxy &) = default; + + RequestListenerProxy(RequestListenerProxy &&from) + : mJniEnv(from.mJniEnv), mJavaObjectReference(from.mJavaObjectReference) { + from.mJavaObjectReference = nullptr; + } + + ~RequestListenerProxy() = default; + + // helper method to get underlay jobject reference + jobject operator*() { + return mJavaObjectReference; + } + + // helper method to delete JNI local ref. + // use only when you really understand JNIEnv::DeleteLocalRef. + void deleteLocalRef() { + if (mJavaObjectReference) { + mJniEnv->DeleteLocalRef(mJavaObjectReference); + mJavaObjectReference = nullptr; + } + } + + // === java methods below === + + + // method: public abstract void onResponse(boolean success, java.lang.String rsp) + void onResponse(jboolean success, jstring rsp) const { + mJniEnv->CallVoidMethod(mJavaObjectReference, sMethod_onResponse_0, success, rsp); + } + + + +private: + static jclass sClazz; + + static jmethodID sMethod_onResponse_0; + + +}; + diff --git a/sample-java/src/main/cpp/jni_onload.cpp b/sample-java/src/main/cpp/jni_onload.cpp new file mode 100644 index 0000000..ff05c43 --- /dev/null +++ b/sample-java/src/main/cpp/jni_onload.cpp @@ -0,0 +1,23 @@ +/** +* <pre> +* Author: landerlyoung@gmail.com +* Date: 2019-09-26 +* Time: 15:32 +* Life with Passion, Code with Creativity. +* </pre> +*/ + +#include <jni.h> +#include <cassert> +#include "ComputeInNative.h" + +JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { + JNIEnv *env; + if (vm->GetEnv(reinterpret_cast<void**>(&env), + JNI_VERSION_1_6) != JNI_OK) { + return -1; + } + assert(ComputeInNative::registerNativeFunctions(env)); + return JNI_VERSION_1_6; +} + diff --git a/sample-java/src/main/java/io/github/landerlyoung/jennysample/ComputeInNative.java b/sample-java/src/main/java/io/github/landerlyoung/jennysample/ComputeInNative.java new file mode 100644 index 0000000..3e2d81a --- /dev/null +++ b/sample-java/src/main/java/io/github/landerlyoung/jennysample/ComputeInNative.java @@ -0,0 +1,60 @@ +/** + * Copyright 2016 landerlyoung@gmail.com + * + * 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 io.github.landerlyoung.jennysample; + +import java.util.Map; + +import io.github.landerlyoung.jenny.NativeClass; +import io.github.landerlyoung.jenny.NativeFieldProxy; +import io.github.landerlyoung.jenny.NativeClassProxy; + +/** + * Author: landerlyoung@gmail.com + * Date: 2016-06-19 + * Time: 21:51 + * Life with Passion, Code with Creativity. + */ + +/**add this annotation to generate native proxy class + so we chan access the mNativeContext field with ease. + and also, disable all field getter/setter and method proxy, + except for those we explicitly annotated with {@link NativeFieldProxy} + */ +@NativeClassProxy(allFields = false, allMethods = false) +//java project should disable android log marcos +@NativeClass() +public class ComputeInNative { + static { + System.loadLibrary("jnilib"); + } + + @NativeFieldProxy + private long nativeContext; + + public ComputeInNative() { + + } + + public native boolean init(); + + public native void release(); + + public native void setParam(Map<String, String> globalHttpParam); + + public native Map<String, String> getGlobalParam(); + + public native boolean request(String json, RequestListener listener); +} diff --git a/sample-java/src/main/java/io/github/landerlyoung/jennysample/Main.java b/sample-java/src/main/java/io/github/landerlyoung/jennysample/Main.java new file mode 100644 index 0000000..66d5c9c --- /dev/null +++ b/sample-java/src/main/java/io/github/landerlyoung/jennysample/Main.java @@ -0,0 +1,37 @@ +/** + * Copyright 2016 landerlyoung@gmail.com + * + * 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 io.github.landerlyoung.jennysample; + +import java.util.HashMap; + +/** + * Author: landerlyoung@gmail.com + * Date: 2016-06-19 + * Time: 21:51 + * Life with Passion, Code with Creativity. + */ + +public class Main { + public static void main(String[] args) { + ComputeInNative engine = new ComputeInNative(); + engine.init(); + engine.setParam(new HashMap<>()); + engine.request("{req:0}", ((success, rsp) -> { + System.out.println("success=" + success + ", rsp=" + rsp); + })); + engine.release(); + } +} diff --git a/sample-java/src/main/java/io/github/landerlyoung/jennysample/RequestListener.java b/sample-java/src/main/java/io/github/landerlyoung/jennysample/RequestListener.java new file mode 100644 index 0000000..fa7e39f --- /dev/null +++ b/sample-java/src/main/java/io/github/landerlyoung/jennysample/RequestListener.java @@ -0,0 +1,29 @@ +/** + * Copyright 2016 landerlyoung@gmail.com + * + * 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 io.github.landerlyoung.jennysample; + +import io.github.landerlyoung.jenny.NativeClassProxy; + +/** + * Author: landerlyoung@gmail.com + * Date: 2016-06-19 + * Time: 21:54 + * Life with Passion, Code with Creativity. + */ +@NativeClassProxy +public interface RequestListener { + void onResponse(boolean success, String rsp); +} |
