Home | History | Annotate | Download | only in jni_generator
      1 // Copyright (c) 2012 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 #include <iostream>
      6 
      7 #include "base/android/jni_generator/sample_for_tests.h"
      8 
      9 #include "base/android/jni_android.h"
     10 #include "base/android/jni_string.h"
     11 #include "base/android/scoped_java_ref.h"
     12 // Generated file for JNI bindings from C++ to Java @CalledByNative methods.
     13 // Only to be included in one .cc file.
     14 // Name is based on the java file name: *.java -> jni/*_jni.h
     15 #include "jni/SampleForTests_jni.h"  // Generated by JNI.
     16 
     17 using base::android::AttachCurrentThread;
     18 using base::android::ConvertJavaStringToUTF8;
     19 using base::android::ConvertUTF8ToJavaString;
     20 using base::android::ScopedJavaLocalRef;
     21 
     22 namespace base {
     23 namespace android {
     24 
     25 jdouble CPPClass::InnerClass::MethodOtherP0(
     26     JNIEnv* env,
     27     const JavaParamRef<jobject>& caller) {
     28   return 0.0;
     29 }
     30 
     31 CPPClass::CPPClass() {
     32 }
     33 
     34 CPPClass::~CPPClass() {
     35 }
     36 
     37 // static
     38 bool CPPClass::RegisterJNI(JNIEnv* env) {
     39   return RegisterNativesImpl(env);  // Generated in SampleForTests_jni.h
     40 }
     41 
     42 void CPPClass::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) {
     43   delete this;
     44 }
     45 
     46 jint CPPClass::Method(JNIEnv* env, const JavaParamRef<jobject>& caller) {
     47   return 0;
     48 }
     49 
     50 void CPPClass::AddStructB(JNIEnv* env,
     51                           const JavaParamRef<jobject>& caller,
     52                           const JavaParamRef<jobject>& structb) {
     53   long key = Java_InnerStructB_getKey(env, structb);
     54   std::string value = ConvertJavaStringToUTF8(
     55       env, Java_InnerStructB_getValue(env, structb).obj());
     56   map_[key] = value;
     57 }
     58 
     59 void CPPClass::IterateAndDoSomethingWithStructB(
     60     JNIEnv* env,
     61     const JavaParamRef<jobject>& caller) {
     62   // Iterate over the elements and do something with them.
     63   for (std::map<long, std::string>::const_iterator it = map_.begin();
     64        it != map_.end(); ++it) {
     65     long key = it->first;
     66     std::string value = it->second;
     67     std::cout << key << value;
     68   }
     69   map_.clear();
     70 }
     71 
     72 ScopedJavaLocalRef<jstring> CPPClass::ReturnAString(
     73     JNIEnv* env,
     74     const JavaParamRef<jobject>& caller) {
     75   return ConvertUTF8ToJavaString(env, "test");
     76 }
     77 
     78 // Static free functions declared and called directly from java.
     79 static jlong Init(JNIEnv* env,
     80                   const JavaParamRef<jobject>& caller,
     81                   const JavaParamRef<jstring>& param) {
     82   return 0;
     83 }
     84 
     85 static jdouble GetDoubleFunction(JNIEnv*, const JavaParamRef<jobject>&) {
     86   return 0;
     87 }
     88 
     89 static jfloat GetFloatFunction(JNIEnv*, const JavaParamRef<jclass>&) {
     90   return 0;
     91 }
     92 
     93 static void SetNonPODDatatype(JNIEnv*,
     94                               const JavaParamRef<jobject>&,
     95                               const JavaParamRef<jobject>&) {}
     96 
     97 static ScopedJavaLocalRef<jobject> GetNonPODDatatype(
     98     JNIEnv*,
     99     const JavaParamRef<jobject>&) {
    100   return ScopedJavaLocalRef<jobject>();
    101 }
    102 
    103 } // namespace android
    104 } // namespace base
    105 
    106 int main() {
    107   // On a regular application, you'd call AttachCurrentThread(). This sample is
    108   // not yet linking with all the libraries.
    109   JNIEnv* env = /* AttachCurrentThread() */ NULL;
    110 
    111   // This is how you call a java static method from C++.
    112   bool foo = base::android::Java_SampleForTests_staticJavaMethod(env);
    113 
    114   // This is how you call a java method from C++. Note that you must have
    115   // obtained the jobject somehow.
    116   jobject my_java_object = NULL;
    117   int bar = base::android::Java_SampleForTests_javaMethod(
    118       env, my_java_object, 1, 2);
    119 
    120   std::cout << foo << bar;
    121 
    122   for (int i = 0; i < 10; ++i) {
    123     // Creates a "struct" that will then be used by the java side.
    124     ScopedJavaLocalRef<jobject> struct_a =
    125         base::android::Java_InnerStructA_create(
    126             env, 0, 1, ConvertUTF8ToJavaString(env, "test").obj());
    127     base::android::Java_SampleForTests_addStructA(
    128         env, my_java_object, struct_a.obj());
    129   }
    130   base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object);
    131   base::android::Java_SampleForTests_packagePrivateJavaMethod(env,
    132                                                               my_java_object);
    133   base::android::Java_SampleForTests_methodThatThrowsException(env,
    134                                                                my_java_object);
    135   return 0;
    136 }
    137