| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <iostream> |
| 6 |
| 5 #include "base/android/jni_generator/sample_for_tests.h" | 7 #include "base/android/jni_generator/sample_for_tests.h" |
| 6 | 8 |
| 7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 9 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods. | 12 // Generated file for JNI bindings from C++ to Java @CalledByNative methods. |
| 11 // Only to be included in one .cc file. | 13 // Only to be included in one .cc file. |
| 12 // Name is based on the java file name: *.java -> jni/*_jni.h | 14 // Name is based on the java file name: *.java -> jni/*_jni.h |
| 13 #include "jni/SampleForTests_jni.h" // Generated by JNI. | 15 #include "jni/SampleForTests_jni.h" // Generated by JNI. |
| 14 | 16 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 } | 57 } |
| 56 | 58 |
| 57 void CPPClass::IterateAndDoSomethingWithStructB( | 59 void CPPClass::IterateAndDoSomethingWithStructB( |
| 58 JNIEnv* env, | 60 JNIEnv* env, |
| 59 const JavaParamRef<jobject>& caller) { | 61 const JavaParamRef<jobject>& caller) { |
| 60 // Iterate over the elements and do something with them. | 62 // Iterate over the elements and do something with them. |
| 61 for (std::map<long, std::string>::const_iterator it = map_.begin(); | 63 for (std::map<long, std::string>::const_iterator it = map_.begin(); |
| 62 it != map_.end(); ++it) { | 64 it != map_.end(); ++it) { |
| 63 long key = it->first; | 65 long key = it->first; |
| 64 std::string value = it->second; | 66 std::string value = it->second; |
| 67 std::cout << key << value; |
| 65 } | 68 } |
| 66 map_.clear(); | 69 map_.clear(); |
| 67 } | 70 } |
| 68 | 71 |
| 69 ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( | 72 ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( |
| 70 JNIEnv* env, | 73 JNIEnv* env, |
| 71 const JavaParamRef<jobject>& caller) { | 74 const JavaParamRef<jobject>& caller) { |
| 72 return ConvertUTF8ToJavaString(env, "test"); | 75 return ConvertUTF8ToJavaString(env, "test"); |
| 73 } | 76 } |
| 74 | 77 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 90 static void SetNonPODDatatype(JNIEnv*, | 93 static void SetNonPODDatatype(JNIEnv*, |
| 91 const JavaParamRef<jobject>&, | 94 const JavaParamRef<jobject>&, |
| 92 const JavaParamRef<jobject>&) {} | 95 const JavaParamRef<jobject>&) {} |
| 93 | 96 |
| 94 static ScopedJavaLocalRef<jobject> GetNonPODDatatype( | 97 static ScopedJavaLocalRef<jobject> GetNonPODDatatype( |
| 95 JNIEnv*, | 98 JNIEnv*, |
| 96 const JavaParamRef<jobject>&) { | 99 const JavaParamRef<jobject>&) { |
| 97 return ScopedJavaLocalRef<jobject>(); | 100 return ScopedJavaLocalRef<jobject>(); |
| 98 } | 101 } |
| 99 | 102 |
| 100 static jint InnerFunction(JNIEnv*, jclass) { | |
| 101 return 0; | |
| 102 } | |
| 103 | |
| 104 } // namespace android | 103 } // namespace android |
| 105 } // namespace base | 104 } // namespace base |
| 106 | 105 |
| 107 int main() { | 106 int main() { |
| 108 // On a regular application, you'd call AttachCurrentThread(). This sample is | 107 // On a regular application, you'd call AttachCurrentThread(). This sample is |
| 109 // not yet linking with all the libraries. | 108 // not yet linking with all the libraries. |
| 110 JNIEnv* env = /* AttachCurrentThread() */ NULL; | 109 JNIEnv* env = /* AttachCurrentThread() */ NULL; |
| 111 | 110 |
| 112 // This is how you call a java static method from C++. | 111 // This is how you call a java static method from C++. |
| 113 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); | 112 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); |
| 114 | 113 |
| 115 // This is how you call a java method from C++. Note that you must have | 114 // This is how you call a java method from C++. Note that you must have |
| 116 // obtained the jobject somehow. | 115 // obtained the jobject somehow. |
| 117 jobject my_java_object = NULL; | 116 jobject my_java_object = NULL; |
| 118 int bar = base::android::Java_SampleForTests_javaMethod( | 117 int bar = base::android::Java_SampleForTests_javaMethod( |
| 119 env, my_java_object, 1, 2); | 118 env, my_java_object, 1, 2); |
| 120 | 119 |
| 120 std::cout << foo << bar; |
| 121 |
| 121 for (int i = 0; i < 10; ++i) { | 122 for (int i = 0; i < 10; ++i) { |
| 122 // Creates a "struct" that will then be used by the java side. | 123 // Creates a "struct" that will then be used by the java side. |
| 123 ScopedJavaLocalRef<jobject> struct_a = | 124 ScopedJavaLocalRef<jobject> struct_a = |
| 124 base::android::Java_InnerStructA_create( | 125 base::android::Java_InnerStructA_create( |
| 125 env, 0, 1, ConvertUTF8ToJavaString(env, "test").obj()); | 126 env, 0, 1, ConvertUTF8ToJavaString(env, "test").obj()); |
| 126 base::android::Java_SampleForTests_addStructA( | 127 base::android::Java_SampleForTests_addStructA( |
| 127 env, my_java_object, struct_a.obj()); | 128 env, my_java_object, struct_a.obj()); |
| 128 } | 129 } |
| 129 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); | 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); |
| 130 return 0; | 135 return 0; |
| 131 } | 136 } |
| OLD | NEW |