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 void CPPClass::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) { 39 delete this; 40 } 41 42 jint CPPClass::Method(JNIEnv* env, const JavaParamRef<jobject>& caller) { 43 return 0; 44 } 45 46 void CPPClass::AddStructB(JNIEnv* env, 47 const JavaParamRef<jobject>& caller, 48 const JavaParamRef<jobject>& structb) { 49 long key = Java_InnerStructB_getKey(env, structb); 50 std::string value = 51 ConvertJavaStringToUTF8(env, Java_InnerStructB_getValue(env, structb)); 52 map_[key] = value; 53 } 54 55 void CPPClass::IterateAndDoSomethingWithStructB( 56 JNIEnv* env, 57 const JavaParamRef<jobject>& caller) { 58 // Iterate over the elements and do something with them. 59 for (std::map<long, std::string>::const_iterator it = map_.begin(); 60 it != map_.end(); ++it) { 61 long key = it->first; 62 std::string value = it->second; 63 std::cout << key << value; 64 } 65 map_.clear(); 66 } 67 68 ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( 69 JNIEnv* env, 70 const JavaParamRef<jobject>& caller) { 71 return ConvertUTF8ToJavaString(env, "test"); 72 } 73 74 // Static free functions declared and called directly from java. 75 static jlong JNI_SampleForTests_Init(JNIEnv* env, 76 const JavaParamRef<jobject>& caller, 77 const JavaParamRef<jstring>& param) { 78 return 0; 79 } 80 81 static jdouble JNI_SampleForTests_GetDoubleFunction( 82 JNIEnv*, 83 const JavaParamRef<jobject>&) { 84 return 0; 85 } 86 87 static jfloat JNI_SampleForTests_GetFloatFunction(JNIEnv*, 88 const JavaParamRef<jclass>&) { 89 return 0; 90 } 91 92 static void JNI_SampleForTests_SetNonPODDatatype(JNIEnv*, 93 const JavaParamRef<jobject>&, 94 const JavaParamRef<jobject>&) { 95 } 96 97 static ScopedJavaLocalRef<jobject> JNI_SampleForTests_GetNonPODDatatype( 98 JNIEnv*, 99 const JavaParamRef<jobject>&) { 100 return ScopedJavaLocalRef<jobject>(); 101 } 102 103 static jint JNI_InnerClass_GetInnerIntFunction(JNIEnv*, 104 const JavaParamRef<jclass>&) { 105 return 0; 106 } 107 108 } // namespace android 109 } // namespace base 110 111 int main() { 112 // On a regular application, you'd call AttachCurrentThread(). This sample is 113 // not yet linking with all the libraries. 114 JNIEnv* env = /* AttachCurrentThread() */ NULL; 115 116 // This is how you call a java static method from C++. 117 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); 118 119 // This is how you call a java method from C++. Note that you must have 120 // obtained the jobject somehow. 121 ScopedJavaLocalRef<jobject> my_java_object; 122 int bar = base::android::Java_SampleForTests_javaMethod( 123 env, my_java_object, 1, 2); 124 125 base::android::Java_SampleForTests_methodWithGenericParams( 126 env, my_java_object, nullptr, nullptr); 127 128 // This is how you call a java constructor method from C++. 129 ScopedJavaLocalRef<jobject> my_created_object = 130 base::android::Java_SampleForTests_Constructor(env, 1, 2); 131 132 std::cout << foo << bar; 133 134 for (int i = 0; i < 10; ++i) { 135 // Creates a "struct" that will then be used by the java side. 136 ScopedJavaLocalRef<jobject> struct_a = 137 base::android::Java_InnerStructA_create( 138 env, 0, 1, ConvertUTF8ToJavaString(env, "test")); 139 base::android::Java_SampleForTests_addStructA(env, my_java_object, 140 struct_a); 141 } 142 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); 143 base::android::Java_SampleForTests_packagePrivateJavaMethod(env, 144 my_java_object); 145 base::android::Java_SampleForTests_methodThatThrowsException(env, 146 my_java_object); 147 base::android::Java_SampleForTests_javaMethodWithAnnotatedParam( 148 env, my_java_object, 42); 149 150 base::android::Java_SampleForTests_getInnerInterface(env); 151 base::android::Java_SampleForTests_getInnerEnum(env); 152 153 return 0; 154 } 155