├── OWNERS ├── samples ├── java │ └── src │ │ └── org │ │ └── jni_zero │ │ └── samples │ │ ├── .gitignore │ │ ├── JavapClass.class │ │ ├── TinySample2.java │ │ ├── TinySample.java │ │ ├── SampleModule.java │ │ ├── SampleUniqueAnnotations.java │ │ ├── JavapClass.java │ │ ├── SampleProxyEdgeCases.java │ │ ├── SampleBidirectionalNonProxy.java │ │ ├── SampleNonProxy.java │ │ └── SampleForAnnotationProcessor.java ├── AndroidManifest.xml ├── sample_entry_point.cc ├── BUILD.gn └── sample_for_tests.h ├── .style.yapf ├── DIR_METADATA ├── proguard_for_test.flags ├── DEPS ├── checkdiscard_proguard.flags ├── golden ├── testEndToEndProxyJniWithModules-Placeholder-GEN_JNI.java.golden ├── testUniqueAnnotations-Placeholder-GEN_JNI.java.golden ├── testEndToEndManualRegistration_NonProxy-Final-GEN_JNI.java.golden ├── testEndToEndProxyJniWithModules-Final-N.java.golden ├── testProxyMocks-Final-GEN_JNI.java.golden ├── testRequireProxyMocks-Final-GEN_JNI.java.golden ├── testEndToEndProxyJniWithModules-Final-GEN_JNI.java.golden ├── testFullStubs-Final-GEN_JNI.java.golden ├── testEndToEndProxyJniWithModules-Registration.h.golden ├── testEndToEndProxyJniWithModules-SampleModule_jni.h.golden ├── testForTestingKept-Placeholder-GEN_JNI.java.golden ├── testBidirectionalClass-Placeholder-GEN_JNI.java.golden ├── testEndToEndProxyJniWithModules-SampleModuleJni.java.golden ├── testUniqueAnnotations-SampleUniqueAnnotationsJni.java.golden ├── testForTestingKept-Final-N.java.golden ├── testForTestingRemoved-Final-N.java.golden ├── testPackagePrefixGenerator-Placeholder-GEN_JNI.java.golden ├── testForTestingKept-Final-GEN_JNI.java.golden ├── testForTestingRemoved-Final-GEN_JNI.java.golden ├── testMultiplexing-Final-N.java.golden ├── testBidirectionalClass-Final-GEN_JNI.java.golden ├── testForTestingKept-Registration.h.golden ├── testForTestingRemoved-Registration.h.golden ├── testEndToEndProxyHashed-Final-N.java.golden ├── testEndToEndProxyHashed-Registration.h.golden ├── testForTestingKept-SampleProxyEdgeCasesJni.java.golden ├── testEndToEndManualRegistration-Final-GEN_JNI.java.golden ├── testUniqueAnnotations-SampleUniqueAnnotations_jni.h.golden ├── testPackagePrefixWithProxyHash-Final-N.java.golden ├── testPackagePrefixWithManualRegistrationWithProxyHash-Final-N.java.golden ├── testEndToEndProxyHashed-Final-GEN_JNI.java.golden ├── testPackagePrefixWithProxyHash-Registration.h.golden ├── testPackagePrefixWithManualRegistration-Final-GEN_JNI.java.golden ├── testMultiplexing-Final-GEN_JNI.java.golden ├── testBidirectionalClass-SampleForTestsJni.java.golden ├── testPackagePrefixWithProxyHash-Final-GEN_JNI.java.golden ├── testPackagePrefixWithManualRegistrationWithProxyHash-Final-GEN_JNI.java.golden ├── testPackagePrefixGenerator-SampleForTestsJni.java.golden ├── testStubRegistration-Final-GEN_JNI.java.golden ├── testForTestingKept-SampleProxyEdgeCases_jni.h.golden ├── testPackagePrefixWithManualRegistrationWithProxyHash-Registration.h.golden ├── testEndToEndManualRegistration_NonProxy-Registration.h.golden ├── testEndToEndManualRegistration-Registration.h.golden └── testNonProxy-SampleNonProxy_jni.h.golden ├── README.chromium ├── proguard.flags ├── java └── src │ └── org │ └── jni_zero │ ├── AccessedByNative.java │ ├── JniStaticTestMocker.java │ ├── JNINamespace.java │ ├── NativeMethods.java │ ├── CheckDiscard.java │ ├── NativeClassQualifiedName.java │ ├── CalledByNativeForTesting.java │ ├── CalledByNative.java │ ├── CalledByNativeUnchecked.java │ └── NativeLibraryLoadedStatus.java ├── codegen ├── placeholder_gen_jni_java.py └── proxy_impl_java.py ├── jni_export.h ├── PRESUBMIT.py ├── common.py ├── LICENSE ├── core.h ├── jni_int_wrapper.h ├── proxy.py ├── BUILD.gn ├── logging.cc ├── scoped_java_ref.cc ├── java_lang_classes.py ├── core.cc ├── logging.h ├── jni_zero_helper.h └── jni_zero.py /OWNERS: -------------------------------------------------------------------------------- 1 | agrieve@chromium.org 2 | smaier@chromium.org 3 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/.gitignore: -------------------------------------------------------------------------------- 1 | !/*.class 2 | -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | based_on_style = pep8 3 | column_limit = 80 4 | indent_width = 2 5 | -------------------------------------------------------------------------------- /DIR_METADATA: -------------------------------------------------------------------------------- 1 | monorail { 2 | component: "Build" 3 | } 4 | team_email: "build@chromium.org" 5 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/JavapClass.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mthli/jni_zero/main/samples/java/src/org/jni_zero/samples/JavapClass.class -------------------------------------------------------------------------------- /proguard_for_test.flags: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | -keepclasseswithmembers,includedescriptorclasses,allowaccessmodification class ** { 6 | @org.jni_zero.CalledByNativeForTesting ; 7 | } 8 | -------------------------------------------------------------------------------- /DEPS: -------------------------------------------------------------------------------- 1 | # -*- Mode: Python; -*- 2 | 3 | include_rules = [ 4 | # This project has always depended on //base, but we are trying to break this 5 | # dependency. See crbug.com/1377351. 6 | '+base', 7 | # This project is a Chromium build project and may always require //build for 8 | # its GN rules. 9 | '+build', 10 | # Used when building without Chromium's base. 11 | '+absl', 12 | ] 13 | -------------------------------------------------------------------------------- /checkdiscard_proguard.flags: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | # Chromium only applies this check when ENABLE_DCHECK=false. 6 | 7 | -checkdiscard @org.jni_zero.CheckDiscard class ** { 8 | *; 9 | } 10 | -checkdiscard class ** { 11 | @org.jni_zero.CheckDiscard *; 12 | } 13 | -------------------------------------------------------------------------------- /samples/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/TinySample2.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.NativeMethods; 8 | 9 | class TinySample2 { 10 | @NativeMethods() 11 | interface Natives { 12 | void test(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/TinySample.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.NativeMethods; 8 | 9 | class TinySample { 10 | @NativeMethods() 11 | interface Natives { 12 | void foo(); 13 | boolean bar(int a); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-Placeholder-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file is a placeholder. It was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | 5 | package org.jni_zero; 6 | 7 | public class module_GEN_JNI { 8 | public static boolean TESTING_ENABLED; 9 | public static boolean REQUIRE_MOCK; 10 | 11 | public static native boolean org_jni_1zero_samples_SampleModule_bar(int a); 12 | public static native void org_jni_1zero_samples_SampleModule_foo(); 13 | } 14 | -------------------------------------------------------------------------------- /golden/testUniqueAnnotations-Placeholder-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file is a placeholder. It was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | 5 | package org.jni_zero; 6 | 7 | public class GEN_JNI { 8 | public static boolean TESTING_ENABLED; 9 | public static boolean REQUIRE_MOCK; 10 | 11 | public static native int org_jni_1zero_samples_SampleUniqueAnnotations_bar(int x, int y); 12 | public static native void org_jni_1zero_samples_SampleUniqueAnnotations_foo(long nativePtr); 13 | } 14 | -------------------------------------------------------------------------------- /README.chromium: -------------------------------------------------------------------------------- 1 | Name: JNI Zero 2 | Short Name: jni_zero 3 | URL: https://chromium.googlesource.com/chromium/src/+/master/third_party/jni_zero 4 | Version: 0 5 | License: BSD 6 | License File: LICENSE 7 | Security Critical: yes 8 | Shipped: yes 9 | 10 | Description: 11 | A Chromium-developed JNI middleware, that is designed to provide a zero cost 12 | (or better!) abstraction when used with R8. 13 | 14 | Local Modifications: 15 | This entire project is developed in //third_party/jni_zero to make it more 16 | easily usable outside of Chromium. 17 | -------------------------------------------------------------------------------- /golden/testEndToEndManualRegistration_NonProxy-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /proguard.flags: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | # Keeps for method level annotations. 6 | -keepclasseswithmembers,allowaccessmodification class ** { 7 | @org.jni_zero.AccessedByNative ; 8 | } 9 | -keepclasseswithmembers,includedescriptorclasses,allowaccessmodification class ** { 10 | @org.jni_zero.CalledByNative ; 11 | } 12 | -keepclasseswithmembers,includedescriptorclasses,allowaccessmodification class ** { 13 | @org.jni_zero.CalledByNativeUnchecked ; 14 | } 15 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleModule.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.NativeMethods; 8 | 9 | class SampleModule { 10 | void test() { 11 | if (SampleForAnnotationProcessorJni.get().bar(1)) { 12 | SampleForAnnotationProcessorJni.get().foo(); 13 | } 14 | } 15 | 16 | @NativeMethods("module") 17 | interface Natives { 18 | void foo(); 19 | boolean bar(int a); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class module_N { 12 | 13 | 14 | // Original name: org_jni_1zero_samples_SampleModule_bar 15 | public static native boolean MaOHChCD(int a); 16 | // Original name: org_jni_1zero_samples_SampleModule_foo 17 | public static native void MF1ZO_It(); 18 | } 19 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/AccessedByNative.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** Applied to fields that are accessed from native via JNI. Causes R8 to not rename them. */ 13 | @Target(ElementType.FIELD) 14 | @Retention(RetentionPolicy.CLASS) 15 | public @interface AccessedByNative { 16 | public String value() default ""; 17 | } 18 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/JniStaticTestMocker.java: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | /** 8 | * Implemented by the TEST_HOOKS field in JNI wrapper classes that are generated 9 | * by the JNI annotation processor. Used in tests for setting the mock 10 | * implementation of a {@link org.chromium.base.annotations.NativeMethods} interface. 11 | * @param The interface annotated with {@link org.chromium.base.annotations.NativeMethods} 12 | */ 13 | public interface JniStaticTestMocker { 14 | void setInstanceForTesting(T instance); 15 | } 16 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/JNINamespace.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * @JNINamespace is used by the JNI generator to create the necessary JNI bindings and expose this 14 | * method to native code using the specified namespace. 15 | */ 16 | @Target(ElementType.TYPE) 17 | @Retention(RetentionPolicy.RUNTIME) 18 | public @interface JNINamespace { 19 | public String value(); 20 | } 21 | -------------------------------------------------------------------------------- /golden/testProxyMocks-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = true; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | // Hashed name: Java_J_N_MJBQZphH 17 | public static native boolean org_jni_1zero_samples_TinySample_bar(int a); 18 | // Hashed name: Java_J_N_MvusOBfA 19 | public static native void org_jni_1zero_samples_TinySample_foo(); 20 | } 21 | -------------------------------------------------------------------------------- /golden/testRequireProxyMocks-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = true; 13 | public static final boolean REQUIRE_MOCK = true; 14 | 15 | 16 | // Hashed name: Java_J_N_MJBQZphH 17 | public static native boolean org_jni_1zero_samples_TinySample_bar(int a); 18 | // Hashed name: Java_J_N_MvusOBfA 19 | public static native void org_jni_1zero_samples_TinySample_foo(); 20 | } 21 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class module_GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static boolean org_jni_1zero_samples_SampleModule_bar(int a) { 17 | return J.module_N.MaOHChCD(a); 18 | } 19 | 20 | public static void org_jni_1zero_samples_SampleModule_foo() { 21 | J.module_N.MF1ZO_It(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleUniqueAnnotations.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | class SampleUniqueAnnotations { 8 | // Poorly spaced intentionally 9 | private void do_not_match(); 10 | 11 | @VisibleForTesting 12 | @NativeMethods 13 | @Generated("Test") 14 | interface Natives { 15 | @NativeClassQualifiedName("FooAndroid::BarDelegate") 16 | 17 | void foo(long nativePtr); 18 | int bar(int x, int y); 19 | } 20 | 21 | @CalledByNative 22 | static void useBar1(Bar1.Inner inner) {} 23 | 24 | @NativeClassQualifiedName("Foo::Bar") 25 | native void nativeCallWithQualifiedObject(long nativePtr); 26 | } 27 | -------------------------------------------------------------------------------- /golden/testFullStubs-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | 17 | public static boolean org_jni_1zero_samples_TinySample_bar(int a) { 18 | throw new RuntimeException("Stub - not implemented!"); 19 | } 20 | 21 | public static void org_jni_1zero_samples_TinySample_foo() { 22 | throw new RuntimeException("Stub - not implemented!"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/NativeMethods.java: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | package org.jni_zero; 5 | 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | @Target(ElementType.TYPE) 12 | @Retention(RetentionPolicy.SOURCE) 13 | public @interface NativeMethods { 14 | /** 15 | * Tells the build system to call a different GEN_JNI, prefixed by the value we put here. This 16 | * should only be used for feature modules where we need a different GEN_JNI. For example, if 17 | * you did @NativeMethods("dfmname"), this would call into dfmname_GEN_JNI.java. 18 | */ 19 | public String value() default ""; 20 | } 21 | -------------------------------------------------------------------------------- /codegen/placeholder_gen_jni_java.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | 6 | def Generate(jni_objs, *, gen_jni_class, script_name): 7 | sb = [] 8 | sb.append(f"""\ 9 | // 10 | // This file is a placeholder. It was generated by {script_name} 11 | // 12 | 13 | package {gen_jni_class.package_with_dots}; 14 | 15 | public class {gen_jni_class.name} {{ 16 | public static boolean TESTING_ENABLED; 17 | public static boolean REQUIRE_MOCK; 18 | """) 19 | 20 | for jni_obj in jni_objs: 21 | for native in jni_obj.proxy_natives: 22 | sig_params = native.proxy_params.to_java_declaration() 23 | sb.append(f""" 24 | public static native {native.proxy_return_type.to_java()} {native.proxy_name}\ 25 | ({sig_params});""") 26 | 27 | sb.append('\n}\n') 28 | return ''.join(sb) 29 | -------------------------------------------------------------------------------- /samples/sample_entry_point.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "base/android/jni_android.h" 6 | #include "base/android/jni_utils.h" 7 | #include "third_party/jni_zero/samples/jni_registration_generated.h" 8 | 9 | // This is called by the VM when the shared library is first loaded. 10 | JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 11 | // By default, all JNI methods are registered. However, since render processes 12 | // don't need very much Java code, we enable selective JNI registration on the 13 | // Java side and only register a subset of JNI methods. 14 | base::android::InitVM(vm); 15 | JNIEnv* env = base::android::AttachCurrentThread(); 16 | 17 | if (!RegisterNatives(env)) { 18 | return -1; 19 | } 20 | return JNI_VERSION_1_4; 21 | } 22 | -------------------------------------------------------------------------------- /jni_export.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "build/build_config.h" 6 | 7 | #ifndef JNI_ZERO_JNI_EXPORT_H_ 8 | #define JNI_ZERO_JNI_EXPORT_H_ 9 | 10 | #if defined(ARCH_CPU_X86) 11 | // Dalvik JIT generated code doesn't guarantee 16-byte stack alignment on 12 | // x86 - use force_align_arg_pointer to realign the stack at the JNI 13 | // boundary. crbug.com/655248 14 | #define JNI_BOUNDARY_EXPORT \ 15 | extern "C" __attribute__((visibility("default"), force_align_arg_pointer)) 16 | #else 17 | #define JNI_BOUNDARY_EXPORT extern "C" __attribute__((visibility("default"))) 18 | #endif 19 | 20 | #if defined(COMPONENT_BUILD) 21 | #define JNI_ZERO_COMPONENT_BUILD_EXPORT __attribute__((visibility("default"))) 22 | #else 23 | #define JNI_ZERO_COMPONENT_BUILD_EXPORT 24 | #endif 25 | 26 | #endif // JNI_ZERO_JNI_EXPORT_H_ 27 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/CheckDiscard.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * Causes build to assert that annotated classes / methods / fields are optimized away in release 14 | * builds (when using checkdiscard_proguard.flags). 15 | */ 16 | @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) 17 | @Retention(RetentionPolicy.CLASS) 18 | public @interface CheckDiscard { 19 | /** 20 | * Describes why the element should be discarded. 21 | * 22 | * @return reason for discarding (crbug links are preferred unless reason is trivial). 23 | */ 24 | String value(); 25 | } 26 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/NativeClassQualifiedName.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * @NativeClassQualifiedName is used by the JNI generator to create the necessary JNI bindings to 14 | * call into the specified native class name. 15 | */ 16 | @Target(ElementType.METHOD) 17 | @Retention(RetentionPolicy.RUNTIME) 18 | public @interface NativeClassQualifiedName { 19 | /* 20 | * Tells which native class the method is going to be bound to. 21 | * The first parameter of the annotated method must be an int nativePtr pointing to 22 | * an instance of this class. 23 | */ 24 | public String value(); 25 | } 26 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/CalledByNativeForTesting.java: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * Used by the JNI generator to create the necessary JNI bindings and expose this method to native 14 | * test-only code. 15 | * 16 | *

Any method annotated by this will be kept around for tests only. If you wish to call your 17 | * method from non-test code, see {@link CalledByNative} instead. 18 | */ 19 | @Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) 20 | @Retention(RetentionPolicy.CLASS) 21 | public @interface CalledByNativeForTesting { 22 | /* 23 | * If present, tells which inner class the method belongs to. 24 | */ 25 | public String value() default ""; 26 | } 27 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | 25 | // Step 2: Forward declarations (methods). 26 | 27 | JNI_BOUNDARY_EXPORT jboolean Java_J_module_1N_MaOHChCD( 28 | JNIEnv* env, 29 | jclass jcaller, 30 | jint a); 31 | JNI_BOUNDARY_EXPORT void Java_J_module_1N_MF1ZO_1It( 32 | JNIEnv* env, 33 | jclass jcaller); 34 | 35 | 36 | 37 | #endif // TEMP_DIR_HEADER_ 38 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/CalledByNative.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * Used by the JNI generator to create the necessary JNI bindings and expose this method to native 14 | * code. 15 | * 16 | *

Any uncaught Java exceptions will crash the current process. This is generally the desired 17 | * behavior, since most exceptions indicate an unexpected error. If your java method expects an 18 | * exception, we recommend refactoring to catch exceptions and indicate errors with special return 19 | * values instead. If this is not possible, see {@link CalledByNativeUnchecked} instead. 20 | */ 21 | @Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) 22 | @Retention(RetentionPolicy.CLASS) 23 | public @interface CalledByNative { 24 | /* 25 | * If present, tells which inner class the method belongs to. 26 | */ 27 | public String value() default ""; 28 | } 29 | -------------------------------------------------------------------------------- /PRESUBMIT.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | """Presubmit script for android buildbot. 5 | 6 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for 7 | details on the presubmit API built into depot_tools. 8 | """ 9 | 10 | 11 | def CommonChecks(input_api, output_api): 12 | base_android_jni_generator_dir = input_api.PresubmitLocalPath() 13 | 14 | env = dict(input_api.environ) 15 | env.update({ 16 | 'PYTHONPATH': base_android_jni_generator_dir, 17 | 'PYTHONDONTWRITEBYTECODE': '1', 18 | }) 19 | 20 | return input_api.RunTests( 21 | input_api.canned_checks.GetUnitTests( 22 | input_api, 23 | output_api, 24 | unit_tests=[ 25 | input_api.os_path.join(base_android_jni_generator_dir, 26 | 'integration_tests.py') 27 | ], 28 | env=env, 29 | )) 30 | 31 | 32 | def CheckChangeOnUpload(input_api, output_api): 33 | return CommonChecks(input_api, output_api) 34 | 35 | 36 | def CheckChangeOnCommit(input_api, output_api): 37 | return CommonChecks(input_api, output_api) 38 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-SampleModule_jni.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_generator.py 8 | // For 9 | // org/jni_zero/samples/SampleModule 10 | 11 | #ifndef org_jni_zero_samples_SampleModule_JNI 12 | #define org_jni_zero_samples_SampleModule_JNI 13 | 14 | #include 15 | 16 | #include "third_party/jni_zero/jni_export.h" 17 | #include "third_party/jni_zero/jni_zero_helper.h" 18 | 19 | 20 | // Step 1: Forward declarations. 21 | 22 | 23 | // Step 2: Constants (optional). 24 | 25 | 26 | // Step 3: Method stubs. 27 | static jboolean JNI_SampleModule_Bar(JNIEnv* env, jint a); 28 | 29 | JNI_BOUNDARY_EXPORT jboolean Java_J_module_1N_MaOHChCD( 30 | JNIEnv* env, 31 | jclass jcaller, 32 | jint a) { 33 | return JNI_SampleModule_Bar(env, a); 34 | } 35 | 36 | static void JNI_SampleModule_Foo(JNIEnv* env); 37 | 38 | JNI_BOUNDARY_EXPORT void Java_J_module_1N_MF1ZO_1It( 39 | JNIEnv* env, 40 | jclass jcaller) { 41 | return JNI_SampleModule_Foo(env); 42 | } 43 | 44 | 45 | #endif // org_jni_zero_samples_SampleModule_JNI 46 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/JavapClass.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | // To regenerate .class file: 8 | // javac java/src/org/jni_zero/samples/JavapClass.java 9 | public class JavapClass { 10 | public static final int CONST_INT = 3; 11 | public static boolean sBoolValue = true; 12 | public static final String CONST_STR = "VaLuE"; 13 | 14 | public final int ignoredField = 7; 15 | 16 | public JavapClass() {} 17 | public JavapClass(int a) {} 18 | public JavapClass(boolean b) {} 19 | 20 | private void ignore(int thing) {} 21 | int intMethod(String value) { 22 | return 0; 23 | } 24 | static int[][] staticIntMethod(String arg) { 25 | return null; 26 | } 27 | static int staticIntMethod(String arg1, JavapClass arg2) { 28 | return 0; 29 | } 30 | static void needsMangling(int a) {} 31 | static void needsMangling(String s) {} 32 | static void needsMangling(java.util.ArrayList x){} 33 | 34 | Class objTest(T thing, T2[] other) { 35 | return null; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /common.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | """Common logic needed by other modules.""" 5 | 6 | import contextlib 7 | import filecmp 8 | import os 9 | import shutil 10 | import tempfile 11 | import zipfile 12 | 13 | 14 | def capitalize(value): 15 | return value[0].upper() + value[1:] 16 | 17 | 18 | def escape_class_name(fully_qualified_class): 19 | """Returns an escaped string concatenating the Java package and class.""" 20 | escaped = fully_qualified_class.replace('_', '_1') 21 | return escaped.replace('/', '_').replace('$', '_00024') 22 | 23 | 24 | @contextlib.contextmanager 25 | def atomic_output(path, mode='w+b'): 26 | with tempfile.NamedTemporaryFile(mode, delete=False) as f: 27 | try: 28 | yield f 29 | finally: 30 | f.close() 31 | 32 | if not (os.path.exists(path) and filecmp.cmp(f.name, path)): 33 | shutil.move(f.name, path) 34 | if os.path.exists(f.name): 35 | os.unlink(f.name) 36 | 37 | 38 | def add_to_zip_hermetic(zip_file, zip_path, data=None): 39 | zipinfo = zipfile.ZipInfo(filename=zip_path) 40 | zipinfo.external_attr = 0o644 << 16 41 | zipinfo.date_time = (2001, 1, 1, 0, 0, 0) 42 | zip_file.writestr(zipinfo, data, zipfile.ZIP_STORED) 43 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/CalledByNativeUnchecked.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * Similar to {@link CalledByNative}, this also exposes JNI bindings to native code. The main 14 | * difference is this will not crash the browser process if the Java method throws an 15 | * exception. However, the C++ caller must handle and clear the exception before calling into 16 | * any other Java code, otherwise the next Java method call will crash (with the previous call's 17 | * exception, which leads to a very confusing debugging experience). 18 | * 19 | *

Usage of this annotation should be very rare; due to the complexity of correctly handling 20 | * exceptions in C++, prefer using {@link CalledByNative}. 21 | */ 22 | @Target(ElementType.METHOD) 23 | @Retention(RetentionPolicy.CLASS) 24 | public @interface CalledByNativeUnchecked { 25 | /* 26 | * If present, tells which inner class the method belongs to. 27 | */ 28 | public String value() default ""; 29 | } 30 | -------------------------------------------------------------------------------- /java/src/org/jni_zero/NativeLibraryLoadedStatus.java: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | import org.chromium.build.BuildConfig; 8 | 9 | /** 10 | * Exposes native library loading status. 11 | */ 12 | public class NativeLibraryLoadedStatus { 13 | /** 14 | * Interface for querying native method availability. 15 | */ 16 | public interface NativeLibraryLoadedStatusProvider { 17 | boolean areNativeMethodsReady(); 18 | } 19 | 20 | private static NativeLibraryLoadedStatusProvider sProvider; 21 | 22 | public static class NativeNotLoadedException extends RuntimeException { 23 | public NativeNotLoadedException(String s) { 24 | super(s); 25 | } 26 | } 27 | 28 | public static void checkLoaded() { 29 | if (sProvider == null) return; 30 | 31 | if (!sProvider.areNativeMethodsReady()) { 32 | throw new NativeNotLoadedException( 33 | "Native method called before the native library was ready."); 34 | } 35 | } 36 | 37 | public static void setProvider(NativeLibraryLoadedStatusProvider statusProvider) { 38 | sProvider = statusProvider; 39 | } 40 | 41 | public static NativeLibraryLoadedStatusProvider getProviderForTesting() { 42 | return sProvider; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /golden/testForTestingKept-Placeholder-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file is a placeholder. It was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | 5 | package org.jni_zero; 6 | 7 | public class GEN_JNI { 8 | public static boolean TESTING_ENABLED; 9 | public static boolean REQUIRE_MOCK; 10 | 11 | public static native int org_jni_1zero_samples_SampleProxyEdgeCases_addStructB(Object caller, Object b); 12 | public static native String[][] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1(int[] a, Object[][] b); 13 | public static native int[] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2(int[] a, Throwable[][] b); 14 | public static native void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest(); 15 | public static native void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting(); 16 | public static native void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests(); 17 | public static native void org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1(); 18 | public static native Object[] org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray(Object[] arg); 19 | public static native void org_jni_1zero_samples_SampleProxyEdgeCases_setBool(Object b, Object i); 20 | public static native boolean org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(Object sb); 21 | public static native int org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(int sb); 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2023 The Chromium Authors 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google LLC nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleProxyEdgeCases.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.samples.Boolean; 8 | 9 | import java.util.Map; 10 | 11 | @SomeAnnotation("that contains class Foo ") 12 | class SampleProxyEdgeCases { 13 | enum Integer {} 14 | @interface ShouldBeIgnored {} 15 | static class OtherInnerClass {} 16 | 17 | public @interface ShouldBeIgnored {} 18 | 19 | @NativeMethods 20 | interface Natives { 21 | void foo__weirdly__escaped_name1(); 22 | String[][] arrayTypes1(int[] a, Object[][] b); 23 | int[] arrayTypes2(int[] a, Throwable[][] b); 24 | void fooForTest(); 25 | void fooForTests(); 26 | void fooForTesting(); 27 | Map[] genericsWithNestedClassArray( 28 | Map[] arg); 29 | 30 | // Tests passing a nested class from another class in the same package. 31 | int addStructB(SampleForTests caller, SampleForTests.InnerStructB b); 32 | 33 | // Tests a java.lang class. 34 | public boolean setStringBuilder(StringBuilder sb); 35 | 36 | // Tests name collisions with java.lang classes. 37 | public void setBool(Boolean b, Integer i); 38 | 39 | // Test IntDef 40 | public @SomeClass.EnumType int setStringBuilder(@MyThing int sb); 41 | } 42 | 43 | // Non-proxy natives in same file. 44 | native void nativeInstanceMethod(long nativeInstance); 45 | static native void nativeStaticMethod(); 46 | } 47 | -------------------------------------------------------------------------------- /golden/testBidirectionalClass-Placeholder-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file is a placeholder. It was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | 5 | package org.jni_zero; 6 | 7 | public class GEN_JNI { 8 | public static boolean TESTING_ENABLED; 9 | public static boolean REQUIRE_MOCK; 10 | 11 | public static native void org_jni_1zero_samples_SampleForTests_addStructB(long nativeCPPClass, Object caller, Object b); 12 | public static native void org_jni_1zero_samples_SampleForTests_destroy(long nativeCPPClass, Object caller); 13 | public static native Class org_jni_1zero_samples_SampleForTests_getClass(Class arg0); 14 | public static native double org_jni_1zero_samples_SampleForTests_getDoubleFunction(Object caller); 15 | public static native float org_jni_1zero_samples_SampleForTests_getFloatFunction(); 16 | public static native Object org_jni_1zero_samples_SampleForTests_getNonPODDatatype(Object caller); 17 | public static native Throwable org_jni_1zero_samples_SampleForTests_getThrowable(Throwable arg0); 18 | public static native long org_jni_1zero_samples_SampleForTests_init(Object caller, String param); 19 | public static native void org_jni_1zero_samples_SampleForTests_iterateAndDoSomethingWithStructB(long nativeCPPClass, Object caller); 20 | public static native int org_jni_1zero_samples_SampleForTests_method(long nativeCPPClass, Object caller); 21 | public static native double org_jni_1zero_samples_SampleForTests_methodOtherP0(long nativePtr, Object caller); 22 | public static native String org_jni_1zero_samples_SampleForTests_returnAString(long nativeCPPClass, Object caller); 23 | public static native void org_jni_1zero_samples_SampleForTests_setNonPODDatatype(Object caller, Object rect); 24 | } 25 | -------------------------------------------------------------------------------- /samples/BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | import("//build/config/android/rules.gni") 6 | import("//testing/test.gni") 7 | import("//third_party/jni_zero/jni_zero.gni") 8 | 9 | generate_jni("jni_sample_header") { 10 | sources = [ 11 | "java/src/org/jni_zero/samples/SampleForAnnotationProcessor.java", 12 | "java/src/org/jni_zero/samples/SampleForTests.java", 13 | ] 14 | } 15 | 16 | android_library("jni_sample_java") { 17 | srcjar_deps = [ ":jni_sample_header" ] 18 | sources = [ 19 | "java/src/org/jni_zero/samples/SampleForAnnotationProcessor.java", 20 | "java/src/org/jni_zero/samples/SampleForTests.java", 21 | ] 22 | 23 | deps = [ 24 | "//build/android:build_java", 25 | "//third_party/jni_zero:jni_zero_java", 26 | ] 27 | } 28 | 29 | source_set("jni_sample_native_side") { 30 | deps = [ 31 | ":jni_sample_header", 32 | "//base", 33 | ] 34 | sources = [ 35 | "sample_for_tests.cc", 36 | "sample_for_tests.h", 37 | ] 38 | } 39 | 40 | generate_jni_registration("jni_registration") { 41 | java_targets = [ ":jni_sample_java" ] 42 | manual_jni_registration = true 43 | } 44 | 45 | shared_library("jni_sample_lib") { 46 | sources = [ "sample_entry_point.cc" ] 47 | 48 | deps = [ 49 | ":jni_registration", 50 | ":jni_sample_native_side", 51 | "//base", 52 | ] 53 | } 54 | 55 | android_apk("sample_jni_apk") { 56 | apk_name = "SampleJni" 57 | android_manifest = "AndroidManifest.xml" 58 | deps = [ ":jni_sample_java" ] 59 | shared_libraries = [ ":jni_sample_lib" ] 60 | } 61 | 62 | # Serves to test that generated bindings compile properly. 63 | group("jni_generator_tests") { 64 | deps = [ ":sample_jni_apk" ] 65 | } 66 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleBidirectionalNonProxy.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.samples.Boolean; 8 | 9 | class SampleBidirectionalNonProxy { 10 | /** The pointer to the native Test. */ 11 | public long nativeTest; 12 | private static native int nativeStaticMethod(long nativeTest, int arg1); 13 | private native int nativeMethod(long nativeTest, int arg1); 14 | @CalledByNative 15 | private void testMethodWithParam(int iParam) {} 16 | @CalledByNative 17 | private String testMethodWithParamAndReturn(int iParam) { 18 | return null; 19 | } 20 | @CalledByNative 21 | private static int testStaticMethodWithParam(int iParam) { 22 | return 0; 23 | } 24 | @CalledByNative 25 | private static double testMethodWithNoParam() { 26 | return 0; 27 | } 28 | @CalledByNative 29 | private static String testStaticMethodWithNoParam() {} 30 | 31 | // Tests passing a nested class from another class in the same package. 32 | @CalledByNative 33 | void addStructB(SampleForTests caller, SampleForTests.InnerStructB b) {} 34 | 35 | // Tests a java.lang class. 36 | @CalledByNative 37 | void setStringBuilder(StringBuilder sb) {} 38 | 39 | // Tests name collisions with java.lang classes. 40 | @CalledByNative 41 | void setBool(Boolean b, Integer i) {} 42 | 43 | class MyInnerClass { 44 | @NativeCall("MyInnerClass") 45 | private native int nativeInit(); 46 | } 47 | class MyOtherInnerClass { 48 | @NativeCall("MyOtherInnerClass") 49 | private native int nativeInit(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyJniWithModules-SampleModuleJni.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | package org.jni_zero.samples; 5 | 6 | import org.jni_zero.CheckDiscard; 7 | import org.jni_zero.JniStaticTestMocker; 8 | import org.jni_zero.NativeLibraryLoadedStatus; 9 | import org.jni_zero.module_GEN_JNI; 10 | import org.jni_zero.NativeMethods; 11 | 12 | @CheckDiscard("crbug.com/993421") 13 | class SampleModuleJni implements SampleModule.Natives { 14 | private static SampleModule.Natives testInstance; 15 | 16 | public static final JniStaticTestMocker TEST_HOOKS = 17 | new JniStaticTestMocker() { 18 | @Override 19 | public void setInstanceForTesting(SampleModule.Natives instance) { 20 | if (!module_GEN_JNI.TESTING_ENABLED) { 21 | throw new RuntimeException( 22 | "Tried to set a JNI mock when mocks aren't enabled!"); 23 | } 24 | testInstance = instance; 25 | } 26 | }; 27 | 28 | @Override 29 | public boolean bar(int a) { 30 | return (boolean) module_GEN_JNI.org_jni_1zero_samples_SampleModule_bar(a); 31 | } 32 | 33 | @Override 34 | public void foo() { 35 | module_GEN_JNI.org_jni_1zero_samples_SampleModule_foo(); 36 | } 37 | 38 | public static SampleModule.Natives get() { 39 | if (module_GEN_JNI.TESTING_ENABLED) { 40 | if (testInstance != null) { 41 | return testInstance; 42 | } 43 | if (module_GEN_JNI.REQUIRE_MOCK) { 44 | throw new UnsupportedOperationException( 45 | "No mock found for the native implementation of SampleModule.Natives. " 46 | + "The current configuration requires implementations be mocked."); 47 | } 48 | } 49 | NativeLibraryLoadedStatus.checkLoaded(); 50 | return new SampleModuleJni(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleNonProxy.java: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import android.graphics.Bitmap; 8 | import android.view.View; 9 | 10 | public class SampleNonProxy { 11 | interface OnFrameAvailableListener {} 12 | private native int nativeInit(); 13 | private native void nativeDestroy(int nativeChromeBrowserProvider); 14 | private native long nativeAddBookmark(int nativeChromeBrowserProvider, String url, String title, 15 | boolean isFolder, long parentId); 16 | private static native String nativeGetDomainAndRegistry(String url); 17 | private static native void nativeCreateHistoricalTabFromState(byte[] state, int tabIndex); 18 | private native byte[] nativeGetStateAsByteArray(View view); 19 | private static native String[] nativeGetAutofillProfileGUIDs(); 20 | private native void nativeSetRecognitionResults(int sessionId, String[] results); 21 | private native long nativeAddBookmarkFromAPI(int nativeChromeBrowserProvider, String url, 22 | Long created, Boolean isBookmark, Long date, byte[] favicon, String title, 23 | Integer visits); 24 | native int nativeFindAll(String find); 25 | private static native OnFrameAvailableListener nativeGetInnerClass(); 26 | private native Bitmap nativeQueryBitmap(int nativeChromeBrowserProvider, String[] projection, 27 | String selection, String[] selectionArgs, String sortOrder); 28 | private native void nativeGotOrientation( 29 | int nativeDataFetcherImplAndroid, double alpha, double beta, double gamma); 30 | private static native Throwable nativeMessWithJavaException(Throwable e); 31 | class MyInnerClass { 32 | private native int nativeInit(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /golden/testUniqueAnnotations-SampleUniqueAnnotationsJni.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | package org.jni_zero.samples; 5 | 6 | import org.jni_zero.CheckDiscard; 7 | import org.jni_zero.JniStaticTestMocker; 8 | import org.jni_zero.NativeLibraryLoadedStatus; 9 | import org.jni_zero.GEN_JNI; 10 | 11 | @CheckDiscard("crbug.com/993421") 12 | class SampleUniqueAnnotationsJni implements SampleUniqueAnnotations.Natives { 13 | private static SampleUniqueAnnotations.Natives testInstance; 14 | 15 | public static final JniStaticTestMocker TEST_HOOKS = 16 | new JniStaticTestMocker() { 17 | @Override 18 | public void setInstanceForTesting(SampleUniqueAnnotations.Natives instance) { 19 | if (!GEN_JNI.TESTING_ENABLED) { 20 | throw new RuntimeException( 21 | "Tried to set a JNI mock when mocks aren't enabled!"); 22 | } 23 | testInstance = instance; 24 | } 25 | }; 26 | 27 | @Override 28 | public int bar(int x, int y) { 29 | return (int) GEN_JNI.org_jni_1zero_samples_SampleUniqueAnnotations_bar(x, y); 30 | } 31 | 32 | @Override 33 | public void foo(long nativePtr) { 34 | GEN_JNI.org_jni_1zero_samples_SampleUniqueAnnotations_foo(nativePtr); 35 | } 36 | 37 | public static SampleUniqueAnnotations.Natives get() { 38 | if (GEN_JNI.TESTING_ENABLED) { 39 | if (testInstance != null) { 40 | return testInstance; 41 | } 42 | if (GEN_JNI.REQUIRE_MOCK) { 43 | throw new UnsupportedOperationException( 44 | "No mock found for the native implementation of SampleUniqueAnnotations.Natives. " 45 | + "The current configuration requires implementations be mocked."); 46 | } 47 | } 48 | NativeLibraryLoadedStatus.checkLoaded(); 49 | return new SampleUniqueAnnotationsJni(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /golden/testForTestingKept-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_addStructB 15 | public static native int MyLYIXSG(Object caller, Object b); 16 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1 17 | public static native String[][] M4tCs4sR(int[] a, Object[][] b); 18 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2 19 | public static native int[] Mfw$B7iJ(int[] a, Throwable[][] b); 20 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest 21 | public static native void M01DKxMA_ForTesting(); 22 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting 23 | public static native void MdPg5w5a_ForTesting(); 24 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests 25 | public static native void Mfc_XrOy_ForTesting(); 26 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1 27 | public static native void MDx0XFh$(); 28 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray 29 | public static native Object[] MPuD3r24(Object[] arg); 30 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setBool 31 | public static native void MYc9Cume(Object b, Object i); 32 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder 33 | public static native boolean MoYVNDqu(Object sb); 34 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder 35 | public static native int MoYVNDqu(int sb); 36 | } 37 | -------------------------------------------------------------------------------- /golden/testForTestingRemoved-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_addStructB 15 | public static native int MyLYIXSG(Object caller, Object b); 16 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1 17 | public static native String[][] M4tCs4sR(int[] a, Object[][] b); 18 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2 19 | public static native int[] Mfw$B7iJ(int[] a, Throwable[][] b); 20 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest 21 | public static native void M01DKxMA_ForTesting(); 22 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting 23 | public static native void MdPg5w5a_ForTesting(); 24 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests 25 | public static native void Mfc_XrOy_ForTesting(); 26 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1 27 | public static native void MDx0XFh$(); 28 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray 29 | public static native Object[] MPuD3r24(Object[] arg); 30 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setBool 31 | public static native void MYc9Cume(Object b, Object i); 32 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder 33 | public static native boolean MoYVNDqu(Object sb); 34 | // Original name: org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder 35 | public static native int MoYVNDqu(int sb); 36 | } 37 | -------------------------------------------------------------------------------- /golden/testPackagePrefixGenerator-Placeholder-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file is a placeholder. It was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | 5 | package this.is.a.package.prefix.org.jni_zero; 6 | 7 | public class GEN_JNI { 8 | public static boolean TESTING_ENABLED; 9 | public static boolean REQUIRE_MOCK; 10 | 11 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_addStructB(long nativeCPPClass, Object caller, Object b); 12 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_destroy(long nativeCPPClass, Object caller); 13 | public static native Class this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getClass(Class arg0); 14 | public static native double this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getDoubleFunction(Object caller); 15 | public static native float this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getFloatFunction(); 16 | public static native Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getNonPODDatatype(Object caller); 17 | public static native Throwable this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getThrowable(Throwable arg0); 18 | public static native long this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_init(Object caller, String param); 19 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_iterateAndDoSomethingWithStructB(long nativeCPPClass, Object caller); 20 | public static native int this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_method(long nativeCPPClass, Object caller); 21 | public static native double this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_methodOtherP0(long nativePtr, Object caller); 22 | public static native String this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_returnAString(long nativeCPPClass, Object caller); 23 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_setNonPODDatatype(Object caller, Object rect); 24 | } 25 | -------------------------------------------------------------------------------- /core.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef JNI_ZERO_CORE_H_ 6 | #define JNI_ZERO_CORE_H_ 7 | 8 | #include 9 | 10 | #include 11 | 12 | #include "third_party/jni_zero/jni_export.h" 13 | 14 | namespace jni_zero { 15 | // Attaches the current thread to the VM (if necessary) and return the JNIEnv*. 16 | JNI_ZERO_COMPONENT_BUILD_EXPORT JNIEnv* AttachCurrentThread(); 17 | 18 | // Same to AttachCurrentThread except that thread name will be set to 19 | // |thread_name| if it is the first call. Otherwise, thread_name won't be 20 | // changed. AttachCurrentThread() doesn't regard underlying platform thread 21 | // name, but just resets it to "Thread-???". This function should be called 22 | // right after new thread is created if it is important to keep thread name. 23 | JNI_ZERO_COMPONENT_BUILD_EXPORT JNIEnv* AttachCurrentThreadWithName( 24 | const std::string& thread_name); 25 | 26 | // Detaches the current thread from VM if it is attached. 27 | JNI_ZERO_COMPONENT_BUILD_EXPORT void DetachFromVM(); 28 | 29 | // Initializes the global JVM. 30 | JNI_ZERO_COMPONENT_BUILD_EXPORT void InitVM(JavaVM* vm); 31 | 32 | // Returns true if the global JVM has been initialized. 33 | JNI_ZERO_COMPONENT_BUILD_EXPORT bool IsVMInitialized(); 34 | 35 | // Returns the global JVM, or nullptr if it has not been initialized. 36 | JNI_ZERO_COMPONENT_BUILD_EXPORT JavaVM* GetVM(); 37 | 38 | // Do not allow any future native->java calls. 39 | // This is necessary in gtest DEATH_TESTS to prevent 40 | // GetJavaStackTraceIfPresent() from accessing a defunct JVM (due to fork()). 41 | // https://crbug.com/1484834 42 | JNI_ZERO_COMPONENT_BUILD_EXPORT void DisableJvmForTesting(); 43 | 44 | JNI_ZERO_COMPONENT_BUILD_EXPORT void SetExceptionHandler( 45 | void (*callback)(JNIEnv*)); 46 | 47 | // If there's any pending exception, this function will call the set exception 48 | // handler, or if none are set, it will fatally LOG. 49 | JNI_ZERO_COMPONENT_BUILD_EXPORT void CheckException(JNIEnv* env); 50 | 51 | } // namespace jni_zero 52 | 53 | #endif // JNI_ZERO_CORE_H_ 54 | -------------------------------------------------------------------------------- /jni_int_wrapper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef JNI_ZERO_JNI_INT_WRAPPER_H_ 6 | #define JNI_ZERO_JNI_INT_WRAPPER_H_ 7 | 8 | // Wrapper used to receive int when calling Java from native. 9 | // The wrapper disallows automatic conversion of long to int. 10 | // This is to avoid a common anti-pattern where a Java int is used 11 | // to receive a native pointer. Please use a Java long to receive 12 | // native pointers, so that the code works on both 32-bit and 64-bit 13 | // platforms. Note the wrapper allows other lossy conversions into 14 | // jint that could be consider anti-patterns, such as from size_t. 15 | 16 | // Checking is only done in debugging builds. 17 | 18 | #ifdef NDEBUG 19 | 20 | typedef jint JniIntWrapper; 21 | 22 | // This inline is sufficiently trivial that it does not change the 23 | // final code generated by g++. 24 | inline jint as_jint(JniIntWrapper wrapper) { 25 | return wrapper; 26 | } 27 | 28 | #else 29 | 30 | class JniIntWrapper { 31 | public: 32 | JniIntWrapper() : i_(0) {} 33 | JniIntWrapper(int i) : i_(i) {} 34 | JniIntWrapper(const JniIntWrapper& ji) : i_(ji.i_) {} 35 | template 36 | JniIntWrapper(const T& t) : i_(t) {} 37 | jint as_jint() const { return i_; } 38 | 39 | private: 40 | // If you get an "is private" error at the line below it is because you used 41 | // an implicit conversion to convert a long to an int when calling Java. 42 | // We disallow this, as a common anti-pattern allows converting a native 43 | // pointer (intptr_t) to a Java int. Please use a Java long to represent 44 | // a native pointer. If you want a lossy conversion, please use an 45 | // explicit conversion in your C++ code. Note an error is only seen when 46 | // compiling on a 64-bit platform, as intptr_t is indistinguishable from 47 | // int on 32-bit platforms. 48 | JniIntWrapper(long); 49 | jint i_; 50 | }; 51 | 52 | inline jint as_jint(const JniIntWrapper& wrapper) { 53 | return wrapper.as_jint(); 54 | } 55 | 56 | #endif // NDEBUG 57 | 58 | #endif // JNI_ZERO_JNI_INT_WRAPPER_H_ 59 | -------------------------------------------------------------------------------- /golden/testForTestingKept-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_addStructB(Object caller, Object b) { 17 | return J.N.MyLYIXSG(caller, b); 18 | } 19 | 20 | public static String[][] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1(int[] a, Object[][] b) { 21 | return J.N.M4tCs4sR(a, b); 22 | } 23 | 24 | public static int[] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2(int[] a, Throwable[][] b) { 25 | return J.N.Mfw$B7iJ(a, b); 26 | } 27 | 28 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest() { 29 | J.N.M01DKxMA_ForTesting(); 30 | } 31 | 32 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting() { 33 | J.N.MdPg5w5a_ForTesting(); 34 | } 35 | 36 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests() { 37 | J.N.Mfc_XrOy_ForTesting(); 38 | } 39 | 40 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1() { 41 | J.N.MDx0XFh$(); 42 | } 43 | 44 | public static Object[] org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray(Object[] arg) { 45 | return J.N.MPuD3r24(arg); 46 | } 47 | 48 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_setBool(Object b, Object i) { 49 | J.N.MYc9Cume(b, i); 50 | } 51 | 52 | public static boolean org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(Object sb) { 53 | return J.N.MoYVNDqu(sb); 54 | } 55 | 56 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(int sb) { 57 | return J.N.MoYVNDqu(sb); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /golden/testForTestingRemoved-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_addStructB(Object caller, Object b) { 17 | return J.N.MyLYIXSG(caller, b); 18 | } 19 | 20 | public static String[][] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1(int[] a, Object[][] b) { 21 | return J.N.M4tCs4sR(a, b); 22 | } 23 | 24 | public static int[] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2(int[] a, Throwable[][] b) { 25 | return J.N.Mfw$B7iJ(a, b); 26 | } 27 | 28 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest() { 29 | J.N.M01DKxMA_ForTesting(); 30 | } 31 | 32 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting() { 33 | J.N.MdPg5w5a_ForTesting(); 34 | } 35 | 36 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests() { 37 | J.N.Mfc_XrOy_ForTesting(); 38 | } 39 | 40 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1() { 41 | J.N.MDx0XFh$(); 42 | } 43 | 44 | public static Object[] org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray(Object[] arg) { 45 | return J.N.MPuD3r24(arg); 46 | } 47 | 48 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_setBool(Object b, Object i) { 49 | J.N.MYc9Cume(b, i); 50 | } 51 | 52 | public static boolean org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(Object sb) { 53 | return J.N.MoYVNDqu(sb); 54 | } 55 | 56 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(int sb) { 57 | return J.N.MoYVNDqu(sb); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /golden/testMultiplexing-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | public static native Class resolve_for_class(long switch_num); 15 | public static native Class[] resolve_for_class_array(long switch_num); 16 | public static native Object resolve_for_object(long switch_num); 17 | public static native Object resolve_for_object_O(long switch_num, Object object_param1); 18 | public static native Object[] resolve_for_object_array(long switch_num); 19 | public static native Object[] resolve_for_object_array_OA(long switch_num, Object[] object_array_param1); 20 | public static native String resolve_for_string(long switch_num); 21 | public static native String resolve_for_string_R(long switch_num, String string_param1); 22 | public static native String[] resolve_for_string_array(long switch_num); 23 | public static native String[] resolve_for_string_array_RA(long switch_num, String[] string_array_param1); 24 | public static native Throwable resolve_for_throwable(long switch_num); 25 | public static native Throwable[] resolve_for_throwable_array(long switch_num); 26 | public static native boolean resolve_for_boolean(long switch_num); 27 | public static native int[] resolve_for_int_array_IIAJJASSACCABBADDAFFAZZA(long switch_num, int int_param1, int[] int_array_param1, long long_param1, long[] long_array_param1, short short_param1, short[] short_array_param1, char char_param1, char[] char_array_param1, byte byte_param1, byte[] byte_array_param1, double double_param1, double[] double_array_param1, float float_param1, float[] float_array_param1, boolean boolean_param1, boolean[] boolean_array_param1); 28 | public static native void resolve_for_void(long switch_num); 29 | public static native void resolve_for_void_LLATTARRAOOAOOA(long switch_num, Class class_param1, Class[] class_array_param1, Throwable throwable_param1, Throwable[] throwable_array_param1, String string_param1, String[] string_array_param1, Object object_param1, Object[] object_array_param1, Object object_param2, Object[] object_array_param2); 30 | } 31 | -------------------------------------------------------------------------------- /proxy.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | """Logic related to proxying calls through GEN_JNI.java.""" 5 | 6 | import base64 7 | import hashlib 8 | 9 | import common 10 | import java_types 11 | 12 | # 'Proxy' native methods are declared in an @NativeMethods interface without 13 | # a native qualifier and indicate that the JNI annotation processor should 14 | # generate code to link between the equivalent native method as if it were 15 | # declared statically. 16 | 17 | _MAX_CHARS_FOR_HASHED_NATIVE_METHODS = 8 18 | 19 | 20 | def get_gen_jni_class(*, short=False, name_prefix=None, package_prefix=None): 21 | """Returns the JavaClass for GEN_JNI.""" 22 | package = 'J' if short else 'org/jni_zero' 23 | name_prefix = name_prefix + '_' if name_prefix else '' 24 | name = name_prefix + ('N' if short else 'GEN_JNI') 25 | 26 | return java_types.JavaClass(f'{package}/{name}').make_prefixed(package_prefix) 27 | 28 | 29 | def _create_hashed_method_name(non_hashed_name, is_test_only): 30 | md5 = hashlib.md5(non_hashed_name.encode('utf8')).digest() 31 | hash_b64 = base64.b64encode(md5, altchars=b'$_').decode('utf-8') 32 | 33 | long_hash = ('M' + hash_b64).rstrip('=') 34 | hashed_name = long_hash[:_MAX_CHARS_FOR_HASHED_NATIVE_METHODS] 35 | 36 | # If the method is a test-only method, we don't care about saving size on 37 | # the method name, since it shouldn't show up in the binary. Additionally, 38 | # if we just hash the name, our checkers which enforce that we have no 39 | # "ForTesting" methods by checking for the suffix "ForTesting" will miss 40 | # these. We could preserve the name entirely and not hash anything, but 41 | # that risks collisions. So, instead, we just append "ForTesting" to any 42 | # test-only hashes, to ensure we catch any test-only methods that 43 | # shouldn't be in our final binary. 44 | if is_test_only: 45 | return hashed_name + '_ForTesting' 46 | return hashed_name 47 | 48 | 49 | def create_method_names(java_class, method_name, is_test_only): 50 | """Returns the method name used in GEN_JNI (both hashed an non-hashed).""" 51 | proxy_name = common.escape_class_name( 52 | f'{java_class.full_name_with_slashes}/{method_name}') 53 | hashed_proxy_name = _create_hashed_method_name(proxy_name, is_test_only) 54 | return proxy_name, hashed_proxy_name 55 | -------------------------------------------------------------------------------- /BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | import("//third_party/jni_zero/jni_zero.gni") 6 | 7 | config("jni_include_dir") { 8 | include_dirs = [ jni_headers_dir ] 9 | } 10 | 11 | config("use_chromium_base_define") { 12 | defines = [ "USE_CHROMIUM_BASE" ] 13 | } 14 | 15 | config("toolchain_define") { 16 | if (is_robolectric) { 17 | defines = [ "JNI_ZERO_IS_ROBOLECTRIC" ] 18 | } 19 | } 20 | 21 | source_set("jni_export") { 22 | sources = [ "jni_export.h" ] 23 | } 24 | 25 | # Base depends on this transitively, so we have to avoid deps onto base. 26 | # Eventually, it will make sense to merge this with :jni_zero once it also no 27 | # longer depends on //base. 28 | component("jni_zero_utils") { 29 | sources = [ 30 | "core.cc", 31 | "core.h", 32 | "logging.cc", 33 | "logging.h", 34 | "scoped_java_ref.cc", 35 | "scoped_java_ref.h", 36 | ] 37 | public_deps = [ ":jni_export" ] 38 | configs += [ ":toolchain_define" ] 39 | if (is_android) { 40 | libs = [ "log" ] 41 | } 42 | if (is_robolectric) { 43 | public_configs = [ "//third_party/jdk:jdk" ] 44 | } 45 | } 46 | 47 | source_set("jni_zero") { 48 | sources = [ 49 | "jni_int_wrapper.h", 50 | "jni_zero_helper.h", 51 | ] 52 | 53 | public_deps = [ 54 | ":jni_export", 55 | ":jni_zero_utils", 56 | "//base", 57 | ] 58 | 59 | public_configs = [ ":use_chromium_base_define" ] 60 | } 61 | 62 | if (enable_java_templates && is_android) { 63 | import("//build/config/android/rules.gni") 64 | 65 | java_library("jni_zero_java") { 66 | supports_android = true 67 | deps = [ "//build/android:build_java" ] 68 | sources = [ 69 | "java/src/org/jni_zero/AccessedByNative.java", 70 | "java/src/org/jni_zero/CalledByNative.java", 71 | "java/src/org/jni_zero/CalledByNativeForTesting.java", 72 | "java/src/org/jni_zero/CalledByNativeUnchecked.java", 73 | "java/src/org/jni_zero/CheckDiscard.java", 74 | "java/src/org/jni_zero/JNINamespace.java", 75 | "java/src/org/jni_zero/JniStaticTestMocker.java", 76 | "java/src/org/jni_zero/NativeClassQualifiedName.java", 77 | "java/src/org/jni_zero/NativeLibraryLoadedStatus.java", 78 | "java/src/org/jni_zero/NativeMethods.java", 79 | ] 80 | proguard_configs = [ "proguard.flags" ] 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /logging.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "third_party/jni_zero/logging.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #ifndef JNI_ZERO_IS_ROBOLECTRIC 12 | #include 13 | #endif 14 | 15 | namespace jni_zero { 16 | 17 | std::atomic g_log_callback{}; 18 | 19 | void SetLogMessageCallback(LogMessageCallback callback) { 20 | g_log_callback.store(callback, std::memory_order_relaxed); 21 | } 22 | 23 | void LogMessage(LogLev level, 24 | const char* fname, 25 | int line, 26 | const char* fmt, 27 | ...) { 28 | char stack_buf[512]; 29 | std::unique_ptr large_buf; 30 | char* log_msg = &stack_buf[0]; 31 | 32 | // By default use a stack allocated buffer because most log messages are quite 33 | // short. In rare cases they can be larger (e.g. --help). In those cases we 34 | // pay the cost of allocating the buffer on the heap. 35 | for (size_t max_len = sizeof(stack_buf);;) { 36 | va_list args; 37 | va_start(args, fmt); 38 | int res = vsnprintf(log_msg, max_len, fmt, args); 39 | va_end(args); 40 | 41 | // If for any reason the print fails, overwrite the message but still print 42 | // it. The code below will attach the filename and line, which is still 43 | // useful. 44 | if (res < 0) { 45 | snprintf(log_msg, max_len, "%s", "[printf format error]"); 46 | break; 47 | } 48 | // if res == max_len, vsnprintf saturated the input buffer. Retry with a 49 | // larger buffer in that case (within reasonable limits). 50 | if (res < static_cast(max_len) || max_len >= 128 * 1024) { 51 | break; 52 | } 53 | 54 | max_len *= 4; 55 | large_buf.reset(new char[max_len]); 56 | log_msg = &large_buf[0]; 57 | } 58 | 59 | LogMessageCallback cb = g_log_callback.load(std::memory_order_relaxed); 60 | if (cb) { 61 | cb({level, line, fname, log_msg}); 62 | return; 63 | } 64 | 65 | #ifdef JNI_ZERO_IS_ROBOLECTRIC 66 | fprintf(stderr, "%s:%d %s\n", fname, line, log_msg); 67 | #else 68 | __android_log_print(int{ANDROID_LOG_DEBUG} + level, "jni_zero", "%s:%d %s", 69 | fname, line, log_msg); 70 | #endif 71 | if (level >= kLogFatal) { 72 | JNI_ZERO_IMMEDIATE_CRASH(); 73 | } 74 | } 75 | 76 | } // namespace jni_zero 77 | -------------------------------------------------------------------------------- /golden/testBidirectionalClass-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | // Hashed name: Java_J_N_MiwbZUBn 17 | public static native void org_jni_1zero_samples_SampleForTests_addStructB(long nativeCPPClass, Object caller, Object b); 18 | // Hashed name: Java_J_N_M8X3bcVm 19 | public static native void org_jni_1zero_samples_SampleForTests_destroy(long nativeCPPClass, Object caller); 20 | // Hashed name: Java_J_N_MVdv2_MU 21 | public static native Class org_jni_1zero_samples_SampleForTests_getClass(Class arg0); 22 | // Hashed name: Java_J_N_My_VMaI8 23 | public static native double org_jni_1zero_samples_SampleForTests_getDoubleFunction(Object caller); 24 | // Hashed name: Java_J_N_MqvTj9Oq 25 | public static native float org_jni_1zero_samples_SampleForTests_getFloatFunction(); 26 | // Hashed name: Java_J_N_MBdY7WM1 27 | public static native Object org_jni_1zero_samples_SampleForTests_getNonPODDatatype(Object caller); 28 | // Hashed name: Java_J_N_MVU4Rpg4 29 | public static native Throwable org_jni_1zero_samples_SampleForTests_getThrowable(Throwable arg0); 30 | // Hashed name: Java_J_N_MQT3eDxV 31 | public static native long org_jni_1zero_samples_SampleForTests_init(Object caller, String param); 32 | // Hashed name: Java_J_N_M5joaZat 33 | public static native void org_jni_1zero_samples_SampleForTests_iterateAndDoSomethingWithStructB(long nativeCPPClass, Object caller); 34 | // Hashed name: Java_J_N_MTJZxiNL 35 | public static native int org_jni_1zero_samples_SampleForTests_method(long nativeCPPClass, Object caller); 36 | // Hashed name: Java_J_N_MUrwcvpz 37 | public static native double org_jni_1zero_samples_SampleForTests_methodOtherP0(long nativePtr, Object caller); 38 | // Hashed name: Java_J_N_McjX$ya9 39 | public static native String org_jni_1zero_samples_SampleForTests_returnAString(long nativeCPPClass, Object caller); 40 | // Hashed name: Java_J_N_MRjua2VX 41 | public static native void org_jni_1zero_samples_SampleForTests_setNonPODDatatype(Object caller, Object rect); 42 | } 43 | -------------------------------------------------------------------------------- /scoped_java_ref.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2012 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "third_party/jni_zero/scoped_java_ref.h" 6 | 7 | #include "third_party/jni_zero/core.h" 8 | 9 | namespace jni_zero { 10 | namespace { 11 | 12 | const int kDefaultLocalFrameCapacity = 16; 13 | 14 | } // namespace 15 | 16 | ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env) : env_(env) { 17 | int failed = env_->PushLocalFrame(kDefaultLocalFrameCapacity); 18 | JNI_ZERO_DCHECK(!failed); 19 | } 20 | 21 | ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env, int capacity) 22 | : env_(env) { 23 | int failed = env_->PushLocalFrame(capacity); 24 | JNI_ZERO_DCHECK(!failed); 25 | } 26 | 27 | ScopedJavaLocalFrame::~ScopedJavaLocalFrame() { 28 | env_->PopLocalFrame(nullptr); 29 | } 30 | 31 | #if JNI_ZERO_DCHECK_IS_ON() 32 | // This constructor is inlined when DCHECKs are disabled; don't add anything 33 | // else here. 34 | JavaRef::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) { 35 | if (obj) { 36 | JNI_ZERO_DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType); 37 | } 38 | } 39 | #endif 40 | 41 | JNIEnv* JavaRef::SetNewLocalRef(JNIEnv* env, jobject obj) { 42 | if (!env) { 43 | env = AttachCurrentThread(); 44 | } else { 45 | JNI_ZERO_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread. 46 | } 47 | if (obj) { 48 | obj = env->NewLocalRef(obj); 49 | } 50 | if (obj_) { 51 | env->DeleteLocalRef(obj_); 52 | } 53 | obj_ = obj; 54 | return env; 55 | } 56 | 57 | void JavaRef::SetNewGlobalRef(JNIEnv* env, jobject obj) { 58 | if (!env) { 59 | env = AttachCurrentThread(); 60 | } else { 61 | JNI_ZERO_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread. 62 | } 63 | if (obj) { 64 | obj = env->NewGlobalRef(obj); 65 | } 66 | if (obj_) { 67 | env->DeleteGlobalRef(obj_); 68 | } 69 | obj_ = obj; 70 | } 71 | 72 | void JavaRef::ResetLocalRef(JNIEnv* env) { 73 | if (obj_) { 74 | JNI_ZERO_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread. 75 | env->DeleteLocalRef(obj_); 76 | obj_ = nullptr; 77 | } 78 | } 79 | 80 | void JavaRef::ResetGlobalRef() { 81 | if (obj_) { 82 | AttachCurrentThread()->DeleteGlobalRef(obj_); 83 | obj_ = nullptr; 84 | } 85 | } 86 | 87 | jobject JavaRef::ReleaseInternal() { 88 | jobject obj = obj_; 89 | obj_ = nullptr; 90 | return obj; 91 | } 92 | 93 | } // namespace jni_zero 94 | -------------------------------------------------------------------------------- /java_lang_classes.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | # List generated via: unzip android.jar | grep java/lang | grep -v '$' 6 | _NAMES = { 7 | 'AbstractMethodError', 8 | 'AbstractStringBuilder', 9 | 'Appendable', 10 | 'ArithmeticException', 11 | 'ArrayIndexOutOfBoundsException', 12 | 'ArrayStoreException', 13 | 'AssertionError', 14 | 'AutoCloseable', 15 | 'Boolean', 16 | 'Byte', 17 | 'Character', 18 | 'CharSequence', 19 | 'ClassCastException', 20 | 'ClassCircularityError', 21 | 'Class', 22 | 'ClassFormatError', 23 | 'ClassLoader', 24 | 'ClassNotFoundException', 25 | 'Cloneable', 26 | 'CloneNotSupportedException', 27 | 'Comparable', 28 | 'Compiler', 29 | 'Deprecated', 30 | 'Double', 31 | 'Enum', 32 | 'EnumConstantNotPresentException', 33 | 'Error', 34 | 'Exception', 35 | 'ExceptionInInitializerError', 36 | 'Float', 37 | 'IllegalAccessError', 38 | 'IllegalAccessException', 39 | 'IllegalArgumentException', 40 | 'IllegalMonitorStateException', 41 | 'IllegalStateException', 42 | 'IncompatibleClassChangeError', 43 | 'IndexOutOfBoundsException', 44 | 'InheritableThreadLocal', 45 | 'InstantiationError', 46 | 'InstantiationException', 47 | 'Integer', 48 | 'InternalError', 49 | 'InterruptedException', 50 | 'Iterable', 51 | 'LinkageError', 52 | 'Long', 53 | 'Math', 54 | 'NegativeArraySizeException', 55 | 'NoClassDefFoundError', 56 | 'NoSuchFieldError', 57 | 'NoSuchFieldException', 58 | 'NoSuchMethodError', 59 | 'NoSuchMethodException', 60 | 'NullPointerException', 61 | 'Number', 62 | 'NumberFormatException', 63 | 'Object', 64 | 'OutOfMemoryError', 65 | 'Override', 66 | 'Package', 67 | 'ProcessBuilder', 68 | 'Process', 69 | 'Readable', 70 | 'ReflectiveOperationException', 71 | 'Runnable', 72 | 'Runtime', 73 | 'RuntimeException', 74 | 'RuntimePermission', 75 | 'SafeVarargs', 76 | 'SecurityException', 77 | 'SecurityManager', 78 | 'Short', 79 | 'StackOverflowError', 80 | 'StackTraceElement', 81 | 'StrictMath', 82 | 'StringBuffer', 83 | 'StringBuilder', 84 | 'String', 85 | 'StringIndexOutOfBoundsException', 86 | 'SuppressWarnings', 87 | 'System', 88 | 'Thread', 89 | 'ThreadDeath', 90 | 'ThreadGroup', 91 | 'ThreadLocal', 92 | 'Throwable', 93 | 'TypeNotPresentException', 94 | 'UnknownError', 95 | 'UnsatisfiedLinkError', 96 | 'UnsupportedClassVersionError', 97 | 'UnsupportedOperationException', 98 | 'VerifyError', 99 | 'VirtualMachineError', 100 | 'Void', 101 | } 102 | 103 | 104 | def contains(unqualified_type_name): 105 | return unqualified_type_name in _NAMES 106 | -------------------------------------------------------------------------------- /golden/testForTestingKept-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | extern const char kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases[]; 25 | extern std::atomic g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz; 26 | #ifndef org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 27 | #define org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 28 | inline jclass org_jni_1zero_samples_SampleProxyEdgeCases_clazz(JNIEnv* env) { 29 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases, 30 | &g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz); 31 | } 32 | #endif 33 | 34 | 35 | // Step 2: Forward declarations (methods). 36 | 37 | JNI_BOUNDARY_EXPORT jint Java_J_N_MyLYIXSG( 38 | JNIEnv* env, 39 | jclass jcaller, 40 | jobject caller, 41 | jobject b); 42 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_M4tCs4sR( 43 | JNIEnv* env, 44 | jclass jcaller, 45 | jintArray a, 46 | jobjectArray b); 47 | JNI_BOUNDARY_EXPORT jintArray Java_J_N_Mfw_00024B7iJ( 48 | JNIEnv* env, 49 | jclass jcaller, 50 | jintArray a, 51 | jobjectArray b); 52 | JNI_BOUNDARY_EXPORT void Java_J_N_M01DKxMA_1ForTesting( 53 | JNIEnv* env, 54 | jclass jcaller); 55 | JNI_BOUNDARY_EXPORT void Java_J_N_MdPg5w5a_1ForTesting( 56 | JNIEnv* env, 57 | jclass jcaller); 58 | JNI_BOUNDARY_EXPORT void Java_J_N_Mfc_1XrOy_1ForTesting( 59 | JNIEnv* env, 60 | jclass jcaller); 61 | JNI_BOUNDARY_EXPORT void Java_J_N_MDx0XFh_00024( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MPuD3r24( 65 | JNIEnv* env, 66 | jclass jcaller, 67 | jobjectArray arg); 68 | JNI_BOUNDARY_EXPORT void Java_J_N_MYc9Cume( 69 | JNIEnv* env, 70 | jclass jcaller, 71 | jobject b, 72 | jobject i); 73 | JNI_BOUNDARY_EXPORT jboolean Java_J_N_MoYVNDqu( 74 | JNIEnv* env, 75 | jclass jcaller, 76 | jobject sb); 77 | JNI_BOUNDARY_EXPORT jint Java_J_N_MoYVNDqu( 78 | JNIEnv* env, 79 | jclass jcaller, 80 | jint sb); 81 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeInstanceMethod( 82 | JNIEnv* env, 83 | jobject jcaller, 84 | jlong nativeInstance); 85 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeStaticMethod( 86 | JNIEnv* env, 87 | jclass jcaller); 88 | 89 | 90 | 91 | #endif // TEMP_DIR_HEADER_ 92 | -------------------------------------------------------------------------------- /golden/testForTestingRemoved-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | extern const char kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases[]; 25 | extern std::atomic g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz; 26 | #ifndef org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 27 | #define org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 28 | inline jclass org_jni_1zero_samples_SampleProxyEdgeCases_clazz(JNIEnv* env) { 29 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases, 30 | &g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz); 31 | } 32 | #endif 33 | 34 | 35 | // Step 2: Forward declarations (methods). 36 | 37 | JNI_BOUNDARY_EXPORT jint Java_J_N_MyLYIXSG( 38 | JNIEnv* env, 39 | jclass jcaller, 40 | jobject caller, 41 | jobject b); 42 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_M4tCs4sR( 43 | JNIEnv* env, 44 | jclass jcaller, 45 | jintArray a, 46 | jobjectArray b); 47 | JNI_BOUNDARY_EXPORT jintArray Java_J_N_Mfw_00024B7iJ( 48 | JNIEnv* env, 49 | jclass jcaller, 50 | jintArray a, 51 | jobjectArray b); 52 | JNI_BOUNDARY_EXPORT void Java_J_N_M01DKxMA_1ForTesting( 53 | JNIEnv* env, 54 | jclass jcaller); 55 | JNI_BOUNDARY_EXPORT void Java_J_N_MdPg5w5a_1ForTesting( 56 | JNIEnv* env, 57 | jclass jcaller); 58 | JNI_BOUNDARY_EXPORT void Java_J_N_Mfc_1XrOy_1ForTesting( 59 | JNIEnv* env, 60 | jclass jcaller); 61 | JNI_BOUNDARY_EXPORT void Java_J_N_MDx0XFh_00024( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MPuD3r24( 65 | JNIEnv* env, 66 | jclass jcaller, 67 | jobjectArray arg); 68 | JNI_BOUNDARY_EXPORT void Java_J_N_MYc9Cume( 69 | JNIEnv* env, 70 | jclass jcaller, 71 | jobject b, 72 | jobject i); 73 | JNI_BOUNDARY_EXPORT jboolean Java_J_N_MoYVNDqu( 74 | JNIEnv* env, 75 | jclass jcaller, 76 | jobject sb); 77 | JNI_BOUNDARY_EXPORT jint Java_J_N_MoYVNDqu( 78 | JNIEnv* env, 79 | jclass jcaller, 80 | jint sb); 81 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeInstanceMethod( 82 | JNIEnv* env, 83 | jobject jcaller, 84 | jlong nativeInstance); 85 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeStaticMethod( 86 | JNIEnv* env, 87 | jclass jcaller); 88 | 89 | 90 | 91 | #endif // TEMP_DIR_HEADER_ 92 | -------------------------------------------------------------------------------- /codegen/proxy_impl_java.py: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | import java_types 6 | 7 | 8 | def Generate(jni_obj, *, gen_jni_class, script_name): 9 | proxy_class = java_types.JavaClass( 10 | f'{jni_obj.java_class.full_name_with_slashes}Jni') 11 | visibility = 'public ' if jni_obj.proxy_visibility == 'public' else '' 12 | interface_name = jni_obj.proxy_interface.name_with_dots 13 | gen_jni = gen_jni_class.name 14 | type_resolver = java_types.TypeResolver(proxy_class) 15 | type_resolver.imports = list(jni_obj.type_resolver.imports) 16 | 17 | sb = [] 18 | sb.append(f"""\ 19 | // 20 | // This file was generated by {script_name} 21 | // 22 | package {jni_obj.java_class.class_without_prefix.package_with_dots}; 23 | 24 | import org.jni_zero.CheckDiscard; 25 | import org.jni_zero.JniStaticTestMocker; 26 | import org.jni_zero.NativeLibraryLoadedStatus; 27 | import {gen_jni_class.full_name_with_dots}; 28 | """) 29 | 30 | # Copy over all imports (some will be unused, but oh well). 31 | for c in type_resolver.imports: 32 | sb.append(f'import {c.full_name_with_dots};\n') 33 | 34 | sb.append(f""" 35 | @CheckDiscard("crbug.com/993421") 36 | {visibility}class {proxy_class.name} implements {interface_name} {{ 37 | private static {interface_name} testInstance; 38 | 39 | public static final JniStaticTestMocker<{interface_name}> TEST_HOOKS = 40 | new JniStaticTestMocker<{interface_name}>() {{ 41 | @Override 42 | public void setInstanceForTesting({interface_name} instance) {{ 43 | if (!{gen_jni}.TESTING_ENABLED) {{ 44 | throw new RuntimeException( 45 | "Tried to set a JNI mock when mocks aren't enabled!"); 46 | }} 47 | testInstance = instance; 48 | }} 49 | }}; 50 | """) 51 | 52 | for native in jni_obj.proxy_natives: 53 | call_params = native.params.to_call_str() 54 | sig_params = native.params.to_java_declaration(type_resolver) 55 | return_type_str = native.return_type.to_java(type_resolver) 56 | return_prefix = '' 57 | if not native.return_type.is_void(): 58 | return_prefix = f'return ({return_type_str}) ' 59 | 60 | sb.append(f""" 61 | @Override 62 | public {return_type_str} {native.name}({sig_params}) {{ 63 | {return_prefix}{gen_jni}.{native.proxy_name}({call_params}); 64 | }} 65 | """) 66 | 67 | sb.append(f""" 68 | public static {interface_name} get() {{ 69 | if ({gen_jni}.TESTING_ENABLED) {{ 70 | if (testInstance != null) {{ 71 | return testInstance; 72 | }} 73 | if ({gen_jni}.REQUIRE_MOCK) {{ 74 | throw new UnsupportedOperationException( 75 | "No mock found for the native implementation of {interface_name}. " 76 | + "The current configuration requires implementations be mocked."); 77 | }} 78 | }} 79 | NativeLibraryLoadedStatus.checkLoaded(); 80 | return new {proxy_class.name}(); 81 | }} 82 | }} 83 | """) 84 | return ''.join(sb) 85 | -------------------------------------------------------------------------------- /samples/java/src/org/jni_zero/samples/SampleForAnnotationProcessor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero.samples; 6 | 7 | import org.jni_zero.NativeMethods; 8 | 9 | /** 10 | * Sample class that uses the JNI annotation processor for static methods. 11 | * See generated files at bottom. 12 | */ 13 | class SampleForAnnotationProcessor { 14 | class TestStruct { 15 | int mA; 16 | int mB; 17 | } 18 | /** 19 | * Static methods declared here, each of these refer to a native method 20 | * which will have its declaration generated by our annotation processor. 21 | * There will also be a class generated to wrap these native methods 22 | * with the name SampleForAnnotationProcessorJni which will implement 23 | * Natives. 24 | */ 25 | @NativeMethods 26 | interface Natives { 27 | void foo(); 28 | SampleForAnnotationProcessor bar(SampleForAnnotationProcessor sample); 29 | String revString(String stringToReverse); 30 | String[] sendToNative(String[] strs); 31 | SampleForAnnotationProcessor[] sendSamplesToNative(SampleForAnnotationProcessor[] strs); 32 | boolean hasPhalange(); 33 | 34 | int[] testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, 35 | short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, 36 | double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 37 | 38 | void testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, 39 | Throwable[] throwables, String string, String[] strings, TestStruct tStruct, 40 | TestStruct[] structs, Object obj, Object[] objects); 41 | 42 | Throwable returnThrowable(); 43 | Throwable[] returnThrowables(); 44 | Class returnClass(); 45 | Class[] returnClasses(); 46 | String returnString(); 47 | String[] returnStrings(); 48 | TestStruct returnStruct(); 49 | TestStruct[] returnStructs(); 50 | Object returnObject(); 51 | Object[] returnObjects(); 52 | } 53 | 54 | void test() { 55 | int[] x = new int[] {1, 2, 3, 4, 5}; 56 | String[] strs = new String[] {"the", "quick", "brown", "fox"}; 57 | strs = SampleForAnnotationProcessorJni.get().sendToNative(strs); 58 | 59 | SampleForAnnotationProcessor[] samples = 60 | new SampleForAnnotationProcessor[] {this, this, this}; 61 | samples = SampleForAnnotationProcessorJni.get().sendSamplesToNative(samples); 62 | 63 | // Instance of Natives accessed through (classname + "Jni").get(). 64 | SampleForAnnotationProcessorJni.get().foo(); 65 | SampleForAnnotationProcessor sample = SampleForAnnotationProcessorJni.get().bar(this); 66 | boolean hasPhalange = SampleForAnnotationProcessorJni.get().hasPhalange(); 67 | String s = SampleForAnnotationProcessorJni.get().revString("abcd"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /core.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "third_party/jni_zero/core.h" 6 | 7 | #include 8 | 9 | #include "third_party/jni_zero/logging.h" 10 | 11 | namespace jni_zero { 12 | namespace { 13 | // Until we fully migrate base's jni_android, we will maintain a copy of this 14 | // global here and will have base set this variable when it sets its own. 15 | JavaVM* g_jvm = nullptr; 16 | 17 | void (*g_exception_handler_callback)(JNIEnv*) = nullptr; 18 | } // namespace 19 | 20 | JNIEnv* AttachCurrentThread() { 21 | JNI_ZERO_DCHECK(g_jvm); 22 | JNIEnv* env = nullptr; 23 | jint ret = g_jvm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_2); 24 | if (ret == JNI_EDETACHED || !env) { 25 | JavaVMAttachArgs args; 26 | args.version = JNI_VERSION_1_2; 27 | args.group = nullptr; 28 | 29 | // 16 is the maximum size for thread names on Android. 30 | char thread_name[16]; 31 | int err = prctl(PR_GET_NAME, thread_name); 32 | if (err < 0) { 33 | JNI_ZERO_ELOG("prctl(PR_GET_NAME)"); 34 | args.name = nullptr; 35 | } else { 36 | args.name = thread_name; 37 | } 38 | 39 | #if defined(JNI_ZERO_IS_ROBOLECTRIC) 40 | ret = g_jvm->AttachCurrentThread(reinterpret_cast(&env), &args); 41 | #else 42 | ret = g_jvm->AttachCurrentThread(&env, &args); 43 | #endif 44 | JNI_ZERO_CHECK(ret == JNI_OK); 45 | } 46 | return env; 47 | } 48 | 49 | JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name) { 50 | JNI_ZERO_DCHECK(g_jvm); 51 | JavaVMAttachArgs args; 52 | args.version = JNI_VERSION_1_2; 53 | args.name = const_cast(thread_name.c_str()); 54 | args.group = nullptr; 55 | JNIEnv* env = nullptr; 56 | #if defined(JNI_ZERO_IS_ROBOLECTRIC) 57 | jint ret = g_jvm->AttachCurrentThread(reinterpret_cast(&env), &args); 58 | #else 59 | jint ret = g_jvm->AttachCurrentThread(&env, &args); 60 | #endif 61 | JNI_ZERO_CHECK(ret == JNI_OK); 62 | return env; 63 | } 64 | 65 | void DetachFromVM() { 66 | // Ignore the return value, if the thread is not attached, DetachCurrentThread 67 | // will fail. But it is ok as the native thread may never be attached. 68 | if (g_jvm) { 69 | g_jvm->DetachCurrentThread(); 70 | } 71 | } 72 | 73 | void InitVM(JavaVM* vm) { 74 | g_jvm = vm; 75 | } 76 | 77 | void DisableJvmForTesting() { 78 | g_jvm = nullptr; 79 | } 80 | 81 | bool IsVMInitialized() { 82 | return g_jvm != nullptr; 83 | } 84 | 85 | JavaVM* GetVM() { 86 | return g_jvm; 87 | } 88 | 89 | void SetExceptionHandler(void (*callback)(JNIEnv*)) { 90 | g_exception_handler_callback = callback; 91 | } 92 | 93 | void CheckException(JNIEnv* env) { 94 | if (env->ExceptionCheck() == JNI_FALSE) { 95 | return; 96 | } 97 | 98 | if (g_exception_handler_callback) { 99 | return g_exception_handler_callback(env); 100 | } 101 | JNI_ZERO_FLOG("jni_zero crashing due to uncaught Java exception"); 102 | } 103 | 104 | } // namespace jni_zero 105 | -------------------------------------------------------------------------------- /logging.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef JNI_ZERO_LOGGING_H_ 6 | #define JNI_ZERO_LOGGING_H_ 7 | 8 | #include "third_party/jni_zero/jni_export.h" 9 | 10 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) 11 | #define JNI_ZERO_DCHECK_IS_ON() false 12 | #else 13 | #define JNI_ZERO_DCHECK_IS_ON() true 14 | #endif 15 | 16 | // Simplified version of Google's logging. Adapted from perfetto's 17 | // implementation. 18 | namespace jni_zero { 19 | 20 | // Constexpr functions to extract basename(__FILE__), e.g.: ../foo/f.c -> f.c . 21 | constexpr const char* StrEnd(const char* s) { 22 | return *s ? StrEnd(s + 1) : s; 23 | } 24 | 25 | constexpr const char* BasenameRecursive(const char* s, 26 | const char* begin, 27 | const char* end) { 28 | return (*s == '/' && s < end) 29 | ? (s + 1) 30 | : ((s > begin) ? BasenameRecursive(s - 1, begin, end) : s); 31 | } 32 | 33 | constexpr const char* Basename(const char* str) { 34 | return BasenameRecursive(StrEnd(str), str, StrEnd(str)); 35 | } 36 | 37 | enum LogLev { kLogInfo = 0, kLogError, kLogFatal }; 38 | 39 | struct LogMessageCallbackArgs { 40 | LogLev level; 41 | int line; 42 | const char* filename; 43 | const char* message; 44 | }; 45 | 46 | using LogMessageCallback = void (*)(LogMessageCallbackArgs); 47 | 48 | // This is not thread safe and must be called before using tracing from other 49 | // threads. 50 | JNI_ZERO_COMPONENT_BUILD_EXPORT void SetLogMessageCallback( 51 | LogMessageCallback callback); 52 | 53 | JNI_ZERO_COMPONENT_BUILD_EXPORT void LogMessage(LogLev, 54 | const char* fname, 55 | int line, 56 | const char* fmt, 57 | ...) 58 | __attribute__((__format__(__printf__, 4, 5))); 59 | 60 | #define JNI_ZERO_IMMEDIATE_CRASH() \ 61 | do { \ 62 | __builtin_trap(); \ 63 | __builtin_unreachable(); \ 64 | } while (0) 65 | #define JNI_ZERO_XLOG(level, fmt, ...) \ 66 | ::jni_zero::LogMessage(level, ::jni_zero::Basename(__FILE__), __LINE__, fmt, \ 67 | ##__VA_ARGS__) 68 | #define JNI_ZERO_ILOG(fmt, ...) \ 69 | JNI_ZERO_XLOG(::jni_zero::kLogInfo, fmt, ##__VA_ARGS__) 70 | #define JNI_ZERO_ELOG(fmt, ...) \ 71 | JNI_ZERO_XLOG(::jni_zero::kLogError, fmt, ##__VA_ARGS__) 72 | #define JNI_ZERO_FLOG(fmt, ...) \ 73 | JNI_ZERO_XLOG(::jni_zero::kLogFatal, fmt, ##__VA_ARGS__) 74 | 75 | #define JNI_ZERO_CHECK(x) \ 76 | do { \ 77 | if (__builtin_expect(!(x), 0)) { \ 78 | JNI_ZERO_FLOG("%s", "JNI_ZERO_CHECK(" #x ")"); \ 79 | } \ 80 | } while (0) 81 | #if JNI_ZERO_DCHECK_IS_ON() 82 | #define JNI_ZERO_DCHECK(x) JNI_ZERO_CHECK(x) 83 | #else 84 | #define JNI_ZERO_DCHECK(x) \ 85 | do { \ 86 | } while (false && (x)) 87 | #endif 88 | } // namespace jni_zero 89 | 90 | #endif // JNI_ZERO_LOGGING_H_ 91 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyHashed-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_bar 15 | public static native Object MxkAf2fl(Object sample); 16 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_foo 17 | public static native void Md6LK1o9(); 18 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange 19 | public static native boolean M$4zYzAg(); 20 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass 21 | public static native Class M4l0zJba(); 22 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses 23 | public static native Class[] MKH66mnL(); 24 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject 25 | public static native Object Mvc7ImXr(); 26 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects 27 | public static native Object[] MocgXEXX(); 28 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnString 29 | public static native String M3yd9YCr(); 30 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings 31 | public static native String[] M_qL1Hyw(); 32 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct 33 | public static native Object MxvVNTxJ(); 34 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs 35 | public static native Object[] MJLr$5QK(); 36 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable 37 | public static native Throwable MXcljN68(); 38 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables 39 | public static native Throwable[] M1FJFJy5(); 40 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_revString 41 | public static native String MGrgOT84(String stringToReverse); 42 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative 43 | public static native Object[] MMtgBdva(Object[] strs); 44 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative 45 | public static native String[] MPVz5ily(String[] strs); 46 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives 47 | public static native int[] MHRNkgW8(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 48 | // Original name: org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes 49 | public static native void MEbp5PPc(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 50 | } 51 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyHashed-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | 25 | // Step 2: Forward declarations (methods). 26 | 27 | JNI_BOUNDARY_EXPORT jobject Java_J_N_MxkAf2fl( 28 | JNIEnv* env, 29 | jclass jcaller, 30 | jobject sample); 31 | JNI_BOUNDARY_EXPORT void Java_J_N_Md6LK1o9( 32 | JNIEnv* env, 33 | jclass jcaller); 34 | JNI_BOUNDARY_EXPORT jboolean Java_J_N_M_000244zYzAg( 35 | JNIEnv* env, 36 | jclass jcaller); 37 | JNI_BOUNDARY_EXPORT jclass Java_J_N_M4l0zJba( 38 | JNIEnv* env, 39 | jclass jcaller); 40 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MKH66mnL( 41 | JNIEnv* env, 42 | jclass jcaller); 43 | JNI_BOUNDARY_EXPORT jobject Java_J_N_Mvc7ImXr( 44 | JNIEnv* env, 45 | jclass jcaller); 46 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MocgXEXX( 47 | JNIEnv* env, 48 | jclass jcaller); 49 | JNI_BOUNDARY_EXPORT jstring Java_J_N_M3yd9YCr( 50 | JNIEnv* env, 51 | jclass jcaller); 52 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_M_1qL1Hyw( 53 | JNIEnv* env, 54 | jclass jcaller); 55 | JNI_BOUNDARY_EXPORT jobject Java_J_N_MxvVNTxJ( 56 | JNIEnv* env, 57 | jclass jcaller); 58 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MJLr_000245QK( 59 | JNIEnv* env, 60 | jclass jcaller); 61 | JNI_BOUNDARY_EXPORT jthrowable Java_J_N_MXcljN68( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_M1FJFJy5( 65 | JNIEnv* env, 66 | jclass jcaller); 67 | JNI_BOUNDARY_EXPORT jstring Java_J_N_MGrgOT84( 68 | JNIEnv* env, 69 | jclass jcaller, 70 | jstring stringToReverse); 71 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MMtgBdva( 72 | JNIEnv* env, 73 | jclass jcaller, 74 | jobjectArray strs); 75 | JNI_BOUNDARY_EXPORT jobjectArray Java_J_N_MPVz5ily( 76 | JNIEnv* env, 77 | jclass jcaller, 78 | jobjectArray strs); 79 | JNI_BOUNDARY_EXPORT jintArray Java_J_N_MHRNkgW8( 80 | JNIEnv* env, 81 | jclass jcaller, 82 | jint zint, 83 | jintArray ints, 84 | jlong zlong, 85 | jlongArray longs, 86 | jshort zshort, 87 | jshortArray shorts, 88 | jchar zchar, 89 | jcharArray chars, 90 | jbyte zbyte, 91 | jbyteArray bytes, 92 | jdouble zdouble, 93 | jdoubleArray doubles, 94 | jfloat zfloat, 95 | jfloatArray floats, 96 | jboolean zbool, 97 | jbooleanArray bools); 98 | JNI_BOUNDARY_EXPORT void Java_J_N_MEbp5PPc( 99 | JNIEnv* env, 100 | jclass jcaller, 101 | jclass clazz, 102 | jobjectArray classes, 103 | jthrowable throwable, 104 | jobjectArray throwables, 105 | jstring string, 106 | jobjectArray strings, 107 | jobject tStruct, 108 | jobjectArray structs, 109 | jobject obj, 110 | jobjectArray objects); 111 | 112 | 113 | 114 | #endif // TEMP_DIR_HEADER_ 115 | -------------------------------------------------------------------------------- /golden/testForTestingKept-SampleProxyEdgeCasesJni.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | package org.jni_zero.samples; 5 | 6 | import org.jni_zero.CheckDiscard; 7 | import org.jni_zero.JniStaticTestMocker; 8 | import org.jni_zero.NativeLibraryLoadedStatus; 9 | import org.jni_zero.GEN_JNI; 10 | import org.jni_zero.samples.Boolean; 11 | import java.util.Map; 12 | 13 | @CheckDiscard("crbug.com/993421") 14 | class SampleProxyEdgeCasesJni implements SampleProxyEdgeCases.Natives { 15 | private static SampleProxyEdgeCases.Natives testInstance; 16 | 17 | public static final JniStaticTestMocker TEST_HOOKS = 18 | new JniStaticTestMocker() { 19 | @Override 20 | public void setInstanceForTesting(SampleProxyEdgeCases.Natives instance) { 21 | if (!GEN_JNI.TESTING_ENABLED) { 22 | throw new RuntimeException( 23 | "Tried to set a JNI mock when mocks aren't enabled!"); 24 | } 25 | testInstance = instance; 26 | } 27 | }; 28 | 29 | @Override 30 | public int addStructB(SampleForTests caller, SampleForTests.InnerStructB b) { 31 | return (int) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_addStructB(caller, b); 32 | } 33 | 34 | @Override 35 | public String[][] arrayTypes1(int[] a, Object[][] b) { 36 | return (String[][]) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1(a, b); 37 | } 38 | 39 | @Override 40 | public int[] arrayTypes2(int[] a, Throwable[][] b) { 41 | return (int[]) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2(a, b); 42 | } 43 | 44 | @Override 45 | public void fooForTest() { 46 | GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_fooForTest(); 47 | } 48 | 49 | @Override 50 | public void fooForTesting() { 51 | GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_fooForTesting(); 52 | } 53 | 54 | @Override 55 | public void fooForTests() { 56 | GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_fooForTests(); 57 | } 58 | 59 | @Override 60 | public void foo__weirdly__escaped_name1() { 61 | GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1(); 62 | } 63 | 64 | @Override 65 | public Map[] genericsWithNestedClassArray(Map[] arg) { 66 | return (Map[]) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray(arg); 67 | } 68 | 69 | @Override 70 | public void setBool(Boolean b, SampleProxyEdgeCases.Integer i) { 71 | GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_setBool(b, i); 72 | } 73 | 74 | @Override 75 | public boolean setStringBuilder(StringBuilder sb) { 76 | return (boolean) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(sb); 77 | } 78 | 79 | @Override 80 | public int setStringBuilder(int sb) { 81 | return (int) GEN_JNI.org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(sb); 82 | } 83 | 84 | public static SampleProxyEdgeCases.Natives get() { 85 | if (GEN_JNI.TESTING_ENABLED) { 86 | if (testInstance != null) { 87 | return testInstance; 88 | } 89 | if (GEN_JNI.REQUIRE_MOCK) { 90 | throw new UnsupportedOperationException( 91 | "No mock found for the native implementation of SampleProxyEdgeCases.Natives. " 92 | + "The current configuration requires implementations be mocked."); 93 | } 94 | } 95 | NativeLibraryLoadedStatus.checkLoaded(); 96 | return new SampleProxyEdgeCasesJni(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /golden/testEndToEndManualRegistration-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | // Hashed name: Java_J_N_MxkAf2fl 17 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample); 18 | // Hashed name: Java_J_N_Md6LK1o9 19 | public static native void org_jni_1zero_samples_SampleForAnnotationProcessor_foo(); 20 | // Hashed name: Java_J_N_M$4zYzAg 21 | public static native boolean org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange(); 22 | // Hashed name: Java_J_N_M4l0zJba 23 | public static native Class org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass(); 24 | // Hashed name: Java_J_N_MKH66mnL 25 | public static native Class[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses(); 26 | // Hashed name: Java_J_N_Mvc7ImXr 27 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject(); 28 | // Hashed name: Java_J_N_MocgXEXX 29 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects(); 30 | // Hashed name: Java_J_N_M3yd9YCr 31 | public static native String org_jni_1zero_samples_SampleForAnnotationProcessor_returnString(); 32 | // Hashed name: Java_J_N_M_qL1Hyw 33 | public static native String[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings(); 34 | // Hashed name: Java_J_N_MxvVNTxJ 35 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct(); 36 | // Hashed name: Java_J_N_MJLr$5QK 37 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs(); 38 | // Hashed name: Java_J_N_MXcljN68 39 | public static native Throwable org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable(); 40 | // Hashed name: Java_J_N_M1FJFJy5 41 | public static native Throwable[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables(); 42 | // Hashed name: Java_J_N_MGrgOT84 43 | public static native String org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse); 44 | // Hashed name: Java_J_N_MMtgBdva 45 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs); 46 | // Hashed name: Java_J_N_MPVz5ily 47 | public static native String[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs); 48 | // Hashed name: Java_J_N_MHRNkgW8 49 | public static native int[] org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 50 | // Hashed name: Java_J_N_MEbp5PPc 51 | public static native void org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 52 | } 53 | -------------------------------------------------------------------------------- /golden/testUniqueAnnotations-SampleUniqueAnnotations_jni.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_generator.py 8 | // For 9 | // org/jni_zero/samples/SampleUniqueAnnotations 10 | 11 | #ifndef org_jni_zero_samples_SampleUniqueAnnotations_JNI 12 | #define org_jni_zero_samples_SampleUniqueAnnotations_JNI 13 | 14 | #include 15 | 16 | #include "third_party/jni_zero/jni_export.h" 17 | #include "third_party/jni_zero/jni_zero_helper.h" 18 | 19 | 20 | // Step 1: Forward declarations. 21 | 22 | JNI_ZERO_COMPONENT_BUILD_EXPORT extern const char 23 | kClassPath_org_jni_1zero_samples_SampleUniqueAnnotations[]; 24 | const char kClassPath_org_jni_1zero_samples_SampleUniqueAnnotations[] = 25 | "org/jni_zero/samples/SampleUniqueAnnotations"; 26 | // Leaking this jclass as we cannot use LazyInstance from some threads. 27 | JNI_ZERO_COMPONENT_BUILD_EXPORT std::atomic 28 | g_org_jni_1zero_samples_SampleUniqueAnnotations_clazz(nullptr); 29 | #ifndef org_jni_1zero_samples_SampleUniqueAnnotations_clazz_defined 30 | #define org_jni_1zero_samples_SampleUniqueAnnotations_clazz_defined 31 | inline jclass org_jni_1zero_samples_SampleUniqueAnnotations_clazz(JNIEnv* env) { 32 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleUniqueAnnotations, 33 | &g_org_jni_1zero_samples_SampleUniqueAnnotations_clazz); 34 | } 35 | #endif 36 | 37 | 38 | // Step 2: Constants (optional). 39 | 40 | 41 | // Step 3: Method stubs. 42 | static jint JNI_SampleUniqueAnnotations_Bar(JNIEnv* env, jint x, 43 | jint y); 44 | 45 | JNI_BOUNDARY_EXPORT jint 46 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleUniqueAnnotations_1bar( 47 | JNIEnv* env, 48 | jclass jcaller, 49 | jint x, 50 | jint y) { 51 | return JNI_SampleUniqueAnnotations_Bar(env, x, y); 52 | } 53 | 54 | JNI_BOUNDARY_EXPORT void 55 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleUniqueAnnotations_1foo( 56 | JNIEnv* env, 57 | jclass jcaller, 58 | jlong nativePtr) { 59 | FooAndroid::BarDelegate* native = reinterpret_cast(nativePtr); 60 | CHECK_NATIVE_PTR(env, jcaller, native, "Foo"); 61 | return native->Foo(env); 62 | } 63 | 64 | JNI_BOUNDARY_EXPORT void 65 | Java_org_jni_1zero_samples_SampleUniqueAnnotations_nativeCallWithQualifiedObject( 66 | JNIEnv* env, 67 | jobject jcaller, 68 | jlong nativePtr) { 69 | Foo::Bar* native = reinterpret_cast(nativePtr); 70 | CHECK_NATIVE_PTR(env, jcaller, native, "CallWithQualifiedObject"); 71 | return native->CallWithQualifiedObject(env, base::android::JavaParamRef(env, jcaller)); 72 | } 73 | 74 | 75 | static std::atomic g_org_jni_1zero_samples_SampleUniqueAnnotations_useBar11(nullptr); 76 | static void Java_SampleUniqueAnnotations_useBar1(JNIEnv* env, const base::android::JavaRef& 77 | inner) { 78 | jclass clazz = org_jni_1zero_samples_SampleUniqueAnnotations_clazz(env); 79 | CHECK_CLAZZ(env, clazz, 80 | org_jni_1zero_samples_SampleUniqueAnnotations_clazz(env)); 81 | 82 | jni_zero::JniJavaCallContextChecked call_context; 83 | call_context.Init< 84 | base::android::MethodID::TYPE_STATIC>( 85 | env, 86 | clazz, 87 | "useBar1", 88 | "(Lorg/jni_zero/samples/Bar1$Inner;)V", 89 | &g_org_jni_1zero_samples_SampleUniqueAnnotations_useBar11); 90 | 91 | env->CallStaticVoidMethod(clazz, 92 | call_context.base.method_id, inner.obj()); 93 | } 94 | 95 | #endif // org_jni_zero_samples_SampleUniqueAnnotations_JNI 96 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithProxyHash-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package this.is.a.package.prefix.J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_bar 15 | public static native Object MYJQG2tV(Object sample); 16 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_foo 17 | public static native void McJTRQ4Z(); 18 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange 19 | public static native boolean MmNHlL$5(); 20 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass 21 | public static native Class MF$OWG3c(); 22 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses 23 | public static native Class[] MI7vvSXV(); 24 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject 25 | public static native Object MQV6X3Sz(); 26 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects 27 | public static native Object[] MntwrJje(); 28 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnString 29 | public static native String MlMyYIKk(); 30 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings 31 | public static native String[] MQ631WBV(); 32 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct 33 | public static native Object M3fBDe9t(); 34 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs 35 | public static native Object[] MKFA0R1H(); 36 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable 37 | public static native Throwable M2vFJGXF(); 38 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables 39 | public static native Throwable[] MWuNxqXZ(); 40 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_revString 41 | public static native String MMV8vkvD(String stringToReverse); 42 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative 43 | public static native Object[] MbvaHriB(Object[] strs); 44 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative 45 | public static native String[] MBko4EWo(String[] strs); 46 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives 47 | public static native int[] MTkl34Bi(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 48 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes 49 | public static native void Mooq_3kI(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 50 | } 51 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithManualRegistrationWithProxyHash-Final-N.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package this.is.a.package.prefix.J; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class N { 12 | 13 | 14 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_bar 15 | public static native Object MYJQG2tV(Object sample); 16 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_foo 17 | public static native void McJTRQ4Z(); 18 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange 19 | public static native boolean MmNHlL$5(); 20 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass 21 | public static native Class MF$OWG3c(); 22 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses 23 | public static native Class[] MI7vvSXV(); 24 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject 25 | public static native Object MQV6X3Sz(); 26 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects 27 | public static native Object[] MntwrJje(); 28 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnString 29 | public static native String MlMyYIKk(); 30 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings 31 | public static native String[] MQ631WBV(); 32 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct 33 | public static native Object M3fBDe9t(); 34 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs 35 | public static native Object[] MKFA0R1H(); 36 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable 37 | public static native Throwable M2vFJGXF(); 38 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables 39 | public static native Throwable[] MWuNxqXZ(); 40 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_revString 41 | public static native String MMV8vkvD(String stringToReverse); 42 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative 43 | public static native Object[] MbvaHriB(Object[] strs); 44 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative 45 | public static native String[] MBko4EWo(String[] strs); 46 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives 47 | public static native int[] MTkl34Bi(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 48 | // Original name: this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes 49 | public static native void Mooq_3kI(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 50 | } 51 | -------------------------------------------------------------------------------- /golden/testEndToEndProxyHashed-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample) { 17 | return J.N.MxkAf2fl(sample); 18 | } 19 | 20 | public static void org_jni_1zero_samples_SampleForAnnotationProcessor_foo() { 21 | J.N.Md6LK1o9(); 22 | } 23 | 24 | public static boolean org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange() { 25 | return J.N.M$4zYzAg(); 26 | } 27 | 28 | public static Class org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass() { 29 | return J.N.M4l0zJba(); 30 | } 31 | 32 | public static Class[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses() { 33 | return J.N.MKH66mnL(); 34 | } 35 | 36 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject() { 37 | return J.N.Mvc7ImXr(); 38 | } 39 | 40 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects() { 41 | return J.N.MocgXEXX(); 42 | } 43 | 44 | public static String org_jni_1zero_samples_SampleForAnnotationProcessor_returnString() { 45 | return J.N.M3yd9YCr(); 46 | } 47 | 48 | public static String[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings() { 49 | return J.N.M_qL1Hyw(); 50 | } 51 | 52 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct() { 53 | return J.N.MxvVNTxJ(); 54 | } 55 | 56 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs() { 57 | return J.N.MJLr$5QK(); 58 | } 59 | 60 | public static Throwable org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable() { 61 | return J.N.MXcljN68(); 62 | } 63 | 64 | public static Throwable[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables() { 65 | return J.N.M1FJFJy5(); 66 | } 67 | 68 | public static String org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse) { 69 | return J.N.MGrgOT84(stringToReverse); 70 | } 71 | 72 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs) { 73 | return J.N.MMtgBdva(strs); 74 | } 75 | 76 | public static String[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs) { 77 | return J.N.MPVz5ily(strs); 78 | } 79 | 80 | public static int[] org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools) { 81 | return J.N.MHRNkgW8(zint, ints, zlong, longs, zshort, shorts, zchar, chars, zbyte, bytes, zdouble, doubles, zfloat, floats, zbool, bools); 82 | } 83 | 84 | public static void org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects) { 85 | J.N.MEbp5PPc(clazz, classes, throwable, throwables, string, strings, tStruct, structs, obj, objects); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithProxyHash-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | 25 | // Step 2: Forward declarations (methods). 26 | 27 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_MYJQG2tV( 28 | JNIEnv* env, 29 | jclass jcaller, 30 | jobject sample); 31 | JNI_BOUNDARY_EXPORT void Java_this_is_a_package_prefix_J_N_McJTRQ4Z( 32 | JNIEnv* env, 33 | jclass jcaller); 34 | JNI_BOUNDARY_EXPORT jboolean Java_this_is_a_package_prefix_J_N_MmNHlL_000245( 35 | JNIEnv* env, 36 | jclass jcaller); 37 | JNI_BOUNDARY_EXPORT jclass Java_this_is_a_package_prefix_J_N_MF_00024OWG3c( 38 | JNIEnv* env, 39 | jclass jcaller); 40 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MI7vvSXV( 41 | JNIEnv* env, 42 | jclass jcaller); 43 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_MQV6X3Sz( 44 | JNIEnv* env, 45 | jclass jcaller); 46 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MntwrJje( 47 | JNIEnv* env, 48 | jclass jcaller); 49 | JNI_BOUNDARY_EXPORT jstring Java_this_is_a_package_prefix_J_N_MlMyYIKk( 50 | JNIEnv* env, 51 | jclass jcaller); 52 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MQ631WBV( 53 | JNIEnv* env, 54 | jclass jcaller); 55 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_M3fBDe9t( 56 | JNIEnv* env, 57 | jclass jcaller); 58 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MKFA0R1H( 59 | JNIEnv* env, 60 | jclass jcaller); 61 | JNI_BOUNDARY_EXPORT jthrowable Java_this_is_a_package_prefix_J_N_M2vFJGXF( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MWuNxqXZ( 65 | JNIEnv* env, 66 | jclass jcaller); 67 | JNI_BOUNDARY_EXPORT jstring Java_this_is_a_package_prefix_J_N_MMV8vkvD( 68 | JNIEnv* env, 69 | jclass jcaller, 70 | jstring stringToReverse); 71 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MbvaHriB( 72 | JNIEnv* env, 73 | jclass jcaller, 74 | jobjectArray strs); 75 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MBko4EWo( 76 | JNIEnv* env, 77 | jclass jcaller, 78 | jobjectArray strs); 79 | JNI_BOUNDARY_EXPORT jintArray Java_this_is_a_package_prefix_J_N_MTkl34Bi( 80 | JNIEnv* env, 81 | jclass jcaller, 82 | jint zint, 83 | jintArray ints, 84 | jlong zlong, 85 | jlongArray longs, 86 | jshort zshort, 87 | jshortArray shorts, 88 | jchar zchar, 89 | jcharArray chars, 90 | jbyte zbyte, 91 | jbyteArray bytes, 92 | jdouble zdouble, 93 | jdoubleArray doubles, 94 | jfloat zfloat, 95 | jfloatArray floats, 96 | jboolean zbool, 97 | jbooleanArray bools); 98 | JNI_BOUNDARY_EXPORT void Java_this_is_a_package_prefix_J_N_Mooq_13kI( 99 | JNIEnv* env, 100 | jclass jcaller, 101 | jclass clazz, 102 | jobjectArray classes, 103 | jthrowable throwable, 104 | jobjectArray throwables, 105 | jstring string, 106 | jobjectArray strings, 107 | jobject tStruct, 108 | jobjectArray structs, 109 | jobject obj, 110 | jobjectArray objects); 111 | 112 | 113 | 114 | #endif // TEMP_DIR_HEADER_ 115 | -------------------------------------------------------------------------------- /samples/sample_for_tests.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 6 | #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "base/android/jni_android.h" 13 | 14 | namespace base { 15 | namespace android { 16 | 17 | // This file is used to: 18 | // - document the best practices and guidelines on JNI usage. 19 | // - ensure sample_for_tests_jni.h compiles and the functions declared in it 20 | // as expected. 21 | // 22 | // Methods are called directly from Java. More documentation in 23 | // SampleForTests.java. See BUILD.gn for the build rules necessary for JNI 24 | // to be used in an APK. 25 | // 26 | // For C++ to access Java methods: 27 | // - GN Build must be configured to generate bindings: 28 | // # Add import at top of file: 29 | // if (is_android) { 30 | // import("//build/config/android/rules.gni") # For generate_jni(). 31 | // } 32 | // # ... 33 | // # An example target that will rely on JNI: 34 | // component("foo") { 35 | // # ... normal sources, defines, deps. 36 | // # For each jni generated .java -> .h header file in jni_headers 37 | // # target there will be a single .cc file here that includes it. 38 | // # 39 | // # Add a dep for JNI: 40 | // if (is_android) { 41 | // deps += [ ":foo_jni" ] 42 | // } 43 | // } 44 | // # ... 45 | // # Create target for JNI: 46 | // if (is_android) { 47 | // generate_jni("jni_headers") { 48 | // sources = [ 49 | // "java/src/org/chromium/example/jni_generator/SampleForTests.java", 50 | // ] 51 | // } 52 | // android_library("java") { 53 | // sources = [ 54 | // "java/src/org/chromium/example/jni_generator/SampleForTests.java", 55 | // "java/src/org/chromium/example/jni_generator/NonJniFile.java", 56 | // ] 57 | // } 58 | // } 59 | // The build rules above are generally that that's needed when adding new 60 | // JNI methods/files. For a full GN example, see 61 | // base/android/jni_generator/BUILD.gn 62 | // 63 | // For C++ methods to be exposed to Java: 64 | // - The Java class must be part of an android_apk target that depends on 65 | // a generate_jni_registration target. This generate_jni_registration target 66 | // automatically generates all necessary registration functions. The 67 | // generated header file exposes RegisterNatives() which registers all 68 | // methods. 69 | // 70 | class CPPClass { 71 | public: 72 | CPPClass(); 73 | 74 | CPPClass(const CPPClass&) = delete; 75 | CPPClass& operator=(const CPPClass&) = delete; 76 | 77 | ~CPPClass(); 78 | 79 | // Java @CalledByNative methods implicitly available to C++ via the _jni.h 80 | // file included in the .cc file. 81 | 82 | class InnerClass { 83 | public: 84 | jdouble MethodOtherP0(JNIEnv* env, 85 | const base::android::JavaParamRef& caller); 86 | }; 87 | 88 | void Destroy(JNIEnv* env, const base::android::JavaParamRef& caller); 89 | 90 | jint Method(JNIEnv* env, const base::android::JavaParamRef& caller); 91 | 92 | void AddStructB(JNIEnv* env, 93 | const base::android::JavaParamRef& caller, 94 | const base::android::JavaParamRef& structb); 95 | 96 | void IterateAndDoSomethingWithStructB( 97 | JNIEnv* env, 98 | const base::android::JavaParamRef& caller); 99 | 100 | base::android::ScopedJavaLocalRef ReturnAString( 101 | JNIEnv* env, 102 | const base::android::JavaParamRef& caller); 103 | 104 | private: 105 | std::map map_; 106 | }; 107 | 108 | } // namespace android 109 | } // namespace base 110 | 111 | #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ 112 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithManualRegistration-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package this.is.a.package.prefix.org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | // Hashed name: Java_J_N_MYJQG2tV 17 | public static native Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample); 18 | // Hashed name: Java_J_N_McJTRQ4Z 19 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_foo(); 20 | // Hashed name: Java_J_N_MmNHlL$5 21 | public static native boolean this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange(); 22 | // Hashed name: Java_J_N_MF$OWG3c 23 | public static native Class this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass(); 24 | // Hashed name: Java_J_N_MI7vvSXV 25 | public static native Class[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses(); 26 | // Hashed name: Java_J_N_MQV6X3Sz 27 | public static native Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject(); 28 | // Hashed name: Java_J_N_MntwrJje 29 | public static native Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects(); 30 | // Hashed name: Java_J_N_MlMyYIKk 31 | public static native String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnString(); 32 | // Hashed name: Java_J_N_MQ631WBV 33 | public static native String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings(); 34 | // Hashed name: Java_J_N_M3fBDe9t 35 | public static native Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct(); 36 | // Hashed name: Java_J_N_MKFA0R1H 37 | public static native Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs(); 38 | // Hashed name: Java_J_N_M2vFJGXF 39 | public static native Throwable this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable(); 40 | // Hashed name: Java_J_N_MWuNxqXZ 41 | public static native Throwable[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables(); 42 | // Hashed name: Java_J_N_MMV8vkvD 43 | public static native String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse); 44 | // Hashed name: Java_J_N_MbvaHriB 45 | public static native Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs); 46 | // Hashed name: Java_J_N_MBko4EWo 47 | public static native String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs); 48 | // Hashed name: Java_J_N_MTkl34Bi 49 | public static native int[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 50 | // Hashed name: Java_J_N_Mooq_3kI 51 | public static native void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 52 | } 53 | -------------------------------------------------------------------------------- /golden/testMultiplexing-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample) { 17 | return J.N.resolve_for_object_O(-4161291034639552194L, sample); 18 | } 19 | 20 | public static void org_jni_1zero_samples_SampleForAnnotationProcessor_foo() { 21 | J.N.resolve_for_void(8620675659615840065L); 22 | } 23 | 24 | public static boolean org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange() { 25 | return J.N.resolve_for_boolean(-320643102627558484L); 26 | } 27 | 28 | public static Class org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass() { 29 | return J.N.resolve_for_class(-2135494411232619400L); 30 | } 31 | 32 | public static Class[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses() { 33 | return J.N.resolve_for_class_array(2917974781097758672L); 34 | } 35 | 36 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject() { 37 | return J.N.resolve_for_object(-4769654393820622682L); 38 | } 39 | 40 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects() { 41 | return J.N.resolve_for_object_array(-6789151074502104874L); 42 | } 43 | 44 | public static String org_jni_1zero_samples_SampleForAnnotationProcessor_returnString() { 45 | return J.N.resolve_for_string(-2366785227171092261L); 46 | } 47 | 48 | public static String[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings() { 49 | return J.N.resolve_for_string_array(-98246727617655206L); 50 | } 51 | 52 | public static Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct() { 53 | return J.N.resolve_for_object(-4110294082898514797L); 54 | } 55 | 56 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs() { 57 | return J.N.resolve_for_object_array(2646707990576644715L); 58 | } 59 | 60 | public static Throwable org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable() { 61 | return J.N.resolve_for_throwable(6758041806660001267L); 62 | } 63 | 64 | public static Throwable[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables() { 65 | return J.N.resolve_for_throwable_array(-3147377154995757672L); 66 | } 67 | 68 | public static String org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse) { 69 | return J.N.resolve_for_string_R(1925304576625325361L, stringToReverse); 70 | } 71 | 72 | public static Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs) { 73 | return J.N.resolve_for_object_array_OA(3663679907322354778L, strs); 74 | } 75 | 76 | public static String[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs) { 77 | return J.N.resolve_for_string_array_RA(4421683306824100923L, strs); 78 | } 79 | 80 | public static int[] org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools) { 81 | return J.N.resolve_for_int_array_IIAJJASSACCABBADDAFFAZZA(2095128758736799674L, zint, ints, zlong, longs, zshort, shorts, zchar, chars, zbyte, bytes, zdouble, doubles, zfloat, floats, zbool, bools); 82 | } 83 | 84 | public static void org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects) { 85 | J.N.resolve_for_void_LLATTARRAOOAOOA(1277466747063181146L, clazz, classes, throwable, throwables, string, strings, tStruct, structs, obj, objects); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /jni_zero_helper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef JNI_ZERO_JNI_ZERO_HELPER_H_ 6 | #define JNI_ZERO_JNI_ZERO_HELPER_H_ 7 | 8 | #include 9 | 10 | #include "base/android/jni_android.h" 11 | #include "base/android/scoped_java_ref.h" 12 | #include "base/compiler_specific.h" 13 | #include "base/memory/raw_ptr.h" 14 | #if defined(USE_CHROMIUM_BASE) 15 | // Used for ARCH_CPU_X86 - embedder must define this correctly if they want 16 | // 16-byte stack alignment on x86. 17 | #include "build/build_config.h" 18 | #endif // defined(USE_CHROMIUM_BASE) 19 | #include "third_party/jni_zero/core.h" 20 | #include "third_party/jni_zero/jni_export.h" 21 | #include "third_party/jni_zero/jni_int_wrapper.h" 22 | #include "third_party/jni_zero/logging.h" 23 | 24 | // Project-specific macros used by the header files generated by 25 | // jni_generator.py. Different projects can then specify their own 26 | // implementation for this file. 27 | #define CHECK_NATIVE_PTR(env, jcaller, native_ptr, method_name, ...) \ 28 | JNI_ZERO_DCHECK(native_ptr); 29 | 30 | #define CHECK_CLAZZ(env, jcaller, clazz, ...) JNI_ZERO_DCHECK(clazz); 31 | 32 | namespace jni_zero { 33 | 34 | inline void HandleRegistrationError(JNIEnv* env, 35 | jclass clazz, 36 | const char* filename) { 37 | JNI_ZERO_ELOG("RegisterNatives failed in %s", filename); 38 | } 39 | 40 | // A 32 bit number could be an address on stack. Random 64 bit marker on the 41 | // stack is much less likely to be present on stack. 42 | constexpr uint64_t kJniStackMarkerValue = 0xbdbdef1bebcade1b; 43 | 44 | // Context about the JNI call with exception checked to be stored in stack. 45 | struct JNI_ZERO_COMPONENT_BUILD_EXPORT JniJavaCallContextUnchecked { 46 | ALWAYS_INLINE JniJavaCallContextUnchecked() { 47 | // TODO(ssid): Implement for other architectures. 48 | #if defined(__arm__) || defined(__aarch64__) 49 | // This assumes that this method does not increment the stack pointer. 50 | asm volatile("mov %0, sp" : "=r"(sp)); 51 | #else 52 | sp = 0; 53 | #endif 54 | } 55 | 56 | // Force no inline to reduce code size. 57 | template 58 | NOINLINE void Init(JNIEnv* env, 59 | jclass clazz, 60 | const char* method_name, 61 | const char* jni_signature, 62 | std::atomic* atomic_method_id) { 63 | env1 = env; 64 | 65 | // Make sure compiler doesn't optimize out the assignment. 66 | memcpy(&marker, &kJniStackMarkerValue, sizeof(kJniStackMarkerValue)); 67 | // Gets PC of the calling function. 68 | pc = reinterpret_cast(__builtin_return_address(0)); 69 | 70 | method_id = base::android::MethodID::LazyGet( 71 | env, clazz, method_name, jni_signature, atomic_method_id); 72 | } 73 | 74 | NOINLINE ~JniJavaCallContextUnchecked() { 75 | // Reset so that spurious marker finds are avoided. 76 | memset(&marker, 0, sizeof(marker)); 77 | } 78 | 79 | uint64_t marker; 80 | uintptr_t sp; 81 | uintptr_t pc; 82 | 83 | raw_ptr env1; 84 | jmethodID method_id; 85 | }; 86 | 87 | // Context about the JNI call with exception unchecked to be stored in stack. 88 | struct JNI_ZERO_COMPONENT_BUILD_EXPORT JniJavaCallContextChecked { 89 | // Force no inline to reduce code size. 90 | template 91 | NOINLINE void Init(JNIEnv* env, 92 | jclass clazz, 93 | const char* method_name, 94 | const char* jni_signature, 95 | std::atomic* atomic_method_id) { 96 | base.Init(env, clazz, method_name, jni_signature, atomic_method_id); 97 | // Reset |pc| to correct caller. 98 | base.pc = reinterpret_cast(__builtin_return_address(0)); 99 | } 100 | 101 | NOINLINE ~JniJavaCallContextChecked() { CheckException(base.env1); } 102 | 103 | JniJavaCallContextUnchecked base; 104 | }; 105 | 106 | static_assert(sizeof(JniJavaCallContextChecked) == 107 | sizeof(JniJavaCallContextUnchecked), 108 | "Stack unwinder cannot work with structs of different sizes."); 109 | 110 | } // namespace jni_zero 111 | 112 | #endif // JNI_ZERO_JNI_ZERO_HELPER_H_ 113 | -------------------------------------------------------------------------------- /golden/testBidirectionalClass-SampleForTestsJni.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | package org.jni_zero.samples; 5 | 6 | import org.jni_zero.CheckDiscard; 7 | import org.jni_zero.JniStaticTestMocker; 8 | import org.jni_zero.NativeLibraryLoadedStatus; 9 | import org.jni_zero.GEN_JNI; 10 | import android.graphics.Rect; 11 | import org.jni_zero.AccessedByNative; 12 | import org.jni_zero.CalledByNative; 13 | import org.jni_zero.CalledByNativeUnchecked; 14 | import org.jni_zero.JNINamespace; 15 | import org.jni_zero.NativeClassQualifiedName; 16 | import org.jni_zero.NativeMethods; 17 | import java.util.ArrayList; 18 | import java.util.Iterator; 19 | import java.util.LinkedList; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | @CheckDiscard("crbug.com/993421") 24 | public class SampleForTestsJni implements SampleForTests.Natives { 25 | private static SampleForTests.Natives testInstance; 26 | 27 | public static final JniStaticTestMocker TEST_HOOKS = 28 | new JniStaticTestMocker() { 29 | @Override 30 | public void setInstanceForTesting(SampleForTests.Natives instance) { 31 | if (!GEN_JNI.TESTING_ENABLED) { 32 | throw new RuntimeException( 33 | "Tried to set a JNI mock when mocks aren't enabled!"); 34 | } 35 | testInstance = instance; 36 | } 37 | }; 38 | 39 | @Override 40 | public void addStructB(long nativeCPPClass, SampleForTests caller, SampleForTests.InnerStructB b) { 41 | GEN_JNI.org_jni_1zero_samples_SampleForTests_addStructB(nativeCPPClass, caller, b); 42 | } 43 | 44 | @Override 45 | public void destroy(long nativeCPPClass, SampleForTests caller) { 46 | GEN_JNI.org_jni_1zero_samples_SampleForTests_destroy(nativeCPPClass, caller); 47 | } 48 | 49 | @Override 50 | public Class getClass(Class arg0) { 51 | return (Class) GEN_JNI.org_jni_1zero_samples_SampleForTests_getClass(arg0); 52 | } 53 | 54 | @Override 55 | public double getDoubleFunction(SampleForTests caller) { 56 | return (double) GEN_JNI.org_jni_1zero_samples_SampleForTests_getDoubleFunction(caller); 57 | } 58 | 59 | @Override 60 | public float getFloatFunction() { 61 | return (float) GEN_JNI.org_jni_1zero_samples_SampleForTests_getFloatFunction(); 62 | } 63 | 64 | @Override 65 | public Object getNonPODDatatype(SampleForTests caller) { 66 | return (Object) GEN_JNI.org_jni_1zero_samples_SampleForTests_getNonPODDatatype(caller); 67 | } 68 | 69 | @Override 70 | public Throwable getThrowable(Throwable arg0) { 71 | return (Throwable) GEN_JNI.org_jni_1zero_samples_SampleForTests_getThrowable(arg0); 72 | } 73 | 74 | @Override 75 | public long init(SampleForTests caller, String param) { 76 | return (long) GEN_JNI.org_jni_1zero_samples_SampleForTests_init(caller, param); 77 | } 78 | 79 | @Override 80 | public void iterateAndDoSomethingWithStructB(long nativeCPPClass, SampleForTests caller) { 81 | GEN_JNI.org_jni_1zero_samples_SampleForTests_iterateAndDoSomethingWithStructB(nativeCPPClass, caller); 82 | } 83 | 84 | @Override 85 | public int method(long nativeCPPClass, SampleForTests caller) { 86 | return (int) GEN_JNI.org_jni_1zero_samples_SampleForTests_method(nativeCPPClass, caller); 87 | } 88 | 89 | @Override 90 | public double methodOtherP0(long nativePtr, SampleForTests caller) { 91 | return (double) GEN_JNI.org_jni_1zero_samples_SampleForTests_methodOtherP0(nativePtr, caller); 92 | } 93 | 94 | @Override 95 | public String returnAString(long nativeCPPClass, SampleForTests caller) { 96 | return (String) GEN_JNI.org_jni_1zero_samples_SampleForTests_returnAString(nativeCPPClass, caller); 97 | } 98 | 99 | @Override 100 | public void setNonPODDatatype(SampleForTests caller, Rect rect) { 101 | GEN_JNI.org_jni_1zero_samples_SampleForTests_setNonPODDatatype(caller, rect); 102 | } 103 | 104 | public static SampleForTests.Natives get() { 105 | if (GEN_JNI.TESTING_ENABLED) { 106 | if (testInstance != null) { 107 | return testInstance; 108 | } 109 | if (GEN_JNI.REQUIRE_MOCK) { 110 | throw new UnsupportedOperationException( 111 | "No mock found for the native implementation of SampleForTests.Natives. " 112 | + "The current configuration requires implementations be mocked."); 113 | } 114 | } 115 | NativeLibraryLoadedStatus.checkLoaded(); 116 | return new SampleForTestsJni(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithProxyHash-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package this.is.a.package.prefix.org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample) { 17 | return this.is.a.package.prefix.J.N.MYJQG2tV(sample); 18 | } 19 | 20 | public static void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_foo() { 21 | this.is.a.package.prefix.J.N.McJTRQ4Z(); 22 | } 23 | 24 | public static boolean this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange() { 25 | return this.is.a.package.prefix.J.N.MmNHlL$5(); 26 | } 27 | 28 | public static Class this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass() { 29 | return this.is.a.package.prefix.J.N.MF$OWG3c(); 30 | } 31 | 32 | public static Class[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses() { 33 | return this.is.a.package.prefix.J.N.MI7vvSXV(); 34 | } 35 | 36 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject() { 37 | return this.is.a.package.prefix.J.N.MQV6X3Sz(); 38 | } 39 | 40 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects() { 41 | return this.is.a.package.prefix.J.N.MntwrJje(); 42 | } 43 | 44 | public static String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnString() { 45 | return this.is.a.package.prefix.J.N.MlMyYIKk(); 46 | } 47 | 48 | public static String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings() { 49 | return this.is.a.package.prefix.J.N.MQ631WBV(); 50 | } 51 | 52 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct() { 53 | return this.is.a.package.prefix.J.N.M3fBDe9t(); 54 | } 55 | 56 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs() { 57 | return this.is.a.package.prefix.J.N.MKFA0R1H(); 58 | } 59 | 60 | public static Throwable this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable() { 61 | return this.is.a.package.prefix.J.N.M2vFJGXF(); 62 | } 63 | 64 | public static Throwable[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables() { 65 | return this.is.a.package.prefix.J.N.MWuNxqXZ(); 66 | } 67 | 68 | public static String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse) { 69 | return this.is.a.package.prefix.J.N.MMV8vkvD(stringToReverse); 70 | } 71 | 72 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs) { 73 | return this.is.a.package.prefix.J.N.MbvaHriB(strs); 74 | } 75 | 76 | public static String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs) { 77 | return this.is.a.package.prefix.J.N.MBko4EWo(strs); 78 | } 79 | 80 | public static int[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools) { 81 | return this.is.a.package.prefix.J.N.MTkl34Bi(zint, ints, zlong, longs, zshort, shorts, zchar, chars, zbyte, bytes, zdouble, doubles, zfloat, floats, zbool, bools); 82 | } 83 | 84 | public static void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects) { 85 | this.is.a.package.prefix.J.N.Mooq_3kI(clazz, classes, throwable, throwables, string, strings, tStruct, structs, obj, objects); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithManualRegistrationWithProxyHash-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package this.is.a.package.prefix.org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample) { 17 | return this.is.a.package.prefix.J.N.MYJQG2tV(sample); 18 | } 19 | 20 | public static void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_foo() { 21 | this.is.a.package.prefix.J.N.McJTRQ4Z(); 22 | } 23 | 24 | public static boolean this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange() { 25 | return this.is.a.package.prefix.J.N.MmNHlL$5(); 26 | } 27 | 28 | public static Class this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass() { 29 | return this.is.a.package.prefix.J.N.MF$OWG3c(); 30 | } 31 | 32 | public static Class[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses() { 33 | return this.is.a.package.prefix.J.N.MI7vvSXV(); 34 | } 35 | 36 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject() { 37 | return this.is.a.package.prefix.J.N.MQV6X3Sz(); 38 | } 39 | 40 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects() { 41 | return this.is.a.package.prefix.J.N.MntwrJje(); 42 | } 43 | 44 | public static String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnString() { 45 | return this.is.a.package.prefix.J.N.MlMyYIKk(); 46 | } 47 | 48 | public static String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings() { 49 | return this.is.a.package.prefix.J.N.MQ631WBV(); 50 | } 51 | 52 | public static Object this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct() { 53 | return this.is.a.package.prefix.J.N.M3fBDe9t(); 54 | } 55 | 56 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs() { 57 | return this.is.a.package.prefix.J.N.MKFA0R1H(); 58 | } 59 | 60 | public static Throwable this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable() { 61 | return this.is.a.package.prefix.J.N.M2vFJGXF(); 62 | } 63 | 64 | public static Throwable[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables() { 65 | return this.is.a.package.prefix.J.N.MWuNxqXZ(); 66 | } 67 | 68 | public static String this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse) { 69 | return this.is.a.package.prefix.J.N.MMV8vkvD(stringToReverse); 70 | } 71 | 72 | public static Object[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs) { 73 | return this.is.a.package.prefix.J.N.MbvaHriB(strs); 74 | } 75 | 76 | public static String[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs) { 77 | return this.is.a.package.prefix.J.N.MBko4EWo(strs); 78 | } 79 | 80 | public static int[] this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools) { 81 | return this.is.a.package.prefix.J.N.MTkl34Bi(zint, ints, zlong, longs, zshort, shorts, zchar, chars, zbyte, bytes, zdouble, doubles, zfloat, floats, zbool, bools); 82 | } 83 | 84 | public static void this_is_a_package_prefix_org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects) { 85 | this.is.a.package.prefix.J.N.Mooq_3kI(clazz, classes, throwable, throwables, string, strings, tStruct, structs, obj, objects); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /golden/testPackagePrefixGenerator-SampleForTestsJni.java.golden: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by third_party/jni_zero/jni_generator.py 3 | // 4 | package org.jni_zero.samples; 5 | 6 | import org.jni_zero.CheckDiscard; 7 | import org.jni_zero.JniStaticTestMocker; 8 | import org.jni_zero.NativeLibraryLoadedStatus; 9 | import this.is.a.package.prefix.org.jni_zero.GEN_JNI; 10 | import android.graphics.Rect; 11 | import org.jni_zero.AccessedByNative; 12 | import org.jni_zero.CalledByNative; 13 | import org.jni_zero.CalledByNativeUnchecked; 14 | import org.jni_zero.JNINamespace; 15 | import org.jni_zero.NativeClassQualifiedName; 16 | import org.jni_zero.NativeMethods; 17 | import java.util.ArrayList; 18 | import java.util.Iterator; 19 | import java.util.LinkedList; 20 | import java.util.List; 21 | import java.util.Map; 22 | 23 | @CheckDiscard("crbug.com/993421") 24 | public class SampleForTestsJni implements SampleForTests.Natives { 25 | private static SampleForTests.Natives testInstance; 26 | 27 | public static final JniStaticTestMocker TEST_HOOKS = 28 | new JniStaticTestMocker() { 29 | @Override 30 | public void setInstanceForTesting(SampleForTests.Natives instance) { 31 | if (!GEN_JNI.TESTING_ENABLED) { 32 | throw new RuntimeException( 33 | "Tried to set a JNI mock when mocks aren't enabled!"); 34 | } 35 | testInstance = instance; 36 | } 37 | }; 38 | 39 | @Override 40 | public void addStructB(long nativeCPPClass, SampleForTests caller, SampleForTests.InnerStructB b) { 41 | GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_addStructB(nativeCPPClass, caller, b); 42 | } 43 | 44 | @Override 45 | public void destroy(long nativeCPPClass, SampleForTests caller) { 46 | GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_destroy(nativeCPPClass, caller); 47 | } 48 | 49 | @Override 50 | public Class getClass(Class arg0) { 51 | return (Class) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getClass(arg0); 52 | } 53 | 54 | @Override 55 | public double getDoubleFunction(SampleForTests caller) { 56 | return (double) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getDoubleFunction(caller); 57 | } 58 | 59 | @Override 60 | public float getFloatFunction() { 61 | return (float) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getFloatFunction(); 62 | } 63 | 64 | @Override 65 | public Object getNonPODDatatype(SampleForTests caller) { 66 | return (Object) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getNonPODDatatype(caller); 67 | } 68 | 69 | @Override 70 | public Throwable getThrowable(Throwable arg0) { 71 | return (Throwable) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_getThrowable(arg0); 72 | } 73 | 74 | @Override 75 | public long init(SampleForTests caller, String param) { 76 | return (long) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_init(caller, param); 77 | } 78 | 79 | @Override 80 | public void iterateAndDoSomethingWithStructB(long nativeCPPClass, SampleForTests caller) { 81 | GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_iterateAndDoSomethingWithStructB(nativeCPPClass, caller); 82 | } 83 | 84 | @Override 85 | public int method(long nativeCPPClass, SampleForTests caller) { 86 | return (int) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_method(nativeCPPClass, caller); 87 | } 88 | 89 | @Override 90 | public double methodOtherP0(long nativePtr, SampleForTests caller) { 91 | return (double) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_methodOtherP0(nativePtr, caller); 92 | } 93 | 94 | @Override 95 | public String returnAString(long nativeCPPClass, SampleForTests caller) { 96 | return (String) GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_returnAString(nativeCPPClass, caller); 97 | } 98 | 99 | @Override 100 | public void setNonPODDatatype(SampleForTests caller, Rect rect) { 101 | GEN_JNI.this_is_a_package_prefix_org_jni_1zero_samples_SampleForTests_setNonPODDatatype(caller, rect); 102 | } 103 | 104 | public static SampleForTests.Natives get() { 105 | if (GEN_JNI.TESTING_ENABLED) { 106 | if (testInstance != null) { 107 | return testInstance; 108 | } 109 | if (GEN_JNI.REQUIRE_MOCK) { 110 | throw new UnsupportedOperationException( 111 | "No mock found for the native implementation of SampleForTests.Natives. " 112 | + "The current configuration requires implementations be mocked."); 113 | } 114 | } 115 | NativeLibraryLoadedStatus.checkLoaded(); 116 | return new SampleForTestsJni(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /golden/testStubRegistration-Final-GEN_JNI.java.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | package org.jni_zero; 6 | 7 | // This file is autogenerated by 8 | // third_party/jni_zero/jni_registration_generator.py 9 | // Please do not change its content. 10 | 11 | public class GEN_JNI { 12 | public static final boolean TESTING_ENABLED = false; 13 | public static final boolean REQUIRE_MOCK = false; 14 | 15 | 16 | // Hashed name: Java_J_N_MxkAf2fl 17 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_bar(Object sample); 18 | // Hashed name: Java_J_N_Md6LK1o9 19 | public static native void org_jni_1zero_samples_SampleForAnnotationProcessor_foo(); 20 | // Hashed name: Java_J_N_M$4zYzAg 21 | public static native boolean org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange(); 22 | // Hashed name: Java_J_N_M4l0zJba 23 | public static native Class org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass(); 24 | // Hashed name: Java_J_N_MKH66mnL 25 | public static native Class[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses(); 26 | // Hashed name: Java_J_N_Mvc7ImXr 27 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject(); 28 | // Hashed name: Java_J_N_MocgXEXX 29 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects(); 30 | // Hashed name: Java_J_N_M3yd9YCr 31 | public static native String org_jni_1zero_samples_SampleForAnnotationProcessor_returnString(); 32 | // Hashed name: Java_J_N_M_qL1Hyw 33 | public static native String[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings(); 34 | // Hashed name: Java_J_N_MxvVNTxJ 35 | public static native Object org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct(); 36 | // Hashed name: Java_J_N_MJLr$5QK 37 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs(); 38 | // Hashed name: Java_J_N_MXcljN68 39 | public static native Throwable org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable(); 40 | // Hashed name: Java_J_N_M1FJFJy5 41 | public static native Throwable[] org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables(); 42 | // Hashed name: Java_J_N_MGrgOT84 43 | public static native String org_jni_1zero_samples_SampleForAnnotationProcessor_revString(String stringToReverse); 44 | // Hashed name: Java_J_N_MMtgBdva 45 | public static native Object[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs); 46 | // Hashed name: Java_J_N_MPVz5ily 47 | public static native String[] org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative(String[] strs); 48 | // Hashed name: Java_J_N_MHRNkgW8 49 | public static native int[] org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools); 50 | // Hashed name: Java_J_N_MEbp5PPc 51 | public static native void org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects); 52 | // Hashed name: Java_J_N_Muyus8yk 53 | public static native void org_jni_1zero_samples_TinySample2_test(); 54 | 55 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_addStructB(Object caller, Object b) { 56 | throw new RuntimeException("Stub - not implemented!"); 57 | } 58 | 59 | public static String[][] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes1(int[] a, Object[][] b) { 60 | throw new RuntimeException("Stub - not implemented!"); 61 | } 62 | 63 | public static int[] org_jni_1zero_samples_SampleProxyEdgeCases_arrayTypes2(int[] a, Throwable[][] b) { 64 | throw new RuntimeException("Stub - not implemented!"); 65 | } 66 | 67 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_foo_1_1weirdly_1_1escaped_1name1() { 68 | throw new RuntimeException("Stub - not implemented!"); 69 | } 70 | 71 | public static Object[] org_jni_1zero_samples_SampleProxyEdgeCases_genericsWithNestedClassArray(Object[] arg) { 72 | throw new RuntimeException("Stub - not implemented!"); 73 | } 74 | 75 | public static void org_jni_1zero_samples_SampleProxyEdgeCases_setBool(Object b, Object i) { 76 | throw new RuntimeException("Stub - not implemented!"); 77 | } 78 | 79 | public static boolean org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(Object sb) { 80 | throw new RuntimeException("Stub - not implemented!"); 81 | } 82 | 83 | public static int org_jni_1zero_samples_SampleProxyEdgeCases_setStringBuilder(int sb) { 84 | throw new RuntimeException("Stub - not implemented!"); 85 | } 86 | 87 | public static boolean org_jni_1zero_samples_TinySample_bar(int a) { 88 | throw new RuntimeException("Stub - not implemented!"); 89 | } 90 | 91 | public static void org_jni_1zero_samples_TinySample_foo() { 92 | throw new RuntimeException("Stub - not implemented!"); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /golden/testForTestingKept-SampleProxyEdgeCases_jni.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_generator.py 8 | // For 9 | // org/jni_zero/samples/SampleProxyEdgeCases 10 | 11 | #ifndef org_jni_zero_samples_SampleProxyEdgeCases_JNI 12 | #define org_jni_zero_samples_SampleProxyEdgeCases_JNI 13 | 14 | #include 15 | 16 | #include "third_party/jni_zero/jni_export.h" 17 | #include "third_party/jni_zero/jni_zero_helper.h" 18 | 19 | 20 | // Step 1: Forward declarations. 21 | 22 | JNI_ZERO_COMPONENT_BUILD_EXPORT extern const char 23 | kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases[]; 24 | const char kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases[] = 25 | "org/jni_zero/samples/SampleProxyEdgeCases"; 26 | // Leaking this jclass as we cannot use LazyInstance from some threads. 27 | JNI_ZERO_COMPONENT_BUILD_EXPORT std::atomic 28 | g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz(nullptr); 29 | #ifndef org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 30 | #define org_jni_1zero_samples_SampleProxyEdgeCases_clazz_defined 31 | inline jclass org_jni_1zero_samples_SampleProxyEdgeCases_clazz(JNIEnv* env) { 32 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleProxyEdgeCases, 33 | &g_org_jni_1zero_samples_SampleProxyEdgeCases_clazz); 34 | } 35 | #endif 36 | 37 | 38 | // Step 2: Constants (optional). 39 | 40 | 41 | // Step 3: Method stubs. 42 | static jint JNI_SampleProxyEdgeCases_AddStructB(JNIEnv* env, const 43 | base::android::JavaParamRef& caller, 44 | const base::android::JavaParamRef& b); 45 | 46 | JNI_BOUNDARY_EXPORT jint 47 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1addStructB( 48 | JNIEnv* env, 49 | jclass jcaller, 50 | jobject caller, 51 | jobject b) { 52 | return JNI_SampleProxyEdgeCases_AddStructB(env, base::android::JavaParamRef(env, caller), 53 | base::android::JavaParamRef(env, b)); 54 | } 55 | 56 | static base::android::ScopedJavaLocalRef JNI_SampleProxyEdgeCases_ArrayTypes1(JNIEnv* 57 | env, const base::android::JavaParamRef& a, 58 | const base::android::JavaParamRef& b); 59 | 60 | JNI_BOUNDARY_EXPORT jobjectArray 61 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1arrayTypes1( 62 | JNIEnv* env, 63 | jclass jcaller, 64 | jintArray a, 65 | jobjectArray b) { 66 | return JNI_SampleProxyEdgeCases_ArrayTypes1(env, base::android::JavaParamRef(env, a), 67 | base::android::JavaParamRef(env, b)).Release(); 68 | } 69 | 70 | static base::android::ScopedJavaLocalRef JNI_SampleProxyEdgeCases_ArrayTypes2(JNIEnv* 71 | env, const base::android::JavaParamRef& a, 72 | const base::android::JavaParamRef& b); 73 | 74 | JNI_BOUNDARY_EXPORT jintArray 75 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1arrayTypes2( 76 | JNIEnv* env, 77 | jclass jcaller, 78 | jintArray a, 79 | jobjectArray b) { 80 | return JNI_SampleProxyEdgeCases_ArrayTypes2(env, base::android::JavaParamRef(env, a), 81 | base::android::JavaParamRef(env, b)).Release(); 82 | } 83 | 84 | static void JNI_SampleProxyEdgeCases_FooForTest(JNIEnv* env); 85 | 86 | JNI_BOUNDARY_EXPORT void 87 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1fooForTest( 88 | JNIEnv* env, 89 | jclass jcaller) { 90 | return JNI_SampleProxyEdgeCases_FooForTest(env); 91 | } 92 | 93 | static void JNI_SampleProxyEdgeCases_FooForTesting(JNIEnv* env); 94 | 95 | JNI_BOUNDARY_EXPORT void 96 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1fooForTesting( 97 | JNIEnv* env, 98 | jclass jcaller) { 99 | return JNI_SampleProxyEdgeCases_FooForTesting(env); 100 | } 101 | 102 | static void JNI_SampleProxyEdgeCases_FooForTests(JNIEnv* env); 103 | 104 | JNI_BOUNDARY_EXPORT void 105 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1fooForTests( 106 | JNIEnv* env, 107 | jclass jcaller) { 108 | return JNI_SampleProxyEdgeCases_FooForTests(env); 109 | } 110 | 111 | static void JNI_SampleProxyEdgeCases_Foo__weirdly__escaped_name1(JNIEnv* env); 112 | 113 | JNI_BOUNDARY_EXPORT void 114 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1foo_11_11weirdly_11_11escaped_11name1( 115 | JNIEnv* env, 116 | jclass jcaller) { 117 | return JNI_SampleProxyEdgeCases_Foo__weirdly__escaped_name1(env); 118 | } 119 | 120 | static base::android::ScopedJavaLocalRef 121 | JNI_SampleProxyEdgeCases_GenericsWithNestedClassArray(JNIEnv* env, const 122 | base::android::JavaParamRef& arg); 123 | 124 | JNI_BOUNDARY_EXPORT jobjectArray 125 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1genericsWithNestedClassArray( 126 | JNIEnv* env, 127 | jclass jcaller, 128 | jobjectArray arg) { 129 | return JNI_SampleProxyEdgeCases_GenericsWithNestedClassArray(env, 130 | base::android::JavaParamRef(env, arg)).Release(); 131 | } 132 | 133 | static void JNI_SampleProxyEdgeCases_SetBool(JNIEnv* env, const 134 | base::android::JavaParamRef& b, 135 | const base::android::JavaParamRef& i); 136 | 137 | JNI_BOUNDARY_EXPORT void 138 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1setBool( 139 | JNIEnv* env, 140 | jclass jcaller, 141 | jobject b, 142 | jobject i) { 143 | return JNI_SampleProxyEdgeCases_SetBool(env, base::android::JavaParamRef(env, b), 144 | base::android::JavaParamRef(env, i)); 145 | } 146 | 147 | static jboolean JNI_SampleProxyEdgeCases_SetStringBuilder(JNIEnv* env, const 148 | base::android::JavaParamRef& sb); 149 | 150 | JNI_BOUNDARY_EXPORT jboolean 151 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1setStringBuilder( 152 | JNIEnv* env, 153 | jclass jcaller, 154 | jobject sb) { 155 | return JNI_SampleProxyEdgeCases_SetStringBuilder(env, base::android::JavaParamRef(env, 156 | sb)); 157 | } 158 | 159 | static jint JNI_SampleProxyEdgeCases_SetStringBuilder(JNIEnv* env, jint sb); 160 | 161 | JNI_BOUNDARY_EXPORT jint 162 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleProxyEdgeCases_1setStringBuilder( 163 | JNIEnv* env, 164 | jclass jcaller, 165 | jint sb) { 166 | return JNI_SampleProxyEdgeCases_SetStringBuilder(env, sb); 167 | } 168 | 169 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeInstanceMethod( 170 | JNIEnv* env, 171 | jobject jcaller, 172 | jlong nativeInstance) { 173 | Instance* native = reinterpret_cast(nativeInstance); 174 | CHECK_NATIVE_PTR(env, jcaller, native, "InstanceMethod"); 175 | return native->InstanceMethod(env, base::android::JavaParamRef(env, jcaller)); 176 | } 177 | 178 | static void JNI_SampleProxyEdgeCases_StaticMethod(JNIEnv* env); 179 | 180 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleProxyEdgeCases_nativeStaticMethod( 181 | JNIEnv* env, 182 | jclass jcaller) { 183 | return JNI_SampleProxyEdgeCases_StaticMethod(env); 184 | } 185 | 186 | 187 | #endif // org_jni_zero_samples_SampleProxyEdgeCases_JNI 188 | -------------------------------------------------------------------------------- /golden/testPackagePrefixWithManualRegistrationWithProxyHash-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | 25 | // Step 2: Forward declarations (methods). 26 | 27 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_MYJQG2tV( 28 | JNIEnv* env, 29 | jclass jcaller, 30 | jobject sample); 31 | JNI_BOUNDARY_EXPORT void Java_this_is_a_package_prefix_J_N_McJTRQ4Z( 32 | JNIEnv* env, 33 | jclass jcaller); 34 | JNI_BOUNDARY_EXPORT jboolean Java_this_is_a_package_prefix_J_N_MmNHlL_000245( 35 | JNIEnv* env, 36 | jclass jcaller); 37 | JNI_BOUNDARY_EXPORT jclass Java_this_is_a_package_prefix_J_N_MF_00024OWG3c( 38 | JNIEnv* env, 39 | jclass jcaller); 40 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MI7vvSXV( 41 | JNIEnv* env, 42 | jclass jcaller); 43 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_MQV6X3Sz( 44 | JNIEnv* env, 45 | jclass jcaller); 46 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MntwrJje( 47 | JNIEnv* env, 48 | jclass jcaller); 49 | JNI_BOUNDARY_EXPORT jstring Java_this_is_a_package_prefix_J_N_MlMyYIKk( 50 | JNIEnv* env, 51 | jclass jcaller); 52 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MQ631WBV( 53 | JNIEnv* env, 54 | jclass jcaller); 55 | JNI_BOUNDARY_EXPORT jobject Java_this_is_a_package_prefix_J_N_M3fBDe9t( 56 | JNIEnv* env, 57 | jclass jcaller); 58 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MKFA0R1H( 59 | JNIEnv* env, 60 | jclass jcaller); 61 | JNI_BOUNDARY_EXPORT jthrowable Java_this_is_a_package_prefix_J_N_M2vFJGXF( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MWuNxqXZ( 65 | JNIEnv* env, 66 | jclass jcaller); 67 | JNI_BOUNDARY_EXPORT jstring Java_this_is_a_package_prefix_J_N_MMV8vkvD( 68 | JNIEnv* env, 69 | jclass jcaller, 70 | jstring stringToReverse); 71 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MbvaHriB( 72 | JNIEnv* env, 73 | jclass jcaller, 74 | jobjectArray strs); 75 | JNI_BOUNDARY_EXPORT jobjectArray Java_this_is_a_package_prefix_J_N_MBko4EWo( 76 | JNIEnv* env, 77 | jclass jcaller, 78 | jobjectArray strs); 79 | JNI_BOUNDARY_EXPORT jintArray Java_this_is_a_package_prefix_J_N_MTkl34Bi( 80 | JNIEnv* env, 81 | jclass jcaller, 82 | jint zint, 83 | jintArray ints, 84 | jlong zlong, 85 | jlongArray longs, 86 | jshort zshort, 87 | jshortArray shorts, 88 | jchar zchar, 89 | jcharArray chars, 90 | jbyte zbyte, 91 | jbyteArray bytes, 92 | jdouble zdouble, 93 | jdoubleArray doubles, 94 | jfloat zfloat, 95 | jfloatArray floats, 96 | jboolean zbool, 97 | jbooleanArray bools); 98 | JNI_BOUNDARY_EXPORT void Java_this_is_a_package_prefix_J_N_Mooq_13kI( 99 | JNIEnv* env, 100 | jclass jcaller, 101 | jclass clazz, 102 | jobjectArray classes, 103 | jthrowable throwable, 104 | jobjectArray throwables, 105 | jstring string, 106 | jobjectArray strings, 107 | jobject tStruct, 108 | jobjectArray structs, 109 | jobject obj, 110 | jobjectArray objects); 111 | 112 | 113 | // Step 3: Method declarations. 114 | 115 | 116 | static const JNINativeMethod kMethods_this_is_a_package_prefix_J_N[] = { 117 | { "MYJQG2tV", "(Ljava/lang/Object;)Ljava/lang/Object;", 118 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MYJQG2tV) }, 119 | { "McJTRQ4Z", "()V", reinterpret_cast(Java_this_is_a_package_prefix_J_N_McJTRQ4Z) }, 120 | { "MmNHlL$5", "()Z", reinterpret_cast(Java_this_is_a_package_prefix_J_N_MmNHlL_000245) }, 121 | { "MF$OWG3c", "()Ljava/lang/Class;", 122 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MF_00024OWG3c) }, 123 | { "MI7vvSXV", "()[Ljava/lang/Class;", 124 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MI7vvSXV) }, 125 | { "MQV6X3Sz", "()Ljava/lang/Object;", 126 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MQV6X3Sz) }, 127 | { "MntwrJje", "()[Ljava/lang/Object;", 128 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MntwrJje) }, 129 | { "MlMyYIKk", "()Ljava/lang/String;", 130 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MlMyYIKk) }, 131 | { "MQ631WBV", "()[Ljava/lang/String;", 132 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MQ631WBV) }, 133 | { "M3fBDe9t", "()Ljava/lang/Object;", 134 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_M3fBDe9t) }, 135 | { "MKFA0R1H", "()[Ljava/lang/Object;", 136 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MKFA0R1H) }, 137 | { "M2vFJGXF", "()Ljava/lang/Throwable;", 138 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_M2vFJGXF) }, 139 | { "MWuNxqXZ", "()[Ljava/lang/Throwable;", 140 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MWuNxqXZ) }, 141 | { "MMV8vkvD", "(Ljava/lang/String;)Ljava/lang/String;", 142 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MMV8vkvD) }, 143 | { "MbvaHriB", "([Ljava/lang/Object;)[Ljava/lang/Object;", 144 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MbvaHriB) }, 145 | { "MBko4EWo", "([Ljava/lang/String;)[Ljava/lang/String;", 146 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MBko4EWo) }, 147 | { "MTkl34Bi", "(I[IJ[JS[SC[CB[BD[DF[FZ[Z)[I", 148 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_MTkl34Bi) }, 149 | { "Mooq_3kI", 150 | "(Ljava/lang/Class;[Ljava/lang/Class;Ljava/lang/Throwable;[Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)V", 151 | reinterpret_cast(Java_this_is_a_package_prefix_J_N_Mooq_13kI) }, 152 | 153 | }; 154 | 155 | namespace { 156 | 157 | JNI_ZERO_COMPONENT_BUILD_EXPORT bool RegisterNative_this_is_a_package_prefix_J_N(JNIEnv* env) { 158 | const int number_of_methods = std::size(kMethods_this_is_a_package_prefix_J_N); 159 | 160 | base::android::ScopedJavaLocalRef native_clazz = 161 | base::android::GetClass(env, "this/is/a/package/prefix/J/N"); 162 | if (env->RegisterNatives( 163 | native_clazz.obj(), 164 | kMethods_this_is_a_package_prefix_J_N, 165 | number_of_methods) < 0) { 166 | 167 | jni_zero::HandleRegistrationError(env, native_clazz.obj(), __FILE__); 168 | return false; 169 | } 170 | 171 | return true; 172 | } 173 | 174 | } // namespace 175 | 176 | 177 | // Step 4: Registration function. 178 | 179 | namespace { 180 | 181 | bool RegisterNatives(JNIEnv* env) { 182 | // Register natives in a proxy. 183 | if (!RegisterNative_this_is_a_package_prefix_J_N(env)) { 184 | return false; 185 | } 186 | 187 | 188 | return true; 189 | } 190 | 191 | } // namespace 192 | 193 | #endif // TEMP_DIR_HEADER_ 194 | -------------------------------------------------------------------------------- /golden/testEndToEndManualRegistration_NonProxy-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | extern const char kClassPath_org_jni_1zero_samples_SampleNonProxy[]; 25 | extern std::atomic g_org_jni_1zero_samples_SampleNonProxy_clazz; 26 | #ifndef org_jni_1zero_samples_SampleNonProxy_clazz_defined 27 | #define org_jni_1zero_samples_SampleNonProxy_clazz_defined 28 | inline jclass org_jni_1zero_samples_SampleNonProxy_clazz(JNIEnv* env) { 29 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleNonProxy, 30 | &g_org_jni_1zero_samples_SampleNonProxy_clazz); 31 | } 32 | #endif 33 | 34 | 35 | // Step 2: Forward declarations (methods). 36 | 37 | JNI_BOUNDARY_EXPORT jlong Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmark( 38 | JNIEnv* env, 39 | jobject jcaller, 40 | jint nativeChromeBrowserProvider, 41 | jstring url, 42 | jstring title, 43 | jboolean isFolder, 44 | jlong parentId); 45 | JNI_BOUNDARY_EXPORT jlong Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmarkFromAPI( 46 | JNIEnv* env, 47 | jobject jcaller, 48 | jint nativeChromeBrowserProvider, 49 | jstring url, 50 | jobject created, 51 | jobject isBookmark, 52 | jobject date, 53 | jbyteArray favicon, 54 | jstring title, 55 | jobject visits); 56 | JNI_BOUNDARY_EXPORT void 57 | Java_org_jni_1zero_samples_SampleNonProxy_nativeCreateHistoricalTabFromState( 58 | JNIEnv* env, 59 | jclass jcaller, 60 | jbyteArray state, 61 | jint tabIndex); 62 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeDestroy( 63 | JNIEnv* env, 64 | jobject jcaller, 65 | jint nativeChromeBrowserProvider); 66 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeFindAll( 67 | JNIEnv* env, 68 | jobject jcaller, 69 | jstring find); 70 | JNI_BOUNDARY_EXPORT jobjectArray 71 | Java_org_jni_1zero_samples_SampleNonProxy_nativeGetAutofillProfileGUIDs( 72 | JNIEnv* env, 73 | jclass jcaller); 74 | JNI_BOUNDARY_EXPORT jstring Java_org_jni_1zero_samples_SampleNonProxy_nativeGetDomainAndRegistry( 75 | JNIEnv* env, 76 | jclass jcaller, 77 | jstring url); 78 | JNI_BOUNDARY_EXPORT jobject Java_org_jni_1zero_samples_SampleNonProxy_nativeGetInnerClass( 79 | JNIEnv* env, 80 | jclass jcaller); 81 | JNI_BOUNDARY_EXPORT jbyteArray Java_org_jni_1zero_samples_SampleNonProxy_nativeGetStateAsByteArray( 82 | JNIEnv* env, 83 | jobject jcaller, 84 | jobject view); 85 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeGotOrientation( 86 | JNIEnv* env, 87 | jobject jcaller, 88 | jint nativeDataFetcherImplAndroid, 89 | jdouble alpha, 90 | jdouble beta, 91 | jdouble gamma); 92 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeInit( 93 | JNIEnv* env, 94 | jobject jcaller); 95 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeInit( 96 | JNIEnv* env, 97 | jobject jcaller); 98 | JNI_BOUNDARY_EXPORT jthrowable 99 | Java_org_jni_1zero_samples_SampleNonProxy_nativeMessWithJavaException( 100 | JNIEnv* env, 101 | jclass jcaller, 102 | jthrowable e); 103 | JNI_BOUNDARY_EXPORT jobject Java_org_jni_1zero_samples_SampleNonProxy_nativeQueryBitmap( 104 | JNIEnv* env, 105 | jobject jcaller, 106 | jint nativeChromeBrowserProvider, 107 | jobjectArray projection, 108 | jstring selection, 109 | jobjectArray selectionArgs, 110 | jstring sortOrder); 111 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeSetRecognitionResults( 112 | JNIEnv* env, 113 | jobject jcaller, 114 | jint sessionId, 115 | jobjectArray results); 116 | 117 | 118 | // Step 3: Method declarations. 119 | 120 | static const JNINativeMethod kMethods_org_jni_1zero_samples_SampleNonProxy[] = { 121 | { "nativeAddBookmark", "(ILjava/lang/String;Ljava/lang/String;ZJ)J", 122 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmark) }, 123 | { "nativeAddBookmarkFromAPI", 124 | "(ILjava/lang/String;Ljava/lang/Long;Ljava/lang/Boolean;Ljava/lang/Long;[BLjava/lang/String;Ljava/lang/Integer;)J", 125 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmarkFromAPI) 126 | }, 127 | { "nativeCreateHistoricalTabFromState", "([BI)V", 128 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeCreateHistoricalTabFromState) 129 | }, 130 | { "nativeDestroy", "(I)V", 131 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeDestroy) }, 132 | { "nativeFindAll", "(Ljava/lang/String;)I", 133 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeFindAll) }, 134 | { "nativeGetAutofillProfileGUIDs", "()[Ljava/lang/String;", 135 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeGetAutofillProfileGUIDs) 136 | }, 137 | { "nativeGetDomainAndRegistry", "(Ljava/lang/String;)Ljava/lang/String;", 138 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeGetDomainAndRegistry) 139 | }, 140 | { "nativeGetInnerClass", "()Lorg/jni_zero/samples/SampleNonProxy$OnFrameAvailableListener;", 141 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeGetInnerClass) }, 142 | { "nativeGetStateAsByteArray", "(Landroid/view/View;)[B", 143 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeGetStateAsByteArray) 144 | }, 145 | { "nativeGotOrientation", "(IDDD)V", 146 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeGotOrientation) }, 147 | { "nativeInit", "()I", 148 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeInit) }, 149 | { "nativeInit", "()I", 150 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeInit) }, 151 | { "nativeMessWithJavaException", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;", 152 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeMessWithJavaException) 153 | }, 154 | { "nativeQueryBitmap", 155 | "(I[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/graphics/Bitmap;", 156 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeQueryBitmap) }, 157 | { "nativeSetRecognitionResults", "(I[Ljava/lang/String;)V", 158 | reinterpret_cast(Java_org_jni_1zero_samples_SampleNonProxy_nativeSetRecognitionResults) 159 | }, 160 | }; 161 | 162 | 163 | JNI_ZERO_COMPONENT_BUILD_EXPORT bool RegisterNative_org_jni_1zero_samples_SampleNonProxy(JNIEnv* 164 | env) { 165 | const int kMethods_org_jni_1zero_samples_SampleNonProxySize = 166 | std::size(kMethods_org_jni_1zero_samples_SampleNonProxy); 167 | if (env->RegisterNatives( 168 | org_jni_1zero_samples_SampleNonProxy_clazz(env), 169 | kMethods_org_jni_1zero_samples_SampleNonProxy, 170 | kMethods_org_jni_1zero_samples_SampleNonProxySize) < 0) { 171 | jni_zero::HandleRegistrationError(env, 172 | org_jni_1zero_samples_SampleNonProxy_clazz(env), 173 | __FILE__); 174 | return false; 175 | } 176 | 177 | return true; 178 | } 179 | 180 | 181 | // Step 4: Registration function. 182 | 183 | namespace { 184 | 185 | bool RegisterNatives(JNIEnv* env) { 186 | if (!RegisterNative_org_jni_1zero_samples_SampleNonProxy(env)) 187 | return false; 188 | 189 | return true; 190 | } 191 | 192 | } // namespace 193 | 194 | #endif // TEMP_DIR_HEADER_ 195 | -------------------------------------------------------------------------------- /jni_zero.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright 2023 The Chromium Authors 3 | # Use of this source code is governed by a BSD-style license that can be 4 | # found in the LICENSE file. 5 | """A bindings generator for JNI on Android.""" 6 | 7 | import argparse 8 | import os 9 | import shutil 10 | import sys 11 | 12 | import jni_generator 13 | import jni_registration_generator 14 | 15 | # jni_zero.py requires Python 3.8+. 16 | _MIN_PYTHON_MINOR = 8 17 | 18 | 19 | def _add_io_args(parser, *, is_final=False, is_javap=False): 20 | group = parser.add_argument_group(title='Inputs & Outputs') 21 | if is_final: 22 | group.add_argument('--java-sources-file', 23 | required=True, 24 | help='A file which contains Java file paths, derived ' 25 | 'from java deps metadata.') 26 | group.add_argument('--native-sources-file', 27 | help='A file which contains Java file paths, derived ' 28 | 'from native deps onto generate_jni.') 29 | group.add_argument('--depfile', 30 | help='Path to depfile (for use with ninja build system)') 31 | else: 32 | if is_javap: 33 | group.add_argument( 34 | '--jar-file', 35 | help='Extract the list of input files from a specified jar file. ' 36 | 'Uses javap to extract the methods from a pre-compiled class.') 37 | 38 | help_text = 'Paths within the .jar' 39 | else: 40 | help_text = 'Paths to .java files to parse.' 41 | group.add_argument('--input-file', 42 | action='append', 43 | required=True, 44 | dest='input_files', 45 | help=help_text) 46 | group.add_argument('--output-name', 47 | action='append', 48 | required=True, 49 | dest='output_names', 50 | help='Output filenames within output directory.') 51 | group.add_argument('--output-dir', 52 | required=True, 53 | help='Output directory. ' 54 | 'Existing .h files in this directory will be assumed ' 55 | 'stale and removed.') 56 | 57 | group.add_argument('--header-path', help='Path to output header file.') 58 | 59 | if is_javap: 60 | group.add_argument('--javap', help='The path to javap command.') 61 | else: 62 | group.add_argument( 63 | '--srcjar-path', 64 | help='Path to output srcjar for GEN_JNI.java (and J/N.java if proxy' 65 | ' hash is enabled).') 66 | 67 | 68 | def _add_codegen_args(parser, *, is_final=False, is_javap=False): 69 | group = parser.add_argument_group(title='Codegen Options') 70 | group.add_argument( 71 | '--module-name', 72 | help='Only look at natives annotated with a specific module name.') 73 | if is_final: 74 | group.add_argument( 75 | '--add-stubs-for-missing-native', 76 | action='store_true', 77 | help='Adds stub methods for any --java-sources-file which are missing ' 78 | 'from --native-sources-files. If not passed, we will assert that none ' 79 | 'of these exist.') 80 | group.add_argument( 81 | '--remove-uncalled-methods', 82 | action='store_true', 83 | help='Removes --native-sources-files which are not in ' 84 | '--java-sources-file. If not passed, we will assert that none of these ' 85 | 'exist.') 86 | group.add_argument( 87 | '--namespace', 88 | help='Native namespace to wrap the registration functions into.') 89 | # TODO(crbug.com/898261) hook these flags up to the build config to enable 90 | # mocking in instrumentation tests 91 | group.add_argument( 92 | '--enable-proxy-mocks', 93 | default=False, 94 | action='store_true', 95 | help='Allows proxy native impls to be mocked through Java.') 96 | group.add_argument( 97 | '--require-mocks', 98 | default=False, 99 | action='store_true', 100 | help='Requires all used native implementations to have a mock set when ' 101 | 'called. Otherwise an exception will be thrown.') 102 | group.add_argument('--manual-jni-registration', 103 | action='store_true', 104 | help='Generate a call to RegisterNatives()') 105 | group.add_argument('--include-test-only', 106 | action='store_true', 107 | help='Whether to maintain ForTesting JNI methods.') 108 | else: 109 | group.add_argument('--extra-include', 110 | action='append', 111 | dest='extra_includes', 112 | help='Header file to #include in the generated header.') 113 | group.add_argument( 114 | '--split-name', 115 | help='Split name that the Java classes should be loaded from.') 116 | 117 | if is_javap: 118 | group.add_argument('--unchecked-exceptions', 119 | action='store_true', 120 | help='Do not check that no exceptions were thrown.') 121 | else: 122 | group.add_argument( 123 | '--use-proxy-hash', 124 | action='store_true', 125 | help='Enables hashing of the native declaration for methods in ' 126 | 'a @NativeMethods interface') 127 | group.add_argument('--enable-jni-multiplexing', 128 | action='store_true', 129 | help='Enables JNI multiplexing for Java native methods') 130 | group.add_argument( 131 | '--package-prefix', 132 | help='Adds a prefix to the classes fully qualified-name. Effectively ' 133 | 'changing a class name from foo.bar -> prefix.foo.bar') 134 | 135 | if not is_final: 136 | if is_javap: 137 | instead_msg = 'instead of the javap class name.' 138 | else: 139 | instead_msg = 'when there is no @JNINamespace set' 140 | 141 | group.add_argument('--namespace', 142 | help='Uses as a namespace in the generated header ' + 143 | instead_msg) 144 | 145 | 146 | def _maybe_relaunch_with_newer_python(): 147 | # If "python3" is < python3.8, but a newer version is available, then use 148 | # that. 149 | py_version = sys.version_info 150 | if py_version < (3, _MIN_PYTHON_MINOR): 151 | if os.environ.get('JNI_ZERO_RELAUNCHED'): 152 | sys.stderr.write('JNI_ZERO_RELAUNCHED failure.\n') 153 | sys.exit(1) 154 | for i in range(_MIN_PYTHON_MINOR, 30): 155 | name = f'python3.{i}' 156 | if shutil.which(name): 157 | cmd = [name] + sys.argv 158 | env = os.environ.copy() 159 | env['JNI_ZERO_RELAUNCHED'] = '1' 160 | os.execvpe(cmd[0], cmd, env) 161 | sys.stderr.write( 162 | f'jni_zero requires Python 3.{_MIN_PYTHON_MINOR} or greater.\n') 163 | sys.exit(1) 164 | 165 | 166 | def _add_args(parser, *, is_final=False, is_javap=False): 167 | _add_io_args(parser, is_final=is_final, is_javap=is_javap) 168 | _add_codegen_args(parser, is_final=is_final, is_javap=is_javap) 169 | 170 | 171 | def main(): 172 | parser = argparse.ArgumentParser(description=__doc__) 173 | subparsers = parser.add_subparsers(required=True) 174 | 175 | subp = subparsers.add_parser( 176 | 'from-source', help='Generates files for a set of .java sources.') 177 | _add_args(subp) 178 | subp.set_defaults(func=jni_generator.GenerateFromSource) 179 | 180 | subp = subparsers.add_parser( 181 | 'from-jar', help='Generates files from a .jar of .class files.') 182 | _add_args(subp, is_javap=True) 183 | subp.set_defaults(func=jni_generator.GenerateFromJar) 184 | 185 | subp = subparsers.add_parser( 186 | 'generate-final', 187 | help='Generates files that require knowledge of all intermediates.') 188 | _add_args(subp, is_final=True) 189 | subp.set_defaults(func=jni_registration_generator.main) 190 | 191 | # Default to showing full help text when no args are passed. 192 | if len(sys.argv) == 1: 193 | parser.print_help() 194 | elif len(sys.argv) == 2 and sys.argv[1] in subparsers.choices: 195 | parser.parse_args(sys.argv[1:] + ['-h']) 196 | else: 197 | args = parser.parse_args() 198 | args.func(parser, args) 199 | 200 | 201 | if __name__ == '__main__': 202 | _maybe_relaunch_with_newer_python() 203 | main() 204 | -------------------------------------------------------------------------------- /golden/testEndToEndManualRegistration-Registration.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_registration_generator.py 8 | // Please do not change its content. 9 | 10 | #ifndef TEMP_DIR_HEADER_ 11 | #define TEMP_DIR_HEADER_ 12 | 13 | #include 14 | 15 | #include 16 | 17 | #include "third_party/jni_zero/jni_export.h" 18 | #include "third_party/jni_zero/jni_int_wrapper.h" 19 | #include "third_party/jni_zero/jni_zero_helper.h" 20 | 21 | 22 | // Step 1: Forward declarations (classes). 23 | 24 | 25 | // Step 2: Forward declarations (methods). 26 | 27 | JNI_BOUNDARY_EXPORT jobject 28 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1bar( 29 | JNIEnv* env, 30 | jclass jcaller, 31 | jobject sample); 32 | JNI_BOUNDARY_EXPORT void 33 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1foo( 34 | JNIEnv* env, 35 | jclass jcaller); 36 | JNI_BOUNDARY_EXPORT jboolean 37 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1hasPhalange( 38 | JNIEnv* env, 39 | jclass jcaller); 40 | JNI_BOUNDARY_EXPORT jclass 41 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnClass( 42 | JNIEnv* env, 43 | jclass jcaller); 44 | JNI_BOUNDARY_EXPORT jobjectArray 45 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnClasses( 46 | JNIEnv* env, 47 | jclass jcaller); 48 | JNI_BOUNDARY_EXPORT jobject 49 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnObject( 50 | JNIEnv* env, 51 | jclass jcaller); 52 | JNI_BOUNDARY_EXPORT jobjectArray 53 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnObjects( 54 | JNIEnv* env, 55 | jclass jcaller); 56 | JNI_BOUNDARY_EXPORT jstring 57 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnString( 58 | JNIEnv* env, 59 | jclass jcaller); 60 | JNI_BOUNDARY_EXPORT jobjectArray 61 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStrings( 62 | JNIEnv* env, 63 | jclass jcaller); 64 | JNI_BOUNDARY_EXPORT jobject 65 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStruct( 66 | JNIEnv* env, 67 | jclass jcaller); 68 | JNI_BOUNDARY_EXPORT jobjectArray 69 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStructs( 70 | JNIEnv* env, 71 | jclass jcaller); 72 | JNI_BOUNDARY_EXPORT jthrowable 73 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnThrowable( 74 | JNIEnv* env, 75 | jclass jcaller); 76 | JNI_BOUNDARY_EXPORT jobjectArray 77 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnThrowables( 78 | JNIEnv* env, 79 | jclass jcaller); 80 | JNI_BOUNDARY_EXPORT jstring 81 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1revString( 82 | JNIEnv* env, 83 | jclass jcaller, 84 | jstring stringToReverse); 85 | JNI_BOUNDARY_EXPORT jobjectArray 86 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1sendSamplesToNative( 87 | JNIEnv* env, 88 | jclass jcaller, 89 | jobjectArray strs); 90 | JNI_BOUNDARY_EXPORT jobjectArray 91 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1sendToNative( 92 | JNIEnv* env, 93 | jclass jcaller, 94 | jobjectArray strs); 95 | JNI_BOUNDARY_EXPORT jintArray 96 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1testAllPrimitives( 97 | JNIEnv* env, 98 | jclass jcaller, 99 | jint zint, 100 | jintArray ints, 101 | jlong zlong, 102 | jlongArray longs, 103 | jshort zshort, 104 | jshortArray shorts, 105 | jchar zchar, 106 | jcharArray chars, 107 | jbyte zbyte, 108 | jbyteArray bytes, 109 | jdouble zdouble, 110 | jdoubleArray doubles, 111 | jfloat zfloat, 112 | jfloatArray floats, 113 | jboolean zbool, 114 | jbooleanArray bools); 115 | JNI_BOUNDARY_EXPORT void 116 | Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1testSpecialTypes( 117 | JNIEnv* env, 118 | jclass jcaller, 119 | jclass clazz, 120 | jobjectArray classes, 121 | jthrowable throwable, 122 | jobjectArray throwables, 123 | jstring string, 124 | jobjectArray strings, 125 | jobject tStruct, 126 | jobjectArray structs, 127 | jobject obj, 128 | jobjectArray objects); 129 | 130 | 131 | // Step 3: Method declarations. 132 | 133 | 134 | static const JNINativeMethod kMethods_org_jni_1zero_GEN_1JNI[] = { 135 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_bar", 136 | "(Ljava/lang/Object;)Ljava/lang/Object;", 137 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1bar) 138 | }, 139 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_foo", "()V", 140 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1foo) 141 | }, 142 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_hasPhalange", "()Z", 143 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1hasPhalange) 144 | }, 145 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnClass", "()Ljava/lang/Class;", 146 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnClass) 147 | }, 148 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnClasses", "()[Ljava/lang/Class;", 149 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnClasses) 150 | }, 151 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnObject", "()Ljava/lang/Object;", 152 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnObject) 153 | }, 154 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnObjects", "()[Ljava/lang/Object;", 155 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnObjects) 156 | }, 157 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnString", "()Ljava/lang/String;", 158 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnString) 159 | }, 160 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnStrings", "()[Ljava/lang/String;", 161 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStrings) 162 | }, 163 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnStruct", "()Ljava/lang/Object;", 164 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStruct) 165 | }, 166 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnStructs", "()[Ljava/lang/Object;", 167 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnStructs) 168 | }, 169 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowable", 170 | "()Ljava/lang/Throwable;", 171 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnThrowable) 172 | }, 173 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_returnThrowables", 174 | "()[Ljava/lang/Throwable;", 175 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1returnThrowables) 176 | }, 177 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_revString", 178 | "(Ljava/lang/String;)Ljava/lang/String;", 179 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1revString) 180 | }, 181 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_sendSamplesToNative", 182 | "([Ljava/lang/Object;)[Ljava/lang/Object;", 183 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1sendSamplesToNative) 184 | }, 185 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_sendToNative", 186 | "([Ljava/lang/String;)[Ljava/lang/String;", 187 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1sendToNative) 188 | }, 189 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_testAllPrimitives", 190 | "(I[IJ[JS[SC[CB[BD[DF[FZ[Z)[I", 191 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1testAllPrimitives) 192 | }, 193 | { "org_jni_1zero_samples_SampleForAnnotationProcessor_testSpecialTypes", 194 | "(Ljava/lang/Class;[Ljava/lang/Class;Ljava/lang/Throwable;[Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)V", 195 | reinterpret_cast(Java_org_jni_1zero_GEN_1JNI_org_1jni_11zero_1samples_1SampleForAnnotationProcessor_1testSpecialTypes) 196 | }, 197 | 198 | }; 199 | 200 | namespace { 201 | 202 | JNI_ZERO_COMPONENT_BUILD_EXPORT bool RegisterNative_org_jni_1zero_GEN_1JNI(JNIEnv* env) { 203 | const int number_of_methods = std::size(kMethods_org_jni_1zero_GEN_1JNI); 204 | 205 | base::android::ScopedJavaLocalRef native_clazz = 206 | base::android::GetClass(env, "org/jni_zero/GEN_JNI"); 207 | if (env->RegisterNatives( 208 | native_clazz.obj(), 209 | kMethods_org_jni_1zero_GEN_1JNI, 210 | number_of_methods) < 0) { 211 | 212 | jni_zero::HandleRegistrationError(env, native_clazz.obj(), __FILE__); 213 | return false; 214 | } 215 | 216 | return true; 217 | } 218 | 219 | } // namespace 220 | 221 | 222 | // Step 4: Registration function. 223 | 224 | namespace { 225 | 226 | bool RegisterNatives(JNIEnv* env) { 227 | // Register natives in a proxy. 228 | if (!RegisterNative_org_jni_1zero_GEN_1JNI(env)) { 229 | return false; 230 | } 231 | 232 | 233 | return true; 234 | } 235 | 236 | } // namespace 237 | 238 | #endif // TEMP_DIR_HEADER_ 239 | -------------------------------------------------------------------------------- /golden/testNonProxy-SampleNonProxy_jni.h.golden: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | 6 | // This file is autogenerated by 7 | // third_party/jni_zero/jni_generator.py 8 | // For 9 | // org/jni_zero/samples/SampleNonProxy 10 | 11 | #ifndef org_jni_zero_samples_SampleNonProxy_JNI 12 | #define org_jni_zero_samples_SampleNonProxy_JNI 13 | 14 | #include 15 | 16 | #include "third_party/jni_zero/jni_export.h" 17 | #include "third_party/jni_zero/jni_zero_helper.h" 18 | 19 | 20 | // Step 1: Forward declarations. 21 | 22 | JNI_ZERO_COMPONENT_BUILD_EXPORT extern const char kClassPath_org_jni_1zero_samples_SampleNonProxy[]; 23 | const char kClassPath_org_jni_1zero_samples_SampleNonProxy[] = 24 | "org/jni_zero/samples/SampleNonProxy"; 25 | // Leaking this jclass as we cannot use LazyInstance from some threads. 26 | JNI_ZERO_COMPONENT_BUILD_EXPORT std::atomic 27 | g_org_jni_1zero_samples_SampleNonProxy_clazz(nullptr); 28 | #ifndef org_jni_1zero_samples_SampleNonProxy_clazz_defined 29 | #define org_jni_1zero_samples_SampleNonProxy_clazz_defined 30 | inline jclass org_jni_1zero_samples_SampleNonProxy_clazz(JNIEnv* env) { 31 | return base::android::LazyGetClass(env, kClassPath_org_jni_1zero_samples_SampleNonProxy, 32 | &g_org_jni_1zero_samples_SampleNonProxy_clazz); 33 | } 34 | #endif 35 | 36 | 37 | // Step 2: Constants (optional). 38 | 39 | 40 | // Step 3: Method stubs. 41 | static jlong JNI_SampleNonProxy_AddBookmark(JNIEnv* env, const base::android::JavaParamRef& 42 | jcaller, 43 | jint nativeChromeBrowserProvider, 44 | const base::android::JavaParamRef& url, 45 | const base::android::JavaParamRef& title, 46 | jboolean isFolder, 47 | jlong parentId); 48 | 49 | JNI_BOUNDARY_EXPORT jlong Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmark( 50 | JNIEnv* env, 51 | jobject jcaller, 52 | jint nativeChromeBrowserProvider, 53 | jstring url, 54 | jstring title, 55 | jboolean isFolder, 56 | jlong parentId) { 57 | return JNI_SampleNonProxy_AddBookmark(env, base::android::JavaParamRef(env, jcaller), 58 | nativeChromeBrowserProvider, base::android::JavaParamRef(env, url), 59 | base::android::JavaParamRef(env, title), isFolder, parentId); 60 | } 61 | 62 | static jlong JNI_SampleNonProxy_AddBookmarkFromAPI(JNIEnv* env, const 63 | base::android::JavaParamRef& jcaller, 64 | jint nativeChromeBrowserProvider, 65 | const base::android::JavaParamRef& url, 66 | const base::android::JavaParamRef& created, 67 | const base::android::JavaParamRef& isBookmark, 68 | const base::android::JavaParamRef& date, 69 | const base::android::JavaParamRef& favicon, 70 | const base::android::JavaParamRef& title, 71 | const base::android::JavaParamRef& visits); 72 | 73 | JNI_BOUNDARY_EXPORT jlong Java_org_jni_1zero_samples_SampleNonProxy_nativeAddBookmarkFromAPI( 74 | JNIEnv* env, 75 | jobject jcaller, 76 | jint nativeChromeBrowserProvider, 77 | jstring url, 78 | jobject created, 79 | jobject isBookmark, 80 | jobject date, 81 | jbyteArray favicon, 82 | jstring title, 83 | jobject visits) { 84 | return JNI_SampleNonProxy_AddBookmarkFromAPI(env, base::android::JavaParamRef(env, 85 | jcaller), nativeChromeBrowserProvider, base::android::JavaParamRef(env, url), 86 | base::android::JavaParamRef(env, created), base::android::JavaParamRef(env, 87 | isBookmark), base::android::JavaParamRef(env, date), 88 | base::android::JavaParamRef(env, favicon), 89 | base::android::JavaParamRef(env, title), base::android::JavaParamRef(env, 90 | visits)); 91 | } 92 | 93 | static void JNI_SampleNonProxy_CreateHistoricalTabFromState(JNIEnv* env, const 94 | base::android::JavaParamRef& state, 95 | jint tabIndex); 96 | 97 | JNI_BOUNDARY_EXPORT void 98 | Java_org_jni_1zero_samples_SampleNonProxy_nativeCreateHistoricalTabFromState( 99 | JNIEnv* env, 100 | jclass jcaller, 101 | jbyteArray state, 102 | jint tabIndex) { 103 | return JNI_SampleNonProxy_CreateHistoricalTabFromState(env, 104 | base::android::JavaParamRef(env, state), tabIndex); 105 | } 106 | 107 | static void JNI_SampleNonProxy_Destroy(JNIEnv* env, const base::android::JavaParamRef& 108 | jcaller, 109 | jint nativeChromeBrowserProvider); 110 | 111 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeDestroy( 112 | JNIEnv* env, 113 | jobject jcaller, 114 | jint nativeChromeBrowserProvider) { 115 | return JNI_SampleNonProxy_Destroy(env, base::android::JavaParamRef(env, jcaller), 116 | nativeChromeBrowserProvider); 117 | } 118 | 119 | static jint JNI_SampleNonProxy_FindAll(JNIEnv* env, const base::android::JavaParamRef& 120 | jcaller, 121 | const base::android::JavaParamRef& find); 122 | 123 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeFindAll( 124 | JNIEnv* env, 125 | jobject jcaller, 126 | jstring find) { 127 | return JNI_SampleNonProxy_FindAll(env, base::android::JavaParamRef(env, jcaller), 128 | base::android::JavaParamRef(env, find)); 129 | } 130 | 131 | static base::android::ScopedJavaLocalRef 132 | JNI_SampleNonProxy_GetAutofillProfileGUIDs(JNIEnv* env); 133 | 134 | JNI_BOUNDARY_EXPORT jobjectArray 135 | Java_org_jni_1zero_samples_SampleNonProxy_nativeGetAutofillProfileGUIDs( 136 | JNIEnv* env, 137 | jclass jcaller) { 138 | return JNI_SampleNonProxy_GetAutofillProfileGUIDs(env).Release(); 139 | } 140 | 141 | static base::android::ScopedJavaLocalRef JNI_SampleNonProxy_GetDomainAndRegistry(JNIEnv* 142 | env, const base::android::JavaParamRef& url); 143 | 144 | JNI_BOUNDARY_EXPORT jstring Java_org_jni_1zero_samples_SampleNonProxy_nativeGetDomainAndRegistry( 145 | JNIEnv* env, 146 | jclass jcaller, 147 | jstring url) { 148 | return JNI_SampleNonProxy_GetDomainAndRegistry(env, base::android::JavaParamRef(env, 149 | url)).Release(); 150 | } 151 | 152 | static base::android::ScopedJavaLocalRef JNI_SampleNonProxy_GetInnerClass(JNIEnv* env); 153 | 154 | JNI_BOUNDARY_EXPORT jobject Java_org_jni_1zero_samples_SampleNonProxy_nativeGetInnerClass( 155 | JNIEnv* env, 156 | jclass jcaller) { 157 | return JNI_SampleNonProxy_GetInnerClass(env).Release(); 158 | } 159 | 160 | static base::android::ScopedJavaLocalRef JNI_SampleNonProxy_GetStateAsByteArray(JNIEnv* 161 | env, const base::android::JavaParamRef& jcaller, 162 | const base::android::JavaParamRef& view); 163 | 164 | JNI_BOUNDARY_EXPORT jbyteArray Java_org_jni_1zero_samples_SampleNonProxy_nativeGetStateAsByteArray( 165 | JNIEnv* env, 166 | jobject jcaller, 167 | jobject view) { 168 | return JNI_SampleNonProxy_GetStateAsByteArray(env, base::android::JavaParamRef(env, 169 | jcaller), base::android::JavaParamRef(env, view)).Release(); 170 | } 171 | 172 | static void JNI_SampleNonProxy_GotOrientation(JNIEnv* env, const 173 | base::android::JavaParamRef& jcaller, 174 | jint nativeDataFetcherImplAndroid, 175 | jdouble alpha, 176 | jdouble beta, 177 | jdouble gamma); 178 | 179 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeGotOrientation( 180 | JNIEnv* env, 181 | jobject jcaller, 182 | jint nativeDataFetcherImplAndroid, 183 | jdouble alpha, 184 | jdouble beta, 185 | jdouble gamma) { 186 | return JNI_SampleNonProxy_GotOrientation(env, base::android::JavaParamRef(env, jcaller), 187 | nativeDataFetcherImplAndroid, alpha, beta, gamma); 188 | } 189 | 190 | static jint JNI_SampleNonProxy_Init(JNIEnv* env, const base::android::JavaParamRef& 191 | jcaller); 192 | 193 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeInit( 194 | JNIEnv* env, 195 | jobject jcaller) { 196 | return JNI_SampleNonProxy_Init(env, base::android::JavaParamRef(env, jcaller)); 197 | } 198 | 199 | static jint JNI_SampleNonProxy_Init(JNIEnv* env, const base::android::JavaParamRef& 200 | jcaller); 201 | 202 | JNI_BOUNDARY_EXPORT jint Java_org_jni_1zero_samples_SampleNonProxy_nativeInit( 203 | JNIEnv* env, 204 | jobject jcaller) { 205 | return JNI_SampleNonProxy_Init(env, base::android::JavaParamRef(env, jcaller)); 206 | } 207 | 208 | static base::android::ScopedJavaLocalRef 209 | JNI_SampleNonProxy_MessWithJavaException(JNIEnv* env, const 210 | base::android::JavaParamRef& e); 211 | 212 | JNI_BOUNDARY_EXPORT jthrowable 213 | Java_org_jni_1zero_samples_SampleNonProxy_nativeMessWithJavaException( 214 | JNIEnv* env, 215 | jclass jcaller, 216 | jthrowable e) { 217 | return JNI_SampleNonProxy_MessWithJavaException(env, base::android::JavaParamRef(env, 218 | e)).Release(); 219 | } 220 | 221 | static base::android::ScopedJavaLocalRef JNI_SampleNonProxy_QueryBitmap(JNIEnv* env, const 222 | base::android::JavaParamRef& jcaller, 223 | jint nativeChromeBrowserProvider, 224 | const base::android::JavaParamRef& projection, 225 | const base::android::JavaParamRef& selection, 226 | const base::android::JavaParamRef& selectionArgs, 227 | const base::android::JavaParamRef& sortOrder); 228 | 229 | JNI_BOUNDARY_EXPORT jobject Java_org_jni_1zero_samples_SampleNonProxy_nativeQueryBitmap( 230 | JNIEnv* env, 231 | jobject jcaller, 232 | jint nativeChromeBrowserProvider, 233 | jobjectArray projection, 234 | jstring selection, 235 | jobjectArray selectionArgs, 236 | jstring sortOrder) { 237 | return JNI_SampleNonProxy_QueryBitmap(env, base::android::JavaParamRef(env, jcaller), 238 | nativeChromeBrowserProvider, base::android::JavaParamRef(env, projection), 239 | base::android::JavaParamRef(env, selection), 240 | base::android::JavaParamRef(env, selectionArgs), 241 | base::android::JavaParamRef(env, sortOrder)).Release(); 242 | } 243 | 244 | static void JNI_SampleNonProxy_SetRecognitionResults(JNIEnv* env, const 245 | base::android::JavaParamRef& jcaller, 246 | jint sessionId, 247 | const base::android::JavaParamRef& results); 248 | 249 | JNI_BOUNDARY_EXPORT void Java_org_jni_1zero_samples_SampleNonProxy_nativeSetRecognitionResults( 250 | JNIEnv* env, 251 | jobject jcaller, 252 | jint sessionId, 253 | jobjectArray results) { 254 | return JNI_SampleNonProxy_SetRecognitionResults(env, base::android::JavaParamRef(env, 255 | jcaller), sessionId, base::android::JavaParamRef(env, results)); 256 | } 257 | 258 | 259 | #endif // org_jni_zero_samples_SampleNonProxy_JNI 260 | --------------------------------------------------------------------------------