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 #include "jni/SampleForTests_jni.h"
13 using base::android::AttachCurrentThread
;
14 using base::android::ScopedJavaLocalRef
;
19 jdouble
CPPClass::InnerClass::MethodOtherP0(JNIEnv
* env
, jobject obj
) {
23 CPPClass::CPPClass() {
26 CPPClass::~CPPClass() {
29 void CPPClass::Destroy(JNIEnv
* env
, jobject obj
) {
33 jint
CPPClass::Method(JNIEnv
* env
, jobject obj
) {
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());
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
) {
49 std::string value
= it
->second
;
54 // Static free functions declared and called directly from java.
55 static jint
Init(JNIEnv
* env
, jobject obj
, jstring param
) {
59 static jdouble
GetDoubleFunction(JNIEnv
*, jobject
) {
63 static jfloat
GetFloatFunction(JNIEnv
*, jclass
) {
67 static void SetNonPODDatatype(JNIEnv
*, jobject
, jobject
) {
70 static jobject
GetNonPODDatatype(JNIEnv
*, jobject
) {
74 static jint
InnerFunction(JNIEnv
*, jclass
) {
78 } // namespace android
82 // On a regular application, you'd call AttachCurrentThread(). This sample is
83 // not yet linking with all the libraries.
84 JNIEnv
* env
= /* AttachCurrentThread() */ NULL
;
86 // This is how you call a java static method from C++.
87 bool foo
= base::android::Java_SampleForTests_staticJavaMethod(env
);
89 // This is how you call a java method from C++. Note that you must have
90 // obtained the jobject somehow.
91 jobject my_java_object
= NULL
;
92 int bar
= base::android::Java_SampleForTests_javaMethod(
93 env
, my_java_object
, 1, 2);
95 for (int i
= 0; i
< 10; ++i
) {
96 // Creates a "struct" that will then be used by the java side.
97 ScopedJavaLocalRef
<jobject
> struct_a
=
98 base::android::Java_InnerStructA_create(
100 base::android::ConvertUTF8ToJavaString(env
, "test").obj());
101 base::android::Java_SampleForTests_addStructA(
102 env
, my_java_object
, struct_a
.obj());
104 base::android::Java_SampleForTests_iterateAndDoSomething(env
, my_java_object
);