Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: base/android/jni_generator/sample_for_tests.cc

Issue 2418893003: Use JavaRef in JNI generator sample. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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> 5 #include <iostream>
6 6
7 #include "base/android/jni_generator/sample_for_tests.h" 7 #include "base/android/jni_generator/sample_for_tests.h"
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 int main() { 110 int main() {
111 // On a regular application, you'd call AttachCurrentThread(). This sample is 111 // On a regular application, you'd call AttachCurrentThread(). This sample is
112 // not yet linking with all the libraries. 112 // not yet linking with all the libraries.
113 JNIEnv* env = /* AttachCurrentThread() */ NULL; 113 JNIEnv* env = /* AttachCurrentThread() */ NULL;
114 114
115 // This is how you call a java static method from C++. 115 // This is how you call a java static method from C++.
116 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env); 116 bool foo = base::android::Java_SampleForTests_staticJavaMethod(env);
117 117
118 // This is how you call a java method from C++. Note that you must have 118 // This is how you call a java method from C++. Note that you must have
119 // obtained the jobject somehow. 119 // obtained the jobject somehow.
120 jobject my_java_object = NULL; 120 ScopedJavaLocalRef<jobject> my_java_object;
121 int bar = base::android::Java_SampleForTests_javaMethod( 121 int bar = base::android::Java_SampleForTests_javaMethod(
122 env, my_java_object, 1, 2); 122 env, my_java_object, 1, 2);
123 123
124 std::cout << foo << bar; 124 std::cout << foo << bar;
125 125
126 for (int i = 0; i < 10; ++i) { 126 for (int i = 0; i < 10; ++i) {
127 // Creates a "struct" that will then be used by the java side. 127 // Creates a "struct" that will then be used by the java side.
128 ScopedJavaLocalRef<jobject> struct_a = 128 ScopedJavaLocalRef<jobject> struct_a =
129 base::android::Java_InnerStructA_create( 129 base::android::Java_InnerStructA_create(
130 env, 0, 1, ConvertUTF8ToJavaString(env, "test")); 130 env, 0, 1, ConvertUTF8ToJavaString(env, "test"));
131 base::android::Java_SampleForTests_addStructA(env, my_java_object, 131 base::android::Java_SampleForTests_addStructA(env, my_java_object,
132 struct_a); 132 struct_a);
133 } 133 }
134 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object); 134 base::android::Java_SampleForTests_iterateAndDoSomething(env, my_java_object);
135 base::android::Java_SampleForTests_packagePrivateJavaMethod(env, 135 base::android::Java_SampleForTests_packagePrivateJavaMethod(env,
136 my_java_object); 136 my_java_object);
137 base::android::Java_SampleForTests_methodThatThrowsException(env, 137 base::android::Java_SampleForTests_methodThatThrowsException(env,
138 my_java_object); 138 my_java_object);
139 base::android::Java_SampleForTests_javaMethodWithAnnotatedParam( 139 base::android::Java_SampleForTests_javaMethodWithAnnotatedParam(
140 env, my_java_object, 42); 140 env, my_java_object, 42);
141 return 0; 141 return 0;
142 } 142 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698