Simple Cache: Enable by default on Android developer builds
[chromium-blink-merge.git] / base / android / jni_generator / sample_for_tests.cc
blob30b994063505b94c854a15153f9d1262198c5a8b
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;
16 namespace base {
17 namespace android {
19 jdouble CPPClass::InnerClass::MethodOtherP0(JNIEnv* env, jobject obj) {
20 return 0.0;
23 CPPClass::CPPClass() {
26 CPPClass::~CPPClass() {
29 void CPPClass::Destroy(JNIEnv* env, jobject obj) {
30 delete this;
33 jint CPPClass::Method(JNIEnv* env, jobject obj) {
34 return 0;
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());
41 map_[key] = value;
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) {
48 long key = it->first;
49 std::string value = it->second;
51 map_.clear();
54 // Static free functions declared and called directly from java.
55 static jint Init(JNIEnv* env, jobject obj, jstring param) {
56 return 0;
59 static jdouble GetDoubleFunction(JNIEnv*, jobject) {
60 return 0;
63 static jfloat GetFloatFunction(JNIEnv*, jclass) {
64 return 0;
67 static void SetNonPODDatatype(JNIEnv*, jobject, jobject) {
70 static jobject GetNonPODDatatype(JNIEnv*, jobject) {
71 return NULL;
74 static jint InnerFunction(JNIEnv*, jclass) {
75 return 0;
78 } // namespace android
79 } // namespace base
81 int main() {
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(
99 env, 0, 1,
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);
105 return 0;