1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "valueOf"
18 
19 #include "valueOf.h"
20 
21 #include <nativehelper/JNIHelp.h>
22 
23 #include "JniConstants.h"
24 
25 template <typename T>
valueOf(JNIEnv * env,jclass c,const char * signature,const T & value)26 static jobject valueOf(JNIEnv* env, jclass c, const char* signature, const T& value) {
27     static jmethodID valueOfMethod = env->GetStaticMethodID(c, "valueOf", signature);
28     if (env->ExceptionCheck()) {
29         return NULL;
30     }
31     jobject result = env->CallStaticObjectMethod(c, valueOfMethod, value);
32     if (env->ExceptionCheck()) {
33         return NULL;
34     }
35     return result;
36 }
37 
booleanValueOf(JNIEnv * env,jboolean value)38 jobject booleanValueOf(JNIEnv* env, jboolean value) {
39     return valueOf(env, JniConstants::GetBooleanClass(env), "(Z)Ljava/lang/Boolean;", value);
40 }
41 
doubleValueOf(JNIEnv * env,jdouble value)42 jobject doubleValueOf(JNIEnv* env, jdouble value) {
43     return valueOf(env, JniConstants::GetDoubleClass(env), "(D)Ljava/lang/Double;", value);
44 }
45 
integerValueOf(JNIEnv * env,jint value)46 jobject integerValueOf(JNIEnv* env, jint value) {
47     return valueOf(env, JniConstants::GetIntegerClass(env), "(I)Ljava/lang/Integer;", value);
48 }
49 
longValueOf(JNIEnv * env,jlong value)50 jobject longValueOf(JNIEnv* env, jlong value) {
51     return valueOf(env, JniConstants::GetLongClass(env), "(J)Ljava/lang/Long;", value);
52 }
53 
booleanValue(JNIEnv * env,jobject javaLangBoolean)54 jboolean booleanValue(JNIEnv* env, jobject javaLangBoolean) {
55     static jfieldID fid = env->GetFieldID(JniConstants::GetBooleanClass(env), "value", "Z");
56     return env->GetBooleanField(javaLangBoolean, fid);
57 }
58 
intValue(JNIEnv * env,jobject javaLangInteger)59 jint intValue(JNIEnv* env, jobject javaLangInteger) {
60     static jfieldID fid = env->GetFieldID(JniConstants::GetIntegerClass(env), "value", "I");
61     return env->GetIntField(javaLangInteger, fid);
62 }
63