Home | History | Annotate | Download | only in jni_generator
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This file is autogenerated by
      6 //     base/android/jni_generator/jni_generator.py
      7 // For
      8 //     org/chromium/example/jni_generator/Test
      9 
     10 #ifndef org_chromium_example_jni_generator_Test_JNI
     11 #define org_chromium_example_jni_generator_Test_JNI
     12 
     13 #include <jni.h>
     14 
     15 #include "base/android/jni_generator/jni_generator_helper.h"
     16 
     17 #include "base/android/jni_int_wrapper.h"
     18 
     19 // Step 1: forward declarations.
     20 namespace {
     21 const char kTestClassPath[] = "org/chromium/example/jni_generator/Test";
     22 // Leaking this jclass as we cannot use LazyInstance from some threads.
     23 jclass g_Test_clazz = NULL;
     24 #define Test_clazz(env) g_Test_clazz
     25 jmethodID g_Test_testMethodWithParam = NULL;
     26 jmethodID g_Test_testStaticMethodWithParam = NULL;
     27 jmethodID g_Test_testMethodWithNoParam = NULL;
     28 jmethodID g_Test_testStaticMethodWithNoParam = NULL;
     29 }  // namespace
     30 
     31 // Step 2: method stubs.
     32 static jint Method(JNIEnv* env, jobject jcaller,
     33     jlong nativeTest,
     34     jint arg1) {
     35   Test* native = reinterpret_cast<Test*>(nativeTest);
     36   CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0);
     37   return native->Method(env, jcaller, arg1);
     38 }
     39 
     40 namespace {
     41 
     42 static void testMethodWithParam(JNIEnv* env, jobject obj, JniIntWrapper iParam)
     43     {
     44   env->CallVoidMethod(obj,
     45       g_Test_testMethodWithParam, as_jint(iParam));
     46 
     47 }
     48 
     49 static jint testStaticMethodWithParam(JNIEnv* env, JniIntWrapper iParam) {
     50   jint ret = env->CallStaticIntMethod(Test_clazz(env),
     51       g_Test_testStaticMethodWithParam, as_jint(iParam));
     52   return ret;
     53 }
     54 
     55 static jdouble testMethodWithNoParam(JNIEnv* env) {
     56   jdouble ret = env->CallStaticDoubleMethod(Test_clazz(env),
     57       g_Test_testMethodWithNoParam);
     58   return ret;
     59 }
     60 
     61 static base::android::ScopedJavaLocalRef<jstring>
     62     testStaticMethodWithNoParam(JNIEnv* env) {
     63   jstring ret =
     64       static_cast<jstring>(env->CallStaticObjectMethod(Test_clazz(env),
     65       g_Test_testStaticMethodWithNoParam));
     66   return base::android::ScopedJavaLocalRef<jstring>(env, ret);
     67 }
     68 }  // namespace
     69 
     70 // Step 3: RegisterNatives.
     71 
     72 static const JNINativeMethod kMethodsTest[] = {
     73     { "nativeMethod",
     74 "("
     75 "J"
     76 "I"
     77 ")"
     78 "I", reinterpret_cast<void*>(Method) },
     79 };
     80 
     81 static bool RegisterNativesImpl(JNIEnv* env, jclass clazz) {
     82   g_Test_clazz = static_cast<jclass>(env->NewWeakGlobalRef(clazz));
     83 
     84   const int kMethodsTestSize = arraysize(kMethodsTest);
     85 
     86   if (env->RegisterNatives(Test_clazz(env),
     87                            kMethodsTest,
     88                            kMethodsTestSize) < 0) {
     89     jni_generator::HandleRegistrationError(
     90         env, Test_clazz(env), __FILE__);
     91     return false;
     92   }
     93 
     94   g_Test_testMethodWithParam = env->GetMethodID(
     95       Test_clazz(env),
     96       "testMethodWithParam",
     97 "("
     98 "I"
     99 ")"
    100 "V");
    101   if (g_Test_testMethodWithParam == NULL) {
    102     return false;
    103   }
    104 
    105   g_Test_testStaticMethodWithParam = env->GetStaticMethodID(
    106       Test_clazz(env),
    107       "testStaticMethodWithParam",
    108 "("
    109 "I"
    110 ")"
    111 "I");
    112   if (g_Test_testStaticMethodWithParam == NULL) {
    113     return false;
    114   }
    115 
    116   g_Test_testMethodWithNoParam = env->GetStaticMethodID(
    117       Test_clazz(env),
    118       "testMethodWithNoParam",
    119 "("
    120 ")"
    121 "D");
    122   if (g_Test_testMethodWithNoParam == NULL) {
    123     return false;
    124   }
    125 
    126   g_Test_testStaticMethodWithNoParam = env->GetStaticMethodID(
    127       Test_clazz(env),
    128       "testStaticMethodWithNoParam",
    129 "("
    130 ")"
    131 "Ljava/lang/String;");
    132   if (g_Test_testStaticMethodWithNoParam == NULL) {
    133     return false;
    134   }
    135 
    136   return true;
    137 }
    138 
    139 extern "C" JNIEXPORT bool JNICALL
    140 Java_org_chromium_example_jni_generator_Test_nativeInitNativeClass(JNIEnv* env,
    141     jclass clazz) {
    142   return RegisterNativesImpl(env, clazz);
    143 }
    144 
    145 #endif  // org_chromium_example_jni_generator_Test_JNI
    146