Home | History | Annotate | Download | only in jni
      1 /* //device/libs/android_runtime/android_debug_JNITest.cpp
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #define LOG_TAG "DebugJNI"
     19 
     20 #include "jni.h"
     21 #include "nativehelper/JNIHelp.h"
     22 #include "utils/Log.h"
     23 #include "utils/misc.h"
     24 //#include "android_runtime/AndroidRuntime.h"
     25 
     26 namespace android {
     27 
     28 /*
     29  * Implements:
     30  *  native int part1(int intArg, double doubleArg, String stringArg,
     31  *      int[] arrayArg)
     32  */
     33 static jint android_debug_JNITest_part1(JNIEnv* env, jobject object,
     34     jint intArg, jdouble doubleArg, jstring stringArg, jobjectArray arrayArg)
     35 {
     36     jclass clazz;
     37     jmethodID part2id;
     38     jsize arrayLen;
     39     jint arrayVal;
     40     int result = -2;
     41 
     42     ALOGI("JNI test: in part1, intArg=%d, doubleArg=%.3f\n", intArg, doubleArg);
     43 
     44     /* find "int part2(double doubleArg, int fromArray, String stringArg)" */
     45     clazz = env->GetObjectClass(object);
     46     part2id = env->GetMethodID(clazz,
     47                 "part2", "(DILjava/lang/String;)I");
     48     if (part2id == NULL) {
     49         ALOGE("JNI test: unable to find part2\n");
     50         return -1;
     51     }
     52 
     53     /* get the length of the array */
     54     arrayLen = env->GetArrayLength(arrayArg);
     55     ALOGI("  array size is %d\n", arrayLen);
     56 
     57     /*
     58      * Get the last element in the array.
     59      * Use the Get<type>ArrayElements functions instead if you need access
     60      * to multiple elements.
     61      */
     62     arrayVal = (int) env->GetObjectArrayElement(arrayArg, arrayLen-1);
     63     ALOGI("  array val is %d\n", arrayVal);
     64 
     65     /* call this->part2 */
     66     result = env->CallIntMethod(object, part2id,
     67         doubleArg, arrayVal, stringArg);
     68 
     69     return result;
     70 }
     71 
     72 /*
     73  * Implements:
     74  *  private static native int part3(String stringArg);
     75  */
     76 static jint android_debug_JNITest_part3(JNIEnv* env, jclass clazz,
     77     jstring stringArg)
     78 {
     79     const char* utfChars;
     80     jboolean isCopy;
     81 
     82     ALOGI("JNI test: in part3\n");
     83 
     84     utfChars = env->GetStringUTFChars(stringArg, &isCopy);
     85 
     86     ALOGI("  String is '%s', isCopy=%d\n", (const char*) utfChars, isCopy);
     87 
     88     env->ReleaseStringUTFChars(stringArg, utfChars);
     89 
     90     return 2000;
     91 }
     92 
     93 /*
     94  * JNI registration.
     95  */
     96 static JNINativeMethod gMethods[] = {
     97     /* name, signature, funcPtr */
     98     { "part1",      "(IDLjava/lang/String;[I)I",
     99             (void*) android_debug_JNITest_part1 },
    100     { "part3",      "(Ljava/lang/String;)I",
    101             (void*) android_debug_JNITest_part3 },
    102 };
    103 int register_android_debug_JNITest(JNIEnv* env)
    104 {
    105     return jniRegisterNativeMethods(env, "android/debug/JNITest",
    106         gMethods, NELEM(gMethods));
    107 }
    108 
    109 #if 0
    110 /* trampoline into C++ */
    111 extern "C"
    112 int register_android_debug_JNITest_C(JNIEnv* env)
    113 {
    114     return android::register_android_debug_JNITest(env);
    115 }
    116 #endif
    117 
    118 }; // namespace android
    119 
    120