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