diff options
21 files changed, 1133 insertions, 499 deletions
diff --git a/base/android/jni_generator/golden_sample_for_tests_jni.h b/base/android/jni_generator/golden_sample_for_tests_jni.h index 2767665..27277b2 100644 --- a/base/android/jni_generator/golden_sample_for_tests_jni.h +++ b/base/android/jni_generator/golden_sample_for_tests_jni.h @@ -12,12 +12,7 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { @@ -33,66 +28,75 @@ jclass g_InnerStructA_clazz = NULL; jclass g_SampleForTests_clazz = NULL; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_InnerStructB_clazz = NULL; + } // namespace namespace base { namespace android { -static jint Init(JNIEnv* env, jobject obj, +static jint Init(JNIEnv* env, jobject jcaller, jstring param); -static jdouble GetDoubleFunction(JNIEnv* env, jobject obj); +static jdouble GetDoubleFunction(JNIEnv* env, jobject jcaller); -static jfloat GetFloatFunction(JNIEnv* env, jclass clazz); +static jfloat GetFloatFunction(JNIEnv* env, jclass jcaller); -static void SetNonPODDatatype(JNIEnv* env, jobject obj, +static void SetNonPODDatatype(JNIEnv* env, jobject jcaller, jobject rect); -static jobject GetNonPODDatatype(JNIEnv* env, jobject obj); +static jobject GetNonPODDatatype(JNIEnv* env, jobject jcaller); // Step 2: method stubs. -static void Destroy(JNIEnv* env, jobject obj, +static void Destroy(JNIEnv* env, jobject jcaller, jint nativeCPPClass) { - DCHECK(nativeCPPClass) << "Destroy"; CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); - return native->Destroy(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "Destroy"); + return native->Destroy(env, jcaller); } -static jint Method(JNIEnv* env, jobject obj, +static jint Method(JNIEnv* env, jobject jcaller, jint nativeCPPClass) { - DCHECK(nativeCPPClass) << "Method"; CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); - return native->Method(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); + return native->Method(env, jcaller); } -static jdouble MethodOtherP0(JNIEnv* env, jobject obj, +static jdouble MethodOtherP0(JNIEnv* env, jobject jcaller, jint nativePtr) { - DCHECK(nativePtr) << "MethodOtherP0"; CPPClass::InnerClass* native = reinterpret_cast<CPPClass::InnerClass*>(nativePtr); - return native->MethodOtherP0(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "MethodOtherP0", 0); + return native->MethodOtherP0(env, jcaller); } -static void AddStructB(JNIEnv* env, jobject obj, +static void AddStructB(JNIEnv* env, jobject jcaller, jint nativeCPPClass, jobject b) { - DCHECK(nativeCPPClass) << "AddStructB"; CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); - return native->AddStructB(env, obj, b); + CHECK_NATIVE_PTR(env, jcaller, native, "AddStructB"); + return native->AddStructB(env, jcaller, b); } -static void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj, +static void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject jcaller, jint nativeCPPClass) { - DCHECK(nativeCPPClass) << "IterateAndDoSomethingWithStructB"; CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); - return native->IterateAndDoSomethingWithStructB(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "IterateAndDoSomethingWithStructB"); + return native->IterateAndDoSomethingWithStructB(env, jcaller); +} + +static jstring ReturnAString(JNIEnv* env, jobject jcaller, + jint nativeCPPClass) { + CPPClass* native = reinterpret_cast<CPPClass*>(nativeCPPClass); + CHECK_NATIVE_PTR(env, jcaller, native, "ReturnAString", NULL); + return native->ReturnAString(env, jcaller).Release(); } static base::subtle::AtomicWord g_SampleForTests_javaMethod = 0; static jint Java_SampleForTests_javaMethod(JNIEnv* env, jobject obj, jint foo, jint bar) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, obj, + g_SampleForTests_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -107,16 +111,17 @@ static jint Java_SampleForTests_javaMethod(JNIEnv* env, jobject obj, jint foo, &g_SampleForTests_javaMethod); jint ret = - env->CallIntMethod(obj, - method_id, foo, bar); - base::android::CheckException(env); + env->CallIntMethod(obj, + method_id, foo, bar); + jni_generator::CheckException(env); return ret; } static base::subtle::AtomicWord g_SampleForTests_staticJavaMethod = 0; static jboolean Java_SampleForTests_staticJavaMethod(JNIEnv* env) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, g_SampleForTests_clazz, + g_SampleForTests_clazz, false); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_STATIC>( @@ -129,9 +134,9 @@ static jboolean Java_SampleForTests_staticJavaMethod(JNIEnv* env) { &g_SampleForTests_staticJavaMethod); jboolean ret = - env->CallStaticBooleanMethod(g_SampleForTests_clazz, - method_id); - base::android::CheckException(env); + env->CallStaticBooleanMethod(g_SampleForTests_clazz, + method_id); + jni_generator::CheckException(env); return ret; } @@ -139,7 +144,8 @@ static base::subtle::AtomicWord g_SampleForTests_packagePrivateJavaMethod = 0; static void Java_SampleForTests_packagePrivateJavaMethod(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, obj, + g_SampleForTests_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -151,9 +157,9 @@ static void Java_SampleForTests_packagePrivateJavaMethod(JNIEnv* env, jobject "V", &g_SampleForTests_packagePrivateJavaMethod); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } @@ -161,7 +167,8 @@ static base::subtle::AtomicWord g_SampleForTests_methodThatThrowsException = 0; static void Java_SampleForTests_methodThatThrowsException(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, obj, + g_SampleForTests_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -173,18 +180,19 @@ static void Java_SampleForTests_methodThatThrowsException(JNIEnv* env, jobject "V", &g_SampleForTests_methodThatThrowsException); - env->CallVoidMethod(obj, - method_id); + env->CallVoidMethod(obj, + method_id); } static base::subtle::AtomicWord g_InnerStructA_create = 0; -static ScopedJavaLocalRef<jobject> Java_InnerStructA_create(JNIEnv* env, jlong - l, +static base::android::ScopedJavaLocalRef<jobject> + Java_InnerStructA_create(JNIEnv* env, jlong l, jint i, jstring s) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InnerStructA_clazz); + CHECK_CLAZZ(env, g_InnerStructA_clazz, + g_InnerStructA_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_STATIC>( @@ -200,17 +208,18 @@ static ScopedJavaLocalRef<jobject> Java_InnerStructA_create(JNIEnv* env, jlong &g_InnerStructA_create); jobject ret = - env->CallStaticObjectMethod(g_InnerStructA_clazz, - method_id, l, i, s); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallStaticObjectMethod(g_InnerStructA_clazz, + method_id, l, i, s); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } static base::subtle::AtomicWord g_SampleForTests_addStructA = 0; static void Java_SampleForTests_addStructA(JNIEnv* env, jobject obj, jobject a) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, obj, + g_SampleForTests_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -223,9 +232,9 @@ static void Java_SampleForTests_addStructA(JNIEnv* env, jobject obj, jobject a) "V", &g_SampleForTests_addStructA); - env->CallVoidMethod(obj, - method_id, a); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id, a); + jni_generator::CheckException(env); } @@ -233,7 +242,8 @@ static base::subtle::AtomicWord g_SampleForTests_iterateAndDoSomething = 0; static void Java_SampleForTests_iterateAndDoSomething(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_SampleForTests_clazz); + CHECK_CLAZZ(env, obj, + g_SampleForTests_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -245,16 +255,17 @@ static void Java_SampleForTests_iterateAndDoSomething(JNIEnv* env, jobject obj) "V", &g_SampleForTests_iterateAndDoSomething); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } static base::subtle::AtomicWord g_InnerStructB_getKey = 0; static jlong Java_InnerStructB_getKey(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InnerStructB_clazz); + CHECK_CLAZZ(env, obj, + g_InnerStructB_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -267,17 +278,18 @@ static jlong Java_InnerStructB_getKey(JNIEnv* env, jobject obj) { &g_InnerStructB_getKey); jlong ret = - env->CallLongMethod(obj, - method_id); - base::android::CheckException(env); + env->CallLongMethod(obj, + method_id); + jni_generator::CheckException(env); return ret; } static base::subtle::AtomicWord g_InnerStructB_getValue = 0; -static ScopedJavaLocalRef<jstring> Java_InnerStructB_getValue(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jstring> + Java_InnerStructB_getValue(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InnerStructB_clazz); + CHECK_CLAZZ(env, obj, + g_InnerStructB_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -290,23 +302,15 @@ static ScopedJavaLocalRef<jstring> Java_InnerStructB_getValue(JNIEnv* env, &g_InnerStructB_getValue); jstring ret = - static_cast<jstring>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jstring>(env, ret); + static_cast<jstring>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jstring>(env, ret); } // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { - - g_InnerStructA_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kInnerStructAClassPath).obj())); - g_SampleForTests_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kSampleForTestsClassPath).obj())); - g_InnerStructB_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kInnerStructBClassPath).obj())); - static const JNINativeMethod kMethodsSampleForTests[] = { +static const JNINativeMethod kMethodsSampleForTests[] = { { "nativeInit", "(" "Ljava/lang/String;" @@ -355,18 +359,34 @@ static bool RegisterNativesImpl(JNIEnv* env) { "I" ")" "V", reinterpret_cast<void*>(IterateAndDoSomethingWithStructB) }, - }; + { "nativeReturnAString", +"(" +"I" +")" +"Ljava/lang/String;", reinterpret_cast<void*>(ReturnAString) }, +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_InnerStructA_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kInnerStructAClassPath).obj())); + g_SampleForTests_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kSampleForTestsClassPath).obj())); + g_InnerStructB_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kInnerStructBClassPath).obj())); + const int kMethodsSampleForTestsSize = arraysize(kMethodsSampleForTests); if (env->RegisterNatives(g_SampleForTests_clazz, kMethodsSampleForTests, kMethodsSampleForTestsSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_SampleForTests_clazz, __FILE__); return false; } return true; } + } // namespace android } // namespace base diff --git a/base/android/jni_generator/java/src/org/chromium/example/jni_generator/SampleForTests.java b/base/android/jni_generator/java/src/org/chromium/example/jni_generator/SampleForTests.java index bad1571..3886355 100644 --- a/base/android/jni_generator/java/src/org/chromium/example/jni_generator/SampleForTests.java +++ b/base/android/jni_generator/java/src/org/chromium/example/jni_generator/SampleForTests.java @@ -6,17 +6,15 @@ package org.chromium.example.jni_generator; import android.graphics.Rect; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import org.chromium.base.AccessedByNative; import org.chromium.base.CalledByNative; import org.chromium.base.CalledByNativeUnchecked; import org.chromium.base.JNINamespace; import org.chromium.base.NativeClassQualifiedName; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; // This class serves as a reference test for the bindings generator, and as example documentation // for how to use the jni generator. @@ -285,4 +283,5 @@ class SampleForTests { native void nativeAddStructB(int nativeCPPClass, InnerStructB b); native void nativeIterateAndDoSomethingWithStructB(int nativeCPPClass); + native String nativeReturnAString(int nativeCPPClass); } diff --git a/base/android/jni_generator/jni_generator.py b/base/android/jni_generator/jni_generator.py index 649182a..dd74c7a 100755 --- a/base/android/jni_generator/jni_generator.py +++ b/base/android/jni_generator/jni_generator.py @@ -99,7 +99,6 @@ def JavaDataTypeToC(java_type): 'void': 'void', 'String': 'jstring', 'java/lang/String': 'jstring', - 'Class': 'jclass', 'java/lang/Class': 'jclass', } @@ -111,10 +110,30 @@ def JavaDataTypeToC(java_type): if java_type[:-2] in java_pod_type_map: return java_pod_type_map[java_type[:-2]] + 'Array' return 'jobjectArray' + elif java_type.startswith('Class'): + # Checking just the start of the name, rather than a direct comparison, + # in order to handle generics. + return 'jclass' else: return 'jobject' +def JavaReturnValueToC(java_type): + """Returns a valid C return value for the given java type.""" + java_pod_type_map = { + 'int': '0', + 'byte': '0', + 'char': '0', + 'short': '0', + 'boolean': 'false', + 'long': '0', + 'double': '0', + 'float': '0', + 'void': '' + } + return java_pod_type_map.get(java_type, 'NULL') + + class JniParams(object): _imports = [] _fully_qualified_class = '' @@ -530,7 +549,7 @@ class JNIFromJavaSource(object): contents = self._RemoveComments(contents) JniParams.SetFullyQualifiedClass(fully_qualified_class) JniParams.ExtractImportsAndInnerClasses(contents) - jni_namespace = ExtractJNINamespace(contents) + jni_namespace = ExtractJNINamespace(contents) or options.namespace natives = ExtractNatives(contents, options.ptr_type) called_by_natives = ExtractCalledByNatives(contents) if len(natives) == 0 and len(called_by_natives) == 0: @@ -581,7 +600,15 @@ class InlHeaderFileGenerator(object): self.natives = natives self.called_by_natives = called_by_natives self.header_guard = fully_qualified_class.replace('/', '_') + '_JNI' - self.script_name = options.script_name + self.options = options + self.init_native = self.ExtractInitNative(options) + + def ExtractInitNative(self, options): + for native in self.natives: + if options.jni_init_native_name == native.name: + self.natives.remove(native) + return native + return None def GetContent(self): """Returns the content of the JNI binding file.""" @@ -601,16 +628,12 @@ class InlHeaderFileGenerator(object): #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +${INCLUDES} // Step 1: forward declarations. namespace { $CLASS_PATH_DEFINITIONS +$METHOD_ID_DEFINITIONS } // namespace $OPEN_NAMESPACE @@ -620,24 +643,26 @@ $FORWARD_DECLARATIONS $METHOD_STUBS // Step 3: RegisterNatives. - -static bool RegisterNativesImpl(JNIEnv* env) { -$REGISTER_NATIVES_IMPL - return true; -} +$JNI_NATIVE_METHODS +$REGISTER_NATIVES $CLOSE_NAMESPACE +$JNI_REGISTER_NATIVES #endif // ${HEADER_GUARD} """) values = { - 'SCRIPT_NAME': self.script_name, + 'SCRIPT_NAME': self.options.script_name, 'FULLY_QUALIFIED_CLASS': self.fully_qualified_class, 'CLASS_PATH_DEFINITIONS': self.GetClassPathDefinitionsString(), + 'METHOD_ID_DEFINITIONS': self.GetMethodIDDefinitionsString(), 'FORWARD_DECLARATIONS': self.GetForwardDeclarationsString(), 'METHOD_STUBS': self.GetMethodStubsString(), 'OPEN_NAMESPACE': self.GetOpenNamespaceString(), - 'REGISTER_NATIVES_IMPL': self.GetRegisterNativesImplString(), + 'JNI_NATIVE_METHODS': self.GetJNINativeMethodsString(), + 'REGISTER_NATIVES': self.GetRegisterNativesString(), 'CLOSE_NAMESPACE': self.GetCloseNamespaceString(), 'HEADER_GUARD': self.header_guard, + 'INCLUDES': self.GetIncludesString(), + 'JNI_REGISTER_NATIVES': self.GetJNIRegisterNativesString() } return WrapOutput(template.substitute(values)) @@ -646,6 +671,21 @@ $CLOSE_NAMESPACE ret += [self.GetClassPathDefinitions()] return '\n'.join(ret) + def GetMethodIDDefinitionsString(self): + """Returns the definition of method ids for the called by native methods.""" + if not self.options.eager_called_by_natives: + return '' + template = Template("""\ +jmethodID g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = NULL;""") + ret = [] + for called_by_native in self.called_by_natives: + values = { + 'JAVA_CLASS': called_by_native.java_class_name or self.class_name, + 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, + } + ret += [template.substitute(values)] + return '\n'.join(ret) + def GetForwardDeclarationsString(self): ret = [] for native in self.natives: @@ -654,14 +694,36 @@ $CLOSE_NAMESPACE return '\n'.join(ret) def GetMethodStubsString(self): + """Returns the code corresponding to method stubs.""" ret = [] for native in self.natives: if native.type == 'method': - ret += [self.GetNativeMethodStub(native)] - for called_by_native in self.called_by_natives: - ret += [self.GetCalledByNativeMethodStub(called_by_native)] + ret += [self.GetNativeMethodStubString(native)] + if self.options.eager_called_by_natives: + ret += self.GetEagerCalledByNativeMethodStubs() + else: + ret += self.GetLazyCalledByNativeMethodStubs() return '\n'.join(ret) + def GetLazyCalledByNativeMethodStubs(self): + return [self.GetLazyCalledByNativeMethodStub(called_by_native) + for called_by_native in self.called_by_natives] + + def GetEagerCalledByNativeMethodStubs(self): + ret = [] + if self.called_by_natives: + ret += ['namespace {'] + for called_by_native in self.called_by_natives: + ret += [self.GetEagerCalledByNativeMethodStub(called_by_native)] + ret += ['} // namespace'] + return ret + + def GetIncludesString(self): + if not self.options.includes: + return '' + includes = self.options.includes.split(',') + return '\n'.join('#include "%s"' % x for x in includes) + def GetKMethodsString(self, clazz): ret = [] for native in self.natives: @@ -670,22 +732,9 @@ $CLOSE_NAMESPACE ret += [self.GetKMethodArrayEntry(native)] return '\n'.join(ret) - def GetRegisterNativesImplString(self): - """Returns the implementation for RegisterNatives.""" - template = Template("""\ - static const JNINativeMethod kMethods${JAVA_CLASS}[] = { -${KMETHODS} - }; - const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); - - if (env->RegisterNatives(g_${JAVA_CLASS}_clazz, - kMethods${JAVA_CLASS}, - kMethods${JAVA_CLASS}Size) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; - return false; - } -""") - ret = [self.GetFindClasses()] + def SubstituteNativeMethods(self, template): + """Substitutes JAVA_CLASS and KMETHODS in the provided template.""" + ret = [] all_classes = self.GetUniqueClasses(self.natives) all_classes[self.class_name] = self.fully_qualified_class for clazz in all_classes: @@ -697,6 +746,97 @@ ${KMETHODS} if not ret: return '' return '\n' + '\n'.join(ret) + def GetJNINativeMethodsString(self): + """Returns the implementation of the array of native methods.""" + template = Template("""\ +static const JNINativeMethod kMethods${JAVA_CLASS}[] = { +${KMETHODS} +}; +""") + return self.SubstituteNativeMethods(template) + + def GetRegisterCalledByNativesImplString(self): + """Returns the code for registering the called by native methods.""" + if not self.options.eager_called_by_natives: + return '' + template = Template("""\ + g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = ${GET_METHOD_ID_IMPL} + if (g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} == NULL) { + return false; + } + """) + ret = [] + for called_by_native in self.called_by_natives: + values = { + 'JAVA_CLASS': called_by_native.java_class_name or self.class_name, + 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, + 'GET_METHOD_ID_IMPL': self.GetMethodIDImpl(called_by_native), + } + ret += [template.substitute(values)] + return '\n'.join(ret) + + def GetRegisterNativesString(self): + """Returns the code for RegisterNatives.""" + template = Template("""\ +${REGISTER_NATIVES_SIGNATURE} { +${CLASSES} +${NATIVES} +${CALLED_BY_NATIVES} + return true; +} +""") + signature = 'static bool RegisterNativesImpl(JNIEnv* env' + if self.init_native: + signature += ', jclass clazz)' + else: + signature += ')' + + natives = self.GetRegisterNativesImplString() + called_by_natives = self.GetRegisterCalledByNativesImplString() + values = {'REGISTER_NATIVES_SIGNATURE': signature, + 'CLASSES': self.GetFindClasses(), + 'NATIVES': natives, + 'CALLED_BY_NATIVES': called_by_natives, + } + return template.substitute(values) + + def GetRegisterNativesImplString(self): + """Returns the shared implementation for RegisterNatives.""" + template = Template("""\ + const int kMethods${JAVA_CLASS}Size = arraysize(kMethods${JAVA_CLASS}); + + if (env->RegisterNatives(g_${JAVA_CLASS}_clazz, + kMethods${JAVA_CLASS}, + kMethods${JAVA_CLASS}Size) < 0) { + jni_generator::HandleRegistrationError( + env, g_${JAVA_CLASS}_clazz, __FILE__); + return false; + } +""") + return self.SubstituteNativeMethods(template) + + def GetJNIRegisterNativesString(self): + """Returns the implementation for the JNI registration of native methods.""" + if not self.init_native: + return '' + + template = Template("""\ +extern "C" JNIEXPORT bool JNICALL +Java_${FULLY_QUALIFIED_CLASS}_${INIT_NATIVE_NAME}(JNIEnv* env, jclass clazz) { + return ${NAMESPACE}RegisterNativesImpl(env, clazz); +} +""") + fully_qualified_class = self.fully_qualified_class.replace('/', '_') + namespace = '' + if self.namespace: + namespace = self.namespace + '::' + values = {'FULLY_QUALIFIED_CLASS': fully_qualified_class, + 'INIT_NATIVE_NAME': self.init_native.name, + 'NAMESPACE': namespace, + 'REGISTER_NATIVES_IMPL': self.GetRegisterNativesImplString() + } + return template.substitute(values) + def GetOpenNamespaceString(self): if self.namespace: all_namespaces = ['namespace %s {' % ns @@ -715,12 +855,12 @@ ${KMETHODS} def GetJNIFirstParam(self, native): ret = [] if native.type == 'method': - ret = ['jobject obj'] + ret = ['jobject jcaller'] elif native.type == 'function': if native.static: - ret = ['jclass clazz'] + ret = ['jclass jcaller'] else: - ret = ['jobject obj'] + ret = ['jobject jcaller'] return ret def GetParamsInDeclaration(self, native): @@ -751,61 +891,41 @@ static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS}); 'PARAMS': self.GetParamsInDeclaration(native)} return template.substitute(values) - def GetNativeMethodStub(self, native): + def GetNativeMethodStubString(self, native): """Returns stubs for native methods.""" template = Template("""\ static ${RETURN} ${NAME}(JNIEnv* env, ${PARAMS_IN_DECLARATION}) { - DCHECK(${PARAM0_NAME}) << "${NAME}"; ${P0_TYPE}* native = reinterpret_cast<${P0_TYPE}*>(${PARAM0_NAME}); - return native->${NAME}(env, obj${PARAMS_IN_CALL})${POST_CALL}; + CHECK_NATIVE_PTR(env, jcaller, native, "${NAME}"${OPTIONAL_ERROR_RETURN}); + return native->${NAME}(${PARAMS_IN_CALL})${POST_CALL}; } """) - params_for_call = ', '.join(p.name for p in native.params[1:]) - if params_for_call: - params_for_call = ', ' + params_for_call + params = [] + if not self.options.pure_native_methods: + params = ['env', 'jcaller'] + params_in_call = ', '.join(params + [p.name for p in native.params[1:]]) return_type = JavaDataTypeToC(native.return_type) + optional_error_return = JavaReturnValueToC(native.return_type) + if optional_error_return: + optional_error_return = ', ' + optional_error_return + post_call = '' if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): - scoped_return_type = 'ScopedJavaLocalRef<' + return_type + '>' post_call = '.Release()' - else: - scoped_return_type = return_type - post_call = '' values = { 'RETURN': return_type, - 'SCOPED_RETURN': scoped_return_type, + 'OPTIONAL_ERROR_RETURN': optional_error_return, 'NAME': native.name, 'PARAMS_IN_DECLARATION': self.GetParamsInDeclaration(native), 'PARAM0_NAME': native.params[0].name, 'P0_TYPE': native.p0_type, - 'PARAMS_IN_CALL': params_for_call, + 'PARAMS_IN_CALL': params_in_call, 'POST_CALL': post_call } return template.substitute(values) - def GetCalledByNativeMethodStub(self, called_by_native): - """Returns a string.""" - function_signature_template = Template("""\ -static ${RETURN_TYPE} Java_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}(\ -JNIEnv* env${FIRST_PARAM_IN_DECLARATION}${PARAMS_IN_DECLARATION})""") - function_header_template = Template("""\ -${FUNCTION_SIGNATURE} {""") - function_header_with_unused_template = Template("""\ -${FUNCTION_SIGNATURE} __attribute__ ((unused)); -${FUNCTION_SIGNATURE} {""") - template = Template(""" -static base::subtle::AtomicWord g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = 0; -${FUNCTION_HEADER} - /* Must call RegisterNativesImpl() */ - DCHECK(g_${JAVA_CLASS}_clazz); - jmethodID method_id = - ${GET_METHOD_ID_IMPL} - ${RETURN_DECLARATION} - ${PRE_CALL}env->${ENV_CALL}(${FIRST_PARAM_IN_CALL}, - method_id${PARAMS_IN_CALL})${POST_CALL}; - ${CHECK_EXCEPTION} - ${RETURN_CLAUSE} -}""") + def GetCalledByNativeValues(self, called_by_native): + """Fills in necessary values for the CalledByNative methods.""" if called_by_native.static or called_by_native.is_constructor: first_param_in_declaration = '' first_param_in_call = ('g_%s_clazz' % @@ -818,10 +938,9 @@ ${FUNCTION_HEADER} called_by_native) if params_in_declaration: params_in_declaration = ', ' + params_in_declaration - params_for_call = ', '.join(param.name - for param in called_by_native.params) - if params_for_call: - params_for_call = ', ' + params_for_call + params_in_call = ', '.join(param.name for param in called_by_native.params) + if params_in_call: + params_in_call = ', ' + params_in_call pre_call = '' post_call = '' if called_by_native.static_cast: @@ -829,36 +948,76 @@ ${FUNCTION_HEADER} post_call = ')' check_exception = '' if not called_by_native.unchecked: - check_exception = 'base::android::CheckException(env);' + check_exception = 'jni_generator::CheckException(env);' return_type = JavaDataTypeToC(called_by_native.return_type) + optional_error_return = JavaReturnValueToC(called_by_native.return_type) + if optional_error_return: + optional_error_return = ', ' + optional_error_return return_declaration = '' return_clause = '' if return_type != 'void': - pre_call = ' ' + pre_call + pre_call = ' ' + pre_call return_declaration = return_type + ' ret =' if re.match(RE_SCOPED_JNI_RETURN_TYPES, return_type): - return_type = 'ScopedJavaLocalRef<' + return_type + '>' + return_type = 'base::android::ScopedJavaLocalRef<' + return_type + '>' return_clause = 'return ' + return_type + '(env, ret);' else: return_clause = 'return ret;' - values = { + return { 'JAVA_CLASS': called_by_native.java_class_name or self.class_name, - 'METHOD': called_by_native.name, 'RETURN_TYPE': return_type, + 'OPTIONAL_ERROR_RETURN': optional_error_return, 'RETURN_DECLARATION': return_declaration, 'RETURN_CLAUSE': return_clause, 'FIRST_PARAM_IN_DECLARATION': first_param_in_declaration, 'PARAMS_IN_DECLARATION': params_in_declaration, - 'STATIC': 'Static' if called_by_native.static else '', 'PRE_CALL': pre_call, 'POST_CALL': post_call, 'ENV_CALL': called_by_native.env_call, 'FIRST_PARAM_IN_CALL': first_param_in_call, - 'PARAMS_IN_CALL': params_for_call, + 'PARAMS_IN_CALL': params_in_call, 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, 'CHECK_EXCEPTION': check_exception, 'GET_METHOD_ID_IMPL': self.GetMethodIDImpl(called_by_native) } + + def GetEagerCalledByNativeMethodStub(self, called_by_native): + """Returns the implementation of the called by native method.""" + template = Template(""" +static ${RETURN_TYPE} ${METHOD_ID_VAR_NAME}(\ +JNIEnv* env${FIRST_PARAM_IN_DECLARATION}${PARAMS_IN_DECLARATION}) { + ${RETURN_DECLARATION}${PRE_CALL}env->${ENV_CALL}(${FIRST_PARAM_IN_CALL}, + g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}${PARAMS_IN_CALL})${POST_CALL}; + ${RETURN_CLAUSE} +}""") + values = self.GetCalledByNativeValues(called_by_native) + return template.substitute(values) + + def GetLazyCalledByNativeMethodStub(self, called_by_native): + """Returns a string.""" + function_signature_template = Template("""\ +static ${RETURN_TYPE} Java_${JAVA_CLASS}_${METHOD_ID_VAR_NAME}(\ +JNIEnv* env${FIRST_PARAM_IN_DECLARATION}${PARAMS_IN_DECLARATION})""") + function_header_template = Template("""\ +${FUNCTION_SIGNATURE} {""") + function_header_with_unused_template = Template("""\ +${FUNCTION_SIGNATURE} __attribute__ ((unused)); +${FUNCTION_SIGNATURE} {""") + template = Template(""" +static base::subtle::AtomicWord g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME} = 0; +${FUNCTION_HEADER} + /* Must call RegisterNativesImpl() */ + CHECK_CLAZZ(env, ${FIRST_PARAM_IN_CALL}, + g_${JAVA_CLASS}_clazz${OPTIONAL_ERROR_RETURN}); + jmethodID method_id = + ${GET_METHOD_ID_IMPL} + ${RETURN_DECLARATION} + ${PRE_CALL}env->${ENV_CALL}(${FIRST_PARAM_IN_CALL}, + method_id${PARAMS_IN_CALL})${POST_CALL}; + ${CHECK_EXCEPTION} + ${RETURN_CLAUSE} +}""") + values = self.GetCalledByNativeValues(called_by_native) values['FUNCTION_SIGNATURE'] = ( function_signature_template.substitute(values)) if called_by_native.system_class: @@ -916,7 +1075,11 @@ jclass g_${JAVA_CLASS}_clazz = NULL;""") def GetFindClasses(self): """Returns the imlementation of FindClass for all known classes.""" - template = Template("""\ + if self.init_native: + template = Template("""\ + g_${JAVA_CLASS}_clazz = static_cast<jclass>(env->NewWeakGlobalRef(clazz));""") + else: + template = Template("""\ g_${JAVA_CLASS}_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetClass(env, k${JAVA_CLASS}ClassPath).obj()));""") ret = [] @@ -927,7 +1090,13 @@ jclass g_${JAVA_CLASS}_clazz = NULL;""") def GetMethodIDImpl(self, called_by_native): """Returns the implementation of GetMethodID.""" - template = Template("""\ + if self.options.eager_called_by_natives: + template = Template("""\ +env->Get${STATIC_METHOD_PART}MethodID( + g_${JAVA_CLASS}_clazz, + "${JNI_NAME}", ${JNI_SIGNATURE});""") + else: + template = Template("""\ base::android::MethodID::LazyGet< base::android::MethodID::TYPE_${STATIC}>( env, g_${JAVA_CLASS}_clazz, @@ -951,6 +1120,7 @@ jclass g_${JAVA_CLASS}_clazz = NULL;""") 'JNI_NAME': jni_name, 'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name, 'STATIC': 'STATIC' if called_by_native.static else 'INSTANCE', + 'STATIC_METHOD_PART': 'Static' if called_by_native.static else '', 'JNI_SIGNATURE': signature, } return template.substitute(values) @@ -1056,8 +1226,9 @@ See SampleForTests.java for more details. ' pre-compiled class. --input should point' ' to pre-compiled Java .class files.') option_parser.add_option('-n', dest='namespace', - help='Uses as a namespace in the generated header,' - ' instead of the javap class name.') + help='Uses as a namespace in the generated header ' + 'instead of the javap class name, or when there is ' + 'no JNINamespace annotation in the java source.') option_parser.add_option('--input_file', help='Single input file name. The output file name ' 'will be derived from it. Must be used with ' @@ -1074,11 +1245,31 @@ See SampleForTests.java for more details. option_parser.add_option('--script_name', default=GetScriptName(), help='The name of this script in the generated ' 'header.') + option_parser.add_option('--includes', + help='The comma-separated list of header files to ' + 'include in the generated header.') + option_parser.add_option('--pure_native_methods', + action='store_true', dest='pure_native_methods', + help='When true, the native methods will be called ' + 'without any JNI-specific arguments.') option_parser.add_option('--ptr_type', default='int', type='choice', choices=['int', 'long'], help='The type used to represent native pointers in ' 'Java code. For 32-bit, use int; ' 'for 64-bit, use long.') + option_parser.add_option('--jni_init_native_name', default='', + help='The name of the JNI registration method that ' + 'is used to initialize all native methods. If a ' + 'method with this name is not present in the Java ' + 'source file, setting this option is a no-op. When ' + 'a method with this name is found however, the ' + 'naming convention Java_<packageName>_<className> ' + 'will limit the initialization to only the ' + 'top-level class.') + option_parser.add_option('--eager_called_by_natives', + action='store_true', dest='eager_called_by_natives', + help='When true, the called-by-native methods will ' + 'be initialized in a non-atomic way.') options, args = option_parser.parse_args(argv) if options.jar_file: input_file = ExtractJarInputFile(options.jar_file, options.input_file, diff --git a/base/android/jni_generator/jni_generator_helper.h b/base/android/jni_generator/jni_generator_helper.h new file mode 100644 index 0000000..b9736e1 --- /dev/null +++ b/base/android/jni_generator/jni_generator_helper.h @@ -0,0 +1,40 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + + +#ifndef BASE_ANDROID_JNI_GENERATOR_JNI_GENERATOR_HELPER_H_ +#define BASE_ANDROID_JNI_GENERATOR_JNI_GENERATOR_HELPER_H_ + +#include <jni.h> + +#include "base/android/jni_android.h" +#include "base/android/scoped_java_ref.h" +#include "base/basictypes.h" +#include "base/logging.h" + +// Project-specific macros used by the header files generated by +// jni_generator.py. Different projects can then specify their own +// implementation for this file. +#define CHECK_NATIVE_PTR(env, jcaller, native_ptr, method_name, ...) \ + DCHECK(native_ptr) << method_name; + +#define CHECK_CLAZZ(env, jcaller, clazz, ...) \ + DCHECK(clazz); + +namespace jni_generator { + + inline void HandleRegistrationError(JNIEnv* env, jclass clazz, + const char* filename) { + LOG(ERROR) << "RegisterNatives failed in " << filename; + } + + inline void CheckException(JNIEnv* env) { + base::android::CheckException(env); + } + +} // namespace jni_generator + +using base::android::ScopedJavaLocalRef; + +#endif // BASE_ANDROID_JNI_GENERATOR_JNI_GENERATOR_HELPER_H_ diff --git a/base/android/jni_generator/jni_generator_tests.py b/base/android/jni_generator/jni_generator_tests.py index 9e73bd9..6f1484c 100755 --- a/base/android/jni_generator/jni_generator_tests.py +++ b/base/android/jni_generator/jni_generator_tests.py @@ -21,6 +21,9 @@ from jni_generator import CalledByNative, JniParams, NativeMethod, Param SCRIPT_NAME = 'base/android/jni_generator/jni_generator.py' +INCLUDES = ( + 'base/android/jni_generator/jni_generator_helper.h' +) # Set this environment variable in order to regenerate the golden text # files. @@ -32,7 +35,11 @@ class TestOptions(object): def __init__(self): self.namespace = None self.script_name = SCRIPT_NAME + self.includes = INCLUDES + self.pure_native_methods = False self.ptr_type = 'int' + self.jni_init_native_name = None + self.eager_called_by_natives = False class TestGenerator(unittest.TestCase): @@ -58,6 +65,10 @@ class TestGenerator(unittest.TestCase): self.assertEquals(first[i], second[i]) def assertTextEquals(self, golden_text, generated_text): + if not self.compareText(golden_text, generated_text): + self.fail('Golden text mismatch.') + + def compareText(self, golden_text, generated_text): def FilterText(text): return [ l.strip() for l in text.split('\n') @@ -65,18 +76,18 @@ class TestGenerator(unittest.TestCase): ] stripped_golden = FilterText(golden_text) stripped_generated = FilterText(generated_text) - if stripped_golden != stripped_generated: - print self.id() - for line in difflib.context_diff(stripped_golden, stripped_generated): - print line - print '\n\nGenerated' - print '=' * 80 - print generated_text - print '=' * 80 - print 'Run with:' - print 'REBASELINE=1', sys.argv[0] - print 'to regenerate the data files.' - self.fail('Golden text mismatch.') + if stripped_golden == stripped_generated: + return True + print self.id() + for line in difflib.context_diff(stripped_golden, stripped_generated): + print line + print '\n\nGenerated' + print '=' * 80 + print generated_text + print '=' * 80 + print 'Run with:' + print 'REBASELINE=1', sys.argv[0] + print 'to regenerate the data files.' def _ReadGoldenFile(self, golden_file): if not os.path.exists(golden_file): @@ -799,12 +810,18 @@ public long skip(long) throws java.io.IOException; content = file(os.path.join(script_dir, 'java/src/org/chromium/example/jni_generator/SampleForTests.java') ).read() - golden_content = file(os.path.join(script_dir, - 'golden_sample_for_tests_jni.h')).read() + golden_file = os.path.join(script_dir, 'golden_sample_for_tests_jni.h') + golden_content = file(golden_file).read() jni_from_java = jni_generator.JNIFromJavaSource( content, 'org/chromium/example/jni_generator/SampleForTests', TestOptions()) - self.assertTextEquals(golden_content, jni_from_java.GetContent()) + generated_text = jni_from_java.GetContent() + if not self.compareText(golden_content, generated_text): + if os.environ.get(REBASELINE_ENV): + with file(golden_file, 'w') as f: + f.write(generated_text) + return + self.fail('testJniSelfDocumentingExample') def testNoWrappingPreprocessorLines(self): test_data = """ @@ -927,6 +944,67 @@ class Foo { natives, [], test_options) self.assertGoldenTextEquals(h.GetContent()) + def testPureNativeMethodsOption(self): + test_data = """ + package org.chromium.example.jni_generator; + + /** The pointer to the native Test. */ + int nativeTest; + + class Test { + private static native int nativeMethod(int nativeTest, int arg1); + } + """ + options = TestOptions() + options.pure_native_methods = True + jni_from_java = jni_generator.JNIFromJavaSource( + test_data, 'org/chromium/example/jni_generator/Test', options) + self.assertGoldenTextEquals(jni_from_java.GetContent()) + + def testJNIInitNativeNameOption(self): + test_data = """ + package org.chromium.example.jni_generator; + + /** The pointer to the native Test. */ + int nativeTest; + + class Test { + private static native boolean initNativeClass(); + private static native int nativeMethod(int nativeTest, int arg1); + } + """ + options = TestOptions() + options.jni_init_native_name = 'initNativeClass' + jni_from_java = jni_generator.JNIFromJavaSource( + test_data, 'org/chromium/example/jni_generator/Test', options) + self.assertGoldenTextEquals(jni_from_java.GetContent()) + + def testEagerCalledByNativesOption(self): + test_data = """ + package org.chromium.example.jni_generator; + + /** The pointer to the native Test. */ + int nativeTest; + + class Test { + private static native boolean initNativeClass(); + private static native int nativeMethod(int nativeTest, int arg1); + @CalledByNative + private void testMethodWithParam(int iParam); + @CalledByNative + private static int testStaticMethodWithParam(int iParam); + @CalledByNative + private static double testMethodWithNoParam(); + @CalledByNative + private static String testStaticMethodWithNoParam(); + } + """ + options = TestOptions() + options.jni_init_native_name = 'initNativeClass' + options.eager_called_by_natives = True + jni_from_java = jni_generator.JNIFromJavaSource( + test_data, 'org/chromium/example/jni_generator/Test', options) + self.assertGoldenTextEquals(jni_from_java.GetContent()) if __name__ == '__main__': unittest.main() diff --git a/base/android/jni_generator/sample_for_tests.cc b/base/android/jni_generator/sample_for_tests.cc index 30b9940..24190c6 100644 --- a/base/android/jni_generator/sample_for_tests.cc +++ b/base/android/jni_generator/sample_for_tests.cc @@ -51,6 +51,13 @@ void CPPClass::IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj) { map_.clear(); } +base::android::ScopedJavaLocalRef<jstring> CPPClass::ReturnAString( + JNIEnv* env, jobject obj) { + base::android::ScopedJavaLocalRef<jstring> ret = ConvertUTF8ToJavaString( + env, "test"); + return ret; +} + // Static free functions declared and called directly from java. static jint Init(JNIEnv* env, jobject obj, jstring param) { return 0; diff --git a/base/android/jni_generator/sample_for_tests.h b/base/android/jni_generator/sample_for_tests.h index 278008b..4d76408 100644 --- a/base/android/jni_generator/sample_for_tests.h +++ b/base/android/jni_generator/sample_for_tests.h @@ -6,6 +6,7 @@ #include <map> #include <string> +#include "base/android/jni_android.h" #include "base/basictypes.h" namespace base { @@ -36,6 +37,9 @@ class CPPClass { void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj); + base::android::ScopedJavaLocalRef<jstring> ReturnAString( + JNIEnv* env, jobject obj); + private: std::map<long, std::string> map_; diff --git a/base/android/jni_generator/testCalledByNatives.golden b/base/android/jni_generator/testCalledByNatives.golden index 332c099..854275c 100644 --- a/base/android/jni_generator/testCalledByNatives.golden +++ b/base/android/jni_generator/testCalledByNatives.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,12 +12,7 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { @@ -27,19 +22,22 @@ const char kInfoBarClassPath[] = "org/chromium/TestJni$InfoBar"; jclass g_TestJni_clazz = NULL; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_InfoBar_clazz = NULL; + } // namespace // Step 2: method stubs. static base::subtle::AtomicWord g_TestJni_showConfirmInfoBar = 0; -static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env, - jobject obj, jint nativeInfoBar, +static base::android::ScopedJavaLocalRef<jobject> + Java_TestJni_showConfirmInfoBar(JNIEnv* env, jobject obj, jint + nativeInfoBar, jstring buttonOk, jstring buttonCancel, jstring title, jobject icon) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -57,20 +55,22 @@ static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env, &g_TestJni_showConfirmInfoBar); jobject ret = - env->CallObjectMethod(obj, - method_id, nativeInfoBar, buttonOk, buttonCancel, title, icon); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallObjectMethod(obj, + method_id, nativeInfoBar, buttonOk, buttonCancel, title, icon); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } static base::subtle::AtomicWord g_TestJni_showAutoLoginInfoBar = 0; -static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* - env, jobject obj, jint nativeInfoBar, +static base::android::ScopedJavaLocalRef<jobject> + Java_TestJni_showAutoLoginInfoBar(JNIEnv* env, jobject obj, jint + nativeInfoBar, jstring realm, jstring account, jstring args) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -87,16 +87,17 @@ static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* &g_TestJni_showAutoLoginInfoBar); jobject ret = - env->CallObjectMethod(obj, - method_id, nativeInfoBar, realm, account, args); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallObjectMethod(obj, + method_id, nativeInfoBar, realm, account, args); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } static base::subtle::AtomicWord g_InfoBar_dismiss = 0; static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InfoBar_clazz); + CHECK_CLAZZ(env, obj, + g_InfoBar_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -108,9 +109,9 @@ static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) { "V", &g_InfoBar_dismiss); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } @@ -120,7 +121,8 @@ static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject view, jstring account, jstring args) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, g_TestJni_clazz, + g_TestJni_clazz, false); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_STATIC>( @@ -137,17 +139,18 @@ static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject view, &g_TestJni_shouldShowAutoLogin); jboolean ret = - env->CallStaticBooleanMethod(g_TestJni_clazz, - method_id, view, realm, account, args); - base::android::CheckException(env); + env->CallStaticBooleanMethod(g_TestJni_clazz, + method_id, view, realm, account, args); + jni_generator::CheckException(env); return ret; } static base::subtle::AtomicWord g_TestJni_openUrl = 0; -static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring - url) { +static base::android::ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* + env, jstring url) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, g_TestJni_clazz, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_STATIC>( @@ -161,10 +164,10 @@ static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring &g_TestJni_openUrl); jobject ret = - env->CallStaticObjectMethod(g_TestJni_clazz, - method_id, url); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallStaticObjectMethod(g_TestJni_clazz, + method_id, url); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } static base::subtle::AtomicWord g_TestJni_activateHardwareAcceleration = 0; @@ -175,7 +178,8 @@ static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj, jint iPrimaryID, jint iSecondaryID) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -192,16 +196,17 @@ static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj, "V", &g_TestJni_activateHardwareAcceleration); - env->CallVoidMethod(obj, - method_id, activated, iPid, iType, iPrimaryID, iSecondaryID); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id, activated, iPid, iType, iPrimaryID, iSecondaryID); + jni_generator::CheckException(env); } static base::subtle::AtomicWord g_TestJni_uncheckedCall = 0; static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -214,16 +219,17 @@ static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) { "V", &g_TestJni_uncheckedCall); - env->CallVoidMethod(obj, - method_id, iParam); + env->CallVoidMethod(obj, + method_id, iParam); } static base::subtle::AtomicWord g_TestJni_returnByteArray = 0; -static ScopedJavaLocalRef<jbyteArray> Java_TestJni_returnByteArray(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jbyteArray> + Java_TestJni_returnByteArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -236,17 +242,18 @@ static ScopedJavaLocalRef<jbyteArray> Java_TestJni_returnByteArray(JNIEnv* env, &g_TestJni_returnByteArray); jbyteArray ret = - static_cast<jbyteArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jbyteArray>(env, ret); + static_cast<jbyteArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jbyteArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnBooleanArray = 0; -static ScopedJavaLocalRef<jbooleanArray> Java_TestJni_returnBooleanArray(JNIEnv* - env, jobject obj) { +static base::android::ScopedJavaLocalRef<jbooleanArray> + Java_TestJni_returnBooleanArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -259,17 +266,18 @@ static ScopedJavaLocalRef<jbooleanArray> Java_TestJni_returnBooleanArray(JNIEnv* &g_TestJni_returnBooleanArray); jbooleanArray ret = - static_cast<jbooleanArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jbooleanArray>(env, ret); + static_cast<jbooleanArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jbooleanArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnCharArray = 0; -static ScopedJavaLocalRef<jcharArray> Java_TestJni_returnCharArray(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jcharArray> + Java_TestJni_returnCharArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -282,17 +290,18 @@ static ScopedJavaLocalRef<jcharArray> Java_TestJni_returnCharArray(JNIEnv* env, &g_TestJni_returnCharArray); jcharArray ret = - static_cast<jcharArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jcharArray>(env, ret); + static_cast<jcharArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jcharArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnShortArray = 0; -static ScopedJavaLocalRef<jshortArray> Java_TestJni_returnShortArray(JNIEnv* - env, jobject obj) { +static base::android::ScopedJavaLocalRef<jshortArray> + Java_TestJni_returnShortArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -305,17 +314,18 @@ static ScopedJavaLocalRef<jshortArray> Java_TestJni_returnShortArray(JNIEnv* &g_TestJni_returnShortArray); jshortArray ret = - static_cast<jshortArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jshortArray>(env, ret); + static_cast<jshortArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jshortArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnIntArray = 0; -static ScopedJavaLocalRef<jintArray> Java_TestJni_returnIntArray(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jintArray> + Java_TestJni_returnIntArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -328,17 +338,18 @@ static ScopedJavaLocalRef<jintArray> Java_TestJni_returnIntArray(JNIEnv* env, &g_TestJni_returnIntArray); jintArray ret = - static_cast<jintArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jintArray>(env, ret); + static_cast<jintArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jintArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnLongArray = 0; -static ScopedJavaLocalRef<jlongArray> Java_TestJni_returnLongArray(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jlongArray> + Java_TestJni_returnLongArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -351,17 +362,18 @@ static ScopedJavaLocalRef<jlongArray> Java_TestJni_returnLongArray(JNIEnv* env, &g_TestJni_returnLongArray); jlongArray ret = - static_cast<jlongArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jlongArray>(env, ret); + static_cast<jlongArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jlongArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnDoubleArray = 0; -static ScopedJavaLocalRef<jdoubleArray> Java_TestJni_returnDoubleArray(JNIEnv* - env, jobject obj) { +static base::android::ScopedJavaLocalRef<jdoubleArray> + Java_TestJni_returnDoubleArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -374,17 +386,18 @@ static ScopedJavaLocalRef<jdoubleArray> Java_TestJni_returnDoubleArray(JNIEnv* &g_TestJni_returnDoubleArray); jdoubleArray ret = - static_cast<jdoubleArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jdoubleArray>(env, ret); + static_cast<jdoubleArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jdoubleArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnObjectArray = 0; -static ScopedJavaLocalRef<jobjectArray> Java_TestJni_returnObjectArray(JNIEnv* - env, jobject obj) { +static base::android::ScopedJavaLocalRef<jobjectArray> + Java_TestJni_returnObjectArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -397,17 +410,18 @@ static ScopedJavaLocalRef<jobjectArray> Java_TestJni_returnObjectArray(JNIEnv* &g_TestJni_returnObjectArray); jobjectArray ret = - static_cast<jobjectArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobjectArray>(env, ret); + static_cast<jobjectArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobjectArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_returnArrayOfByteArray = 0; -static ScopedJavaLocalRef<jobjectArray> +static base::android::ScopedJavaLocalRef<jobjectArray> Java_TestJni_returnArrayOfByteArray(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -420,17 +434,18 @@ static ScopedJavaLocalRef<jobjectArray> &g_TestJni_returnArrayOfByteArray); jobjectArray ret = - static_cast<jobjectArray>(env->CallObjectMethod(obj, - method_id)); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobjectArray>(env, ret); + static_cast<jobjectArray>(env->CallObjectMethod(obj, + method_id)); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobjectArray>(env, ret); } static base::subtle::AtomicWord g_TestJni_getCompressFormat = 0; -static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormat(JNIEnv* env, - jobject obj) { +static base::android::ScopedJavaLocalRef<jobject> + Java_TestJni_getCompressFormat(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -443,17 +458,18 @@ static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormat(JNIEnv* env, &g_TestJni_getCompressFormat); jobject ret = - env->CallObjectMethod(obj, - method_id); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallObjectMethod(obj, + method_id); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } static base::subtle::AtomicWord g_TestJni_getCompressFormatList = 0; -static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormatList(JNIEnv* - env, jobject obj) { +static base::android::ScopedJavaLocalRef<jobject> + Java_TestJni_getCompressFormatList(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_TestJni_clazz); + CHECK_CLAZZ(env, obj, + g_TestJni_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -466,20 +482,20 @@ static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormatList(JNIEnv* &g_TestJni_getCompressFormatList); jobject ret = - env->CallObjectMethod(obj, - method_id); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->CallObjectMethod(obj, + method_id); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } // Step 3: RegisterNatives. static bool RegisterNativesImpl(JNIEnv* env) { - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetClass(env, kTestJniClassPath).obj())); g_InfoBar_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetClass(env, kInfoBarClassPath).obj())); + return true; } diff --git a/base/android/jni_generator/testEagerCalledByNativesOption.golden b/base/android/jni_generator/testEagerCalledByNativesOption.golden new file mode 100644 index 0000000..7ed0b6d --- /dev/null +++ b/base/android/jni_generator/testEagerCalledByNativesOption.golden @@ -0,0 +1,140 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file is autogenerated by +// base/android/jni_generator/jni_generator.py +// For +// org/chromium/example/jni_generator/Test + +#ifndef org_chromium_example_jni_generator_Test_JNI +#define org_chromium_example_jni_generator_Test_JNI + +#include <jni.h> + +#include "base/android/jni_generator/jni_generator_helper.h" + +// Step 1: forward declarations. +namespace { +const char kTestClassPath[] = "org/chromium/example/jni_generator/Test"; +// Leaking this jclass as we cannot use LazyInstance from some threads. +jclass g_Test_clazz = NULL; +jmethodID g_Test_testMethodWithParam = NULL; +jmethodID g_Test_testStaticMethodWithParam = NULL; +jmethodID g_Test_testMethodWithNoParam = NULL; +jmethodID g_Test_testStaticMethodWithNoParam = NULL; +} // namespace + +// Step 2: method stubs. +static jint Method(JNIEnv* env, jobject jcaller, + jint nativeTest, + jint arg1) { + Test* native = reinterpret_cast<Test*>(nativeTest); + CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); + return native->Method(env, jcaller, arg1); +} + +namespace { + +static void testMethodWithParam(JNIEnv* env, jobject obj, jint iParam) { + env->CallVoidMethod(obj, + g_Test_testMethodWithParam, iParam); + +} + +static jint testStaticMethodWithParam(JNIEnv* env, jint iParam) { + jint ret = env->CallStaticIntMethod(g_Test_clazz, + g_Test_testStaticMethodWithParam, iParam); + return ret; +} + +static jdouble testMethodWithNoParam(JNIEnv* env) { + jdouble ret = env->CallStaticDoubleMethod(g_Test_clazz, + g_Test_testMethodWithNoParam); + return ret; +} + +static base::android::ScopedJavaLocalRef<jstring> + testStaticMethodWithNoParam(JNIEnv* env) { + jstring ret = static_cast<jstring>(env->CallStaticObjectMethod(g_Test_clazz, + g_Test_testStaticMethodWithNoParam)); + return base::android::ScopedJavaLocalRef<jstring>(env, ret); +} +} // namespace + +// Step 3: RegisterNatives. + +static const JNINativeMethod kMethodsTest[] = { + { "nativeMethod", +"(" +"I" +"I" +")" +"I", reinterpret_cast<void*>(Method) }, +}; + +static bool RegisterNativesImpl(JNIEnv* env, jclass clazz) { + g_Test_clazz = static_cast<jclass>(env->NewWeakGlobalRef(clazz)); + + const int kMethodsTestSize = arraysize(kMethodsTest); + + if (env->RegisterNatives(g_Test_clazz, + kMethodsTest, + kMethodsTestSize) < 0) { + jni_generator::HandleRegistrationError( + env, g_Test_clazz, __FILE__); + return false; + } + + g_Test_testMethodWithParam = env->GetMethodID( + g_Test_clazz, + "testMethodWithParam", +"(" +"I" +")" +"V"); + if (g_Test_testMethodWithParam == NULL) { + return false; + } + + g_Test_testStaticMethodWithParam = env->GetStaticMethodID( + g_Test_clazz, + "testStaticMethodWithParam", +"(" +"I" +")" +"I"); + if (g_Test_testStaticMethodWithParam == NULL) { + return false; + } + + g_Test_testMethodWithNoParam = env->GetStaticMethodID( + g_Test_clazz, + "testMethodWithNoParam", +"(" +")" +"D"); + if (g_Test_testMethodWithNoParam == NULL) { + return false; + } + + g_Test_testStaticMethodWithNoParam = env->GetStaticMethodID( + g_Test_clazz, + "testStaticMethodWithNoParam", +"(" +")" +"Ljava/lang/String;"); + if (g_Test_testStaticMethodWithNoParam == NULL) { + return false; + } + + return true; +} + +extern "C" JNIEXPORT bool JNICALL +Java_org_chromium_example_jni_generator_Test_initNativeClass(JNIEnv* env, jclass + clazz) { + return RegisterNativesImpl(env, clazz); +} + +#endif // org_chromium_example_jni_generator_Test_JNI diff --git a/base/android/jni_generator/testFromJavaP.golden b/base/android/jni_generator/testFromJavaP.golden index 3797d3e..d30acb1 100644 --- a/base/android/jni_generator/testFromJavaP.golden +++ b/base/android/jni_generator/testFromJavaP.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,18 +12,14 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { const char kInputStreamClassPath[] = "java/io/InputStream"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_InputStream_clazz = NULL; + } // namespace namespace JNI_InputStream { @@ -35,7 +31,8 @@ static jint Java_InputStream_available(JNIEnv* env, jobject obj) __attribute__ ((unused)); static jint Java_InputStream_available(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -45,9 +42,9 @@ static jint Java_InputStream_available(JNIEnv* env, jobject obj) { &g_InputStream_available); jint ret = - env->CallIntMethod(obj, - method_id); - base::android::CheckException(env); + env->CallIntMethod(obj, + method_id); + jni_generator::CheckException(env); return ret; } @@ -56,7 +53,8 @@ static void Java_InputStream_close(JNIEnv* env, jobject obj) __attribute__ ((unused)); static void Java_InputStream_close(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -65,9 +63,9 @@ static void Java_InputStream_close(JNIEnv* env, jobject obj) { "()V", &g_InputStream_close); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } @@ -76,7 +74,8 @@ static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) __attribute__ ((unused)); static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -85,9 +84,9 @@ static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) { "(I)V", &g_InputStream_mark); - env->CallVoidMethod(obj, - method_id, p0); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id, p0); + jni_generator::CheckException(env); } @@ -96,7 +95,8 @@ static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) __attribute__ ((unused)); static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, false); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -106,9 +106,9 @@ static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) { &g_InputStream_markSupported); jboolean ret = - env->CallBooleanMethod(obj, - method_id); - base::android::CheckException(env); + env->CallBooleanMethod(obj, + method_id); + jni_generator::CheckException(env); return ret; } @@ -117,7 +117,8 @@ static jint Java_InputStream_readI(JNIEnv* env, jobject obj) __attribute__ ((unused)); static jint Java_InputStream_readI(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -127,9 +128,9 @@ static jint Java_InputStream_readI(JNIEnv* env, jobject obj) { &g_InputStream_readI); jint ret = - env->CallIntMethod(obj, - method_id); - base::android::CheckException(env); + env->CallIntMethod(obj, + method_id); + jni_generator::CheckException(env); return ret; } @@ -138,7 +139,8 @@ static jint Java_InputStream_readI_AB(JNIEnv* env, jobject obj, jbyteArray p0) __attribute__ ((unused)); static jint Java_InputStream_readI_AB(JNIEnv* env, jobject obj, jbyteArray p0) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -148,9 +150,9 @@ static jint Java_InputStream_readI_AB(JNIEnv* env, jobject obj, jbyteArray p0) { &g_InputStream_readI_AB); jint ret = - env->CallIntMethod(obj, - method_id, p0); - base::android::CheckException(env); + env->CallIntMethod(obj, + method_id, p0); + jni_generator::CheckException(env); return ret; } @@ -164,7 +166,8 @@ static jint Java_InputStream_readI_AB_I_I(JNIEnv* env, jobject obj, jbyteArray jint p1, jint p2) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -174,9 +177,9 @@ static jint Java_InputStream_readI_AB_I_I(JNIEnv* env, jobject obj, jbyteArray &g_InputStream_readI_AB_I_I); jint ret = - env->CallIntMethod(obj, - method_id, p0, p1, p2); - base::android::CheckException(env); + env->CallIntMethod(obj, + method_id, p0, p1, p2); + jni_generator::CheckException(env); return ret; } @@ -185,7 +188,8 @@ static void Java_InputStream_reset(JNIEnv* env, jobject obj) __attribute__ ((unused)); static void Java_InputStream_reset(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -194,9 +198,9 @@ static void Java_InputStream_reset(JNIEnv* env, jobject obj) { "()V", &g_InputStream_reset); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } @@ -205,7 +209,8 @@ static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) __attribute__ ((unused)); static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, obj, + g_InputStream_clazz, 0); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -215,18 +220,20 @@ static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) { &g_InputStream_skip); jlong ret = - env->CallLongMethod(obj, - method_id, p0); - base::android::CheckException(env); + env->CallLongMethod(obj, + method_id, p0); + jni_generator::CheckException(env); return ret; } static base::subtle::AtomicWord g_InputStream_Constructor = 0; -static ScopedJavaLocalRef<jobject> Java_InputStream_Constructor(JNIEnv* env) - __attribute__ ((unused)); -static ScopedJavaLocalRef<jobject> Java_InputStream_Constructor(JNIEnv* env) { +static base::android::ScopedJavaLocalRef<jobject> + Java_InputStream_Constructor(JNIEnv* env) __attribute__ ((unused)); +static base::android::ScopedJavaLocalRef<jobject> + Java_InputStream_Constructor(JNIEnv* env) { /* Must call RegisterNativesImpl() */ - DCHECK(g_InputStream_clazz); + CHECK_CLAZZ(env, g_InputStream_clazz, + g_InputStream_clazz, NULL); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -236,20 +243,21 @@ static ScopedJavaLocalRef<jobject> Java_InputStream_Constructor(JNIEnv* env) { &g_InputStream_Constructor); jobject ret = - env->NewObject(g_InputStream_clazz, - method_id); - base::android::CheckException(env); - return ScopedJavaLocalRef<jobject>(env, ret); + env->NewObject(g_InputStream_clazz, + method_id); + jni_generator::CheckException(env); + return base::android::ScopedJavaLocalRef<jobject>(env, ret); } // Step 3: RegisterNatives. static bool RegisterNativesImpl(JNIEnv* env) { - g_InputStream_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetClass(env, kInputStreamClassPath).obj())); + return true; } + } // namespace JNI_InputStream #endif // java_io_InputStream_JNI diff --git a/base/android/jni_generator/testFromJavaPGenerics.golden b/base/android/jni_generator/testFromJavaPGenerics.golden index 6e084fd..1ac340e 100644 --- a/base/android/jni_generator/testFromJavaPGenerics.golden +++ b/base/android/jni_generator/testFromJavaPGenerics.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,18 +12,14 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { const char kHashSetClassPath[] = "java/util/HashSet"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_HashSet_clazz = NULL; + } // namespace namespace JNI_HashSet { @@ -35,7 +31,8 @@ static void Java_HashSet_dummy(JNIEnv* env, jobject obj) __attribute__ ((unused)); static void Java_HashSet_dummy(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ - DCHECK(g_HashSet_clazz); + CHECK_CLAZZ(env, obj, + g_HashSet_clazz); jmethodID method_id = base::android::MethodID::LazyGet< base::android::MethodID::TYPE_INSTANCE>( @@ -44,20 +41,21 @@ static void Java_HashSet_dummy(JNIEnv* env, jobject obj) { "()V", &g_HashSet_dummy); - env->CallVoidMethod(obj, - method_id); - base::android::CheckException(env); + env->CallVoidMethod(obj, + method_id); + jni_generator::CheckException(env); } // Step 3: RegisterNatives. static bool RegisterNativesImpl(JNIEnv* env) { - g_HashSet_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetClass(env, kHashSetClassPath).obj())); + return true; } + } // namespace JNI_HashSet #endif // java_util_HashSet_JNI diff --git a/base/android/jni_generator/testInnerClassNatives.golden b/base/android/jni_generator/testInnerClassNatives.golden index a655dc3..2bc2161 100644 --- a/base/android/jni_generator/testInnerClassNatives.golden +++ b/base/android/jni_generator/testInnerClassNatives.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,12 +12,7 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { @@ -25,30 +20,33 @@ const char kTestJniClassPath[] = "org/chromium/TestJni"; const char kMyInnerClassClassPath[] = "org/chromium/TestJni$MyInnerClass"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; + } // namespace -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); // Step 2: method stubs. // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { - - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kTestJniClassPath).obj())); - static const JNINativeMethod kMethodsMyInnerClass[] = { +static const JNINativeMethod kMethodsMyInnerClass[] = { { "nativeInit", "(" ")" "I", reinterpret_cast<void*>(Init) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestJniClassPath).obj())); + const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass); if (env->RegisterNatives(g_MyInnerClass_clazz, kMethodsMyInnerClass, kMethodsMyInnerClassSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_MyInnerClass_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testInnerClassNativesBothInnerAndOuter.golden b/base/android/jni_generator/testInnerClassNativesBothInnerAndOuter.golden index 45b963e..6b85ea9 100644 --- a/base/android/jni_generator/testInnerClassNativesBothInnerAndOuter.golden +++ b/base/android/jni_generator/testInnerClassNativesBothInnerAndOuter.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,12 +12,7 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { @@ -26,48 +21,53 @@ const char kMyOtherInnerClassClassPath[] = const char kTestJniClassPath[] = "org/chromium/TestJni"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; + } // namespace -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); // Step 2: method stubs. // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { +static const JNINativeMethod kMethodsMyOtherInnerClass[] = { + { "nativeInit", +"(" +")" +"I", reinterpret_cast<void*>(Init) }, +}; - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kTestJniClassPath).obj())); - static const JNINativeMethod kMethodsMyOtherInnerClass[] = { +static const JNINativeMethod kMethodsTestJni[] = { { "nativeInit", "(" ")" "I", reinterpret_cast<void*>(Init) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestJniClassPath).obj())); + const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass); if (env->RegisterNatives(g_MyOtherInnerClass_clazz, kMethodsMyOtherInnerClass, kMethodsMyOtherInnerClassSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_MyOtherInnerClass_clazz, __FILE__); return false; } - static const JNINativeMethod kMethodsTestJni[] = { - { "nativeInit", -"(" -")" -"I", reinterpret_cast<void*>(Init) }, - }; const int kMethodsTestJniSize = arraysize(kMethodsTestJni); if (env->RegisterNatives(g_TestJni_clazz, kMethodsTestJni, kMethodsTestJniSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_TestJni_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testInnerClassNativesMultiple.golden b/base/android/jni_generator/testInnerClassNativesMultiple.golden index 81c97e9..1341c89 100644 --- a/base/android/jni_generator/testInnerClassNativesMultiple.golden +++ b/base/android/jni_generator/testInnerClassNativesMultiple.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,12 +12,7 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { @@ -27,48 +22,53 @@ const char kTestJniClassPath[] = "org/chromium/TestJni"; const char kMyInnerClassClassPath[] = "org/chromium/TestJni$MyInnerClass"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; + } // namespace -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); // Step 2: method stubs. // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { +static const JNINativeMethod kMethodsMyOtherInnerClass[] = { + { "nativeInit", +"(" +")" +"I", reinterpret_cast<void*>(Init) }, +}; - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kTestJniClassPath).obj())); - static const JNINativeMethod kMethodsMyOtherInnerClass[] = { +static const JNINativeMethod kMethodsMyInnerClass[] = { { "nativeInit", "(" ")" "I", reinterpret_cast<void*>(Init) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestJniClassPath).obj())); + const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass); if (env->RegisterNatives(g_MyOtherInnerClass_clazz, kMethodsMyOtherInnerClass, kMethodsMyOtherInnerClassSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_MyOtherInnerClass_clazz, __FILE__); return false; } - static const JNINativeMethod kMethodsMyInnerClass[] = { - { "nativeInit", -"(" -")" -"I", reinterpret_cast<void*>(Init) }, - }; const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass); if (env->RegisterNatives(g_MyInnerClass_clazz, kMethodsMyInnerClass, kMethodsMyInnerClassSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_MyInnerClass_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testJNIInitNativeNameOption.golden b/base/android/jni_generator/testJNIInitNativeNameOption.golden new file mode 100644 index 0000000..4ede46a --- /dev/null +++ b/base/android/jni_generator/testJNIInitNativeNameOption.golden @@ -0,0 +1,67 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file is autogenerated by +// base/android/jni_generator/jni_generator.py +// For +// org/chromium/example/jni_generator/Test + +#ifndef org_chromium_example_jni_generator_Test_JNI +#define org_chromium_example_jni_generator_Test_JNI + +#include <jni.h> + +#include "base/android/jni_generator/jni_generator_helper.h" + +// Step 1: forward declarations. +namespace { +const char kTestClassPath[] = "org/chromium/example/jni_generator/Test"; +// Leaking this jclass as we cannot use LazyInstance from some threads. +jclass g_Test_clazz = NULL; + +} // namespace + +// Step 2: method stubs. +static jint Method(JNIEnv* env, jobject jcaller, + jint nativeTest, + jint arg1) { + Test* native = reinterpret_cast<Test*>(nativeTest); + CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); + return native->Method(env, jcaller, arg1); +} + +// Step 3: RegisterNatives. + +static const JNINativeMethod kMethodsTest[] = { + { "nativeMethod", +"(" +"I" +"I" +")" +"I", reinterpret_cast<void*>(Method) }, +}; + +static bool RegisterNativesImpl(JNIEnv* env, jclass clazz) { + g_Test_clazz = static_cast<jclass>(env->NewWeakGlobalRef(clazz)); + + const int kMethodsTestSize = arraysize(kMethodsTest); + + if (env->RegisterNatives(g_Test_clazz, + kMethodsTest, + kMethodsTestSize) < 0) { + jni_generator::HandleRegistrationError( + env, g_Test_clazz, __FILE__); + return false; + } + + return true; +} + +extern "C" JNIEXPORT bool JNICALL +Java_org_chromium_example_jni_generator_Test_initNativeClass(JNIEnv* env, jclass + clazz) { + return RegisterNativesImpl(env, clazz); +} + +#endif // org_chromium_example_jni_generator_Test_JNI diff --git a/base/android/jni_generator/testJarJarRemapping.golden b/base/android/jni_generator/testJarJarRemapping.golden index 4b65b65..26406e4 100644 --- a/base/android/jni_generator/testJarJarRemapping.golden +++ b/base/android/jni_generator/testJarJarRemapping.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,44 +12,42 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { const char kExampleClassPath[] = "com/test/jni_generator/Example"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_Example_clazz = NULL; + } // namespace -static void Test(JNIEnv* env, jclass clazz, +static void Test(JNIEnv* env, jclass jcaller, jobject t); // Step 2: method stubs. // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { - - g_Example_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kExampleClassPath).obj())); - static const JNINativeMethod kMethodsExample[] = { +static const JNINativeMethod kMethodsExample[] = { { "nativeTest", "(" "Lorg/test2/org/chromium/example2/Test;" ")" "V", reinterpret_cast<void*>(Test) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_Example_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kExampleClassPath).obj())); + const int kMethodsExampleSize = arraysize(kMethodsExample); if (env->RegisterNatives(g_Example_clazz, kMethodsExample, kMethodsExampleSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_Example_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testNatives.golden b/base/android/jni_generator/testNatives.golden index 2d42f27..de109e7 100644 --- a/base/android/jni_generator/testNatives.golden +++ b/base/android/jni_generator/testNatives.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,65 +12,61 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { const char kTestJniClassPath[] = "org/chromium/TestJni"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; + } // namespace -static jint Init(JNIEnv* env, jobject obj); +static jint Init(JNIEnv* env, jobject jcaller); -static jstring GetDomainAndRegistry(JNIEnv* env, jclass clazz, +static jstring GetDomainAndRegistry(JNIEnv* env, jclass jcaller, jstring url); -static void CreateHistoricalTabFromState(JNIEnv* env, jclass clazz, +static void CreateHistoricalTabFromState(JNIEnv* env, jclass jcaller, jbyteArray state, jint tab_index); -static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject obj, +static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject jcaller, jobject view); -static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass clazz); +static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass jcaller); -static void SetRecognitionResults(JNIEnv* env, jobject obj, +static void SetRecognitionResults(JNIEnv* env, jobject jcaller, jint sessionId, jobjectArray results); -static jint FindAll(JNIEnv* env, jobject obj, +static jint FindAll(JNIEnv* env, jobject jcaller, jstring find); -static jobject GetInnerClass(JNIEnv* env, jclass clazz); +static jobject GetInnerClass(JNIEnv* env, jclass jcaller); // Step 2: method stubs. -static void Destroy(JNIEnv* env, jobject obj, +static void Destroy(JNIEnv* env, jobject jcaller, jint nativeChromeBrowserProvider) { - DCHECK(nativeChromeBrowserProvider) << "Destroy"; ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); - return native->Destroy(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "Destroy"); + return native->Destroy(env, jcaller); } -static jlong AddBookmark(JNIEnv* env, jobject obj, +static jlong AddBookmark(JNIEnv* env, jobject jcaller, jint nativeChromeBrowserProvider, jstring url, jstring title, jboolean isFolder, jlong parentId) { - DCHECK(nativeChromeBrowserProvider) << "AddBookmark"; ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); - return native->AddBookmark(env, obj, url, title, isFolder, parentId); + CHECK_NATIVE_PTR(env, jcaller, native, "AddBookmark", 0); + return native->AddBookmark(env, jcaller, url, title, isFolder, parentId); } -static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj, +static jlong AddBookmarkFromAPI(JNIEnv* env, jobject jcaller, jint nativeChromeBrowserProvider, jstring url, jobject created, @@ -79,44 +75,40 @@ static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj, jbyteArray favicon, jstring title, jobject visits) { - DCHECK(nativeChromeBrowserProvider) << "AddBookmarkFromAPI"; ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); - return native->AddBookmarkFromAPI(env, obj, url, created, isBookmark, date, - favicon, title, visits); + CHECK_NATIVE_PTR(env, jcaller, native, "AddBookmarkFromAPI", 0); + return native->AddBookmarkFromAPI(env, jcaller, url, created, isBookmark, + date, favicon, title, visits); } -static jobject QueryBitmap(JNIEnv* env, jobject obj, +static jobject QueryBitmap(JNIEnv* env, jobject jcaller, jint nativeChromeBrowserProvider, jobjectArray projection, jstring selection, jobjectArray selectionArgs, jstring sortOrder) { - DCHECK(nativeChromeBrowserProvider) << "QueryBitmap"; ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); - return native->QueryBitmap(env, obj, projection, selection, selectionArgs, + CHECK_NATIVE_PTR(env, jcaller, native, "QueryBitmap", NULL); + return native->QueryBitmap(env, jcaller, projection, selection, selectionArgs, sortOrder).Release(); } -static void GotOrientation(JNIEnv* env, jobject obj, +static void GotOrientation(JNIEnv* env, jobject jcaller, jint nativeDataFetcherImplAndroid, jdouble alpha, jdouble beta, jdouble gamma) { - DCHECK(nativeDataFetcherImplAndroid) << "GotOrientation"; DataFetcherImplAndroid* native = reinterpret_cast<DataFetcherImplAndroid*>(nativeDataFetcherImplAndroid); - return native->GotOrientation(env, obj, alpha, beta, gamma); + CHECK_NATIVE_PTR(env, jcaller, native, "GotOrientation"); + return native->GotOrientation(env, jcaller, alpha, beta, gamma); } // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { - - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kTestJniClassPath).obj())); - static const JNINativeMethod kMethodsTestJni[] = { +static const JNINativeMethod kMethodsTestJni[] = { { "nativeInit", "(" ")" @@ -200,13 +192,19 @@ static bool RegisterNativesImpl(JNIEnv* env) { "D" ")" "V", reinterpret_cast<void*>(GotOrientation) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestJniClassPath).obj())); + const int kMethodsTestJniSize = arraysize(kMethodsTestJni); if (env->RegisterNatives(g_TestJni_clazz, kMethodsTestJni, kMethodsTestJniSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_TestJni_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testNativesLong.golden b/base/android/jni_generator/testNativesLong.golden index 5a862f2..24d4a15 100644 --- a/base/android/jni_generator/testNativesLong.golden +++ b/base/android/jni_generator/testNativesLong.golden @@ -1,4 +1,4 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,48 +12,46 @@ #include <jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "base/basictypes.h" -#include "base/logging.h" - -using base::android::ScopedJavaLocalRef; +#include "base/android/jni_generator/jni_generator_helper.h" // Step 1: forward declarations. namespace { const char kTestJniClassPath[] = "org/chromium/TestJni"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; + } // namespace // Step 2: method stubs. -static void Destroy(JNIEnv* env, jobject obj, +static void Destroy(JNIEnv* env, jobject jcaller, jlong nativeChromeBrowserProvider) { - DCHECK(nativeChromeBrowserProvider) << "Destroy"; ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); - return native->Destroy(env, obj); + CHECK_NATIVE_PTR(env, jcaller, native, "Destroy"); + return native->Destroy(env, jcaller); } // Step 3: RegisterNatives. -static bool RegisterNativesImpl(JNIEnv* env) { - - g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( - base::android::GetClass(env, kTestJniClassPath).obj())); - static const JNINativeMethod kMethodsTestJni[] = { +static const JNINativeMethod kMethodsTestJni[] = { { "nativeDestroy", "(" "J" ")" "V", reinterpret_cast<void*>(Destroy) }, - }; +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestJniClassPath).obj())); + const int kMethodsTestJniSize = arraysize(kMethodsTestJni); if (env->RegisterNatives(g_TestJni_clazz, kMethodsTestJni, kMethodsTestJniSize) < 0) { - LOG(ERROR) << "RegisterNatives failed in " << __FILE__; + jni_generator::HandleRegistrationError( + env, g_TestJni_clazz, __FILE__); return false; } diff --git a/base/android/jni_generator/testPureNativeMethodsOption.golden b/base/android/jni_generator/testPureNativeMethodsOption.golden new file mode 100644 index 0000000..e876ab1 --- /dev/null +++ b/base/android/jni_generator/testPureNativeMethodsOption.golden @@ -0,0 +1,62 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file is autogenerated by +// base/android/jni_generator/jni_generator.py +// For +// org/chromium/example/jni_generator/Test + +#ifndef org_chromium_example_jni_generator_Test_JNI +#define org_chromium_example_jni_generator_Test_JNI + +#include <jni.h> + +#include "base/android/jni_generator/jni_generator_helper.h" + +// Step 1: forward declarations. +namespace { +const char kTestClassPath[] = "org/chromium/example/jni_generator/Test"; +// Leaking this jclass as we cannot use LazyInstance from some threads. +jclass g_Test_clazz = NULL; + +} // namespace + +// Step 2: method stubs. +static jint Method(JNIEnv* env, jobject jcaller, + jint nativeTest, + jint arg1) { + Test* native = reinterpret_cast<Test*>(nativeTest); + CHECK_NATIVE_PTR(env, jcaller, native, "Method", 0); + return native->Method(arg1); +} + +// Step 3: RegisterNatives. + +static const JNINativeMethod kMethodsTest[] = { + { "nativeMethod", +"(" +"I" +"I" +")" +"I", reinterpret_cast<void*>(Method) }, +}; + +static bool RegisterNativesImpl(JNIEnv* env) { + g_Test_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( + base::android::GetClass(env, kTestClassPath).obj())); + + const int kMethodsTestSize = arraysize(kMethodsTest); + + if (env->RegisterNatives(g_Test_clazz, + kMethodsTest, + kMethodsTestSize) < 0) { + jni_generator::HandleRegistrationError( + env, g_Test_clazz, __FILE__); + return false; + } + + return true; +} + +#endif // org_chromium_example_jni_generator_Test_JNI diff --git a/build/jar_file_jni_generator.gypi b/build/jar_file_jni_generator.gypi index 2e1b084..dc43c49 100644 --- a/build/jar_file_jni_generator.gypi +++ b/build/jar_file_jni_generator.gypi @@ -22,6 +22,10 @@ { 'variables': { 'jni_generator': '<(DEPTH)/base/android/jni_generator/jni_generator.py', + # A comma separated string of include files. + 'jni_generator_includes%': ( + 'base/android/jni_generator/jni_generator_helper.h' + ), }, 'actions': [ { @@ -46,6 +50,8 @@ '<(input_java_class)', '--output_dir', '<(SHARED_INTERMEDIATE_DIR)/<(jni_gen_package)/jni', + '--includes', + '<(jni_generator_includes)', '--optimize_generation', '<(optimize_jni_generation)', ], diff --git a/build/jni_generator.gypi b/build/jni_generator.gypi index 52df4b1..45b3884 100644 --- a/build/jni_generator.gypi +++ b/build/jni_generator.gypi @@ -32,6 +32,10 @@ 'jni_generator': '<(DEPTH)/base/android/jni_generator/jni_generator.py', 'jni_generator_jarjar_file%': '', 'jni_generator_ptr_type%': 'int', + # A comma separated string of include files. + 'jni_generator_includes%': ( + 'base/android/jni_generator/jni_generator_helper.h' + ), }, 'rules': [ { @@ -49,6 +53,8 @@ '<(RULE_INPUT_PATH)', '--output_dir', '<(SHARED_INTERMEDIATE_DIR)/<(jni_gen_package)/jni', + '--includes', + '<(jni_generator_includes)', '--optimize_generation', '<(optimize_jni_generation)', '--jarjar', |