├── .bazelrc ├── .bazelversion ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── BUILD ├── CMakeLists.txt ├── CMakePresets.json ├── CONTRIBUTING.md ├── JNI_BIND_VERSION.inc ├── LICENSE ├── MODULE.bazel ├── README.md ├── WORKSPACE ├── WORKSPACE.bzlmod ├── build_jni_bind_release.sh ├── class_defs ├── BUILD ├── README.md ├── java_lang_classes.h ├── java_lang_exception.h ├── java_lang_throwable.h ├── java_util_array_list.h └── java_util_classes.h ├── cmake ├── add_jni_test.cmake ├── compile_flags.cmake └── find_dependencies.cmake ├── godbolt.png ├── implementation ├── BUILD ├── array.h ├── array_ref.h ├── array_test.cc ├── array_type_conversion.h ├── array_type_conversion_test.cc ├── array_view.h ├── array_view_test.cc ├── class.h ├── class_loader.h ├── class_loader_ref.h ├── class_loader_ref_second_test.cc ├── class_loader_ref_test.cc ├── class_loader_test.cc ├── class_ref.h ├── class_test.cc ├── configuration.h ├── constructor.h ├── constructor_test.cc ├── default_class_loader.h ├── extends.h ├── field.h ├── field_ref.h ├── field_ref_test.cc ├── field_selection.h ├── find_class_fallback.h ├── forward_declarations.h ├── global_class_loader.h ├── global_object.h ├── global_object_test.cc ├── global_string.h ├── id.h ├── id_test.cc ├── id_type.h ├── jni_helper │ ├── BUILD │ ├── arg_string.h │ ├── arg_string_test.cc │ ├── fake_test_constants.h │ ├── field_value.h │ ├── get_array_element_result.h │ ├── invoke.h │ ├── invoke_static.h │ ├── invoke_test.cc │ ├── jni_array_helper.h │ ├── jni_env.h │ ├── jni_env_test.cc │ ├── jni_helper.h │ ├── jni_helper_test.cc │ ├── jni_typename_to_string.h │ ├── jni_typename_to_string_test.cc │ ├── lifecycle.h │ ├── lifecycle_object.h │ ├── lifecycle_object_test.cc │ ├── lifecycle_string.h │ ├── lifecycle_string_test.cc │ ├── static_field_value.h │ ├── trace.h │ └── trace_test.cc ├── jni_type.h ├── jni_type_test.cc ├── jvm.h ├── jvm_ref.h ├── jvm_ref_base.h ├── jvm_ref_test.cc ├── jvm_test.cc ├── legacy │ ├── BUILD │ ├── README.md │ ├── array_view_test.cc │ ├── class_loader_ref_second_test.cc │ ├── class_loader_ref_test.cc │ ├── field_ref_test.cc │ ├── global_object_test.cc │ ├── local_array_field_multidimensional_test.cc │ ├── local_array_field_test.cc │ ├── local_array_iteration_test.cc │ ├── local_array_method_multidimensional_test.cc │ ├── local_array_method_test.cc │ ├── local_array_multidimensional_test.cc │ ├── local_array_string_test.cc │ ├── local_object_test.cc │ ├── method_ref_test.cc │ ├── multi_type_test.cc │ ├── overload_ref_test.cc │ ├── self_test.cc │ ├── static_ref_test.cc │ └── string_ref_test.cc ├── loaded_by.h ├── local_array.h ├── local_array_field_multidimensional_test.cc ├── local_array_field_test.cc ├── local_array_iteration_test.cc ├── local_array_method_multidimensional_test.cc ├── local_array_method_test.cc ├── local_array_multidimensional_test.cc ├── local_array_string.h ├── local_array_string_test.cc ├── local_array_test.cc ├── local_class_loader.h ├── local_object.h ├── local_object_test.cc ├── local_string.h ├── method.h ├── method_ref_test.cc ├── method_selection.h ├── method_selection_test.cc ├── multi_type_test.cc ├── name_constants.h ├── name_constants_test.cc ├── no_class_specified.h ├── no_idx.h ├── object.h ├── object_ref.h ├── overload_ref.h ├── overload_ref_test.cc ├── params.h ├── promotion_mechanics.h ├── promotion_mechanics_tags.h ├── proxy.h ├── proxy_convenience_aliases.h ├── proxy_definitions.h ├── proxy_definitions_array.h ├── proxy_definitions_string.h ├── proxy_temporary.h ├── proxy_test.cc ├── ref_base.h ├── ref_storage.h ├── return.h ├── selector_static_info.h ├── self.h ├── self_test.cc ├── signature.h ├── signature_field_test.cc ├── signature_inherited_field_test.cc ├── signature_inherited_method_test.cc ├── signature_method_test.cc ├── static.h ├── static_ref.h ├── static_ref_test.cc ├── string_ref.h ├── string_ref_test.cc ├── supported_class_set.h ├── thread_guard.h ├── thread_guard_test.cc └── void.h ├── java └── com │ └── google │ ├── BUILD │ ├── README.md │ ├── RandomString.java │ ├── java_runtime_environment.h │ └── main.cc ├── javatests └── com │ └── jnibind │ ├── android │ ├── AndroidDefaultManifest.xml │ ├── ArrayTest.java │ ├── BUILD_android_targets_only_work_in_google3 │ ├── ClassLoaderHelperClass.java │ ├── ClassLoaderHelperSubclass.java │ ├── ClassLoaderTest.java │ ├── ContextTest.java │ ├── FieldTest.java │ ├── FieldTestHelper.java │ ├── GlobalObjectTest.java │ ├── LocalObjectTest.java │ ├── MethodTest.java │ ├── MethodTestHelper.java │ ├── ObjectTestHelper.java │ ├── StringTest.java │ ├── StringTestHelper.java │ ├── ThreadTest.java │ ├── array_test_jni.cc │ ├── class_loader_test_jni.cc │ ├── context_test_jni.cc │ ├── field_test_jni.cc │ ├── global_object_test_jni.cc │ ├── local_object_test_jni.cc │ ├── method_test_jni.cc │ ├── object_test_helper_jni.h │ ├── string_test_jni.cc │ └── thread_test_jni.cc │ └── test │ ├── AndroidDefaultManifest.xml │ ├── ArrayTestFieldRank1.java │ ├── ArrayTestFieldRank2.java │ ├── ArrayTestHelpers.java │ ├── ArrayTestMethodRank1.java │ ├── ArrayTestMethodRank2.java │ ├── BUILD │ ├── BuilderTest.java │ ├── ClassLoaderHelperClass.java │ ├── ClassLoaderTest.java │ ├── ContextTest.java │ ├── FieldTest.java │ ├── FieldTestHelper.java │ ├── GlobalObjectTest.java │ ├── LocalObjectTest.java │ ├── MethodTest.java │ ├── MethodTestHelper.java │ ├── ObjectTestHelper.java │ ├── StaticTest.java │ ├── StaticTestHelper.java │ ├── StringTest.java │ ├── StringTestHelper.java │ ├── ThreadTest.java │ ├── array_test_field_rank_1_jni.cc │ ├── array_test_field_rank_2_jni.cc │ ├── array_test_helpers_native.h │ ├── array_test_method_rank_1_jni.cc │ ├── array_test_method_rank_2_jni.cc │ ├── builder_jni.cc │ ├── class_loader_test_jni.cc │ ├── context_test_jni.cc │ ├── field_test_jni.cc │ ├── global_object_test_jni.cc │ ├── local_object_test_jni.cc │ ├── method_test_jni.cc │ ├── modulo.h │ ├── modulo_test.cc │ ├── object_test_helper_jni.h │ ├── static_test_jni.cc │ ├── string_test_jni.cc │ └── thread_test_jni.cc ├── jni_bind.h ├── jni_bind_release.h ├── jni_bind_release_leader.inc ├── jni_bind_release_trailer.inc ├── jni_dep.h ├── jni_test.h ├── metaprogramming ├── BUILD ├── all.h ├── all_test.cc ├── all_unique.h ├── all_unique_test.cc ├── any.h ├── any_test.cc ├── apply.h ├── apply_test.cc ├── base.h ├── base_filter.h ├── base_filter_test.cc ├── call.h ├── call_test.cc ├── cartesian_product.h ├── cartesian_product_test.cc ├── chain.h ├── chain_test.cc ├── color.h ├── color_test.cc ├── combine.h ├── combine_test.cc ├── concatenate.h ├── concatenate_test.cc ├── conditional.h ├── conditional_test.cc ├── contains.h ├── contains_base.h ├── contains_base_test.cc ├── contains_test.cc ├── corpus.h ├── corpus_tag.h ├── corpus_test.cc ├── deep_equal.h ├── deep_equal_diminished.h ├── deep_equal_diminished_test.cc ├── deep_equal_test.cc ├── detect.h ├── detect_test.cc ├── double_locked_value.h ├── double_locked_value_test.cc ├── even_odd.h ├── even_odd_test.cc ├── filter.h ├── filter_test.cc ├── find_idx_of_val.h ├── find_idx_of_val_test.cc ├── flatten.h ├── flatten_test.cc ├── function_traits.h ├── function_traits_test.cc ├── increment.h ├── increment_test.cc ├── index.h ├── index_test.cc ├── interleave.h ├── interleave_test.cc ├── invocable_map.h ├── invocable_map_20.h ├── invocable_map_20_test.cc ├── invocable_map_test.cc ├── invoke.h ├── invoke_test.cc ├── lambda_string.h ├── lambda_string_test.cc ├── min_max.h ├── min_max_test.cc ├── modified_max.h ├── modified_max_test.cc ├── n_bit.h ├── n_bit_sequence.h ├── n_bit_sequence_test.cc ├── n_bit_test.cc ├── name_constants.h ├── next.h ├── next_test.cc ├── optional_wrap.h ├── optional_wrap_test.cc ├── pack_discriminator.h ├── pack_discriminator_test.cc ├── passthrough.h ├── passthrough_test.cc ├── per_element.h ├── per_element_test.cc ├── per_element_val.h ├── per_element_val_test.cc ├── queryable_map.h ├── queryable_map_20.h ├── queryable_map_20_test.cc ├── queryable_map_test.cc ├── reduce.h ├── reduce_test.cc ├── repeat_string.h ├── repeat_string_test.cc ├── replace_string.h ├── replace_string_test.cc ├── reverse.h ├── reverse_test.cc ├── same.h ├── same_test.cc ├── singleton.h ├── singleton_test.cc ├── string_concatenate.h ├── string_concatenate_test.cc ├── string_contains.h ├── string_contains_test.cc ├── string_literal.h ├── string_literal_test.cc ├── tuple_from_size.h ├── tuple_from_size_test.cc ├── tuple_manipulation.h ├── tuple_manipulation_test.cc ├── type_index_mask.h ├── type_index_mask_test.cc ├── type_of_nth_element.h ├── type_of_nth_element_test.cc ├── type_to_type_map.h ├── type_to_type_map_test.cc ├── unfurl.h ├── unfurl_test.cc ├── unique_set.h ├── unique_set_test.cc ├── unwrap.h ├── unwrap_test.cc ├── vals.h ├── vals_equal.h ├── vals_equal_diminished.h ├── vals_equal_diminished_test.cc ├── vals_equal_test.cc ├── zip_invoke.h └── zip_invoke_test.cc ├── mock_jni_env.h ├── mock_jvm.h ├── release_header_smoke_test.cc └── slides-screenshot.png /.bazelrc: -------------------------------------------------------------------------------- 1 | # General build settings 2 | build --cxxopt='-std=c++20' 3 | 4 | # Set Java compiler options 5 | build --java_language_version=17 6 | build --java_runtime_version=17 7 | build --tool_java_language_version=17 8 | build --tool_java_runtime_version=17 -------------------------------------------------------------------------------- /.bazelversion: -------------------------------------------------------------------------------- 1 | 7.0.2 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: JNI Bind Build/Test Status 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | permissions: 10 | contents: read 11 | 12 | jobs: 13 | ubuntu-latest-cpp17-clang: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: test 18 | run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++17' --repo_env=CC=clang --test_output=errors //implementation/legacy/... 19 | 20 | ubuntu-latest-cpp20-clang: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: test 25 | run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++20' --repo_env=CC=clang --test_output=errors ... 26 | 27 | # Commented out because for some reason Github's action runner seems to be 28 | # forcing C++14 which I can't reproduce locally. 29 | # ubuntu-latest-cpp20-gcc: 30 | # runs-on: ubuntu-latest 31 | # steps: 32 | # - uses: actions/checkout@v4 33 | # - name: test 34 | # run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++20' --repo_env=CC=gcc --test_output=errors ... -- -//implementation/legacy/... 35 | 36 | macos-latest-cpp17-clang: 37 | runs-on: macos-latest 38 | steps: 39 | - uses: actions/checkout@v4 40 | - name: test 41 | run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++17' --repo_env=CC=clang --test_output=errors //implementation/legacy/... 42 | 43 | macos-latest-cpp20-clang: 44 | runs-on: macos-latest 45 | steps: 46 | - uses: actions/checkout@v4 47 | - name: test 48 | run: bazel test --cxxopt='-Werror' --cxxopt='-std=c++20' --repo_env=CC=clang --test_output=errors ... 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bazel* 2 | .vscode/ 3 | .clwb/ 4 | .ijwb/ 5 | .idea/ 6 | .vscode/ 7 | MODULE.bazel.lock 8 | cmake_build 9 | cmake_install 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.20.0) 2 | 3 | file(READ ${CMAKE_SOURCE_DIR}/JNI_BIND_VERSION.inc JNI_BIND_VERSION) 4 | project(jni-bind VERSION "${JNI_BIND_VERSION}" LANGUAGES C CXX) 5 | 6 | include(${CMAKE_SOURCE_DIR}/cmake/add_jni_test.cmake) 7 | include(${CMAKE_SOURCE_DIR}/cmake/compile_flags.cmake) 8 | include(${CMAKE_SOURCE_DIR}/cmake/find_dependencies.cmake) 9 | 10 | # main target 11 | add_library(jni_bind INTERFACE) 12 | 13 | target_include_directories(jni_bind INTERFACE 14 | ${CMAKE_CURRENT_SOURCE_DIR} 15 | ${CMAKE_CURRENT_SOURCE_DIR}/class_defs 16 | ${CMAKE_CURRENT_SOURCE_DIR}/implementation 17 | ${CMAKE_CURRENT_SOURCE_DIR}/implementation/jni_helper 18 | ${CMAKE_CURRENT_SOURCE_DIR}/metaprogramming) 19 | 20 | target_link_libraries(jni_bind INTERFACE jdk:jni) 21 | 22 | # Testsuite 23 | if (NOT TARGET gtest_main) 24 | return() 25 | endif() 26 | 27 | enable_testing() 28 | 29 | file(GLOB METAPROGRAMMING_TESTS "${CMAKE_SOURCE_DIR}/metaprogramming/*_test.cc") 30 | foreach(TEST_SRC ${METAPROGRAMMING_TESTS}) 31 | get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) 32 | add_jni_test(${TEST_NAME} ${TEST_SRC}) 33 | endforeach() 34 | 35 | file(GLOB IMPLEMENTATION_TESTS "${CMAKE_SOURCE_DIR}/implementation/*_test.cc") 36 | foreach(TEST_SRC ${IMPLEMENTATION_TESTS}) 37 | get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) 38 | add_jni_test(${TEST_NAME} ${TEST_SRC}) 39 | endforeach() 40 | 41 | file(GLOB JNI_HELPER_TESTS "${CMAKE_SOURCE_DIR}/implementation/jni_helper/*_test.cc") 42 | foreach(TEST_SRC ${JNI_HELPER_TESTS}) 43 | get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) 44 | add_jni_test(${TEST_NAME} ${TEST_SRC}) 45 | endforeach() -------------------------------------------------------------------------------- /CMakePresets.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 6, 3 | 4 | "cmakeMinimumRequired": { 5 | "major": 3, 6 | "minor": 20, 7 | "patch": 0 8 | }, 9 | 10 | "configurePresets": [ 11 | { 12 | "hidden": true, 13 | "name": "msvc-16-base", 14 | "generator": "Visual Studio 16 2019", 15 | "binaryDir": "${sourceDir}/cmake_build", 16 | "architecture": { 17 | "value": "x64", 18 | "strategy": "external" 19 | } 20 | }, 21 | { 22 | "name": "msvc-16-config-debug", 23 | "inherits": "msvc-16-base", 24 | "installDir": "${sourceDir}/cmake_install/Debug", 25 | "cacheVariables": { 26 | "CMAKE_BUILD_TYPE": "Debug" 27 | } 28 | }, 29 | { 30 | "name": "msvc-16-config-release", 31 | "inherits": "msvc-16-base", 32 | "installDir": "${sourceDir}/cmake_install/Release", 33 | "cacheVariables": { 34 | "CMAKE_BUILD_TYPE": "Release" 35 | } 36 | } 37 | ], 38 | 39 | "buildPresets": [ 40 | { 41 | "name": "msvc-16-build-debug", 42 | "configurePreset": "msvc-16-config-debug", 43 | "configuration": "Debug", 44 | "jobs": 4 45 | }, 46 | { 47 | "name": "msvc-16-build-release", 48 | "configurePreset": "msvc-16-config-release", 49 | "configuration": "Release", 50 | "jobs": 4 51 | } 52 | ], 53 | 54 | "testPresets": [ 55 | { 56 | "name": "msvc-16-test-debug", 57 | "configurePreset": "msvc-16-config-debug", 58 | "configuration": "Debug", 59 | "output": { 60 | "outputOnFailure": true, 61 | "verbosity": "verbose" 62 | }, 63 | "execution": { 64 | "noTestsAction": "error", 65 | "stopOnFailure": true 66 | } 67 | } 68 | ] 69 | 70 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow. 5 | 6 | ## Contributor License Agreement 7 | 8 | Contributions to this project must be accompanied by a Contributor License 9 | Agreement (CLA). You (or your employer) retain the copyright to your 10 | contribution; this simply gives us permission to use and redistribute your 11 | contributions as part of the project. Head over to 12 | to see your current agreements on file or 13 | to sign a new one. 14 | 15 | You generally only need to submit a CLA once, so if you've already submitted one 16 | (even if it was for a different project), you probably don't need to do it 17 | again. 18 | 19 | ## Code Reviews 20 | 21 | All submissions, including submissions by project members, require review. We 22 | use GitHub pull requests for this purpose. Consult 23 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 24 | information on using pull requests. 25 | 26 | ## Community Guidelines 27 | 28 | This project follows 29 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/). 30 | -------------------------------------------------------------------------------- /JNI_BIND_VERSION.inc: -------------------------------------------------------------------------------- 1 | 1.2.2 2 | -------------------------------------------------------------------------------- /MODULE.bazel: -------------------------------------------------------------------------------- 1 | module( 2 | name = "jni_bind", 3 | version = "1.1.0", 4 | compatibility_level = 1, 5 | ) 6 | 7 | bazel_dep(name = "platforms", version = "0.0.10") 8 | bazel_dep(name = "bazel_skylib", version = "1.4.2") 9 | bazel_dep(name = "rules_java", version = "7.1.0") 10 | bazel_dep(name = "rules_jvm_external", version = "5.2") 11 | bazel_dep(name = "rules_license", version = "0.0.7") 12 | bazel_dep(name = "freetype", version = "2.9") 13 | bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf") 14 | 15 | bazel_dep( 16 | name = "googletest", 17 | version = "1.14.0", 18 | repo_name = "googletest", 19 | dev_dependency = True, 20 | ) 21 | 22 | # Maven artifacts required by Stardoc; keep consistent with deps.bzl 23 | MAVEN_ARTIFACTS = [ 24 | "com.google.truth:truth:1.1.2", 25 | "junit:junit:4.13.2", 26 | "org.mockito:mockito-core:4.3.1", 27 | ] 28 | 29 | maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven") 30 | 31 | maven.install( 32 | name = "maven", 33 | artifacts = MAVEN_ARTIFACTS, 34 | fail_if_repin_required = True, 35 | repositories = [ 36 | "https://repo1.maven.org/maven2", 37 | ], 38 | strict_visibility = True, 39 | ) 40 | use_repo(maven, "maven") 41 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | # All dependencies have moved to `MODULE.bazel`. 2 | -------------------------------------------------------------------------------- /WORKSPACE.bzlmod: -------------------------------------------------------------------------------- 1 | # Empty to prevent dependencies added via default WORKSPACE. 2 | -------------------------------------------------------------------------------- /class_defs/README.md: -------------------------------------------------------------------------------- 1 | ** WARNING ** 2 | 3 | Class definitions in this directory may have their path change in the future, you may be better suited creating copies until full scrapes of APIs are provided in the future. 4 | 5 | These are used to bootstrap JNI Bind, but may change in the future, and may also be superseded by separate more complete definitions. 6 | 7 | Some definitions in this directory exist solely for phase 1 compilation and as such may be incomplete. 8 | 9 | -------------------------------------------------------------------------------- /class_defs/java_lang_classes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_CLASS_DEFS_JAVA_LANG_CLASSES_H_ 18 | #define JNI_BIND_CLASS_DEFS_JAVA_LANG_CLASSES_H_ 19 | 20 | #include "implementation/array.h" 21 | #include "implementation/class.h" 22 | #include "implementation/constructor.h" 23 | #include "implementation/method.h" 24 | #include "implementation/params.h" 25 | #include "implementation/return.h" 26 | #include "implementation/self.h" 27 | #include "implementation/static.h" 28 | #include "jni_dep.h" 29 | 30 | namespace jni { 31 | 32 | // clang-format off 33 | inline constexpr Class kJavaLangClass{ 34 | "java/lang/Class", 35 | Method{"getClassLoader", Return{ Class { "java/lang/ClassLoader" } }}, 36 | Method{"toString", Return{jstring{}}, Params<>{}}, 37 | Method{"getName", Return{jstring{}}, Params<>{}} 38 | }; 39 | 40 | inline constexpr Class kJavaLangObject{ 41 | "java/lang/Object", 42 | Method{"getClass", Return{kJavaLangClass}}, 43 | }; 44 | 45 | inline constexpr Class kJavaLangClassLoader{ 46 | "java/lang/ClassLoader", 47 | Static { 48 | Method{"getSystemClassLoader", Return{Self{}}}, 49 | }, 50 | Method{"loadClass", Return{kJavaLangClass}, Params{}}, 51 | Method{"toString", Return{jstring{}}, Params<>{}}, 52 | }; 53 | 54 | static constexpr Class kJavaLangString{ 55 | "java/lang/String", 56 | 57 | Constructor{jstring{}}, 58 | Constructor{Array{jbyte{}}}, 59 | 60 | Method{"toString", Return{jstring{}}, Params<>{}}, 61 | }; 62 | 63 | // clang-format on 64 | 65 | } // namespace jni 66 | 67 | #endif // JNI_BIND_CLASS_DEFS_JAVA_LANG_CLASSES_H_ 68 | -------------------------------------------------------------------------------- /class_defs/java_lang_exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ 18 | #define JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ 19 | 20 | #include "class_defs/java_lang_throwable.h" 21 | #include "implementation/class.h" 22 | #include "implementation/constructor.h" 23 | #include "implementation/method.h" 24 | #include "implementation/params.h" 25 | #include "implementation/return.h" 26 | #include "jni_dep.h" 27 | 28 | namespace jni { 29 | 30 | static constexpr Class kJavaLangException{ 31 | "java/lang/Exception", 32 | Constructor{}, 33 | Constructor{}, 34 | Constructor{jstring{}, kJavaLangThrowable, jboolean{}, jboolean{}}, 35 | Constructor{kJavaLangThrowable}, 36 | 37 | // Inherited. 38 | Method{"getMessage", Return{jstring{}}, Params<>{}}, 39 | Method{"printStackTrace", Return{}, Params<>{}}, 40 | Method{"toString", Return{jstring{}}, Params<>{}}, 41 | }; 42 | 43 | } // namespace jni 44 | 45 | #endif // JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ 46 | -------------------------------------------------------------------------------- /class_defs/java_lang_throwable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ 18 | #define JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ 19 | 20 | #include "implementation/class.h" 21 | #include "implementation/constructor.h" 22 | #include "implementation/self.h" 23 | #include "jni_dep.h" 24 | 25 | namespace jni { 26 | 27 | static constexpr Class kJavaLangThrowable{ 28 | "java/lang/Throwable", 29 | Constructor{}, 30 | Constructor{}, 31 | Constructor{jstring{}, Self{}}, 32 | Constructor{jstring{}, Self{}, jboolean{}, jboolean{}}, 33 | Constructor{Self{}}}; 34 | 35 | } // namespace jni 36 | 37 | #endif // JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ 38 | -------------------------------------------------------------------------------- /class_defs/java_util_array_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef JNI_BIND_CLASS_DEFS_JAVA_UTIL_ARRAY_LIST_H_ 17 | #define JNI_BIND_CLASS_DEFS_JAVA_UTIL_ARRAY_LIST_H_ 18 | 19 | #include "class_defs/java_lang_classes.h" 20 | #include "implementation/class.h" 21 | #include "implementation/constructor.h" 22 | #include "implementation/method.h" 23 | #include "implementation/params.h" 24 | #include "implementation/return.h" 25 | #include "jni_dep.h" 26 | 27 | namespace jni { 28 | 29 | static constexpr ::jni::Class kJavaUtilArrayList{ 30 | "java/util/ArrayList", 31 | Constructor{}, 32 | Constructor{kJavaLangObject}, 33 | Constructor{int{}}, 34 | 35 | Method{"add", Return{}, Params{kJavaLangObject}}, 36 | }; 37 | 38 | } // namespace jni 39 | 40 | #endif // JNI_BIND_CLASS_DEFS_JAVA_UTIL_ARRAY_LIST_H_ 41 | -------------------------------------------------------------------------------- /class_defs/java_util_classes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_CLASS_DEFS_JAVA_UTIL_CLASSES_H_ 18 | #define JNI_BIND_CLASS_DEFS_JAVA_UTIL_CLASSES_H_ 19 | 20 | #include "class_defs/java_lang_classes.h" 21 | #include "implementation/class.h" 22 | #include "implementation/method.h" 23 | #include "implementation/params.h" 24 | #include "implementation/return.h" 25 | #include "jni_dep.h" 26 | 27 | namespace jni { 28 | 29 | inline constexpr Class kJavaUtilList{ 30 | "java/util/List", 31 | Method{"add", jni::Return{}, jni::Params{kJavaLangObject}}, 32 | Method{"clear", jni::Return{}, jni::Params{}}, 33 | Method{"get", jni::Return{kJavaLangObject}, jni::Params{}}, 34 | Method{"remove", jni::Return{kJavaLangObject}, jni::Params{}}, 35 | Method{"size", jni::Return{}, jni::Params{}}}; 36 | 37 | } // namespace jni 38 | 39 | #endif // JNI_BIND_CLASS_DEFS_JAVA_UTIL_CLASSES_H_ 40 | -------------------------------------------------------------------------------- /cmake/add_jni_test.cmake: -------------------------------------------------------------------------------- 1 | function(add_jni_test test_name test_source) 2 | 3 | # workaround duplicated target test name "invoke_test" 4 | if (TARGET ${test_name}) 5 | set(test_name ${test_name}_2) 6 | endif() 7 | 8 | add_executable(${test_name} ${test_source}) 9 | 10 | target_link_libraries(${test_name} 11 | PRIVATE 12 | jni_bind 13 | gmock 14 | gtest_main) 15 | 16 | add_test( 17 | NAME ${test_name} 18 | COMMAND ${test_name}) 19 | endfunction() -------------------------------------------------------------------------------- /cmake/compile_flags.cmake: -------------------------------------------------------------------------------- 1 | message("Detected compiler [${CMAKE_CXX_COMPILER_ID}]") 2 | 3 | SET(CMAKE_CXX_STANDARD 20) 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 5 | 6 | if(MSVC) 7 | set(CXX_FLAGS "/Zc:__cplusplus /EHsc") 8 | endif() 9 | 10 | set(CMAKE_CXX_FLAGS "${CXX_FLAGS}" CACHE STRING "" FORCE) 11 | -------------------------------------------------------------------------------- /cmake/find_dependencies.cmake: -------------------------------------------------------------------------------- 1 | # JNI 2 | find_package(JNI REQUIRED) 3 | if (NOT JNI_FOUND) 4 | message(FATAL_ERROR "JDK/JNI environment not found") 5 | endif() 6 | 7 | if (NOT TARGET jdk:jni) 8 | add_library(jdk:jni INTERFACE IMPORTED GLOBAL) 9 | 10 | set_target_properties( 11 | jdk:jni 12 | PROPERTIES 13 | INTERFACE_INCLUDE_DIRECTORIES "${JNI_INCLUDE_DIRS}" 14 | INTERFACE_LINK_LIBRARIES "${JNI_LIBRARIES}") 15 | endif() 16 | 17 | # GoogleTest 18 | include(FetchContent) 19 | FetchContent_Declare(googletest 20 | GIT_REPOSITORY https://github.com/google/googletest.git 21 | GIT_TAG f8d7d77) # 1.14.0 22 | FetchContent_MakeAvailable(googletest) -------------------------------------------------------------------------------- /godbolt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/jni-bind/3ad7c7a304a40179c780004b7954bd353e50f9b7/godbolt.png -------------------------------------------------------------------------------- /implementation/array_type_conversion_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "jni_bind.h" 20 | 21 | namespace { 22 | 23 | using ::jni::StorageHelper_t; 24 | 25 | static_assert(std::is_same_v, jint>); 26 | static_assert(std::is_same_v, jintArray>); 27 | static_assert(std::is_same_v, jobjectArray>); 28 | 29 | static_assert(std::is_same_v, jstring>); 30 | static_assert(std::is_same_v, jobjectArray>); 31 | static_assert(std::is_same_v, jobjectArray>); 32 | 33 | static_assert(std::is_same_v, jobject>); 34 | static_assert(std::is_same_v, jobjectArray>); 35 | static_assert(std::is_same_v, jobjectArray>); 36 | 37 | } // namespace 38 | -------------------------------------------------------------------------------- /implementation/configuration.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_CONFIGURATION_H_ 18 | #define JNI_BIND_IMPLEMENTATION_CONFIGURATION_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | struct Configuration { 25 | // Release jclassID on JVM teardown (needed in test to balance global IDs). 26 | bool release_class_ids_on_teardown_ = false; 27 | 28 | // Release jmethodID on JVM teardown (needed in test to balance global IDs). 29 | bool release_method_ids_on_teardown_ = false; 30 | }; 31 | 32 | static inline Configuration kConfiguration = {}; 33 | 34 | } // namespace jni 35 | 36 | #endif // JNI_BIND_IMPLEMENTATION_CONFIGURATION_H_ 37 | -------------------------------------------------------------------------------- /implementation/extends.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_EXTENDS_H_ 18 | #define JNI_BIND_IMPLEMENTATION_EXTENDS_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include "implementation/no_class_specified.h" 23 | #include "metaprogramming/tuple_manipulation.h" 24 | #include "metaprogramming/type_of_nth_element.h" 25 | 26 | namespace jni { 27 | 28 | // Metafunction for helping strip the type for input Extends params. 29 | struct ExtendsStrip { 30 | template 31 | struct Helper { 32 | // Input is in a tuple of 1 element. 33 | using StripOuterTuple = metaprogramming::TypeOfNthTupleElement_t<0, T>; 34 | // Steal the outer Extends parameter pack into a tuple. 35 | // e.g. Extends => std::tuple. 36 | using ExtendsToTuple = 37 | metaprogramming::ExtractTupleFromType_t; 38 | // Extracts the single element. 39 | using type = metaprogramming::TypeOfNthTupleElement_t<0, ExtendsToTuple>; 40 | }; 41 | 42 | template 43 | using type = typename Helper::type; 44 | }; 45 | 46 | template 47 | using ExtendsStrip_t = typename ExtendsStrip::template type; 48 | 49 | template 50 | struct Extends : public ExtendsBase { 51 | constexpr Extends(ParentT parent) : parent_(parent) {} 52 | 53 | const ParentT parent_; 54 | }; 55 | 56 | template 57 | Extends(T) -> Extends; 58 | 59 | } // namespace jni 60 | 61 | #endif // JNI_BIND_IMPLEMENTATION_EXTENDS_H_ 62 | -------------------------------------------------------------------------------- /implementation/field.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_FIELD_H_ 18 | #define JNI_BIND_FIELD_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | 24 | #include "params.h" 25 | 26 | namespace jni { 27 | 28 | struct FieldBase {}; 29 | 30 | template 31 | struct Field : public FieldBase { 32 | public: 33 | using Raw = Raw_; 34 | 35 | const char* name_; 36 | 37 | const Raw_ raw_ = {}; 38 | 39 | constexpr Field(const char* name) : name_(name) {} 40 | constexpr Field(const char* name, Raw_ value_raw) 41 | : name_(name), raw_(value_raw) {} 42 | }; 43 | 44 | template 45 | Field(const char*, Raw_) -> Field; 46 | 47 | template 48 | using Raw_t = typename T::Raw; 49 | 50 | } // namespace jni 51 | 52 | #endif // JNI_BIND_FIELD_H_ 53 | -------------------------------------------------------------------------------- /implementation/field_selection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_FIELD_SELECTION_H_ 18 | #define JNI_BIND_FIELD_SELECTION_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include "implementation/id.h" 23 | #include "implementation/id_type.h" 24 | #include "implementation/jni_helper/jni_typename_to_string.h" 25 | #include "implementation/name_constants.h" 26 | #include "implementation/no_idx.h" 27 | #include "implementation/object.h" 28 | #include "implementation/selector_static_info.h" 29 | #include "metaprogramming/string_concatenate.h" 30 | 31 | namespace jni { 32 | 33 | template 34 | struct FieldSelection { 35 | using IdT = Id; 36 | static constexpr IdType kRetTypeId = IdType::FIELD; 37 | }; 38 | 39 | } // namespace jni 40 | 41 | #endif // JNI_BIND_FIELD_SELECTION_H_ 42 | -------------------------------------------------------------------------------- /implementation/global_class_loader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_GLOBAL_CLASS_LOADER_H_ 18 | #define JNI_BIND_GLOBAL_CLASS_LOADER_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include "class_defs/java_lang_classes.h" 23 | #include "implementation/class_loader.h" 24 | #include "implementation/class_loader_ref.h" 25 | #include "implementation/default_class_loader.h" 26 | #include "implementation/jni_helper/lifecycle_object.h" 27 | #include "implementation/jvm.h" 28 | #include "implementation/promotion_mechanics.h" 29 | #include "jni_dep.h" 30 | 31 | namespace jni { 32 | 33 | template 35 | class GlobalClassLoader 36 | : public ClassLoaderRef { 37 | public: 38 | using Base = ClassLoaderRef; 39 | using Base::Base; 40 | using SpanType = jobject; 41 | 42 | template 43 | GlobalClassLoader(GlobalClassLoader&& rhs) 44 | : Base(rhs.Release()) {} 45 | }; 46 | 47 | } // namespace jni 48 | 49 | #endif // JNI_BIND_GLOBAL_CLASS_LOADER_H_ 50 | -------------------------------------------------------------------------------- /implementation/id_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_ID_TYPE_H_ 18 | #define JNI_BIND_IMPLEMENTATION_ID_TYPE_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | enum class IdType { 25 | CLASS, 26 | STATIC_OVERLOAD_SET, 27 | STATIC_OVERLOAD, 28 | STATIC_OVERLOAD_PARAM, 29 | OVERLOAD_SET, 30 | OVERLOAD, 31 | OVERLOAD_PARAM, 32 | STATIC_FIELD, 33 | FIELD, 34 | }; 35 | 36 | } // namespace jni 37 | 38 | #endif // JNI_BIND_IMPLEMENTATION_ID_TYPE_H_ 39 | -------------------------------------------------------------------------------- /implementation/jni_helper/get_array_element_result.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_JNI_HELPER_GET_ARRAY_ELEMENT_RESULT_H_ 18 | #define JNI_BIND_IMPLEMENTATION_JNI_HELPER_GET_ARRAY_ELEMENT_RESULT_H_ 19 | 20 | #include "jni_dep.h" 21 | 22 | namespace jni { 23 | 24 | // Convenience struct for returning results from pinning array. 25 | template 26 | struct GetArrayElementsResult { 27 | SpanType* ptr_; 28 | jboolean is_copy; 29 | }; 30 | 31 | } // namespace jni 32 | 33 | #endif // JNI_BIND_IMPLEMENTATION_JNI_HELPER_GET_ARRAY_ELEMENT_RESULT_H_ 34 | -------------------------------------------------------------------------------- /implementation/jni_helper/jni_env_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "jni_env.h" 18 | 19 | #include 20 | #include "implementation/jni_helper/fake_test_constants.h" 21 | #include "jni_dep.h" 22 | 23 | namespace { 24 | 25 | using ::jni::Fake; 26 | 27 | class JniEnvForTest : public jni::JniEnv { 28 | public: 29 | static JNIEnv* GetEnv_ForTest() { return GetEnv(); } 30 | static void SetEnv_ForTest(JNIEnv* env) { return SetEnv(env); } 31 | }; 32 | 33 | // Why not split this test apart? 34 | // 35 | // JniEnv is by design based on a process level static. Separate tests here 36 | // will actually have crosstalk with variables and simply re-ordering them could 37 | // cause tests to fail (which would be totally unobvious). 38 | // 39 | // Instead, they are crammed into this single test and this obnoxious comment 40 | // has been added. 41 | TEST(JniEnv, StartsNullAndSetsAndGetsTheSameValue) { 42 | EXPECT_EQ(JniEnvForTest::GetEnv(), nullptr); 43 | JniEnvForTest::SetEnv_ForTest(Fake(1)); 44 | EXPECT_EQ(JniEnvForTest::GetEnv(), Fake(1)); 45 | JniEnvForTest::SetEnv_ForTest(Fake(2)); 46 | EXPECT_EQ(JniEnvForTest::GetEnv(), Fake(2)); 47 | JniEnvForTest::SetEnv_ForTest(nullptr); 48 | EXPECT_EQ(JniEnvForTest::GetEnv(), nullptr); 49 | } 50 | 51 | // TODO: Write a test that spawns multiple threads and validates they all 52 | // separately cache their JNIENV*. 53 | 54 | } // namespace 55 | -------------------------------------------------------------------------------- /implementation/jni_helper/jni_helper_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "jni_helper.h" 18 | 19 | #include 20 | #include 21 | #include "implementation/jni_helper/fake_test_constants.h" 22 | #include "implementation/jni_helper/lifecycle.h" 23 | #include "jni_bind.h" 24 | #include "jni_dep.h" 25 | #include "jni_test.h" 26 | 27 | namespace { 28 | 29 | using ::jni::Fake; 30 | using ::jni::JniHelper; 31 | using ::jni::LifecycleHelper; 32 | using ::jni::LifecycleType; 33 | using ::jni::test::JniTest; 34 | using ::testing::InSequence; 35 | using ::testing::StrEq; 36 | 37 | TEST_F(JniTest, JniHelper_CallsReleaseClass) { 38 | InSequence seq; 39 | EXPECT_CALL(*env_, FindClass(StrEq("Test2"))) 40 | .Times(1) 41 | .WillOnce(testing::Return(Fake())); 42 | 43 | EXPECT_EQ(JniHelper::FindClass("Test2"), Fake()); 44 | LifecycleHelper::Delete(Fake()); 45 | } 46 | 47 | TEST_F(JniTest, JniHelper_CallsNewStringUTF) { 48 | EXPECT_CALL(*env_, GetStringUTFChars(Fake(), nullptr)); 49 | JniHelper::GetStringUTFChars(Fake()); 50 | } 51 | 52 | TEST_F(JniTest, JniHelper_CallsReleaseStringUTFChars) { 53 | const char* fake_pinned_chars = "foo"; 54 | EXPECT_CALL(*env_, ReleaseStringUTFChars(Fake(), fake_pinned_chars)); 55 | JniHelper::ReleaseStringUTFChars(Fake(), fake_pinned_chars); 56 | } 57 | 58 | } // namespace 59 | -------------------------------------------------------------------------------- /implementation/jni_helper/jni_typename_to_string_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "jni_typename_to_string.h" 18 | 19 | #include // NOLINT 20 | 21 | #include 22 | #include "jni_dep.h" 23 | 24 | namespace { 25 | 26 | using ::jni::JavaTypeToString; 27 | 28 | TEST(TypeToString, CompilesBasicTypeTests) { 29 | using namespace std::literals; 30 | static_assert(JavaTypeToString() == "V"sv); 31 | static_assert(JavaTypeToString() == "Z"sv); 32 | static_assert(JavaTypeToString() == "C"sv); 33 | static_assert(JavaTypeToString() == "B"sv); 34 | static_assert(JavaTypeToString() == "S"sv); 35 | static_assert(JavaTypeToString() == "I"sv); 36 | static_assert(JavaTypeToString() == "J"sv); 37 | static_assert(JavaTypeToString() == "F"sv); 38 | static_assert(JavaTypeToString() == "D"sv); 39 | static_assert(JavaTypeToString() == "Ljava/lang/String;"sv); 40 | } 41 | 42 | } // namespace 43 | -------------------------------------------------------------------------------- /implementation/jni_helper/lifecycle_string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_JNI_HELPER_LIFECYCLE_STRING_H_ 18 | #define JNI_BIND_IMPLEMENTATION_JNI_HELPER_LIFECYCLE_STRING_H_ 19 | 20 | #include "jni_env.h" 21 | #include "implementation/jni_helper/lifecycle.h" 22 | #include "jni_dep.h" 23 | #include "metaprogramming/lambda_string.h" 24 | #include "trace.h" 25 | 26 | namespace jni { 27 | 28 | template <> 29 | struct LifecycleHelper 30 | : public LifecycleLocalBase { 31 | static inline jstring Construct(const char* chars) { 32 | Trace(metaprogramming::LambdaToStr(STR("NewStringUTF")), chars); 33 | 34 | #ifdef DRY_RUN 35 | return Fake(); 36 | #else 37 | return jni::JniEnv::GetEnv()->NewStringUTF(chars); 38 | #endif // DRY_RUN 39 | } 40 | }; 41 | 42 | template <> 43 | struct LifecycleHelper 44 | : public LifecycleGlobalBase { 45 | template 46 | static inline jstring Construct(const char* chars) { 47 | using Local = LifecycleHelper; 48 | 49 | jstring local_string = Local::Construct(chars); 50 | jstring global_string = Promote(local_string); 51 | Local::Delete(local_string); 52 | 53 | return global_string; 54 | } 55 | }; 56 | 57 | } // namespace jni 58 | 59 | #endif // JNI_BIND_IMPLEMENTATION_JNI_HELPER_LIFECYCLE_STRING_H_ 60 | -------------------------------------------------------------------------------- /implementation/jvm_ref_base.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ 18 | #define JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | 24 | #include "implementation/configuration.h" 25 | #include "implementation/forward_declarations.h" 26 | #include "jni_dep.h" 27 | 28 | namespace jni { 29 | 30 | // Helper for JvmRef to enforce correct sequencing of getting and setting 31 | // process level static fo JavaVM*. 32 | class JvmRefBase { 33 | protected: 34 | friend class ThreadGuard; 35 | friend class ThreadLocalGuardDestructor; 36 | 37 | JvmRefBase(JavaVM* vm, const Configuration& configuration) { 38 | process_level_jvm_.store(vm); 39 | kConfiguration = configuration; 40 | } 41 | 42 | ~JvmRefBase() { process_level_jvm_.store(nullptr); } 43 | 44 | static JavaVM* GetJavaVm() { return process_level_jvm_.load(); } 45 | static void SetJavaVm(JavaVM* jvm) { process_level_jvm_.store(jvm); } 46 | 47 | static inline std::atomic process_level_jvm_ = nullptr; 48 | }; 49 | 50 | } // namespace jni 51 | 52 | #endif // JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ 53 | -------------------------------------------------------------------------------- /implementation/legacy/README.md: -------------------------------------------------------------------------------- 1 | This directory contains a copy of the tests from implementation but using the legacy `obj.Foo("methodName", args...);` syntax. 2 | 3 | These tests are effectively pure duplication and may be removed at some future point. They are here to maintain coverage as JNI Bind migrates to a C++20 friendly syntax. 4 | -------------------------------------------------------------------------------- /implementation/local_class_loader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_LOCAL_CLASS_LOADER_H_ 18 | #define JNI_BIND_LOCAL_CLASS_LOADER_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include "implementation/class_loader_ref.h" 23 | #include "implementation/default_class_loader.h" 24 | #include "implementation/forward_declarations.h" 25 | #include "implementation/jni_helper/lifecycle.h" 26 | #include "implementation/jvm.h" 27 | #include "implementation/promotion_mechanics_tags.h" 28 | #include "jni_dep.h" 29 | 30 | namespace jni { 31 | 32 | template 34 | class LocalClassLoader 35 | : public ClassLoaderRef { 36 | public: 37 | using Base = ClassLoaderRef; 38 | using Base::Base; 39 | using SpanType = jobject; 40 | 41 | template 42 | explicit LocalClassLoader(LocalClassLoader&& lhs) 43 | : LocalClassLoader(AdoptLocal{}, lhs.Release()) {} 44 | 45 | private: 46 | template 47 | friend class ObjectRef; 48 | }; 49 | 50 | } // namespace jni 51 | 52 | #endif // JNI_BIND_LOCAL_CLASS_LOADER_H_ 53 | -------------------------------------------------------------------------------- /implementation/name_constants.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_NAME_CONSTANTS_H_ 18 | #define JNI_BIND_NAME_CONSTANTS_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | #include 24 | 25 | namespace jni { 26 | 27 | // Metafunction that returns either "" if a member called |name_| isn't 28 | // present, or a constexpr std::string_view of the name if it is. 29 | template 30 | struct NameOrNothing { 31 | static constexpr std::string_view val{""}; 32 | }; 33 | 34 | template 35 | struct NameOrNothing> { 36 | static constexpr std::string_view val{val_.name_}; 37 | }; 38 | 39 | template 40 | static constexpr auto NameOrNothing_v = NameOrNothing::val; 41 | 42 | static constexpr std::string_view kInit{""}; 43 | 44 | } // namespace jni 45 | 46 | #endif // JNI_BIND_NAME_CONSTANTS_H_ 47 | -------------------------------------------------------------------------------- /implementation/name_constants_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "jni_bind.h" 20 | 21 | namespace { 22 | 23 | using ::jni::Class; 24 | using ::jni::NameOrNothing_v; 25 | 26 | static constexpr int kNoNameInt = 3; 27 | static constexpr float kNoNameFloat = 4.f; 28 | static_assert(NameOrNothing_v == std::string_view{""}); 29 | static_assert(NameOrNothing_v == std::string_view{""}); 30 | 31 | static constexpr Class k_class_one{"classOne"}; 32 | static constexpr Class k_class_two{"classTwo"}; 33 | static_assert(NameOrNothing_v == std::string_view{"classOne"}); 34 | static_assert(NameOrNothing_v != std::string_view{"classTwo"}); 35 | static_assert(NameOrNothing_v == std::string_view{"classTwo"}); 36 | 37 | } // namespace 38 | -------------------------------------------------------------------------------- /implementation/no_class_specified.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_NO_CLASS_SPECIFIED_H_ 18 | #define JNI_BIND_IMPLEMENTATION_NO_CLASS_SPECIFIED_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | 24 | #include "implementation/static.h" 25 | 26 | namespace jni { 27 | 28 | struct ExtendsBase {}; 29 | 30 | struct RootObject { 31 | constexpr RootObject() = default; 32 | }; 33 | 34 | static constexpr RootObject kObject{}; 35 | 36 | static constexpr struct NoClass { 37 | // For compatability reasons, this must be defined (not default) because some 38 | // compilers will complain about defaulted constructors being deleted. 39 | constexpr NoClass() {} 40 | 41 | const char* name_ = "__JNI_BIND__NO_CLASS__"; 42 | const RootObject parent_; 43 | const Static, std::tuple<>> static_{}; 44 | const std::tuple<> methods_{}; 45 | const std::tuple<> fields_{}; 46 | } kNoClassSpecified; 47 | 48 | constexpr bool operator==(const NoClass& lhs, const NoClass& rhs) { 49 | return true; 50 | } 51 | constexpr bool operator!=(const NoClass& lhs, const NoClass& rhs) { 52 | return false; 53 | } 54 | 55 | } // namespace jni 56 | 57 | #endif // JNI_BIND_IMPLEMENTATION_NO_CLASS_SPECIFIED_H_ 58 | -------------------------------------------------------------------------------- /implementation/no_idx.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_NO_IDX_H_ 18 | #define JNI_BIND_IMPLEMENTATION_NO_IDX_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | #include 24 | 25 | namespace jni { 26 | 27 | static constexpr std::size_t kNoIdx{std::numeric_limits::max()}; 28 | 29 | } // namespace jni 30 | 31 | #endif // JNI_BIND_IMPLEMENTATION_NO_IDX_H_ 32 | -------------------------------------------------------------------------------- /implementation/object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_OBJECT_H_ 18 | #define JNI_BIND_OBJECT_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | struct Object { 25 | const char* name_; 26 | constexpr explicit Object(const char* name) : name_(name) {} 27 | }; 28 | 29 | } // namespace jni 30 | 31 | #endif // JNI_BIND_OBJECT_H_ 32 | -------------------------------------------------------------------------------- /implementation/promotion_mechanics_tags.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_PROMOTION_MECHANICS_TAGS_H_ 18 | #define JNI_BIND_IMPLEMENTATION_PROMOTION_MECHANICS_TAGS_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | // Adopts a local. 25 | struct AdoptLocal {}; 26 | 27 | // Creates an additional reference to the underlying object. 28 | // When used for local, presumes local, for global, presumes global. 29 | struct NewRef {}; 30 | 31 | // This tag allows the constructor to promote underlying jobject for you. 32 | struct PromoteToGlobal {}; 33 | 34 | // CAUTION: This tag assume the underlying jobject has been pinned as a global. 35 | // This is atypical when solely using JNI Bind, use with caution. 36 | struct AdoptGlobal {}; 37 | 38 | } // namespace jni 39 | 40 | #endif // JNI_BIND_IMPLEMENTATION_PROMOTION_MECHANICS_TAGS_H_ 41 | -------------------------------------------------------------------------------- /implementation/return.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_RETURN_H_ 18 | #define JNI_BIND_RETURN_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include "implementation/void.h" 23 | 24 | namespace jni { 25 | 26 | struct ReturnBase {}; 27 | 28 | template 29 | struct Return : ReturnBase { 30 | const Raw_ raw_ = {}; 31 | 32 | using Raw = Raw_; 33 | 34 | constexpr Return() {} 35 | 36 | template 37 | constexpr explicit Return(Raw raw) : raw_(raw) {} 38 | }; 39 | 40 | template <> 41 | struct Return : ReturnBase { 42 | using Raw = void; 43 | const Void raw_{}; 44 | 45 | constexpr Return() {} 46 | }; 47 | 48 | Return() -> Return; 49 | 50 | template 51 | Return(Raw) -> Return; 52 | 53 | template 54 | using Raw_t = typename T::Raw; 55 | 56 | } // namespace jni 57 | 58 | #endif // JNI_BIND_RETURN_H_ 59 | -------------------------------------------------------------------------------- /implementation/self.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_SELF_H_ 18 | #define JNI_BIND_IMPLEMENTATION_SELF_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | // Tag to indicate you are referring to the enclosing class. 25 | // Useful for builder patterns where the decorated object returned is identical. 26 | struct Self {}; 27 | 28 | } // namespace jni 29 | 30 | #endif // JNI_BIND_IMPLEMENTATION_SELF_H_ 31 | -------------------------------------------------------------------------------- /implementation/static.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_STATIC_H_ 18 | #define JNI_BIND_IMPLEMENTATION_STATIC_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | 24 | #include "implementation/field.h" 25 | #include "implementation/method.h" 26 | #include "metaprogramming/base_filter.h" 27 | 28 | namespace jni { 29 | 30 | class StaticBase {}; 31 | 32 | template 33 | class Static; 34 | 35 | template 36 | class Static, std::tuple> 37 | : public StaticBase { 38 | public: 39 | const std::tuple methods_; 40 | const std::tuple fields_; 41 | 42 | constexpr Static(Methods_... methods, Fields_... fields) 43 | : methods_(methods...), fields_(fields...) {} 44 | }; 45 | 46 | Static() -> Static, std::tuple<>>; 47 | 48 | template 49 | Static(Params...) 50 | -> Static, 51 | metaprogramming::BaseFilter_t>; 52 | 53 | } // namespace jni 54 | 55 | #endif // JNI_BIND_IMPLEMENTATION_STATIC_H_ 56 | -------------------------------------------------------------------------------- /implementation/supported_class_set.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_SUPPORTED_CLASS_SET_H_ 18 | #define JNI_BIND_SUPPORTED_CLASS_SET_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | #include 23 | 24 | namespace jni { 25 | 26 | // The set of classes that a ClassLoader can load. 27 | template 28 | class SupportedClassSet { 29 | public: 30 | const std::tuple supported_classes_; 31 | 32 | constexpr SupportedClassSet(Classes_... supported_classes) 33 | : supported_classes_(supported_classes...) { 34 | // TODO(b/143908983): Classloaders should enforce unique classes. 35 | // static_assert(metaprogramming::AllUniqueValues(supported_classes...), 36 | //"All classes supported by the class loader must be unique."); 37 | } 38 | }; 39 | 40 | template 41 | SupportedClassSet(Classes...) -> SupportedClassSet; 42 | 43 | } // namespace jni 44 | 45 | #endif // JNI_BIND_SUPPORTED_CLASS_SET_H_ 46 | -------------------------------------------------------------------------------- /implementation/void.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_IMPLEMENTATION_VOID_H_ 18 | #define JNI_BIND_IMPLEMENTATION_VOID_H_ 19 | 20 | // IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h" 21 | 22 | namespace jni { 23 | 24 | // Single type that be used as a value when expressing void. 25 | struct Void { 26 | using Raw = void; 27 | }; 28 | 29 | template 30 | struct VoidIfVoid { 31 | using type = T; 32 | }; 33 | 34 | template <> 35 | struct VoidIfVoid { 36 | using type = void; 37 | }; 38 | 39 | template 40 | using VoidIfVoid_t = typename VoidIfVoid::type; 41 | 42 | } // namespace jni 43 | 44 | #endif // JNI_BIND_IMPLEMENTATION_VOID_H_ 45 | -------------------------------------------------------------------------------- /java/com/google/BUILD: -------------------------------------------------------------------------------- 1 | load("@rules_java//java:defs.bzl", "java_library") 2 | 3 | licenses(["notice"]) 4 | 5 | java_library( 6 | name = "random_string_java", 7 | srcs = ["RandomString.java"], 8 | ) 9 | 10 | cc_library( 11 | name = "java_runtime_environment", 12 | hdrs = ["java_runtime_environment.h"], 13 | deps = ["//:jni_bind"], 14 | ) 15 | 16 | cc_binary( 17 | name = "main", 18 | srcs = ["main.cc"], 19 | data = [":random_string_java"], 20 | deps = [ 21 | ":java_runtime_environment", 22 | "//:jni_bind", 23 | ], 24 | ) 25 | -------------------------------------------------------------------------------- /java/com/google/README.md: -------------------------------------------------------------------------------- 1 | This binary is meant to demonstrate a simple binary that is entirely written in native. 2 | 3 | This is experimental. 4 | -------------------------------------------------------------------------------- /java/com/google/RandomString.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google; 18 | 19 | import java.util.Random; 20 | 21 | /** Just a toy model to make sure that we have a large data structure in memory. */ 22 | class RandomString { 23 | public RandomString() {} 24 | 25 | public static String generateRandomString(int length) { 26 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 27 | 28 | Random random = new Random(); 29 | StringBuilder sb = new StringBuilder(length); 30 | 31 | for (int i = 0; i < length; i++) { 32 | int randomIndex = random.nextInt(characters.length()); 33 | char randomChar = characters.charAt(randomIndex); 34 | sb.append(randomChar); 35 | } 36 | 37 | return sb.toString(); 38 | } 39 | 40 | public String format() { 41 | return generateRandomString(50); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/AndroidDefaultManifest.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/ClassLoaderHelperClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | /** A helper class to be loaded remotely. */ 20 | class ClassLoaderHelperClass { 21 | private final int intVal; 22 | 23 | public ClassLoaderHelperClass(int intVal) { 24 | this.intVal = intVal; 25 | } 26 | 27 | /** Returns the the value passed into the constructor */ 28 | public int getValue() { 29 | return intVal; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/ClassLoaderHelperSubclass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | /** 20 | * A subclass of {@link ClassLoaderHelperClass} that returns a different value. 21 | * 22 | *

This helps test when subclasses are placed in side of native wrappers for their parent 23 | * classes. 24 | */ 25 | class ClassLoaderHelperSubclass extends ClassLoaderHelperClass { 26 | public ClassLoaderHelperSubclass(int intVal) { 27 | super(intVal); 28 | } 29 | 30 | /** Returns the inverse of the value passed into the constructor */ 31 | @Override 32 | public int getValue() { 33 | return -super.getValue(); 34 | } 35 | 36 | /** 37 | * The wrapper currently does not have information about relationships between classes, so this 38 | * method explicitly returns an object as its parent class. 39 | */ 40 | public ClassLoaderHelperClass castToParent() { 41 | return this; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/FieldTestHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | import com.google.android.apps.common.proguard.UsedByNative; 20 | 21 | /** 22 | * This object is used to set mock expectations for FieldTest as inner classes do not support 23 | * mocking and the class definition must match the filename. 24 | */ 25 | public class FieldTestHelper { 26 | @UsedByNative("field_test_jni.cc") 27 | public int intField; 28 | 29 | @UsedByNative("field_test_jni.cc") 30 | public float floatField; 31 | 32 | @UsedByNative("field_test_jni.cc") 33 | public double doubleField; 34 | } 35 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/ObjectTestHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | /** Simple object for use with RJni object tests. */ 20 | public class ObjectTestHelper { 21 | public ObjectTestHelper() { 22 | intVal1 = 0; 23 | intVal2 = 0; 24 | intVal3 = 0; 25 | objectVal = null; 26 | } 27 | 28 | public ObjectTestHelper(Object objectVal) { 29 | this.objectVal = objectVal; 30 | } 31 | 32 | public ObjectTestHelper(int intVal) { 33 | this.intVal1 = intVal; 34 | } 35 | 36 | public ObjectTestHelper(int intVal, int intVal2) { 37 | this.intVal1 = intVal; 38 | this.intVal2 = intVal2; 39 | } 40 | 41 | public ObjectTestHelper(int intVal1, int intVal2, int intVal3) { 42 | this.intVal1 = intVal1; 43 | this.intVal2 = intVal2; 44 | this.intVal3 = intVal3; 45 | } 46 | 47 | public ObjectTestHelper returnNewObjectWithFieldSetToSum(int val1, int val2) { 48 | return new ObjectTestHelper(val1 + val2); 49 | } 50 | 51 | public void foo() {} 52 | 53 | public ObjectTestHelper returnNewObjectWithFieldSetToSum(ObjectTestHelper rhs) { 54 | return new ObjectTestHelper( 55 | this.intVal1 + rhs.intVal1, this.intVal2 + rhs.intVal2, this.intVal3 + rhs.intVal3); 56 | } 57 | 58 | public boolean isEqualTo(ObjectTestHelper rhs) { 59 | return this.intVal1 == rhs.intVal1 60 | && this.intVal2 == rhs.intVal2 61 | && this.intVal3 == rhs.intVal3; 62 | } 63 | 64 | public int intVal1; 65 | public int intVal2; 66 | public int intVal3; 67 | public Object objectVal; 68 | } 69 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/StringTestHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | import com.google.android.apps.common.proguard.UsedByNative; 20 | 21 | /** 22 | * This object is used to set mock expectations for StringTest as inner classes do not support 23 | * mocking and the class definition must match the filename. 24 | */ 25 | public class StringTestHelper { 26 | /** Void Methods. */ 27 | @UsedByNative("string_test_jni.cc") 28 | public void voidMethodTakesString(String s) {} 29 | 30 | @UsedByNative("string_test_jni.cc") 31 | public void voidMethodTakesTwoStrings(String s1, String s2) {} 32 | 33 | @UsedByNative("string_test_jni.cc") 34 | public void voidMethodTakesFiveStrings(String s1, String s2, String s3, String s4, String s5) {} 35 | 36 | /** String Methods. */ 37 | @UsedByNative("string_test_jni.cc") 38 | public String stringMethodTakesString(String s) { 39 | return s; 40 | } 41 | 42 | @UsedByNative("string_test_jni.cc") 43 | public String stringMethodTakesTwoStrings(String s1, String s2) { 44 | return s1 + s2; 45 | } 46 | 47 | @UsedByNative("string_test_jni.cc") 48 | public String stringMethodTakesFiveStrings( 49 | String s1, String s2, String s3, String s4, String s5) { 50 | return s1 + s2 + s3 + s4 + s5; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/ThreadTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.android; 18 | 19 | import static org.mockito.Mockito.verify; 20 | 21 | import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; 22 | import org.junit.AfterClass; 23 | import org.junit.Rule; 24 | import org.junit.Test; 25 | import org.junit.runner.RunWith; 26 | import org.mockito.Mock; 27 | import org.mockito.junit.MockitoJUnit; 28 | import org.mockito.junit.MockitoRule; 29 | 30 | @RunWith(AndroidJUnit4ClassRunner.class) 31 | public class ThreadTest { 32 | private static final String TAG = "ThreadTest"; 33 | 34 | @Mock public ObjectTestHelper objectTestHelper; 35 | @Rule public final MockitoRule mockito = MockitoJUnit.rule(); 36 | 37 | static { 38 | System.loadLibrary("thread_test_jni"); 39 | } 40 | 41 | public ThreadTest() {} 42 | 43 | @AfterClass 44 | public static void doShutDown() { 45 | jniTearDown(); 46 | } 47 | 48 | @Test 49 | public void runsThreadedWork() { 50 | runsThreadedWorkOnObject(this); 51 | verify(objectTestHelper).foo(); 52 | } 53 | 54 | // Tears down the JvmRef. 55 | static native void jniTearDown(); 56 | 57 | // Creates a thread, waits until it finishes some work, uses passed object. 58 | // native void RunsThreadedWorkOnObject(ObjectTestHelper objectTestHelper); 59 | native void runsThreadedWorkOnObject(ThreadTest testFixture); 60 | } 61 | -------------------------------------------------------------------------------- /javatests/com/jnibind/android/object_test_helper_jni.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 18 | #define JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 19 | 20 | #include "jni_bind.h" 21 | 22 | // clang-format off 23 | constexpr jni::Class kObjectClass { 24 | "java/lang/Object" 25 | }; 26 | 27 | constexpr jni::Class kObjectTestHelperClass { 28 | "com/jnibind/android/ObjectTestHelper", 29 | 30 | jni::Constructor<>{}, 31 | jni::Constructor{kObjectClass}, 32 | jni::Constructor{}, 33 | jni::Constructor{}, 34 | 35 | jni::Method{"returnNewObjectWithFieldSetToSum", 36 | jni::Overload{ 37 | jni::Return{jni::Class{"com/jnibind/android/ObjectTestHelper"}}, 38 | jni::Params{} 39 | }, 40 | jni::Overload{ 41 | jni::Return{jni::Class{"com/jnibind/android/ObjectTestHelper"}}, 42 | jni::Params{jni::Class{"com/jnibind/android/ObjectTestHelper"}} 43 | }, 44 | }, 45 | ::jni::Method{"foo", ::jni::Return{}, ::jni::Params{}}, 46 | 47 | jni::Field{"intVal1", int{}}, 48 | jni::Field{"intVal2", int{}}, 49 | jni::Field{"intVal3", int{}} 50 | }; 51 | // clang-format on 52 | 53 | #endif // JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 54 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/AndroidDefaultManifest.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/ClassLoaderHelperClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.jnibind.test; 17 | 18 | /** A helper class to be loaded remotely. */ 19 | public class ClassLoaderHelperClass { 20 | private final int intVal; 21 | 22 | public ClassLoaderHelperClass() { 23 | this.intVal = 123456; 24 | } 25 | 26 | public ClassLoaderHelperClass(int intVal) { 27 | this.intVal = intVal; 28 | } 29 | 30 | /** Returns the the value passed into the constructor */ 31 | public int getValue() { 32 | return intVal; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/FieldTestHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.test; 18 | 19 | /** 20 | * This object is used to set mock expectations for FieldTest as inner classes do not support 21 | * mocking and the class definition must match the filename. 22 | */ 23 | public class FieldTestHelper { 24 | public int intField = 0; 25 | public float floatField = 0; 26 | public double doubleField = 0; 27 | public String stringField = ""; 28 | 29 | FieldTestHelper() {} 30 | 31 | FieldTestHelper(int intField, float floatField, double doubleField) { 32 | this.intField = intField; 33 | this.floatField = floatField; 34 | this.doubleField = doubleField; 35 | } 36 | 37 | boolean isEqualTo(FieldTestHelper rhs) { 38 | return intField == rhs.intField 39 | && floatField == rhs.floatField 40 | && doubleField == rhs.doubleField 41 | && stringField.equals(rhs.stringField); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/StringTestHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.jnibind.test; 18 | 19 | /** 20 | * This object is used to set mock expectations for StringTest as inner classes do not support 21 | * mocking and the class definition must match the filename. 22 | */ 23 | public class StringTestHelper { 24 | /** Void Methods. */ 25 | public void voidMethodTakesString(String s) {} 26 | public void voidMethodTakesTwoStrings(String s1, String s2) {} 27 | public void voidMethodTakesFiveStrings(String s1, String s2, String s3, String s4, String s5) {} 28 | 29 | /** String Methods. */ 30 | public String stringMethodTakesString(String s) { 31 | return s; 32 | } 33 | 34 | public String stringMethodTakesTwoStrings(String s1, String s2) { 35 | return s1 + s2; 36 | } 37 | 38 | public String stringMethodTakesFiveStrings( 39 | String s1, String s2, String s3, String s4, String s5) { 40 | return s1 + s2 + s3 + s4 + s5; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/builder_jni.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2023 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "object_test_helper_jni.h" 20 | #include "jni_bind.h" 21 | 22 | #define JNI_MTHD(x, y, ...) \ 23 | x Java_com_jnibind_test_BuilderTest_##y(JNIEnv*, jclass, ##__VA_ARGS__) 24 | 25 | using ::jni::Class; 26 | using ::jni::Constructor; 27 | using ::jni::LocalObject; 28 | using ::jni::Method; 29 | using ::jni::Params; 30 | using ::jni::Return; 31 | using ::jni::Self; 32 | 33 | static std::unique_ptr> jvm; 34 | 35 | // clang-format off 36 | constexpr Class kBuilder { 37 | "com/jnibind/test/BuilderTest$Builder", 38 | 39 | Constructor<>{}, 40 | 41 | Method{"setOne", Return{Self{}}, Params{}}, 42 | Method{"setTwo", Return{Self{}}, Params{}}, 43 | Method{"setThree", Return{Self{}}, Params{}}, 44 | Method{"build", Return{kObjectTestHelperClass}}, 45 | }; 46 | // clang-format on 47 | 48 | extern "C" { 49 | 50 | JNI_BIND_EXPORT jint JNI_BIND_CALL JNI_OnLoad(JavaVM* pjvm, void* reserved) { 51 | jvm.reset(new jni::JvmRef(pjvm)); 52 | 53 | return JNI_VERSION_1_6; 54 | } 55 | 56 | JNI_MTHD(void, nativeJniTeardown) { jvm = nullptr; } 57 | 58 | JNI_MTHD(jobject, useBuilderToCreateObject) { 59 | return LocalObject{} 60 | .Call<"setOne">(111) 61 | .Call<"setTwo">(222) 62 | .Call<"setThree">(333) 63 | .Call<"build">() 64 | .Release(); 65 | } 66 | 67 | } // extern "C" 68 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/class_loader_test_jni.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "jni_bind.h" 20 | 21 | using ::jni::Class; 22 | using ::jni::ClassLoader; 23 | using ::jni::kDefaultClassLoader; 24 | using ::jni::LocalClassLoader; 25 | using ::jni::SupportedClassSet; 26 | 27 | static constexpr Class kClassLoaderHelperClass{ 28 | "com/jnibind/test/ClassLoaderHelperClass"}; 29 | 30 | static constexpr ClassLoader kTestClassLoader{ 31 | kDefaultClassLoader, SupportedClassSet{kClassLoaderHelperClass}}; 32 | 33 | static constexpr jni::Jvm kJvmWithCustomClassLoaderSupport{kTestClassLoader}; 34 | 35 | static std::unique_ptr> jvm; 36 | 37 | extern "C" { 38 | 39 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) { 40 | jvm.reset(new jni::JvmRef(pjvm)); 41 | 42 | return JNI_VERSION_1_6; 43 | } 44 | 45 | JNIEXPORT void JNICALL 46 | Java_com_jnibind_test_ClassLoaderTest_jniTearDown(JNIEnv* env, jclass) { 47 | jvm.reset(); 48 | } 49 | 50 | JNIEXPORT jobject JNICALL 51 | Java_com_jnibind_test_ClassLoaderTest_jniBuildNewObjectsFromClassLoader( 52 | JNIEnv* env, jclass, jobject jclass_loader_obj) { 53 | LocalClassLoader 54 | class_loader{jclass_loader_obj}; 55 | 56 | return class_loader.BuildLocalObject().Release(); 57 | } 58 | 59 | } // extern "C" 60 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/modulo.h: -------------------------------------------------------------------------------- 1 | #ifndef JNI_BIND_METAPROGRAMMING_MODULO_H_ 2 | #define JNI_BIND_METAPROGRAMMING_MODULO_H_ 3 | 4 | #include 5 | #include 6 | 7 | namespace jni { 8 | 9 | // Increments `val` by `increment_count`, wrapping to {0} at `max`. 10 | // This can be useful for generalising tests over different size types. 11 | template 12 | T Modulo(std::size_t increment_count, T val = T{0}, 13 | T max = std::numeric_limits::max()) { 14 | T ret = val; 15 | 16 | for (std::size_t i = 0; i < increment_count; ++i) { 17 | ret += T{1}; 18 | while (ret >= max) { 19 | ret -= max; 20 | } 21 | } 22 | 23 | return ret; 24 | } 25 | 26 | } // namespace jni 27 | 28 | #endif // JNI_BIND_METAPROGRAMMING_MODULO_H_ 29 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/modulo_test.cc: -------------------------------------------------------------------------------- 1 | #include "javatests/com/jnibind/test/modulo.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | using ::jni::Modulo; 8 | 9 | namespace { 10 | 11 | TEST(Modulo, SimpleIntTests) { 12 | EXPECT_EQ(Modulo(0), 0); 13 | EXPECT_EQ(Modulo(1), 1); 14 | EXPECT_EQ(Modulo(std::numeric_limits::max() - 1), 15 | std::numeric_limits::max() - 1); 16 | EXPECT_EQ(Modulo(std::numeric_limits::max()), 0); 17 | 18 | EXPECT_EQ(Modulo(0, 1), 1); 19 | EXPECT_EQ(Modulo(0, 5), 5); 20 | EXPECT_EQ(Modulo(1, 1), 2); 21 | EXPECT_EQ(Modulo(1, 5), 6); 22 | } 23 | 24 | } // namespace 25 | -------------------------------------------------------------------------------- /javatests/com/jnibind/test/object_test_helper_jni.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 18 | #define JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 19 | 20 | #include "jni_bind.h" 21 | 22 | // clang-format off 23 | constexpr jni::Class kObjectClass { 24 | "java/lang/Object" 25 | }; 26 | 27 | constexpr jni::Class kObjectTestHelperClass { 28 | "com/jnibind/test/ObjectTestHelper", 29 | 30 | ::jni::Constructor<>{}, 31 | ::jni::Constructor{kObjectClass}, 32 | ::jni::Constructor{}, 33 | ::jni::Constructor{}, 34 | 35 | ::jni::Method{"returnNewObjectWithFieldSetToSum", 36 | ::jni::Overload{ 37 | ::jni::Return{jni::Class{"com/jnibind/test/ObjectTestHelper"}}, 38 | ::jni::Params{} 39 | }, 40 | ::jni::Overload{ 41 | ::jni::Return{jni::Class{"com/jnibind/test/ObjectTestHelper"}}, 42 | ::jni::Params{jni::Class{"com/jnibind/test/ObjectTestHelper"}} 43 | }, 44 | }, 45 | 46 | ::jni::Method{"foo", ::jni::Return{}, ::jni::Params{}}, 47 | ::jni::Method{"increment", ::jni::Return{}, ::jni::Params{}}, 48 | ::jni::Method{"print", ::jni::Return{}, ::jni::Params{}}, 49 | 50 | ::jni::Field{"intVal1", int{}}, 51 | ::jni::Field{"intVal2", int{}}, 52 | ::jni::Field{"intVal3", int{}} 53 | }; 54 | 55 | // clang-format on 56 | 57 | #endif // JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H 58 | -------------------------------------------------------------------------------- /jni_bind_release_leader.inc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /******************************************************************************* 18 | * JNI Bind Version __JNI_BIND_VERSION__. 19 | * Beta Public Release. 20 | ******************************************************************************** 21 | * This header is the single header version which you can use to quickly test or 22 | * deploy in your own project without using Bazel. It is self contained. 23 | * 24 | * To use this header you must be compiling with clang and with C++17 support. 25 | * 26 | * It must also have jni.h in its build path. I.e. the following must compile: 27 | * #include 28 | * 29 | * See GitHub for sample API usage. 30 | * https://github.com/google/jni-bind 31 | * 32 | * 🚨 Are you enjoying JNI Bind? Please consider adding a ⭐️ on GitHub! 🚨 33 | * JNI Bind is the culmination of a lot of hard work and your support is greatly 34 | * appreciated. 35 | * 36 | * CODE BELOW IS AUTO GENERATED. 37 | *******************************************************************************/ 38 | 39 | #ifndef JNI_BIND_RELEASE_ 40 | #define JNI_BIND_RELEASE_ 41 | 42 | #include 43 | -------------------------------------------------------------------------------- /jni_bind_release_trailer.inc: -------------------------------------------------------------------------------- 1 | 2 | #endif // JNI_BIND_RELEASE_ 3 | -------------------------------------------------------------------------------- /jni_dep.h: -------------------------------------------------------------------------------- 1 | #ifndef JNI_BIND_JNI_HELPER_JNI_DEP_H_ 2 | #define JNI_BIND_JNI_HELPER_JNI_DEP_H_ 3 | 4 | // IWYU pragma: begin_exports 5 | #include 6 | 7 | // These declarations help prevent bad clang-tidy warnings. 8 | using jbyte = jbyte; 9 | using jboolean = jboolean; 10 | using jchar = jchar; 11 | using jshort = jshort; 12 | using jint = jint; 13 | using jlong = jlong; 14 | using jfloat = jfloat; 15 | using jdouble = jdouble; 16 | using jsize = jsize; 17 | 18 | using jobject = jobject; 19 | using jclass = jclass; 20 | using jthrowable = jthrowable; 21 | using jstring = jstring; 22 | using jarray = jarray; 23 | using jbooleanArray = jbooleanArray; 24 | using jbyteArray = jbyteArray; 25 | using jcharArray = jcharArray; 26 | using jshortArray = jshortArray; 27 | using jintArray = jintArray; 28 | using jlongArray = jlongArray; 29 | using jfloatArray = jfloatArray; 30 | using jdoubleArray = jdoubleArray; 31 | using jobjectArray = jobjectArray; 32 | 33 | using jweak = jweak; 34 | using jvalue = jvalue; 35 | using jfieldID = jfieldID; 36 | using jmethodID = jmethodID; 37 | 38 | using JavaVM = JavaVM; 39 | using JNIEnv = JNIEnv; 40 | 41 | // IWYU pragma: end_exports 42 | 43 | #endif // JNI_BIND_JNI_HELPER_JNI_DEP_H_ 44 | -------------------------------------------------------------------------------- /metaprogramming/all.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_METAPROGRAMMING_ALL_H_ 18 | #define JNI_BIND_METAPROGRAMMING_ALL_H_ 19 | 20 | #include 21 | 22 | namespace jni::metaprogramming { 23 | 24 | template 25 | struct All { 26 | template 27 | using type = 28 | typename std::conjunction...>::type; 29 | }; 30 | 31 | template 32 | using All_t = typename All::template type; 33 | 34 | template 35 | static constexpr bool All_v = All_t::value; 36 | 37 | } // namespace jni::metaprogramming 38 | 39 | #endif // JNI_BIND_METAPROGRAMMING_ALL_H_ 40 | -------------------------------------------------------------------------------- /metaprogramming/all_unique.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_ 18 | #define JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_ 19 | 20 | #include 21 | 22 | #include "contains.h" 23 | 24 | namespace jni::metaprogramming { 25 | 26 | template 27 | struct AllUnique { 28 | static constexpr bool value = true; 29 | using type = std::bool_constant; 30 | }; 31 | 32 | template 33 | struct AllUnique { 34 | static constexpr bool value = 35 | !Contains_v && AllUnique::value; 36 | using type = std::bool_constant; 37 | }; 38 | 39 | template 40 | static constexpr bool AllUnique_v = AllUnique::value; 41 | 42 | // Constexpr value implementation. 43 | constexpr bool AllUniqueValues(...) { return true; } 44 | 45 | template 46 | constexpr bool AllUniqueValues(const T1&& t1, const Ts&&... ts) { 47 | return (!ContainsValue(t1, ts...)) && AllUniqueValues(ts...); 48 | } 49 | 50 | } // namespace jni::metaprogramming 51 | 52 | #endif // JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_ 53 | -------------------------------------------------------------------------------- /metaprogramming/any.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_METAPROGRAMMING_ANY_H_ 18 | #define JNI_BIND_METAPROGRAMMING_ANY_H_ 19 | 20 | #include 21 | 22 | #include "tuple_manipulation.h" 23 | 24 | namespace jni::metaprogramming { 25 | 26 | template 27 | struct Any { 28 | template 29 | using type = 30 | typename std::disjunction...>::type; 31 | }; 32 | 33 | template 34 | using Any_t = typename Any::template type; 35 | 36 | template 37 | static constexpr bool Any_v = Any_t::value; 38 | 39 | template 40 | using Any_Tup = TupleUnroller_t, Ts>; 41 | 42 | template 43 | static constexpr bool Any_Tup_v = TupleUnroller_t, Ts>::value; 44 | 45 | } // namespace jni::metaprogramming 46 | 47 | #endif // JNI_BIND_METAPROGRAMMING_ANY_H_ 48 | -------------------------------------------------------------------------------- /metaprogramming/any_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "any.h" 18 | 19 | #include 20 | 21 | #include "invoke.h" 22 | #include "same.h" 23 | 24 | using ::jni::metaprogramming::Any; 25 | using ::jni::metaprogramming::Any_t; 26 | using ::jni::metaprogramming::Any_v; 27 | using ::jni::metaprogramming::Invoke_t; 28 | using ::jni::metaprogramming::Same; 29 | 30 | namespace { 31 | 32 | struct A {}; 33 | struct B {}; 34 | struct C {}; 35 | struct D {}; 36 | struct E {}; 37 | 38 | static_assert( 39 | std::is_same_v>, A, B, C, D>, std::true_type>); 40 | static_assert( 41 | std::is_same_v>, A, B, C, D>, std::true_type>); 42 | static_assert( 43 | std::is_same_v>, A, B, C, D>, std::true_type>); 44 | static_assert( 45 | std::is_same_v>, A, B, C, D>, std::true_type>); 46 | static_assert( 47 | std::is_same_v>, A, B, C, D>, std::false_type>); 48 | 49 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>); 50 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>); 51 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>); 52 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>); 53 | static_assert(std::is_same_v, A, B, C, D>, std::false_type>); 54 | 55 | static_assert(Any_v, A, B, C, D>); 56 | static_assert(Any_v, A, B, C, D>); 57 | static_assert(Any_v, A, B, C, D>); 58 | static_assert(Any_v, A, B, C, D>); 59 | static_assert(!Any_v, A, B, C, D>); 60 | 61 | } // namespace 62 | -------------------------------------------------------------------------------- /metaprogramming/apply.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Google LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef JNI_BIND_METAPROGRAMMING_APPLY_H_ 18 | #define JNI_BIND_METAPROGRAMMING_APPLY_H_ 19 | 20 | #include "tuple_manipulation.h" 21 | 22 | namespace jni::metaprogramming { 23 | 24 | // Forwards all arguments to a metafunction. Note, Apply is itself a type 25 | // whose |type| (aka its metafunction's invocation). 26 | template