1232d8c195ea5d0bdf60f6178c14aa694f6e73f9
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / jni / util / JavaClasses.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 "JavaClasses.h"
22
23 #include "JNIEnvWrapper.h"
24
25 jclass g_cls_String;
26 jclass g_cls_Integer;
27 jclass g_cls_Double;
28 jclass g_cls_Boolean;
29
30 jclass g_cls_ArrayList;
31 jclass g_cls_Set;
32 jclass g_cls_Map;
33 jclass g_cls_MapEntry;
34 jclass g_cls_Iterator;
35
36 jmethodID g_method_Boolean_booleanValue;
37 jmethodID g_method_Integer_intValue;
38 jmethodID g_method_Double_doubleValue;
39
40 jmethodID g_method_Collection_add;
41
42 jmethodID g_method_Set_iterator;
43
44 jmethodID g_method_Map_entrySet;
45 jmethodID g_method_Map_put;
46
47 jmethodID g_method_MapEntry_getKey;
48 jmethodID g_method_MapEntry_getValue;
49
50 jmethodID g_method_Iterator_hasNext;
51 jmethodID g_method_Iterator_next;
52
53 jmethodID g_ctor_Boolean;
54 jmethodID g_ctor_Integer;
55 jmethodID g_ctor_Double;
56
57 jmethodID g_ctor_ArrayList;
58
59 namespace
60 {
61     inline void initPrimitiveTypes(JNIEnvWrapper* env)
62     {
63         g_cls_Boolean = env->FindClassAsGlobalRef(CLS_NAME_BOOLEAN);
64         g_ctor_Boolean = env->GetConstructorID(g_cls_Boolean, "(Z)V");
65         g_method_Boolean_booleanValue = env->GetMethodID(g_cls_Boolean, "booleanValue", "()Z");
66
67         g_cls_Integer = env->FindClassAsGlobalRef(CLS_NAME_INTEGER);
68         g_ctor_Integer = env->GetConstructorID(g_cls_Integer, "(I)V");
69         g_method_Integer_intValue = env->GetMethodID(g_cls_Integer, "intValue", "()I");
70
71         g_cls_Double = env->FindClassAsGlobalRef(CLS_NAME_DOUBLE);
72         g_ctor_Double = env->GetConstructorID(g_cls_Double, "(D)V");
73         g_method_Double_doubleValue = env->GetMethodID(g_cls_Double, "doubleValue", "()D");
74
75         g_cls_String = env->FindClassAsGlobalRef(CLS_NAME_STRING);
76     }
77 }
78
79 void initJavaClasses(JNIEnvWrapper* env)
80 {
81     initPrimitiveTypes(env);
82
83     auto clsCollection = env->FindClass(CLS_NAME_COLLECTION);
84     g_method_Collection_add = env->GetMethodID(clsCollection, "add",
85             "(" AS_SIG(CLS_NAME_OBJECT) ")Z");
86
87     g_cls_ArrayList = env->FindClassAsGlobalRef(CLS_NAME_ARRAY_LIST);
88     g_ctor_ArrayList = env->GetConstructorID(g_cls_ArrayList, "()V");
89
90     g_cls_Set = env->FindClassAsGlobalRef(CLS_NAME_SET);
91     g_method_Set_iterator = env->GetMethodID(g_cls_Set, "iterator", "()" AS_SIG(CLS_NAME_ITERATOR));
92
93     g_cls_Map = env->FindClassAsGlobalRef(CLS_NAME_MAP);
94     g_method_Map_entrySet = env->GetMethodID(g_cls_Map, "entrySet", "()" AS_SIG(CLS_NAME_SET));
95     g_method_Map_put = env->GetMethodID(g_cls_Map, "put",
96             "(" AS_SIG(CLS_NAME_OBJECT) AS_SIG(CLS_NAME_OBJECT) ")" AS_SIG(CLS_NAME_OBJECT));
97
98     g_cls_MapEntry = env->FindClassAsGlobalRef(CLS_NAME_MAP_ENTRY);
99     g_method_MapEntry_getKey = env->GetMethodID(g_cls_MapEntry, "getKey",
100             "()" AS_SIG(CLS_NAME_OBJECT));
101     g_method_MapEntry_getValue = env->GetMethodID(g_cls_MapEntry, "getValue",
102             "()" AS_SIG(CLS_NAME_OBJECT));
103
104     g_cls_Iterator = env->FindClassAsGlobalRef(CLS_NAME_ITERATOR);
105     g_method_Iterator_hasNext = env->GetMethodID(g_cls_Iterator, "hasNext", "()Z");
106     g_method_Iterator_next = env->GetMethodID(g_cls_Iterator, "next", "()" AS_SIG(CLS_NAME_OBJECT));
107 }
108
109 void clearJavaClasses(JNIEnvWrapper* env)
110 {
111     env->DeleteGlobalRef(g_cls_Boolean);
112     env->DeleteGlobalRef(g_cls_Integer);
113     env->DeleteGlobalRef(g_cls_Double);
114     env->DeleteGlobalRef(g_cls_String);
115     env->DeleteGlobalRef(g_cls_Set);
116     env->DeleteGlobalRef(g_cls_Map);
117     env->DeleteGlobalRef(g_cls_MapEntry);
118     env->DeleteGlobalRef(g_cls_Iterator);
119 }