Commit 52750882 authored by bulach@chromium.org's avatar bulach@chromium.org

Android: moves jni_generator samples to use long for JNI.

BUG=317523

Review URL: https://codereview.chromium.org/132013003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243988 0039d316-1c4b-4281-b951-d872f2087c98
parent 89e09aeb
...@@ -34,7 +34,7 @@ jclass g_InnerStructB_clazz = NULL; ...@@ -34,7 +34,7 @@ jclass g_InnerStructB_clazz = NULL;
namespace base { namespace base {
namespace android { namespace android {
static jint Init(JNIEnv* env, jobject jcaller, static jlong Init(JNIEnv* env, jobject jcaller,
jstring param); jstring param);
static jdouble GetDoubleFunction(JNIEnv* env, jobject jcaller); static jdouble GetDoubleFunction(JNIEnv* env, jobject jcaller);
...@@ -48,21 +48,21 @@ static jobject GetNonPODDatatype(JNIEnv* env, jobject jcaller); ...@@ -48,21 +48,21 @@ static jobject GetNonPODDatatype(JNIEnv* env, jobject jcaller);
// Step 2: method stubs. // Step 2: method stubs.
static void Destroy(JNIEnv* env, jobject jcaller, static void Destroy(JNIEnv* env, jobject jcaller,
jint nativeCPPClass) { jlong nativeCPPClass) {
CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass);
CHECK_NATIVE_PTR(env, jcaller, native, "Destroy"); CHECK_NATIVE_PTR(env, jcaller, native, "Destroy");
return native->Destroy(env, jcaller); return native->Destroy(env, jcaller);
} }
static jint Method(JNIEnv* env, jobject jcaller, static jint Method(JNIEnv* env, jobject jcaller,
jint nativeCPPClass) { jlong nativeCPPClass) {
CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass);
CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0);
return native->Method(env, jcaller); return native->Method(env, jcaller);
} }
static jdouble MethodOtherP0(JNIEnv* env, jobject jcaller, static jdouble MethodOtherP0(JNIEnv* env, jobject jcaller,
jint nativePtr) { jlong nativePtr) {
CPPClass::InnerClass* native = CPPClass::InnerClass* native =
reinterpret_cast<CPPClass::InnerClass*>(nativePtr); reinterpret_cast<CPPClass::InnerClass*>(nativePtr);
CHECK_NATIVE_PTR(env, jcaller, native, "MethodOtherP0", 0); CHECK_NATIVE_PTR(env, jcaller, native, "MethodOtherP0", 0);
...@@ -70,7 +70,7 @@ static jdouble MethodOtherP0(JNIEnv* env, jobject jcaller, ...@@ -70,7 +70,7 @@ static jdouble MethodOtherP0(JNIEnv* env, jobject jcaller,
} }
static void AddStructB(JNIEnv* env, jobject jcaller, static void AddStructB(JNIEnv* env, jobject jcaller,
jint nativeCPPClass, jlong nativeCPPClass,
jobject b) { jobject b) {
CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass);
CHECK_NATIVE_PTR(env, jcaller, native, "AddStructB"); CHECK_NATIVE_PTR(env, jcaller, native, "AddStructB");
...@@ -78,14 +78,14 @@ static void AddStructB(JNIEnv* env, jobject jcaller, ...@@ -78,14 +78,14 @@ static void AddStructB(JNIEnv* env, jobject jcaller,
} }
static void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject jcaller, static void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject jcaller,
jint nativeCPPClass) { jlong nativeCPPClass) {
CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass);
CHECK_NATIVE_PTR(env, jcaller, native, "IterateAndDoSomethingWithStructB"); CHECK_NATIVE_PTR(env, jcaller, native, "IterateAndDoSomethingWithStructB");
return native->IterateAndDoSomethingWithStructB(env, jcaller); return native->IterateAndDoSomethingWithStructB(env, jcaller);
} }
static jstring ReturnAString(JNIEnv* env, jobject jcaller, static jstring ReturnAString(JNIEnv* env, jobject jcaller,
jint nativeCPPClass) { jlong nativeCPPClass) {
CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass);
CHECK_NATIVE_PTR(env, jcaller, native, "ReturnAString", NULL); CHECK_NATIVE_PTR(env, jcaller, native, "ReturnAString", NULL);
return native->ReturnAString(env, jcaller).Release(); return native->ReturnAString(env, jcaller).Release();
...@@ -315,10 +315,10 @@ static const JNINativeMethod kMethodsSampleForTests[] = { ...@@ -315,10 +315,10 @@ static const JNINativeMethod kMethodsSampleForTests[] = {
"(" "("
"Ljava/lang/String;" "Ljava/lang/String;"
")" ")"
"I", reinterpret_cast<void*>(Init) }, "J", reinterpret_cast<void*>(Init) },
{ "nativeDestroy", { "nativeDestroy",
"(" "("
"I" "J"
")" ")"
"V", reinterpret_cast<void*>(Destroy) }, "V", reinterpret_cast<void*>(Destroy) },
{ "nativeGetDoubleFunction", { "nativeGetDoubleFunction",
...@@ -340,28 +340,28 @@ static const JNINativeMethod kMethodsSampleForTests[] = { ...@@ -340,28 +340,28 @@ static const JNINativeMethod kMethodsSampleForTests[] = {
"Ljava/lang/Object;", reinterpret_cast<void*>(GetNonPODDatatype) }, "Ljava/lang/Object;", reinterpret_cast<void*>(GetNonPODDatatype) },
{ "nativeMethod", { "nativeMethod",
"(" "("
"I" "J"
")" ")"
"I", reinterpret_cast<void*>(Method) }, "I", reinterpret_cast<void*>(Method) },
{ "nativeMethodOtherP0", { "nativeMethodOtherP0",
"(" "("
"I" "J"
")" ")"
"D", reinterpret_cast<void*>(MethodOtherP0) }, "D", reinterpret_cast<void*>(MethodOtherP0) },
{ "nativeAddStructB", { "nativeAddStructB",
"(" "("
"I" "J"
"Lorg/chromium/example/jni_generator/SampleForTests$InnerStructB;" "Lorg/chromium/example/jni_generator/SampleForTests$InnerStructB;"
")" ")"
"V", reinterpret_cast<void*>(AddStructB) }, "V", reinterpret_cast<void*>(AddStructB) },
{ "nativeIterateAndDoSomethingWithStructB", { "nativeIterateAndDoSomethingWithStructB",
"(" "("
"I" "J"
")" ")"
"V", reinterpret_cast<void*>(IterateAndDoSomethingWithStructB) }, "V", reinterpret_cast<void*>(IterateAndDoSomethingWithStructB) },
{ "nativeReturnAString", { "nativeReturnAString",
"(" "("
"I" "J"
")" ")"
"Ljava/lang/String;", reinterpret_cast<void*>(ReturnAString) }, "Ljava/lang/String;", reinterpret_cast<void*>(ReturnAString) },
}; };
......
...@@ -39,7 +39,7 @@ import java.util.List; ...@@ -39,7 +39,7 @@ import java.util.List;
// functions across boundaries, call only one (and then, internally in the other side, // functions across boundaries, call only one (and then, internally in the other side,
// call as many little functions as required). // call as many little functions as required).
// //
// - If a Java object "owns" a native object, stash the pointer in a "int mNativeClassName". // - If a Java object "owns" a native object, stash the pointer in a "long mNativeClassName".
// Note that it needs to have a "destruction path", i.e., it must eventually call a method // Note that it needs to have a "destruction path", i.e., it must eventually call a method
// to delete the native object (for example, the java object has a "close()" method that // to delete the native object (for example, the java object has a "close()" method that
// in turn deletes the native object). Avoid relying on finalizers: those run in a different // in turn deletes the native object). Avoid relying on finalizers: those run in a different
...@@ -78,7 +78,7 @@ import java.util.List; ...@@ -78,7 +78,7 @@ import java.util.List;
class SampleForTests { class SampleForTests {
// Classes can store their C++ pointer counter part as an int that is normally initialized by // Classes can store their C++ pointer counter part as an int that is normally initialized by
// calling out a nativeInit() function. // calling out a nativeInit() function.
int mNativeCPPObject; long mNativeCPPObject;
// You can define methods and attributes on the java class just like any other. // You can define methods and attributes on the java class just like any other.
// Methods without the @CalledByNative annotation won't be exposed to JNI. // Methods without the @CalledByNative annotation won't be exposed to JNI.
...@@ -168,14 +168,14 @@ class SampleForTests { ...@@ -168,14 +168,14 @@ class SampleForTests {
// The caller of this method should store it, and supply it as a the nativeCPPClass param to // The caller of this method should store it, and supply it as a the nativeCPPClass param to
// subsequent native method calls (see the methods below that take an "int native..." as first // subsequent native method calls (see the methods below that take an "int native..." as first
// param). // param).
private native int nativeInit(String param); private native long nativeInit(String param);
// This defines a function binding to the associated C++ class member function. The name is // This defines a function binding to the associated C++ class member function. The name is
// derived from |nativeDestroy| and |nativeCPPClass| to arrive at CPPClass::Destroy() (i.e. native // derived from |nativeDestroy| and |nativeCPPClass| to arrive at CPPClass::Destroy() (i.e. native
// prefixes stripped). // prefixes stripped).
// The |nativeCPPClass| is automatically cast to type CPPClass* in order to obtain the object on // The |nativeCPPClass| is automatically cast to type CPPClass* in order to obtain the object on
// which to invoke the member function. // which to invoke the member function.
private native void nativeDestroy(int nativeCPPClass); private native void nativeDestroy(long nativeCPPClass);
// This declares a C++ function which the application code must implement: // This declares a C++ function which the application code must implement:
// static jdouble GetDoubleFunction(JNIEnv* env, jobject obj); // static jdouble GetDoubleFunction(JNIEnv* env, jobject obj);
...@@ -200,12 +200,12 @@ class SampleForTests { ...@@ -200,12 +200,12 @@ class SampleForTests {
// Similar to nativeDestroy above, this will cast nativeCPPClass into pointer of CPPClass type and // Similar to nativeDestroy above, this will cast nativeCPPClass into pointer of CPPClass type and
// call its Method member function. // call its Method member function.
private native int nativeMethod(int nativeCPPClass); private native int nativeMethod(long nativeCPPClass);
// Similar to nativeMethod above, but here the C++ fully qualified class name is taken from the // Similar to nativeMethod above, but here the C++ fully qualified class name is taken from the
// annotation rather than parameter name, which can thus be chosen freely. // annotation rather than parameter name, which can thus be chosen freely.
@NativeClassQualifiedName("CPPClass::InnerClass") @NativeClassQualifiedName("CPPClass::InnerClass")
private native double nativeMethodOtherP0(int nativePtr); private native double nativeMethodOtherP0(long nativePtr);
// This "struct" will be created by the native side using |createInnerStructA|, // This "struct" will be created by the native side using |createInnerStructA|,
// and used by the java-side somehow. // and used by the java-side somehow.
...@@ -281,7 +281,7 @@ class SampleForTests { ...@@ -281,7 +281,7 @@ class SampleForTests {
nativeIterateAndDoSomethingWithStructB(mNativeCPPObject); nativeIterateAndDoSomethingWithStructB(mNativeCPPObject);
} }
native void nativeAddStructB(int nativeCPPClass, InnerStructB b); native void nativeAddStructB(long nativeCPPClass, InnerStructB b);
native void nativeIterateAndDoSomethingWithStructB(int nativeCPPClass); native void nativeIterateAndDoSomethingWithStructB(long nativeCPPClass);
native String nativeReturnAString(int nativeCPPClass); native String nativeReturnAString(long nativeCPPClass);
} }
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
], ],
'variables': { 'variables': {
'jni_gen_package': 'example', 'jni_gen_package': 'example',
'jni_generator_ptr_type': 'long',
}, },
'includes': [ '../../../build/jni_generator.gypi' ], 'includes': [ '../../../build/jni_generator.gypi' ],
}, },
......
...@@ -37,7 +37,7 @@ class TestOptions(object): ...@@ -37,7 +37,7 @@ class TestOptions(object):
self.script_name = SCRIPT_NAME self.script_name = SCRIPT_NAME
self.includes = INCLUDES self.includes = INCLUDES
self.pure_native_methods = False self.pure_native_methods = False
self.ptr_type = 'int' self.ptr_type = 'long'
self.jni_init_native_name = None self.jni_init_native_name = None
self.eager_called_by_natives = False self.eager_called_by_natives = False
...@@ -949,10 +949,10 @@ class Foo { ...@@ -949,10 +949,10 @@ class Foo {
package org.chromium.example.jni_generator; package org.chromium.example.jni_generator;
/** The pointer to the native Test. */ /** The pointer to the native Test. */
int nativeTest; long nativeTest;
class Test { class Test {
private static native int nativeMethod(int nativeTest, int arg1); private static native long nativeMethod(long nativeTest, int arg1);
} }
""" """
options = TestOptions() options = TestOptions()
...@@ -966,11 +966,11 @@ class Foo { ...@@ -966,11 +966,11 @@ class Foo {
package org.chromium.example.jni_generator; package org.chromium.example.jni_generator;
/** The pointer to the native Test. */ /** The pointer to the native Test. */
int nativeTest; long nativeTest;
class Test { class Test {
private static native boolean initNativeClass(); private static native boolean initNativeClass();
private static native int nativeMethod(int nativeTest, int arg1); private static native int nativeMethod(long nativeTest, int arg1);
} }
""" """
options = TestOptions() options = TestOptions()
...@@ -984,11 +984,11 @@ class Foo { ...@@ -984,11 +984,11 @@ class Foo {
package org.chromium.example.jni_generator; package org.chromium.example.jni_generator;
/** The pointer to the native Test. */ /** The pointer to the native Test. */
int nativeTest; long nativeTest;
class Test { class Test {
private static native boolean initNativeClass(); private static native boolean initNativeClass();
private static native int nativeMethod(int nativeTest, int arg1); private static native int nativeMethod(long nativeTest, int arg1);
@CalledByNative @CalledByNative
private void testMethodWithParam(int iParam); private void testMethodWithParam(int iParam);
@CalledByNative @CalledByNative
......
...@@ -59,7 +59,7 @@ base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( ...@@ -59,7 +59,7 @@ base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString(
} }
// Static free functions declared and called directly from java. // Static free functions declared and called directly from java.
static jint Init(JNIEnv* env, jobject obj, jstring param) { static jlong Init(JNIEnv* env, jobject obj, jstring param) {
return 0; return 0;
} }
......
...@@ -27,7 +27,7 @@ jmethodID g_Test_testStaticMethodWithNoParam = NULL; ...@@ -27,7 +27,7 @@ jmethodID g_Test_testStaticMethodWithNoParam = NULL;
// Step 2: method stubs. // Step 2: method stubs.
static jint Method(JNIEnv* env, jobject jcaller, static jint Method(JNIEnv* env, jobject jcaller,
jint nativeTest, jlong nativeTest,
jint arg1) { jint arg1) {
Test* native = reinterpret_cast<Test*>(nativeTest); Test* native = reinterpret_cast<Test*>(nativeTest);
CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0);
...@@ -67,7 +67,7 @@ static base::android::ScopedJavaLocalRef<jstring> ...@@ -67,7 +67,7 @@ static base::android::ScopedJavaLocalRef<jstring>
static const JNINativeMethod kMethodsTest[] = { static const JNINativeMethod kMethodsTest[] = {
{ "nativeMethod", { "nativeMethod",
"(" "("
"I" "J"
"I" "I"
")" ")"
"I", reinterpret_cast<void*>(Method) }, "I", reinterpret_cast<void*>(Method) },
......
...@@ -24,7 +24,7 @@ jclass g_Test_clazz = NULL; ...@@ -24,7 +24,7 @@ jclass g_Test_clazz = NULL;
// Step 2: method stubs. // Step 2: method stubs.
static jint Method(JNIEnv* env, jobject jcaller, static jint Method(JNIEnv* env, jobject jcaller,
jint nativeTest, jlong nativeTest,
jint arg1) { jint arg1) {
Test* native = reinterpret_cast<Test*>(nativeTest); Test* native = reinterpret_cast<Test*>(nativeTest);
CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0);
...@@ -36,7 +36,7 @@ static jint Method(JNIEnv* env, jobject jcaller, ...@@ -36,7 +36,7 @@ static jint Method(JNIEnv* env, jobject jcaller,
static const JNINativeMethod kMethodsTest[] = { static const JNINativeMethod kMethodsTest[] = {
{ "nativeMethod", { "nativeMethod",
"(" "("
"I" "J"
"I" "I"
")" ")"
"I", reinterpret_cast<void*>(Method) }, "I", reinterpret_cast<void*>(Method) },
......
...@@ -23,8 +23,8 @@ jclass g_Test_clazz = NULL; ...@@ -23,8 +23,8 @@ jclass g_Test_clazz = NULL;
} // namespace } // namespace
// Step 2: method stubs. // Step 2: method stubs.
static jint Method(JNIEnv* env, jobject jcaller, static jlong Method(JNIEnv* env, jobject jcaller,
jint nativeTest, jlong nativeTest,
jint arg1) { jint arg1) {
Test* native = reinterpret_cast<Test*>(nativeTest); Test* native = reinterpret_cast<Test*>(nativeTest);
CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0);
...@@ -36,10 +36,10 @@ static jint Method(JNIEnv* env, jobject jcaller, ...@@ -36,10 +36,10 @@ static jint Method(JNIEnv* env, jobject jcaller,
static const JNINativeMethod kMethodsTest[] = { static const JNINativeMethod kMethodsTest[] = {
{ "nativeMethod", { "nativeMethod",
"(" "("
"I" "J"
"I" "I"
")" ")"
"I", reinterpret_cast<void*>(Method) }, "J", reinterpret_cast<void*>(Method) },
}; };
static bool RegisterNativesImpl(JNIEnv* env) { static bool RegisterNativesImpl(JNIEnv* env) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment