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 caller
) {
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 caller
) {
40 jint
CPPClass::Method(JNIEnv
* env
, jobject caller
) {
44 void CPPClass::AddStructB(JNIEnv
* env
, jobject caller
, 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 caller
) {
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 caller
) {
63 base::android::ScopedJavaLocalRef
<jstring
> ret
= ConvertUTF8ToJavaString(
68 // Static free functions declared and called directly from java.
69 static jlong
Init(JNIEnv
* env
,
70 const JavaParamRef
<jobject
>& caller
,
71 const JavaParamRef
<jstring
>& param
) {
75 static jdouble
GetDoubleFunction(JNIEnv
*, const JavaParamRef
<jobject
>&) {
79 static jfloat
GetFloatFunction(JNIEnv
*, const JavaParamRef
<jclass
>&) {
83 static void SetNonPODDatatype(JNIEnv
*,
84 const JavaParamRef
<jobject
>&,
85 const JavaParamRef
<jobject
>&) {}
87 static ScopedJavaLocalRef
<jobject
> GetNonPODDatatype(
89 const JavaParamRef
<jobject
>&) {
90 return ScopedJavaLocalRef
<jobject
>();
93 static jint
InnerFunction(JNIEnv
*, jclass
) {
97 } // namespace android
101 // On a regular application, you'd call AttachCurrentThread(). This sample is
102 // not yet linking with all the libraries.
103 JNIEnv
* env
= /* AttachCurrentThread() */ NULL
;
105 // This is how you call a java static method from C++.
106 bool foo
= base::android::Java_SampleForTests_staticJavaMethod(env
);
108 // This is how you call a java method from C++. Note that you must have
109 // obtained the jobject somehow.
110 jobject my_java_object
= NULL
;
111 int bar
= base::android::Java_SampleForTests_javaMethod(
112 env
, my_java_object
, 1, 2);
114 for (int i
= 0; i
< 10; ++i
) {
115 // Creates a "struct" that will then be used by the java side.
116 ScopedJavaLocalRef
<jobject
> struct_a
=
117 base::android::Java_InnerStructA_create(
119 base::android::ConvertUTF8ToJavaString(env
, "test").obj());
120 base::android::Java_SampleForTests_addStructA(
121 env
, my_java_object
, struct_a
.obj());
123 base::android::Java_SampleForTests_iterateAndDoSomething(env
, my_java_object
);