19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* //device/libs/android_runtime/android_debug_JNITest.cpp
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2006, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** you may not use this file except in compliance with the License.
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "DebugJNI"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "nativehelper/JNIHelp.h"
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "utils/Log.h"
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "utils/misc.h"
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//#include "android_runtime/AndroidRuntime.h"
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Implements:
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *  native int part1(int intArg, double doubleArg, String stringArg,
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      int[] arrayArg)
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_debug_JNITest_part1(JNIEnv* env, jobject object,
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint intArg, jdouble doubleArg, jstring stringArg, jobjectArray arrayArg)
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass clazz;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jmethodID part2id;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jsize arrayLen;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint arrayVal;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int result = -2;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
426215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    ALOGI("JNI test: in part1, intArg=%d, doubleArg=%.3f\n", intArg, doubleArg);
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* find "int part2(double doubleArg, int fromArray, String stringArg)" */
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    clazz = env->GetObjectClass(object);
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    part2id = env->GetMethodID(clazz,
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                "part2", "(DILjava/lang/String;)I");
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (part2id == NULL) {
493762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("JNI test: unable to find part2\n");
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* get the length of the array */
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    arrayLen = env->GetArrayLength(arrayArg);
556215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    ALOGI("  array size is %d\n", arrayLen);
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /*
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Get the last element in the array.
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Use the Get<type>ArrayElements functions instead if you need access
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to multiple elements.
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    arrayVal = (int) env->GetObjectArrayElement(arrayArg, arrayLen-1);
636215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    ALOGI("  array val is %d\n", arrayVal);
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* call this->part2 */
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    result = env->CallIntMethod(object, part2id,
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doubleArg, arrayVal, stringArg);
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return result;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Implements:
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *  private static native int part3(String stringArg);
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint android_debug_JNITest_part3(JNIEnv* env, jclass clazz,
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jstring stringArg)
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* utfChars;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jboolean isCopy;
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
826215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    ALOGI("JNI test: in part3\n");
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    utfChars = env->GetStringUTFChars(stringArg, &isCopy);
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
866215d3ff4b5dfa52a5d8b9a42e343051f31066a5Steve Block    ALOGI("  String is '%s', isCopy=%d\n", (const char*) utfChars, isCopy);
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseStringUTFChars(stringArg, utfChars);
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 2000;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * JNI registration.
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gMethods[] = {
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* name, signature, funcPtr */
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    { "part1",      "(IDLjava/lang/String;[I)I",
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (void*) android_debug_JNITest_part1 },
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    { "part3",      "(Ljava/lang/String;)I",
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (void*) android_debug_JNITest_part3 },
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_debug_JNITest(JNIEnv* env)
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return jniRegisterNativeMethods(env, "android/debug/JNITest",
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        gMethods, NELEM(gMethods));
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if 0
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* trampoline into C++ */
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectextern "C"
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_debug_JNITest_C(JNIEnv* env)
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return android::register_android_debug_JNITest(env);
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}; // namespace android
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
120