Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / jniutil / src / jni_object.cpp
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "jni_object.h"
22
23 #define LOG_TAG "TM_JObject"
24
25 JObject::JObject(JNIEnv *env) : m_pEnv( env ),
26     m_pObject(NULL),
27     m_pClazz( NULL ),
28     m_fIsNewObject(true)
29 {
30 }
31
32 JObject::JObject(JNIEnv *env, jobject obj) : m_pEnv(NULL),
33     m_pObject(NULL),
34     m_pClazz(NULL),
35     m_fIsNewObject(false)
36 {
37     if ( NULL == env || NULL == obj)
38     {
39         return;
40     }
41
42     m_pEnv = env;
43     m_pObject = obj;
44     m_pClazz = m_pEnv->GetObjectClass( obj );
45 }
46
47 JObject::JObject(JNIEnv *env, const char *classPath) : m_pEnv(NULL),
48     m_pObject(NULL),
49     m_pClazz(NULL),
50     m_fIsNewObject(true)
51 {
52     if ( NULL == env || NULL == classPath)
53     {
54         LOGI("JObject Invalid parameters");
55         return;
56     }
57
58     m_pEnv = env;
59     //m_pClazz = GetJClass( classPath );
60
61     if (NULL == m_pClazz)
62     {
63         LOGE( "GetJClass failed [%s]" , classPath);
64         return;
65     }
66
67     jmethodID mid = env->GetMethodID(m_pClazz, "<init>", "()V");
68     if (NULL == mid)
69     {
70         LOGE( "GetMethodID failed [%s]" , classPath);
71         return;
72     }
73
74     m_pObject = env->NewObject(m_pClazz, mid);
75 }
76
77 JObject::~JObject()
78 {
79     if (m_pEnv)
80     {
81         if (m_pObject && m_fIsNewObject)
82         {
83             m_pEnv->DeleteLocalRef( m_pObject );
84         }
85
86         if (m_pClazz && !m_fIsNewObject)
87         {
88             m_pEnv->DeleteLocalRef( m_pClazz );
89         }
90     }
91 }
92
93 jobject JObject::getObject() const
94 {
95     return m_pObject;
96 }
97
98
99 void JObject::detachObject()
100 {
101     if (m_fIsNewObject)
102     {
103         m_fIsNewObject = false;
104         m_pClazz = NULL;
105     }
106 }
107