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.
5 #include "base/android/jni_generator/sample_for_tests.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 // Generated file for JNI bindings from C++ to Java @CalledByNative methods.
11 // Only to be included in one .cc file.
12 // Name is based on the java file name: *.java -> jni/*_jni.h
13 #include "jni/SampleForTests_jni.h" // Generated by JNI.
15 using base::android::AttachCurrentThread
;
16 using base::android::ScopedJavaLocalRef
;
21 jdouble
CPPClass::InnerClass::MethodOtherP0(JNIEnv
* env
, jobject obj
) {
25 CPPClass::CPPClass() {
28 CPPClass::~CPPClass() {
32 bool CPPClass::RegisterJNI(JNIEnv
* env
) {
33 return RegisterNativesImpl(env
); // Generated in SampleForTests_jni.h
36 void CPPClass::Destroy(JNIEnv
* env
, jobject obj
) {
40 jint
CPPClass::Method(JNIEnv
* env
, jobject obj
) {
44 void CPPClass::AddStructB(JNIEnv
* env
, jobject obj
, jobject structb
) {
45 long key
= Java_InnerStructB_getKey(env
, structb
);
46 std::string value
= ConvertJavaStringToUTF8(
47 env
, Java_InnerStructB_getValue(env
, structb
).obj());
51 void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv
* env
, jobject obj
) {
52 // Iterate over the elements and do something with them.
53 for (std::map
<long, std::string
>::const_iterator it
= map_
.begin();
54 it
!= map_
.end(); ++it
) {
56 std::string value
= it
->second
;
61 base::android::ScopedJavaLocalRef
<jstring
> CPPClass::ReturnAString(
62 JNIEnv
* env
, jobject obj
) {
63 base::android::ScopedJavaLocalRef
<jstring
> ret
= ConvertUTF8ToJavaString(
68 // Static free functions declared and called directly from java.
69 static jlong
Init(JNIEnv
* env
, jobject obj
, jstring param
) {
73 static jdouble
GetDoubleFunction(JNIEnv
*, jobject
) {
77 static jfloat
GetFloatFunction(JNIEnv
*, jclass
) {
81 static void SetNonPODDatatype(JNIEnv
*, jobject
, jobject
) {
84 static jobject
GetNonPODDatatype(JNIEnv
*, jobject
) {
88 static jint
InnerFunction(JNIEnv
*, jclass
) {
92 } // namespace android
96 // On a regular application, you'd call AttachCurrentThread(). This sample is
97 // not yet linking with all the libraries.
98 JNIEnv
* env
= /* AttachCurrentThread() */ NULL
;
100 // This is how you call a java static method from C++.
101 bool foo
= base::android::Java_SampleForTests_staticJavaMethod(env
);
103 // This is how you call a java method from C++. Note that you must have
104 // obtained the jobject somehow.
105 jobject my_java_object
= NULL
;
106 int bar
= base::android::Java_SampleForTests_javaMethod(
107 env
, my_java_object
, 1, 2);
109 for (int i
= 0; i
< 10; ++i
) {
110 // Creates a "struct" that will then be used by the java side.
111 ScopedJavaLocalRef
<jobject
> struct_a
=
112 base::android::Java_InnerStructA_create(
114 base::android::ConvertUTF8ToJavaString(env
, "test").obj());
115 base::android::Java_SampleForTests_addStructA(
116 env
, my_java_object
, struct_a
.obj());
118 base::android::Java_SampleForTests_iterateAndDoSomething(env
, my_java_object
);