From e94622960dc156dfc94f6f58eb978bef5dcb6e31 Mon Sep 17 00:00:00 2001 From: coderhyme Date: Thu, 8 Oct 2015 06:13:35 -0700 Subject: [PATCH] Remove unnecessary files and codes from resource-container android. RcsBundleInfo not needs native peer any more. The dependency on rcs_common.so for building jni is removed. Change-Id: I8e8af13a95f418eb9a3d2683eefd7f320ed6668d Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/3777 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- service/resource-container/android/.gitignore | 6 +- service/resource-container/android/SConscript | 6 +- .../service/resourcecontainer/RcsBundleInfo.java | 36 ++--- .../org/iotivity/service/utils/RcsException.java | 33 ----- .../service/utils/RcsIllegalStateException.java | 35 ----- .../java/org/iotivity/service/utils/RcsObject.java | 58 -------- .../service/utils/RcsPlatformException.java | 22 --- .../resource-container/src/main/jni/Android.mk | 9 +- .../resource-container/src/main/jni/JniMain.cpp | 7 - .../src/main/jni/JniRcsBundleInfo.cpp | 100 ------------- .../src/main/jni/JniRcsBundleInfo.h | 60 -------- .../src/main/jni/JniRcsObject.cpp | 48 ------ .../resource-container/src/main/jni/JniRcsObject.h | 125 ---------------- .../src/main/jni/JniRcsResourceContainer.cpp | 49 ++++--- .../src/main/jni/util/JavaClasses.cpp | 39 ----- .../src/main/jni/util/JavaClasses.h | 58 -------- .../src/main/jni/util/JavaExceptions.cpp | 57 -------- .../src/main/jni/util/JavaExceptions.h | 50 ------- .../src/main/jni/util/JavaGlobalRef.h | 61 -------- .../src/main/jni/util/JavaLocalRef.h | 42 +++--- .../src/main/jni/util/ScopedEnv.h | 162 --------------------- 21 files changed, 73 insertions(+), 990 deletions(-) delete mode 100644 service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsException.java delete mode 100644 service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsIllegalStateException.java delete mode 100644 service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsObject.java delete mode 100644 service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsPlatformException.java delete mode 100644 service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.cpp delete mode 100644 service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.h delete mode 100644 service/resource-container/android/resource-container/src/main/jni/JniRcsObject.cpp delete mode 100644 service/resource-container/android/resource-container/src/main/jni/JniRcsObject.h delete mode 100644 service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.cpp delete mode 100644 service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.h delete mode 100644 service/resource-container/android/resource-container/src/main/jni/util/JavaGlobalRef.h delete mode 100644 service/resource-container/android/resource-container/src/main/jni/util/ScopedEnv.h diff --git a/service/resource-container/android/.gitignore b/service/resource-container/android/.gitignore index 49ffed7..3f04414 100644 --- a/service/resource-container/android/.gitignore +++ b/service/resource-container/android/.gitignore @@ -4,6 +4,6 @@ /.idea/libraries .DS_Store /build/* -/service/build/* -/service/src/main/obj/* -/service/src/main/libs/* +/resource-container/build/* +/resource-container/src/main/obj/* +/resource-container/src/main/libs/* diff --git a/service/resource-container/android/SConscript b/service/resource-container/android/SConscript index ce0d922..0fa51a3 100644 --- a/service/resource-container/android/SConscript +++ b/service/resource-container/android/SConscript @@ -44,9 +44,7 @@ if not os.path.exists(android_home + '/platforms/android-21') or not os.path.exi def ensure_libs(target, source, env): - return target, [source, env.get('BUILD_DIR') + 'librcs_server.so', - env.get('BUILD_DIR') + 'librcs_client.so', - env.get('BUILD_DIR') + 'librcs_container.so'] + return target, [source, env.get('BUILD_DIR') + 'librcs_container.so'] jdk_env = Environment(ENV=os.environ) jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + @@ -54,5 +52,5 @@ jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + emitter = ensure_libs) jdk_env['BUILD_DIR'] = env.get('BUILD_DIR') -jdk_env.Gradle(target="service/objs", +jdk_env.Gradle(target="resource-container/objs", source="resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsBundleInfo.java") diff --git a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsBundleInfo.java b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsBundleInfo.java index 670ceca..f782a5b 100644 --- a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsBundleInfo.java +++ b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/resourcecontainer/RcsBundleInfo.java @@ -24,23 +24,23 @@ */ package org.iotivity.service.resourcecontainer; -import org.iotivity.service.utils.RcsObject; - /** * This class provides APIs for getting and setting the Bundle Information */ -public class RcsBundleInfo extends RcsObject { - private native String nativeGetID(); - - private native String nativeGetPath(); - - private native String nativeGetActivatorName(); - - private native String nativeGetLibraryPath(); - - private native String nativeGetVersion(); +public class RcsBundleInfo { + private final String mId; + private final String mPath; + private final String mActivatorName; + private final String mLibraryPath; + private final String mVersion; - private RcsBundleInfo() { + private RcsBundleInfo(String id, String path, String activatorName, + String libraryPath, String version) { + mId = id; + mPath = path; + mActivatorName = activatorName; + mLibraryPath = libraryPath; + mVersion = version; } /** @@ -50,7 +50,7 @@ public class RcsBundleInfo extends RcsObject { * */ public String getID() { - return nativeGetID(); + return mId; } /** @@ -60,7 +60,7 @@ public class RcsBundleInfo extends RcsObject { * */ public String getPath() { - return nativeGetPath(); + return mPath; } /** @@ -70,7 +70,7 @@ public class RcsBundleInfo extends RcsObject { * */ public String getActivatorName() { - return nativeGetActivatorName(); + return mActivatorName; } /** @@ -80,7 +80,7 @@ public class RcsBundleInfo extends RcsObject { * */ public String getLibraryPath() { - return nativeGetLibraryPath(); + return mLibraryPath; } /** @@ -90,7 +90,7 @@ public class RcsBundleInfo extends RcsObject { * */ public String getVersion() { - return nativeGetVersion(); + return mVersion; } } diff --git a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsException.java b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsException.java deleted file mode 100644 index 70b46c7..0000000 --- a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsException.java +++ /dev/null @@ -1,33 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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 org.iotivity.service.utils; - -/** - * An exception that indicates there was an error with execution of Rcs APIs. - */ -public class RcsException extends Exception { - - private static final long serialVersionUID = 7044421943523001940L; - - public RcsException(String message) { - super(message); - } -} diff --git a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsIllegalStateException.java b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsIllegalStateException.java deleted file mode 100644 index c6150f5..0000000 --- a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsIllegalStateException.java +++ /dev/null @@ -1,35 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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 org.iotivity.service.utils; - -/** - * Thrown when an action is attempted at a time when not in the correct state. - * - */ -public class RcsIllegalStateException extends RcsException { - - private static final long serialVersionUID = 6142669404885957616L; - - public RcsIllegalStateException(String message) { - super(message); - } - -} diff --git a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsObject.java b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsObject.java deleted file mode 100644 index 203651c..0000000 --- a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsObject.java +++ /dev/null @@ -1,58 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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 org.iotivity.service.utils; - -public class RcsObject { - static { - System.loadLibrary("gnustl_shared"); - System.loadLibrary("oc_logger"); - System.loadLibrary("connectivity_abstraction"); - System.loadLibrary("ca-interface"); - System.loadLibrary("octbstack"); - System.loadLibrary("oc"); - System.loadLibrary("rcs_client"); - System.loadLibrary("rcs_server"); - System.loadLibrary("rcs_common"); - System.loadLibrary("rcs_container"); - System.loadLibrary("resource_container_jni"); - } - - private long mNativeHandle; - - private native void nativeDispose(); - - protected RcsObject() { - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - - dispose(); - } - - protected void dispose() { - if (mNativeHandle != 0L) nativeDispose(); - } - - protected boolean hasHandle() { - return mNativeHandle != 0L; - } -} diff --git a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsPlatformException.java b/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsPlatformException.java deleted file mode 100644 index 52297be..0000000 --- a/service/resource-container/android/resource-container/src/main/java/org/iotivity/service/utils/RcsPlatformException.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.iotivity.service.utils; - -/** - * Thrown when an operation that has base-layer dependency is failed. - * - */ -public class RcsPlatformException extends RcsException { - - private static final long serialVersionUID = -6093438347973754721L; - - private final int mReasonCode; - - public RcsPlatformException(String message, int reasonCode) { - super(message); - - mReasonCode = reasonCode; - } - - public int getReasonCode() { - return mReasonCode; - } -} diff --git a/service/resource-container/android/resource-container/src/main/jni/Android.mk b/service/resource-container/android/resource-container/src/main/jni/Android.mk index 7a01fc1..09a2288 100644 --- a/service/resource-container/android/resource-container/src/main/jni/Android.mk +++ b/service/resource-container/android/resource-container/src/main/jni/Android.mk @@ -9,26 +9,19 @@ LOCAL_SRC_FILES := $(IOTIVITY_LIB_PATH)/librcs_container.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) -LOCAL_LDLIBS := -L$(LOCAL_PATH)/$(IOTIVITY_LIB_PATH)/ -LOCAL_LDLIBS += -lrcs_common - OIC_SRC_DIR := ../../../../../.. LOCAL_MODULE := resource_container_jni LOCAL_C_INCLUDES := $(LOCAL_PATH)/util -LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/resource/c_common -LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/resource/csdk/stack/include LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/extlibs/boost/boost_1_58_0 -LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/service/resource-encapsulation/include LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/service/resource-container/include -LOCAL_C_INCLUDES += $(OIC_SRC_DIR)/service/resource-encapsulation/src/serverBuilder/include LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard $(LOCAL_PATH)/util/*.cpp)) LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard $(LOCAL_PATH)/*.cpp)) LOCAL_CPPFLAGS := -std=c++0x -frtti -fexceptions -LOCAL_LDLIBS += -llog +LOCAL_LDLIBS := -llog LOCAL_SHARED_LIBRARIES += rcs_container diff --git a/service/resource-container/android/resource-container/src/main/jni/JniMain.cpp b/service/resource-container/android/resource-container/src/main/jni/JniMain.cpp index 16b6894..999a171 100644 --- a/service/resource-container/android/resource-container/src/main/jni/JniMain.cpp +++ b/service/resource-container/android/resource-container/src/main/jni/JniMain.cpp @@ -18,11 +18,8 @@ * ******************************************************************/ -#include "JniRcsObject.h" - #include "JniRcsResourceContainer.h" #include "JavaClasses.h" -#include "JavaExceptions.h" #include "JNIEnvWrapper.h" #include "Log.h" @@ -49,8 +46,6 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) try { initJavaClasses(&envWrapper); - initJavaExceptions(&envWrapper); - initRCSObject(&envWrapper); initRCSResourceContainer(&envWrapper); } catch (const JavaException &) @@ -78,8 +73,6 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) try { clearRCSResourceContainer(&envWrapper); - clearRCSObject(&envWrapper); - clearJavaExceptions(&envWrapper); clearJavaClasses(&envWrapper); } catch (const JavaException &) diff --git a/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.cpp b/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.cpp deleted file mode 100644 index 7bbac93..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#include "JniRcsBundleInfo.h" - -#include "Log.h" -#include "Verify.h" -#include "JniRcsObject.h" - -#include "RCSBundleInfo.h" - -#define LOG_TAG "JNI-RCSBundleInfo" - -using namespace OIC::Service; - -namespace -{ - RCSBundleInfo *getNativeBundleInfo(JNIEnv *env, jobject obj) - { - return getNativeHandleAs< RCSBundleInfo * >(env, obj); - } -} - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetID -(JNIEnv *env, jobject obj) -{ - LOGI("nativeGetID"); - - auto bundleInfo = getNativeBundleInfo(env, obj); - VERIFY_NO_EXC_RET_DEF(env); - - return newStringObject(env, bundleInfo->getID()); -} - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetPath -(JNIEnv *env, jobject obj) -{ - LOGI("nativeGetPath"); - - auto bundleInfo = getNativeBundleInfo(env, obj); - VERIFY_NO_EXC_RET_DEF(env); - - return newStringObject(env, bundleInfo->getPath()); -} - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetActivatorName -(JNIEnv *env, jobject obj) -{ - LOGI("nativeGetActivatorName"); - - auto bundleInfo = getNativeBundleInfo(env, obj); - VERIFY_NO_EXC_RET_DEF(env); - - return newStringObject(env, bundleInfo->getActivatorName()); -} - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetLibraryPath -(JNIEnv *env, jobject obj) -{ - LOGI("nativeGetLibraryPath"); - - auto bundleInfo = getNativeBundleInfo(env, obj); - VERIFY_NO_EXC_RET_DEF(env); - - return newStringObject(env, bundleInfo->getLibraryPath()); -} - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetVersion -(JNIEnv *env, jobject obj) -{ - LOGI("nativeGetVersion"); - - auto bundleInfo = getNativeBundleInfo(env, obj); - VERIFY_NO_EXC_RET_DEF(env); - - return newStringObject(env, bundleInfo->getVersion()); -} - diff --git a/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.h b/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.h deleted file mode 100644 index 83b5dcd..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/JniRcsBundleInfo.h +++ /dev/null @@ -1,60 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -/** @file jni_re_rcs_bundle_info.h - * - * @brief This file contains the JniBundleInfo class - * & declaration of RCSBundleInfo APIs for JNI implementation - */ - -#ifndef JNI_RCS_BUNDLE_INFO_H_ -#define JNI_RCS_BUNDLE_INFO_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetID -(JNIEnv *, jobject); - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetPath -(JNIEnv *, jobject); - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetActivatorName -(JNIEnv *, jobject); - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetLibraryPath -(JNIEnv *, jobject); - -JNIEXPORT jstring JNICALL -Java_org_iotivity_service_resourcecontainer_RcsBundleInfo_nativeGetVersion -(JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif // JNI_RCS_BUNDLE_INFO_H_ - diff --git a/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.cpp b/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.cpp deleted file mode 100644 index 7a3c014..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#include "JniRcsObject.h" - -#include "JavaClasses.h" -#include "JNIEnvWrapper.h" -#include "Log.h" -#include "Verify.h" - -#define LOG_TAG "JNI-RCSObject" - -jfieldID g_field_mNativeHandle; - -void initRCSObject(JNIEnvWrapper *env) -{ - auto clsRCSObject = env->FindClass(PACKAGE_NAME "/utils/RcsObject"); - - g_field_mNativeHandle = env->GetFieldID(clsRCSObject, "mNativeHandle", "J"); -} - -void clearRCSObject(JNIEnvWrapper *env) -{ -} - -JNIEXPORT void JNICALL -Java_org_iotivity_service_utils_RcsObject_nativeDispose(JNIEnv *env, jobject obj) -{ - LOGD("release nativeHandle!"); - releaseNativeHandle(env, obj); -} diff --git a/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.h b/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.h deleted file mode 100644 index cfa7a71..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/JniRcsObject.h +++ /dev/null @@ -1,125 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#ifndef JNI_RCS_OBJECT_H_ -#define JNI_RCS_OBJECT_H_ - -#include - -#include - -#include "JavaClasses.h" -#include "JNIEnvWrapper.h" - -extern jfieldID g_field_mNativeHandle; - -void initRCSObject(JNIEnvWrapper *); -void clearRCSObject(JNIEnvWrapper *); - -namespace Detail -{ - struct BaseHandleHolder - { - virtual ~BaseHandleHolder() {} - }; - - template< typename T > - struct HandleHolder: public BaseHandleHolder - { - HandleHolder(T *ptr) : m_ptr { ptr } {} - - virtual ~HandleHolder() { delete m_ptr; } - - T *m_ptr; - }; - - template< typename ENV > - void *getNativeHandle(ENV *env, jobject obj) - { - return reinterpret_cast< void * >(env->GetLongField(obj, g_field_mNativeHandle)); - } -} - -template< typename ENV > -bool hasNativeHandle(ENV *env, jobject obj) -{ - return Detail::getNativeHandle(env, obj) != nullptr; -} - -template< typename T, typename ENV, typename ...PARAMS > -inline void setSafeNativeHandle(ENV *env, jobject obj, PARAMS &&... params) -{ - static_assert(!std::is_array< T >::value, "Array is not supported!"); - - std::unique_ptr< Detail::HandleHolder< T > > p( - new Detail::HandleHolder< T > { new T{ std::forward< PARAMS >(params)... } }); - - env->SetLongField(obj, g_field_mNativeHandle, reinterpret_cast< jlong >(p.get())); - - if (env->ExceptionCheck()) return; - - p.release(); -} - -template< typename ENV > -void releaseNativeHandle(ENV *env, jobject obj) -{ - auto handleHolder = reinterpret_cast< Detail::BaseHandleHolder * >( - env->GetLongField(obj, g_field_mNativeHandle)); - - delete handleHolder; - - env->SetLongField(obj, g_field_mNativeHandle, 0); -} - - -template< typename T > -inline T &getNativeHandleAs(JNIEnv *env, jobject obj) -{ - auto handleHolder = static_cast< Detail::HandleHolder< T >* >(Detail::getNativeHandle(env, obj)); - - if (!handleHolder) - { - env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), "Internal handle is null!"); - } - - return *handleHolder->m_ptr; -} - -template< typename T > -inline T &getNativeHandleAs(JNIEnvWrapper *env, jobject obj) -{ - getNativeHandleAs< T >(env->get(), obj); - if (env->ExceptionCheck()) throw JavaException(); -} - - -#ifdef __cplusplus -extern "C" { -#endif - -JNIEXPORT void JNICALL -Java_org_iotivity_service_utils_RcsObject_nativeDispose(JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif - -#endif // JNI_RCS_OBJECT_H_ diff --git a/service/resource-container/android/resource-container/src/main/jni/JniRcsResourceContainer.cpp b/service/resource-container/android/resource-container/src/main/jni/JniRcsResourceContainer.cpp index 5b829d7..e23662a 100644 --- a/service/resource-container/android/resource-container/src/main/jni/JniRcsResourceContainer.cpp +++ b/service/resource-container/android/resource-container/src/main/jni/JniRcsResourceContainer.cpp @@ -20,24 +20,19 @@ #include "JniRcsResourceContainer.h" +#include "JavaClasses.h" #include "JavaLocalRef.h" #include "JNIEnvWrapper.h" #include "Log.h" #include "Verify.h" -#include "JniRcsObject.h" #include "RCSResourceContainer.h" -#include "RCSBundleInfo.h" #define LOG_TAG "JNI-RCSResourceContainer" using namespace OIC::Service; -#define CLS_NAME_BUNDLE_INFO PACKAGE_NAME "/resourcecontainer/RcsBundleInfo" - -#include - - +#define CLS_NAME_BUNDLE_INFO "org/iotivity/service/resourcecontainer/RcsBundleInfo" namespace { @@ -60,24 +55,41 @@ namespace JavaLocalObject entryObj { env, invoke_Iterator_next(env, iterObj) }; JavaLocalString keyObj { env, - static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj)) - }; + static_cast< jstring >(invoke_MapEntry_getKey(env, entryObj)) }; + JavaLocalString valueObj { env, - static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj)) - }; + static_cast< jstring >(invoke_MapEntry_getValue(env, entryObj)) }; ret.emplace(toStdString(env, keyObj), toStdString(env, valueObj)); } return ret; } + + jobject newBundleInfoObj(JNIEnvWrapper *env, const std::unique_ptr< RCSBundleInfo > &bundleInfo) + { + JavaLocalString id{env, newStringObject(env, bundleInfo->getID()) }; + JavaLocalString path{env, newStringObject(env, bundleInfo->getPath()) }; + JavaLocalString activatorName{env, newStringObject(env, bundleInfo->getActivatorName()) }; + JavaLocalString libraryPath{env, newStringObject(env, bundleInfo->getLibraryPath()) }; + JavaLocalString version{env, newStringObject(env, bundleInfo->getVersion()) }; + + return env->NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo, + id.get(), path.get(), activatorName.get(), libraryPath.get(), version.get()); + } } void initRCSResourceContainer(JNIEnvWrapper *env) { g_cls_RCSBundleInfo = env->FindClassAsGlobalRef(CLS_NAME_BUNDLE_INFO); - g_ctor_RCSBundleInfo = env->GetConstructorID(g_cls_RCSBundleInfo, "()V"); + g_ctor_RCSBundleInfo = env->GetConstructorID(g_cls_RCSBundleInfo, "(" + AS_SIG(CLS_NAME_STRING) + AS_SIG(CLS_NAME_STRING) + AS_SIG(CLS_NAME_STRING) + AS_SIG(CLS_NAME_STRING) + AS_SIG(CLS_NAME_STRING) + ")V"); } void clearRCSResourceContainer(JNIEnvWrapper *env) @@ -165,14 +177,9 @@ Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundl { auto listObj = newArrayList(&envWrapper); - for (auto& uniqeBundleInfo : RCSResourceContainer::getInstance()->listBundles()) + for (const auto& bundleInfo : RCSResourceContainer::getInstance()->listBundles()) { - RCSBundleInfo* bundleInfo = uniqeBundleInfo.release(); - JavaLocalObject bundleInfoObj { &envWrapper, - envWrapper.NewObject(g_cls_RCSBundleInfo, g_ctor_RCSBundleInfo) }; - - setSafeNativeHandle< RCSBundleInfo* >(&envWrapper, bundleInfoObj, bundleInfo); - + JavaLocalObject bundleInfoObj{ &envWrapper, newBundleInfoObj(&envWrapper, bundleInfo) }; invoke_Collection_add(&envWrapper, listObj, bundleInfoObj); } return listObj; @@ -268,9 +275,9 @@ Java_org_iotivity_service_resourcecontainer_RcsResourceContainer_nativeListBundl auto listObj = newArrayList(&envWrapper); - for (const auto & s : RCSResourceContainer::getInstance()->listBundleResources(id)) + for (const auto& s : RCSResourceContainer::getInstance()->listBundleResources(id)) { - JavaLocalString strObj { &envWrapper, newStringObject(&envWrapper, s) }; + JavaLocalString strObj{ &envWrapper, newStringObject(&envWrapper, s) }; invoke_Collection_add(&envWrapper, listObj, strObj); } diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.cpp b/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.cpp index f84f4d9..e9c88a8 100644 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.cpp +++ b/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.cpp @@ -22,21 +22,12 @@ #include "JNIEnvWrapper.h" -jclass g_cls_String; -jclass g_cls_Integer; -jclass g_cls_Double; -jclass g_cls_Boolean; - jclass g_cls_ArrayList; jclass g_cls_Set; jclass g_cls_Map; jclass g_cls_MapEntry; jclass g_cls_Iterator; -jmethodID g_method_Boolean_booleanValue; -jmethodID g_method_Integer_intValue; -jmethodID g_method_Double_doubleValue; - jmethodID g_method_Collection_add; jmethodID g_method_Set_iterator; @@ -50,36 +41,10 @@ jmethodID g_method_MapEntry_getValue; jmethodID g_method_Iterator_hasNext; jmethodID g_method_Iterator_next; -jmethodID g_ctor_Boolean; -jmethodID g_ctor_Integer; -jmethodID g_ctor_Double; - jmethodID g_ctor_ArrayList; -namespace -{ - inline void initPrimitiveTypes(JNIEnvWrapper *env) - { - g_cls_Boolean = env->FindClassAsGlobalRef(CLS_NAME_BOOLEAN); - g_ctor_Boolean = env->GetConstructorID(g_cls_Boolean, "(Z)V"); - g_method_Boolean_booleanValue = env->GetMethodID(g_cls_Boolean, "booleanValue", "()Z"); - - g_cls_Integer = env->FindClassAsGlobalRef(CLS_NAME_INTEGER); - g_ctor_Integer = env->GetConstructorID(g_cls_Integer, "(I)V"); - g_method_Integer_intValue = env->GetMethodID(g_cls_Integer, "intValue", "()I"); - - g_cls_Double = env->FindClassAsGlobalRef(CLS_NAME_DOUBLE); - g_ctor_Double = env->GetConstructorID(g_cls_Double, "(D)V"); - g_method_Double_doubleValue = env->GetMethodID(g_cls_Double, "doubleValue", "()D"); - - g_cls_String = env->FindClassAsGlobalRef(CLS_NAME_STRING); - } -} - void initJavaClasses(JNIEnvWrapper *env) { - initPrimitiveTypes(env); - auto clsCollection = env->FindClass(CLS_NAME_COLLECTION); g_method_Collection_add = env->GetMethodID(clsCollection, "add", "(" AS_SIG(CLS_NAME_OBJECT) ")Z"); @@ -108,10 +73,6 @@ void initJavaClasses(JNIEnvWrapper *env) void clearJavaClasses(JNIEnvWrapper *env) { - env->DeleteGlobalRef(g_cls_Boolean); - env->DeleteGlobalRef(g_cls_Integer); - env->DeleteGlobalRef(g_cls_Double); - env->DeleteGlobalRef(g_cls_String); env->DeleteGlobalRef(g_cls_Set); env->DeleteGlobalRef(g_cls_Map); env->DeleteGlobalRef(g_cls_MapEntry); diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.h b/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.h index b91ffea..b37211b 100644 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.h +++ b/service/resource-container/android/resource-container/src/main/jni/util/JavaClasses.h @@ -25,13 +25,8 @@ #include -#define PACKAGE_NAME "org/iotivity/service" - #define CLS_NAME_OBJECT "java/lang/Object" #define CLS_NAME_STRING "java/lang/String" -#define CLS_NAME_INTEGER "java/lang/Integer" -#define CLS_NAME_DOUBLE "java/lang/Double" -#define CLS_NAME_BOOLEAN "java/lang/Boolean" #define CLS_NAME_COLLECTION "java/util/Collection" #define CLS_NAME_ARRAY_LIST "java/util/ArrayList" @@ -40,29 +35,16 @@ #define CLS_NAME_MAP_ENTRY "java/util/Map$Entry" #define CLS_NAME_ITERATOR "java/util/Iterator" -#define EXC_NAME_RCS PACKAGE_NAME "/utils/RcsException" -#define EXC_NAME_PLATFORM PACKAGE_NAME "/utils/RcsPlatformException" -#define EXC_NAME_ILLEGAL_STATE PACKAGE_NAME "/utils/RcsIllegalStateException" - #define AS_SIG(CLS_NAME) "L" CLS_NAME ";" class JNIEnvWrapper; -extern jclass g_cls_Integer; -extern jclass g_cls_Double; -extern jclass g_cls_Boolean; -extern jclass g_cls_String; - extern jclass g_cls_ArrayList; extern jclass g_cls_Set; extern jclass g_cls_Map; extern jclass g_cls_MapEntry; extern jclass g_cls_Iterator; -extern jmethodID g_method_Boolean_booleanValue; -extern jmethodID g_method_Integer_intValue; -extern jmethodID g_method_Double_doubleValue; - extern jmethodID g_method_Collection_add; extern jmethodID g_method_Set_iterator; @@ -76,34 +58,12 @@ extern jmethodID g_method_MapEntry_getValue; extern jmethodID g_method_Iterator_hasNext; extern jmethodID g_method_Iterator_next; -extern jmethodID g_ctor_Boolean; -extern jmethodID g_ctor_Integer; -extern jmethodID g_ctor_Double; - extern jmethodID g_ctor_ArrayList; void initJavaClasses(JNIEnvWrapper *); void clearJavaClasses(JNIEnvWrapper *); template< typename ENV > -inline jobject newBooleanObject(ENV *env, bool value) -{ - return env->NewObject(g_cls_Boolean, g_ctor_Boolean, value); -} - -template< typename ENV > -inline jobject newIntegerObject(ENV *env, int value) -{ - return env->NewObject(g_cls_Integer, g_ctor_Integer, value); -} - -template< typename ENV > -inline jobject newDoubleObject(ENV *env, double value) -{ - return env->NewObject(g_cls_Double, g_ctor_Double, value); -} - -template< typename ENV > inline jstring newStringObject(ENV *env, const std::string &value) { return env->NewStringUTF(value.c_str()); @@ -138,24 +98,6 @@ inline jobject newArrayList(ENV *env) } template< typename ENV > -inline bool invoke_Boolean_booleanValue(ENV *env, jobject obj) -{ - return env->CallBooleanMethod(obj, g_method_Boolean_booleanValue); -} - -template< typename ENV > -inline int invoke_Integer_intValue(ENV *env, jobject obj) -{ - return env->CallIntMethod(obj, g_method_Integer_intValue); -} - -template< typename ENV > -inline double invoke_Double_doubleValue(ENV *env, jobject obj) -{ - return env->CallDoubleMethod(obj, g_method_Double_doubleValue); -} - -template< typename ENV > inline jboolean invoke_Collection_add(ENV *env, jobject collectionObj, jobject valueObj) { return env->CallBooleanMethod(collectionObj, g_method_Collection_add, valueObj); diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.cpp b/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.cpp deleted file mode 100644 index 4774a72..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#include "JavaExceptions.h" - -#include "JNIEnvWrapper.h" -#include "Verify.h" - -#include "RCSException.h" - -namespace -{ - jclass g_cls_PlatformException; - - jmethodID g_ctor_PlatformException; -} - -void initJavaExceptions(JNIEnvWrapper *env) -{ - g_cls_PlatformException = env->FindClassAsGlobalRef(EXC_NAME_PLATFORM); - g_ctor_PlatformException = env->GetConstructorID(g_cls_PlatformException, - "(" AS_SIG(CLS_NAME_STRING) "I)V"); -} - -void clearJavaExceptions(JNIEnvWrapper *env) -{ - env->DeleteGlobalRef(g_cls_PlatformException); -} - -void throwPlatformException(JNIEnv *env, const OIC::Service::RCSPlatformException &e) -{ - auto msg = newStringObject(env, e.getReason()); - VERIFY_NO_EXC(env); - - auto exObj = env->NewObject(g_cls_PlatformException, g_ctor_PlatformException, - msg, e.getReasonCode()); - VERIFY_NO_EXC(env); - - env->Throw(static_cast< jthrowable >(exObj)); -} diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.h b/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.h deleted file mode 100644 index 40b8999..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaExceptions.h +++ /dev/null @@ -1,50 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#ifndef JAVA_EXCEPTIONS_H_ -#define JAVA_EXCEPTIONS_H_ - -#include - -#include "JavaClasses.h" - -namespace OIC -{ - namespace Service - { - class RCSPlatformException; - } -} - -class JNIEnvWrapper; - -void initJavaExceptions(JNIEnvWrapper *); -void clearJavaExceptions(JNIEnvWrapper *); - -void throwPlatformException(JNIEnv *, const OIC::Service::RCSPlatformException &); - -template < typename ENV > -void throwRCSException(ENV *env, const char *msg) -{ - env->ThrowNew(env->FindClass(EXC_NAME_RCS), msg); -} - - -#endif // JAVA_EXCEPTIONS_H_ diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaGlobalRef.h b/service/resource-container/android/resource-container/src/main/jni/util/JavaGlobalRef.h deleted file mode 100644 index 812fb41..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaGlobalRef.h +++ /dev/null @@ -1,61 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#ifndef JAVA_GLOBAL_REF_H_ -#define JAVA_GLOBAL_REF_H_ - -#include -#include - -#include "ScopedEnv.h" - -class JavaGlobalRef -{ - public: - JavaGlobalRef(JNIEnv *env, jobject obj) noexcept : - m_obj { } - { - assert(env &&"JNIEnv is nullptr"); - - static auto deleter = [](jobject * obj) - { - if (obj && *obj) - { - ScopedEnv env; - env->DeleteGlobalRef(*obj); - } - delete obj; - }; - - m_obj.reset(new jobject{ env->NewGlobalRef(obj) }, deleter); - } - - operator jobject() const noexcept - { - return *m_obj; - } - - private: - std::shared_ptr< jobject > m_obj; -}; - - - -#endif // JAVA_GLOBAL_REF_H_ diff --git a/service/resource-container/android/resource-container/src/main/jni/util/JavaLocalRef.h b/service/resource-container/android/resource-container/src/main/jni/util/JavaLocalRef.h index 34e7d1e..af4f204 100644 --- a/service/resource-container/android/resource-container/src/main/jni/util/JavaLocalRef.h +++ b/service/resource-container/android/resource-container/src/main/jni/util/JavaLocalRef.h @@ -27,40 +27,40 @@ template < typename T > class JavaLocalRef { - public: +public: JavaLocalRef(JNIEnv *env, T obj) noexcept : m_env { env }, m_obj { obj } { - assert(env &&"JNIEnv is nullptr"); + assert(env && "JNIEnv is nullptr"); } - template< typename ENV > + template< typename ENV > JavaLocalRef(ENV *env, T obj) noexcept : - m_env { env->get() }, - m_obj { obj } - { - assert(env &&"JNIEnv is nullptr"); - } + m_env { env->get() }, + m_obj { obj } + { + assert(env && "JNIEnv is nullptr"); + } - ~JavaLocalRef() - { - if (m_obj) m_env->DeleteLocalRef(m_obj); - } + ~JavaLocalRef() + { + if (m_obj) m_env->DeleteLocalRef(m_obj); + } - operator bool() const noexcept { return m_obj; } - operator T() const noexcept { return m_obj; } + operator bool() const noexcept { return m_obj; } + operator T() const noexcept { return m_obj; } - jobject get() const noexcept { return m_obj; } + jobject get() const noexcept { return m_obj; } - JavaLocalRef(const JavaLocalRef &) = delete; - JavaLocalRef &operator=(const JavaLocalRef &) = delete; + JavaLocalRef(const JavaLocalRef &) = delete; + JavaLocalRef &operator=(const JavaLocalRef &) = delete; - JavaLocalRef &operator=(JavaLocalRef && ) = delete; + JavaLocalRef &operator=(JavaLocalRef && ) = delete; - private: - JNIEnv *m_env; - T m_obj; +private: + JNIEnv *m_env; + T m_obj; }; typedef JavaLocalRef< jobject > JavaLocalObject; diff --git a/service/resource-container/android/resource-container/src/main/jni/util/ScopedEnv.h b/service/resource-container/android/resource-container/src/main/jni/util/ScopedEnv.h deleted file mode 100644 index a8b7d4b..0000000 --- a/service/resource-container/android/resource-container/src/main/jni/util/ScopedEnv.h +++ /dev/null @@ -1,162 +0,0 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * 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. - * - ******************************************************************/ - -#ifndef RCS_JIN_SCOPEDENV_H_ -#define RCS_JIN_SCOPEDENV_H_ - -#include - -#include - -#include "JNIEnvWrapper.h" -#include "Log.h" - -extern JavaVM *g_jvm; - -namespace Detail -{ - inline std::pair getEnv() - { - JNIEnv *env { }; - bool needToDetach { }; - - auto ret = g_jvm->GetEnv((void **) &env, JNI_VERSION_1_6); - - switch (ret) - { - case JNI_OK: - break; - - case JNI_EDETACHED: - { - auto attachRet = g_jvm->AttachCurrentThread(&env, NULL); - - if (attachRet != JNI_OK) - { - LOGT_E("JNI-ScopedEnv", "Failed to get the environment : %d", attachRet); - } - else - { - needToDetach = true; - } - break; - } - case JNI_EVERSION: - LOGT_E("JNI-ScopedEnv", "JNI version not supported"); - break; - - default: - LOGT_E("JNI-ScopedEnv", "Failed to get the environment"); - break; - } - - return { env, needToDetach }; - } -} - -class ScopedEnv -{ - public: - ScopedEnv() noexcept : - m_env { }, - m_needToDetach { false } - { - auto val = Detail::getEnv(); - - m_env = val.first; - m_needToDetach = val.second; - } - - ~ScopedEnv() - { - if (m_env && m_needToDetach) - { - g_jvm->DetachCurrentThread(); - } - } - - ScopedEnv(const ScopedEnv &) = delete; - ScopedEnv &operator=(const ScopedEnv &) = delete; - - operator bool() const noexcept - { - return m_env; - } - - JNIEnv *operator->() noexcept - { - return m_env; - } - - JNIEnv *get() noexcept - { - return m_env; - } - - private: - JNIEnv *m_env; - bool m_needToDetach; -}; - -class ScopedEnvWrapper -{ - public: - ScopedEnvWrapper() noexcept : - m_env { }, - m_needToDetach { false } - { - auto val = Detail::getEnv(); - - m_env = val.first; - m_needToDetach = val.second; - } - - ~ScopedEnvWrapper() - { - if (m_env && m_needToDetach) - { - g_jvm->DetachCurrentThread(); - } - } - - ScopedEnvWrapper(const ScopedEnvWrapper &) = delete; - ScopedEnvWrapper &operator=(const ScopedEnvWrapper &) = delete; - - operator bool() const noexcept - { - return m_env; - } - - JNIEnvWrapper *operator->() noexcept - { - return &m_env; - } - - JNIEnvWrapper *get() noexcept - { - return &m_env; - } - - private: - JNIEnvWrapper m_env; - bool m_needToDetach; -}; - -#endif // RCS_JIN_SCOPEDENV_H_ -- 2.7.4