diff --git a/lib/fb/BUCK b/lib/fb/BUCK deleted file mode 100644 index f283dca1..00000000 --- a/lib/fb/BUCK +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright (c) Facebook, Inc. and its affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. -load("@fbsource//tools/build_defs:platform_defs.bzl", "ANDROID", "APPLE", "CXX", "FBCODE", "WINDOWS") -load("//tools/build_defs/oss:yoga_defs.bzl", "JNI_TARGET", "YOGA_ROOTS", "subdir_glob", "yoga_cxx_library", "yoga_prebuilt_cxx_library") - -yoga_prebuilt_cxx_library( - name = "ndklog", - exported_platform_linker_flags = [ - ( - "^android.*", - ["-llog"], - ), - ], - header_only = True, - platforms = (ANDROID, APPLE, CXX, FBCODE, WINDOWS), - visibility = YOGA_ROOTS, -) - -yoga_cxx_library( - name = "fbjni", - srcs = glob(["src/main/cpp/**/*.cpp"]), - header_namespace = "", - exported_headers = subdir_glob([("src/main/cpp/include", "**/*.h")]), - compiler_flags = [ - "-DLOG_TAG=\"libfb\"", - "-DDISABLE_CPUCAP", - "-DDISABLE_XPLAT", - "-DHAVE_POSIX_CLOCKS", - "-fno-omit-frame-pointer", - "-fexceptions", - "-frtti", - "-Wall", - "-Werror", - "-Wno-unused-parameter", - "-Wno-unused-variable", - "-std=c++11", - ], - platforms = (CXX, ANDROID), - soname = "libfbjni.$(ext)", - visibility = ["PUBLIC"], - deps = [ - ":ndklog", - JNI_TARGET, - ], -) diff --git a/lib/fb/build.gradle b/lib/fb/build.gradle deleted file mode 100644 index 8194a2b6..00000000 --- a/lib/fb/build.gradle +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -apply plugin: 'com.android.library' - -android { - compileSdkVersion rootProject.compileSdkVersion - buildToolsVersion rootProject.buildToolsVersion - - defaultConfig { - minSdkVersion rootProject.minSdkVersion - targetSdkVersion rootProject.targetSdkVersion - - ndk { - abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' - } - - externalNativeBuild { - cmake { - arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static' - } - } - } - - externalNativeBuild { - cmake { - path 'src/main/cpp/CMakeLists.txt' - } - } - - dependencies { - implementation 'com.facebook.soloader:soloader:0.10.4' - implementation 'com.google.code.findbugs:jsr305:3.0.2' - implementation project(':yoga:proguard-annotations') - } -} diff --git a/lib/fb/src/main/AndroidManifest.xml b/lib/fb/src/main/AndroidManifest.xml deleted file mode 100644 index 6d0fd184..00000000 --- a/lib/fb/src/main/AndroidManifest.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - diff --git a/lib/fb/src/main/cpp/CMakeLists.txt b/lib/fb/src/main/cpp/CMakeLists.txt deleted file mode 100644 index 8a6a045c..00000000 --- a/lib/fb/src/main/cpp/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# -# Copyright (c) Facebook, Inc. and its affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. -# - -cmake_minimum_required(VERSION 3.4.1) - -set(CMAKE_VERBOSE_MAKEFILE on) - -add_compile_options( - -fno-omit-frame-pointer - -fexceptions - -Wall - -std=c++11 - -DDISABLE_CPUCAP - -DDISABLE_XPLAT) - -file(GLOB fb_SRC - *.cpp - jni/*.cpp - jni/detail/*.cpp - lyra/*.cpp) - -add_library(fb SHARED - ${fb_SRC}) - -target_include_directories(fb PRIVATE - include) - -target_link_libraries(fb android log) diff --git a/lib/fb/src/main/cpp/Doxyfile b/lib/fb/src/main/cpp/Doxyfile deleted file mode 100644 index b44118dd..00000000 --- a/lib/fb/src/main/cpp/Doxyfile +++ /dev/null @@ -1,15 +0,0 @@ -PROJECT_NAME = "Facebook Android Support" -JAVADOC_AUTOBRIEF = YES -EXTRACT_ALL = YES -RECURSIVE = YES -EXCLUDE = tests -EXCLUDE_PATTERNS = *.cpp -GENERATE_HTML = YES -GENERATE_LATEX = NO -ENABLE_PREPROCESSING = YES -HIDE_UNDOC_MEMBERS = YES -HIDE_SCOPE_NAMES = YES -HIDE_FRIEND_COMPOUNDS = YES -HIDE_UNDOC_CLASSES = YES -SHOW_INCLUDE_FILES = NO -#ENABLED_SECTIONS = INTERNAL diff --git a/lib/fb/src/main/cpp/include/fb/Doxyfile b/lib/fb/src/main/cpp/include/fb/Doxyfile deleted file mode 100644 index 8b4df6a7..00000000 --- a/lib/fb/src/main/cpp/include/fb/Doxyfile +++ /dev/null @@ -1,18 +0,0 @@ -PROJECT_NAME = "Facebook JNI" -PROJECT_BRIEF = "Helper library to provide safe and convenient access to JNI with very low overhead" -JAVADOC_AUTOBRIEF = YES -EXTRACT_ALL = YES -RECURSIVE = YES -EXCLUDE = tests Asserts.h Countable.h GlobalReference.h LocalReference.h LocalString.h Registration.h WeakReference.h jni_helpers.h Environment.h -EXCLUDE_PATTERNS = *-inl.h *.cpp -GENERATE_HTML = YES -GENERATE_LATEX = NO -ENABLE_PREPROCESSING = YES -HIDE_UNDOC_MEMBERS = YES -HIDE_SCOPE_NAMES = YES -HIDE_FRIEND_COMPOUNDS = YES -HIDE_UNDOC_CLASSES = YES -SHOW_INCLUDE_FILES = NO -PREDEFINED = LOG_TAG=fbjni -EXAMPLE_PATH = samples -#ENABLED_SECTIONS = INTERNAL diff --git a/lib/fb/src/main/cpp/include/fbjni/ByteBuffer.h b/lib/fb/src/main/cpp/include/fbjni/ByteBuffer.h deleted file mode 100644 index 55223e46..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/ByteBuffer.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -namespace facebook { -namespace jni { - -class JBuffer : public JavaClass { -public: - static constexpr const char* kJavaDescriptor = "Ljava/nio/Buffer;"; - - void rewind() const; - bool isDirect() const; - void* getDirectAddress() const; - size_t getDirectCapacity() const; -}; - -// JNI's NIO support has some awkward preconditions and error reporting. This -// class provides much more user-friendly access. -class JByteBuffer : public JavaClass { - public: - static constexpr const char* kJavaDescriptor = "Ljava/nio/ByteBuffer;"; - - static local_ref wrapBytes(uint8_t* data, size_t size); - static local_ref allocateDirect(jint size); - - uint8_t* getDirectBytes() const { - return static_cast(getDirectAddress()); - } - - size_t getDirectSize() const { - return getDirectCapacity(); - } -}; - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/Context.h b/lib/fb/src/main/cpp/include/fbjni/Context.h deleted file mode 100644 index 331de691..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/Context.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include - -namespace facebook { -namespace jni { - -class AContext : public JavaClass { - public: - static constexpr const char* kJavaDescriptor = "Landroid/content/Context;"; - - // Define a method that calls into the represented Java class - local_ref getCacheDir() { - static const auto method = getClass()->getMethod("getCacheDir"); - return method(self()); - } - - local_ref getFilesDir() { - static const auto method = getClass()->getMethod("getFilesDir"); - return method(self()); - } -}; - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/File.h b/lib/fb/src/main/cpp/include/fbjni/File.h deleted file mode 100644 index 852202ba..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/File.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -namespace facebook { -namespace jni { - -class JFile : public JavaClass { - public: - static constexpr const char* kJavaDescriptor = "Ljava/io/File;"; - - // Define a method that calls into the represented Java class - std::string getAbsolutePath() { - static const auto method = getClass()->getMethod("getAbsolutePath"); - return method(self())->toStdString(); - } - -}; - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/JThread.h b/lib/fb/src/main/cpp/include/fbjni/JThread.h deleted file mode 100644 index a343e022..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/JThread.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include - -namespace facebook { -namespace jni { - -class JThread : public JavaClass { - public: - static constexpr const char* kJavaDescriptor = "Ljava/lang/Thread;"; - - void start() { - static const auto method = javaClassStatic()->getMethod("start"); - method(self()); - } - - void join() { - static const auto method = javaClassStatic()->getMethod("join"); - method(self()); - } - - static local_ref create(std::function&& runnable) { - auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); - return newInstance(static_ref_cast(jrunnable)); - } - - static local_ref create(std::function&& runnable, std::string&& name) { - auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); - return newInstance(static_ref_cast(jrunnable), make_jstring(std::move(name))); - } - - static local_ref getCurrent() { - static const auto method = javaClassStatic()->getStaticMethod()>("currentThread"); - return method(javaClassStatic()); - } - - int getPriority() { - static const auto method = getClass()->getMethod("getPriority"); - return method(self()); - } - - void setPriority(int priority) { - static const auto method = getClass()->getMethod("setPriority"); - method(self(), priority); - } -}; - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/NativeRunnable.h b/lib/fb/src/main/cpp/include/fbjni/NativeRunnable.h deleted file mode 100644 index 7bb915f4..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/NativeRunnable.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -#include - -namespace facebook { -namespace jni { - -struct JRunnable : public JavaClass { - static auto constexpr kJavaDescriptor = "Ljava/lang/Runnable;"; -}; - -struct JNativeRunnable : public HybridClass { - public: - static auto constexpr kJavaDescriptor = "Lcom/facebook/jni/NativeRunnable;"; - - JNativeRunnable(std::function&& runnable) : runnable_(std::move(runnable)) {} - - static void OnLoad() { - registerHybrid({ - makeNativeMethod("run", JNativeRunnable::run), - }); - } - - void run() { - runnable_(); - } - - private: - std::function runnable_; -}; - - -} // namespace jni -} // namespace facebook diff --git a/lib/fb/src/main/cpp/include/fbjni/ReadableByteChannel.h b/lib/fb/src/main/cpp/include/fbjni/ReadableByteChannel.h deleted file mode 100644 index f524d26c..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/ReadableByteChannel.h +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include - -namespace facebook { -namespace jni { - -class JReadableByteChannel : public JavaClass { -public: - static constexpr const char* kJavaDescriptor = "Ljava/nio/channels/ReadableByteChannel;"; - - int read(alias_ref dest) const; -}; - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Boxed.h b/lib/fb/src/main/cpp/include/fbjni/detail/Boxed.h deleted file mode 100644 index e231b958..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Boxed.h +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include "CoreClasses.h" - -namespace facebook { -namespace jni { - -namespace detail { -template -struct JPrimitive : JavaClass { - using typename JavaClass::javaobject; - using JavaClass::javaClassStatic; - static local_ref valueOf(jprim val) { - static const auto cls = javaClassStatic(); - static const auto method = - cls->template getStaticMethod("valueOf"); - return method(cls, val); - } - jprim value() const { - static const auto method = - javaClassStatic()->template getMethod(T::kValueMethod); - return method(this->self()); - } -}; - -} // namespace detail - - -#define DEFINE_BOXED_PRIMITIVE(LITTLE, BIG) \ - struct J ## BIG : detail::JPrimitive { \ - static auto constexpr kJavaDescriptor = "Ljava/lang/" #BIG ";"; \ - static auto constexpr kValueMethod = #LITTLE "Value"; \ - j ## LITTLE LITTLE ## Value() const { \ - return value(); \ - } \ - }; \ - inline local_ref autobox(j ## LITTLE val) { \ - return J ## BIG::valueOf(val); \ - } - -DEFINE_BOXED_PRIMITIVE(boolean, Boolean) -DEFINE_BOXED_PRIMITIVE(byte, Byte) -DEFINE_BOXED_PRIMITIVE(char, Character) -DEFINE_BOXED_PRIMITIVE(short, Short) -DEFINE_BOXED_PRIMITIVE(int, Integer) -DEFINE_BOXED_PRIMITIVE(long, Long) -DEFINE_BOXED_PRIMITIVE(float, Float) -DEFINE_BOXED_PRIMITIVE(double, Double) - -#undef DEFINE_BOXED_PRIMITIVE - -template -inline typename std::enable_if< - (std::is_same::value || std::is_same::value) && !std::is_same::value, - local_ref ->::type autobox(T val) { - return JLong::valueOf(val); -} - -struct JVoid : public jni::JavaClass { - static auto constexpr kJavaDescriptor = "Ljava/lang/Void;"; -}; - -inline local_ref autobox(alias_ref val) { - return make_local(val); -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Common.h b/lib/fb/src/main/cpp/include/fbjni/detail/Common.h deleted file mode 100644 index 573fcc75..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Common.h +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -/** @file Common.h - * - * Defining the stuff that don't deserve headers of their own... - */ - -#pragma once - -#include - -#include - -#ifdef FBJNI_DEBUG_REFS -# ifdef __ANDROID__ -# include -# else -# include -# endif -#endif - -// If a pending JNI Java exception is found, wraps it in a JniException object and throws it as -// a C++ exception. -#define FACEBOOK_JNI_THROW_PENDING_EXCEPTION() \ - ::facebook::jni::throwPendingJniExceptionAsCppException() - -// If the condition is true, throws a JniException object, which wraps the pending JNI Java -// exception if any. If no pending exception is found, throws a JniException object that wraps a -// RuntimeException throwable.  -#define FACEBOOK_JNI_THROW_EXCEPTION_IF(CONDITION) \ - ::facebook::jni::throwCppExceptionIf(CONDITION) - -/// @cond INTERNAL - -namespace facebook { -namespace jni { - -void throwPendingJniExceptionAsCppException(); -void throwCppExceptionIf(bool condition); - -[[noreturn]] void throwNewJavaException(jthrowable); -[[noreturn]] void throwNewJavaException(const char* throwableName, const char* msg); -template -[[noreturn]] void throwNewJavaException(const char* throwableName, const char* fmt, Args... args); - - -/** - * This needs to be called at library load time, typically in your JNI_OnLoad method. - * - * The intended use is to return the result of initialize() directly - * from JNI_OnLoad and to do nothing else there. Library specific - * initialization code should go in the function passed to initialize - * (which can be, and probably should be, a C++ lambda). This approach - * provides correct error handling and translation errors during - * initialization into Java exceptions when appropriate. - * - * Failure to call this will cause your code to crash in a remarkably - * unhelpful way (typically a segfault) while trying to handle an exception - * which occurs later. - */ -jint initialize(JavaVM*, std::function&&) noexcept; - -namespace internal { - -// Define to get extremely verbose logging of references and to enable reference stats -#ifdef FBJNI_DEBUG_REFS -template -inline void dbglog(const char* msg, Args... args) { -# ifdef __ANDROID__ - __android_log_print(ANDROID_LOG_VERBOSE, "fbjni_dbg", msg, args...); -# else - std::fprintf(stderr, msg, args...); -# endif -} - -#else - -template -inline void dbglog(const char*, Args...) { -} - -#endif - -}}} - -/// @endcond diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses-inl.h deleted file mode 100644 index d8214fde..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses-inl.h +++ /dev/null @@ -1,677 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include -#include - -#include "Common.h" -#include "Exceptions.h" -#include "Meta.h" -#include "MetaConvert.h" - -namespace facebook { -namespace jni { - -// jobject ///////////////////////////////////////////////////////////////////////////////////////// - -inline bool isSameObject(alias_ref lhs, alias_ref rhs) noexcept { - return Environment::current()->IsSameObject(lhs.get(), rhs.get()) != JNI_FALSE; -} - -inline local_ref JObject::getClass() const noexcept { - return adopt_local(Environment::current()->GetObjectClass(self())); -} - -inline bool JObject::isInstanceOf(alias_ref cls) const noexcept { - return Environment::current()->IsInstanceOf(self(), cls.get()) != JNI_FALSE; -} - -template -inline T JObject::getFieldValue(JField field) const noexcept { - return field.get(self()); -} - -template -inline local_ref JObject::getFieldValue(JField field) const noexcept { - return adopt_local(field.get(self())); -} - -template -inline void JObject::setFieldValue(JField field, T value) noexcept { - field.set(self(), value); -} - -template -inline void JObject::setFieldValue(JField field, alias_ref value) noexcept { - setFieldValue(field, value.get()); -} - -inline std::string JObject::toString() const { - static const auto method = findClassLocal("java/lang/Object")->getMethod("toString"); - - return method(self())->toStdString(); -} - - -// Class is here instead of CoreClasses.h because we need -// alias_ref to be complete. -class MonitorLock { - public: - inline MonitorLock() noexcept; - inline MonitorLock(alias_ref object) noexcept; - inline ~MonitorLock() noexcept; - - inline MonitorLock(MonitorLock&& other) noexcept; - inline MonitorLock& operator=(MonitorLock&& other) noexcept; - - inline MonitorLock(const MonitorLock&) = delete; - inline MonitorLock& operator=(const MonitorLock&) = delete; - - private: - inline void reset() noexcept; - alias_ref owned_; -}; - -MonitorLock::MonitorLock() noexcept : owned_(nullptr) {} - -MonitorLock::MonitorLock(alias_ref object) noexcept - : owned_(object) { - Environment::current()->MonitorEnter(object.get()); -} - -void MonitorLock::reset() noexcept { - if (owned_) { - Environment::current()->MonitorExit(owned_.get()); - if (Environment::current()->ExceptionCheck()) { - abort(); // Lock mismatch - } - owned_ = nullptr; - } -} - -MonitorLock::~MonitorLock() noexcept { - reset(); -} - -MonitorLock::MonitorLock(MonitorLock&& other) noexcept - : owned_(other.owned_) -{ - other.owned_ = nullptr; -} - -MonitorLock& MonitorLock::operator=(MonitorLock&& other) noexcept { - reset(); - owned_ = other.owned_; - other.owned_ = nullptr; - return *this; -} - -inline MonitorLock JObject::lock() const noexcept { - return MonitorLock(this_); -} - -inline jobject JObject::self() const noexcept { - return this_; -} - -inline void swap(JObject& a, JObject& b) noexcept { - using std::swap; - swap(a.this_, b.this_); -} - -// JavaClass /////////////////////////////////////////////////////////////////////////////////////// - -namespace detail { -template -static local_ref newInstance(Args... args) { - static auto cls = JC::javaClassStatic(); - static const auto constructor = cls->template getConstructor(); - return cls->newObject(constructor, args...); -} -} - - -template -auto JavaClass::self() const noexcept -> javaobject { - return static_cast(JObject::self()); -} - -// jclass ////////////////////////////////////////////////////////////////////////////////////////// - -namespace detail { - -// This is not a real type. It is used so people won't accidentally -// use a void* to initialize a NativeMethod. -struct NativeMethodWrapper; - -} - -struct NativeMethod { - const char* name; - std::string descriptor; - detail::NativeMethodWrapper* wrapper; -}; - -inline local_ref JClass::getSuperclass() const noexcept { - return adopt_local(Environment::current()->GetSuperclass(self())); -} - -inline void JClass::registerNatives(std::initializer_list methods) { - const auto env = Environment::current(); - - JNINativeMethod jnimethods[methods.size()]; - size_t i = 0; - for (auto it = methods.begin(); it < methods.end(); ++it, ++i) { - // The JNI struct members are unnecessarily non-const. - jnimethods[i].name = const_cast(it->name); - jnimethods[i].signature = const_cast(it->descriptor.c_str()); - jnimethods[i].fnPtr = reinterpret_cast(it->wrapper); - } - - auto result = env->RegisterNatives(self(), jnimethods, methods.size()); - FACEBOOK_JNI_THROW_EXCEPTION_IF(result != JNI_OK); -} - -inline bool JClass::isAssignableFrom(alias_ref other) const noexcept { - const auto env = Environment::current(); - // Ths method has behavior compatible with the - // java.lang.Class#isAssignableFrom method. The order of the - // arguments to the JNI IsAssignableFrom C function is "opposite" - // from what some might expect, which makes this code look a little - // odd, but it is correct. - const auto result = env->IsAssignableFrom(other.get(), self()); - return result; -} - -template -inline JConstructor JClass::getConstructor() const { - return getConstructor(jmethod_traits_from_cxx::constructor_descriptor().c_str()); -} - -template -inline JConstructor JClass::getConstructor(const char* descriptor) const { - constexpr auto constructor_method_name = ""; - return getMethod(constructor_method_name, descriptor); -} - -template -inline JMethod JClass::getMethod(const char* name) const { - return getMethod(name, jmethod_traits_from_cxx::descriptor().c_str()); -} - -template -inline JMethod JClass::getMethod( - const char* name, - const char* descriptor) const { - const auto env = Environment::current(); - const auto method = env->GetMethodID(self(), name, descriptor); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!method); - return JMethod{method}; -} - -template -inline JStaticMethod JClass::getStaticMethod(const char* name) const { - return getStaticMethod(name, jmethod_traits_from_cxx::descriptor().c_str()); -} - -template -inline JStaticMethod JClass::getStaticMethod( - const char* name, - const char* descriptor) const { - const auto env = Environment::current(); - const auto method = env->GetStaticMethodID(self(), name, descriptor); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!method); - return JStaticMethod{method}; -} - -template -inline JNonvirtualMethod JClass::getNonvirtualMethod(const char* name) const { - return getNonvirtualMethod(name, jmethod_traits_from_cxx::descriptor().c_str()); -} - -template -inline JNonvirtualMethod JClass::getNonvirtualMethod( - const char* name, - const char* descriptor) const { - const auto env = Environment::current(); - const auto method = env->GetMethodID(self(), name, descriptor); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!method); - return JNonvirtualMethod{method}; -} - -template -inline JField(), T>> -JClass::getField(const char* name) const { - return getField(name, jtype_traits::descriptor().c_str()); -} - -template -inline JField(), T>> JClass::getField( - const char* name, - const char* descriptor) const { - const auto env = Environment::current(); - auto field = env->GetFieldID(self(), name, descriptor); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!field); - return JField{field}; -} - -template -inline JStaticField(), T>> JClass::getStaticField( - const char* name) const { - return getStaticField(name, jtype_traits::descriptor().c_str()); -} - -template -inline JStaticField(), T>> JClass::getStaticField( - const char* name, - const char* descriptor) const { - const auto env = Environment::current(); - auto field = env->GetStaticFieldID(self(), name, descriptor); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!field); - return JStaticField{field}; -} - -template -inline T JClass::getStaticFieldValue(JStaticField field) const noexcept { - return field.get(self()); -} - -template -inline local_ref JClass::getStaticFieldValue(JStaticField field) noexcept { - return adopt_local(field.get(self())); -} - -template -inline void JClass::setStaticFieldValue(JStaticField field, T value) noexcept { - field.set(self(), value); -} - -template -inline void JClass::setStaticFieldValue(JStaticField field, alias_ref value) noexcept { - setStaticFieldValue(field, value.get()); -} - -template -inline local_ref JClass::newObject( - JConstructor constructor, - Args... args) const { - const auto env = Environment::current(); - auto object = env->NewObject(self(), constructor.getId(), - detail::callToJni( - detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!object); - return adopt_local(static_cast(object)); -} - -inline jclass JClass::self() const noexcept { - return static_cast(JObject::self()); -} - -inline void registerNatives(const char* name, std::initializer_list methods) { - findClassLocal(name)->registerNatives(methods); -} - - -// jstring ///////////////////////////////////////////////////////////////////////////////////////// - -inline local_ref make_jstring(const std::string& modifiedUtf8) { - return make_jstring(modifiedUtf8.c_str()); -} - -namespace detail { -// convert to std::string from jstring -template <> -struct Convert { - typedef jstring jniType; - static std::string fromJni(jniType t) { - return wrap_alias(t)->toStdString(); - } - static jniType toJniRet(const std::string& t) { - return make_jstring(t).release(); - } - static local_ref toCall(const std::string& t) { - return make_jstring(t); - } -}; - -// convert return from const char* -template <> -struct Convert { - typedef jstring jniType; - // no automatic synthesis of const char*. (It can't be freed.) - static jniType toJniRet(const char* t) { - return make_jstring(t).release(); - } - static local_ref toCall(const char* t) { - return make_jstring(t); - } -}; -} - -// jtypeArray ////////////////////////////////////////////////////////////////////////////////////// - -namespace detail { -inline size_t JArray::size() const noexcept { - const auto env = Environment::current(); - return env->GetArrayLength(self()); -} -} - -namespace detail { -template -inline ElementProxy::ElementProxy( - Target* target, - size_t idx) - : target_{target}, idx_{idx} {} - -template -inline ElementProxy& ElementProxy::operator=(const T& o) { - target_->setElement(idx_, o); - return *this; -} - -template -inline ElementProxy& ElementProxy::operator=(alias_ref& o) { - target_->setElement(idx_, o.get()); - return *this; -} - -template -inline ElementProxy& ElementProxy::operator=(alias_ref&& o) { - target_->setElement(idx_, o.get()); - return *this; -} - -template -inline ElementProxy& ElementProxy::operator=(const ElementProxy& o) { - auto src = o.target_->getElement(o.idx_); - target_->setElement(idx_, src.get()); - return *this; -} - -template -inline ElementProxy::ElementProxy::operator const local_ref () const { - return target_->getElement(idx_); -} - -template -inline ElementProxy::ElementProxy::operator local_ref () { - return target_->getElement(idx_); -} -} - -template -std::string JArrayClass::get_instantiated_java_descriptor() { - return "[" + jtype_traits::descriptor(); -}; - -template -std::string JArrayClass::get_instantiated_base_name() { - return get_instantiated_java_descriptor(); -}; - -template -auto JArrayClass::newArray(size_t size) -> local_ref { - static const auto elementClass = findClassStatic(jtype_traits::base_name().c_str()); - const auto env = Environment::current(); - auto rawArray = env->NewObjectArray(size, elementClass.get(), nullptr); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!rawArray); - return adopt_local(static_cast(rawArray)); -} - -template -inline void JArrayClass::setElement(size_t idx, const T& value) { - const auto env = Environment::current(); - env->SetObjectArrayElement(this->self(), idx, value); -} - -template -inline local_ref JArrayClass::getElement(size_t idx) { - const auto env = Environment::current(); - auto rawElement = env->GetObjectArrayElement(this->self(), idx); - return adopt_local(static_cast(rawElement)); -} - -template -inline detail::ElementProxy> JArrayClass::operator[](size_t index) { - return detail::ElementProxy>(this, index); -} - -template -local_ref::javaobject> adopt_local_array(jobjectArray ref) { - return adopt_local(static_cast::javaobject>(ref)); -} - -// jarray ///////////////////////////////////////////////////////////////////////////////////////// - -template -auto JPrimitiveArray::getRegion(jsize start, jsize length) - -> std::unique_ptr { - auto buf = std::unique_ptr{new T[length]}; - getRegion(start, length, buf.get()); - return buf; -} - -template -std::string JPrimitiveArray::get_instantiated_java_descriptor() { - return jtype_traits::descriptor(); -} -template -std::string JPrimitiveArray::get_instantiated_base_name() { - return JPrimitiveArray::get_instantiated_java_descriptor(); -} - -template -auto JPrimitiveArray::pin() -> PinnedPrimitiveArray> { - return PinnedPrimitiveArray>{this->self(), 0, 0}; -} - -template -auto JPrimitiveArray::pinRegion(jsize start, jsize length) - -> PinnedPrimitiveArray> { - return PinnedPrimitiveArray>{this->self(), start, length}; -} - -template -auto JPrimitiveArray::pinCritical() - -> PinnedPrimitiveArray> { - return PinnedPrimitiveArray>{this->self(), 0, 0}; -} - -template -class PinnedArrayAlloc { - public: - static void allocate( - alias_ref::array_type> array, - jsize start, - jsize length, - T** elements, - size_t* size, - jboolean* isCopy) { - (void) start; - (void) length; - *elements = array->getElements(isCopy); - *size = array->size(); - } - static void release( - alias_ref::array_type> array, - T* elements, - jint start, - jint size, - jint mode) { - (void) start; - (void) size; - array->releaseElements(elements, mode); - } -}; - -template -class PinnedCriticalAlloc { - public: - static void allocate( - alias_ref::array_type> array, - jsize start, - jsize length, - T** elements, - size_t* size, - jboolean* isCopy) { - (void)start; - (void)length; - const auto env = Environment::current(); - *elements = static_cast(env->GetPrimitiveArrayCritical(array.get(), isCopy)); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!elements); - *size = array->size(); - } - static void release( - alias_ref::array_type> array, - T* elements, - jint start, - jint size, - jint mode) { - (void)start; - (void)size; - const auto env = Environment::current(); - env->ReleasePrimitiveArrayCritical(array.get(), elements, mode); - } -}; - -template -class PinnedRegionAlloc { - public: - static void allocate( - alias_ref::array_type> array, - jsize start, - jsize length, - T** elements, - size_t* size, - jboolean* isCopy) { - auto buf = array->getRegion(start, length); - FACEBOOK_JNI_THROW_EXCEPTION_IF(!buf); - *elements = buf.release(); - *size = length; - *isCopy = true; - } - static void release( - alias_ref::array_type> array, - T* elements, - jint start, - jint size, - jint mode) { - std::unique_ptr holder; - if (mode == 0 || mode == JNI_ABORT) { - holder.reset(elements); - } - if (mode == 0 || mode == JNI_COMMIT) { - array->setRegion(start, size, elements); - } - } -}; - -// PinnedPrimitiveArray /////////////////////////////////////////////////////////////////////////// - -template -PinnedPrimitiveArray::PinnedPrimitiveArray(PinnedPrimitiveArray&& o) { - *this = std::move(o); -} - -template -PinnedPrimitiveArray& -PinnedPrimitiveArray::operator=(PinnedPrimitiveArray&& o) { - if (array_) { - release(); - } - array_ = std::move(o.array_); - elements_ = o.elements_; - isCopy_ = o.isCopy_; - size_ = o.size_; - start_ = o.start_; - o.clear(); - return *this; -} - -template -T* PinnedPrimitiveArray::get() { - return elements_; -} - -template -inline void PinnedPrimitiveArray::release() { - releaseImpl(0); - clear(); -} - -template -inline void PinnedPrimitiveArray::commit() { - releaseImpl(JNI_COMMIT); -} - -template -inline void PinnedPrimitiveArray::abort() { - releaseImpl(JNI_ABORT); - clear(); -} - -template -inline void PinnedPrimitiveArray::releaseImpl(jint mode) { - FACEBOOK_JNI_THROW_EXCEPTION_IF(array_.get() == nullptr); - Alloc::release(array_, elements_, start_, size_, mode); -} - -template -inline void PinnedPrimitiveArray::clear() noexcept { - array_ = nullptr; - elements_ = nullptr; - isCopy_ = false; - start_ = 0; - size_ = 0; -} - -template -inline T& PinnedPrimitiveArray::operator[](size_t index) { - FACEBOOK_JNI_THROW_EXCEPTION_IF(elements_ == nullptr); - return elements_[index]; -} - -template -inline bool PinnedPrimitiveArray::isCopy() const noexcept { - return isCopy_ == JNI_TRUE; -} - -template -inline size_t PinnedPrimitiveArray::size() const noexcept { - return size_; -} - -template -inline PinnedPrimitiveArray::~PinnedPrimitiveArray() noexcept { - if (elements_) { - release(); - } -} - -template -inline PinnedPrimitiveArray::PinnedPrimitiveArray(alias_ref::array_type> array, jint start, jint length) { - array_ = array; - start_ = start; - Alloc::allocate(array, start, length, &elements_, &size_, &isCopy_); -} - -template -inline alias_ref JavaClass::javaClassStatic() { - static auto cls = findClassStatic(jtype_traits::base_name().c_str()); - return cls; -} - -template -inline local_ref JavaClass::javaClassLocal() { - std::string className(jtype_traits::base_name().c_str()); - return findClassLocal(className.c_str()); -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses.h b/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses.h deleted file mode 100644 index 53b4118b..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/CoreClasses.h +++ /dev/null @@ -1,625 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -/** @file CoreClasses.h - * - * In CoreClasses.h wrappers for the core classes (jobject, jclass, and jstring) is defined - * to provide access to corresponding JNI functions + some conveniance. - */ - -#include "References-forward.h" -#include "Meta-forward.h" -#include "TypeTraits.h" - -#include - -#include - -namespace facebook { -namespace jni { - -class JClass; -class JObject; - -namespace detail { - -/// Lookup a class by name. This should only be used internally. -jclass findClass(JNIEnv* env, const char* name); - -} - -/// Lookup a class by name. Note this functions returns an alias_ref that -/// points to a leaked global reference. This is appropriate for classes -/// that are never unloaded (which is any class in an Android app and most -/// Java programs). -/// -/// The most common use case for this is storing the result -/// in a "static auto" variable, or a static global. -/// -/// @return Returns a leaked global reference to the class -alias_ref findClassStatic(const char* name); - -/// Lookup a class by name. Note this functions returns a local reference, -/// which means that it must not be stored in a static variable. -/// -/// The most common use case for this is one-time initialization -/// (like caching method ids). -/// -/// @return Returns a global reference to the class -local_ref findClassLocal(const char* name); - -/// Check to see if two references refer to the same object. Comparison with nullptr -/// returns true if and only if compared to another nullptr. A weak reference that -/// refers to a reclaimed object count as nullptr. -bool isSameObject(alias_ref lhs, alias_ref rhs) noexcept; - -// Together, these classes allow convenient use of any class with the fbjni -// helpers. To use: -// -// struct MyClass : public JavaClass { -// constexpr static auto kJavaDescriptor = "Lcom/example/package/MyClass;"; -// }; -// -// Then, an alias_ref will be backed by an instance of -// MyClass. JavaClass provides a convenient way to add functionality to these -// smart references. -// -// For example: -// -// struct MyClass : public JavaClass { -// constexpr static auto kJavaDescriptor = "Lcom/example/package/MyClass;"; -// -// void foo() { -// static const auto method = javaClassStatic()->getMethod("foo"); -// method(self()); -// } -// -// static local_ref create(int i) { -// return newInstance(i); -// } -// }; -// -// auto obj = MyClass::create(10); -// obj->foo(); -// -// While users of a JavaClass-type can lookup methods and fields through the -// underlying JClass, those calls can only be checked at runtime. It is recommended -// that the JavaClass-type instead explicitly expose it's methods as in the example -// above. - -namespace detail { -template -static local_ref newInstance(Args... args); -} - -class MonitorLock; - -class JObject : detail::JObjectBase { -public: - static constexpr auto kJavaDescriptor = "Ljava/lang/Object;"; - - static constexpr const char* get_instantiated_java_descriptor() { return nullptr; } - static constexpr const char* get_instantiated_base_name() { return nullptr; } - - /// Get a @ref local_ref of the object's class - local_ref getClass() const noexcept; - - /// Checks if the object is an instance of a class - bool isInstanceOf(alias_ref cls) const noexcept; - - /// Get the primitive value of a field - template - T getFieldValue(JField field) const noexcept; - - /// Get and wrap the value of a field in a @ref local_ref - template - local_ref getFieldValue(JField field) const noexcept; - - /// Set the value of field. Any Java type is accepted. - template - void setFieldValue(JField field, T value) noexcept; - template(), T>::type> - void setFieldValue(JField field, alias_ref value) noexcept; - - /// Convenience method to create a std::string representing the object - std::string toString() const; - - // Take this object's monitor lock - MonitorLock lock() const noexcept; - - typedef _jobject _javaobject; - typedef _javaobject* javaobject; - -protected: - jobject self() const noexcept; -private: - friend void swap(JObject& a, JObject& b) noexcept; - template - friend struct detail::ReprAccess; - template - friend class JavaClass; - - template - friend class JObjectWrapper; -}; - -// This is only to maintain backwards compatibility with things that are -// already providing a specialization of JObjectWrapper. Any such instances -// should be updated to use a JavaClass. -template<> -class JObjectWrapper : public JObject { -}; - - -namespace detail { -template -struct JTypeFor { - static_assert( - std::is_base_of< - std::remove_pointer::type, - typename std::remove_pointer::type - >::value, ""); - using _javaobject = typename std::remove_pointer::type; - using javaobject = JType; -}; - -template -struct JTypeFor { - // JNI pattern for jobject assignable pointer - struct _javaobject : Base::_javaobject { - // This allows us to map back to the defining type (in ReprType, for - // example). - typedef T JniRefRepr; - }; - using javaobject = _javaobject*; -}; -} - -// JavaClass provides a method to inform fbjni about user-defined Java types. -// Given a class: -// struct Foo : JavaClass { -// static constexpr auto kJavaDescriptor = "Lcom/example/package/Foo;"; -// }; -// fbjni can determine the java type/method signatures for Foo::javaobject and -// smart refs (like alias_ref) will hold an instance of Foo -// and provide access to it through the -> and * operators. -// -// The "Base" template argument can be used to specify the JavaClass superclass -// of this type (for instance, JString's Base is JObject). -// -// The "JType" template argument is used to provide a jni type (like jstring, -// jthrowable) to be used as javaobject. This should only be necessary for -// built-in jni types and not user-defined ones. -template -class JavaClass : public Base { - using JObjType = typename detail::JTypeFor; -public: - using _javaobject = typename JObjType::_javaobject; - using javaobject = typename JObjType::javaobject; - - using JavaBase = JavaClass; - - static alias_ref javaClassStatic(); - static local_ref javaClassLocal(); -protected: - /// Allocates a new object and invokes the specified constructor - /// Like JClass's getConstructor, this function can only check at runtime if - /// the class actually has a constructor that accepts the corresponding types. - /// While a JavaClass-type can expose this function directly, it is recommended - /// to instead to use this to explicitly only expose those constructors that - /// the Java class actually has (i.e. with static create() functions). - template - static local_ref newInstance(Args... args) { - return detail::newInstance(args...); - } - - javaobject self() const noexcept; -}; - -/// Wrapper to provide functionality to jclass references -struct NativeMethod; - -class JClass : public JavaClass { - public: - /// Java type descriptor - static constexpr const char* kJavaDescriptor = "Ljava/lang/Class;"; - - /// Get a @local_ref to the super class of this class - local_ref getSuperclass() const noexcept; - - /// Register native methods for the class. Usage looks like this: - /// - /// classRef->registerNatives({ - /// makeNativeMethod("nativeMethodWithAutomaticDescriptor", - /// methodWithAutomaticDescriptor), - /// makeNativeMethod("nativeMethodWithExplicitDescriptor", - /// "(Lcom/facebook/example/MyClass;)V", - /// methodWithExplicitDescriptor), - /// makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED("criticalNativeMethodWithAutomaticDescriptor", - /// criticalNativeMethodWithAutomaticDescriptor), - /// makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED("criticalNativeMethodWithExplicitDescriptor", - /// "(IIF)Z", - /// criticalNativeMethodWithExplicitDescriptor), - /// }); - /// - /// By default, C++ exceptions raised will be converted to Java exceptions. - /// To avoid this and get the "standard" JNI behavior of a crash when a C++ - /// exception is crashing out of the JNI method, declare the method noexcept. - /// This does NOT apply to critical native methods, where exceptions causes - /// a crash. - void registerNatives(std::initializer_list methods); - - /// Check to see if the class is assignable from another class - /// @pre cls != nullptr - bool isAssignableFrom(alias_ref cls) const noexcept; - - /// Convenience method to lookup the constructor with descriptor as specified by the - /// type arguments - template - JConstructor getConstructor() const; - - /// Convenience method to lookup the constructor with specified descriptor - template - JConstructor getConstructor(const char* descriptor) const; - - /// Look up the method with given name and descriptor as specified with the type arguments - template - JMethod getMethod(const char* name) const; - - /// Look up the method with given name and descriptor - template - JMethod getMethod(const char* name, const char* descriptor) const; - - /// Lookup the field with the given name and deduced descriptor - template - JField(), T>> getField(const char* name) const; - - /// Lookup the field with the given name and descriptor - template - JField(), T>> getField(const char* name, const char* descriptor) const; - - /// Lookup the static field with the given name and deduced descriptor - template - JStaticField(), T>> getStaticField(const char* name) const; - - /// Lookup the static field with the given name and descriptor - template - JStaticField(), T>> getStaticField( - const char* name, - const char* descriptor) const; - - /// Get the primitive value of a static field - template - T getStaticFieldValue(JStaticField field) const noexcept; - - /// Get and wrap the value of a field in a @ref local_ref - template - local_ref getStaticFieldValue(JStaticField field) noexcept; - - /// Set the value of field. Any Java type is accepted. - template - void setStaticFieldValue(JStaticField field, T value) noexcept; - template(), T>::type> - void setStaticFieldValue(JStaticField field, alias_ref value) noexcept; - - /// Allocates a new object and invokes the specified constructor - template - local_ref newObject(JConstructor constructor, Args... args) const; - - /// Look up the static method with given name and descriptor as specified with the type arguments - template - JStaticMethod getStaticMethod(const char* name) const; - - /// Look up the static method with given name and descriptor - template - JStaticMethod getStaticMethod(const char* name, const char* descriptor) const; - - /// Look up the non virtual method with given name and descriptor as specified with the - /// type arguments - template - JNonvirtualMethod getNonvirtualMethod(const char* name) const; - - /// Look up the non virtual method with given name and descriptor - template - JNonvirtualMethod getNonvirtualMethod(const char* name, const char* descriptor) const; - -private: - jclass self() const noexcept; -}; - -// Convenience method to register methods on a class without holding -// onto the class object. -void registerNatives(const char* name, std::initializer_list methods); - -/// Wrapper to provide functionality to jstring references -class JString : public JavaClass { - public: - /// Java type descriptor - static constexpr const char* kJavaDescriptor = "Ljava/lang/String;"; - - /// Convenience method to convert a jstring object to a std::string - std::string toStdString() const; - - /// Convenience method to convert a jstring object to a std::u16string - std::u16string toU16String() const; -}; - -/// Convenience functions to convert a const char*, std::string, or std::u16string -/// into a @ref local_ref to a jstring. -local_ref make_jstring(const char* modifiedUtf8); -local_ref make_jstring(const std::string& modifiedUtf8); -local_ref make_jstring(const std::u16string& utf16); - -namespace detail { -template -class ElementProxy { - private: - Target* target_; - size_t idx_; - - public: - using T = typename Target::javaentry; - ElementProxy(Target* target, size_t idx); - - ElementProxy& operator=(const T& o); - - ElementProxy& operator=(alias_ref& o); - - ElementProxy& operator=(alias_ref&& o); - - ElementProxy& operator=(const ElementProxy& o); - - operator const local_ref () const; - - operator local_ref (); -}; -} - -namespace detail { -class JArray : public JavaClass { - public: - // This cannot be used in a scope that derives a descriptor (like in a method - // signature). Use a more derived type instead (like JArrayInt or - // JArrayClass). - static constexpr const char* kJavaDescriptor = nullptr; - size_t size() const noexcept; -}; - -// This is used so that the JArrayClass javaobject extends jni's -// jobjectArray. This class should not be used directly. A general Object[] -// should use JArrayClass. -class JTypeArray : public JavaClass { - // This cannot be used in a scope that derives a descriptor (like in a method - // signature). - static constexpr const char* kJavaDescriptor = nullptr; -}; -} - -template -class JArrayClass : public JavaClass, detail::JTypeArray> { - public: - static_assert(is_plain_jni_reference(), ""); - // javaentry is the jni type of an entry in the array (i.e. jint). - using javaentry = T; - // javaobject is the jni type of the array. - using javaobject = typename JavaClass, detail::JTypeArray>::javaobject; - static constexpr const char* kJavaDescriptor = nullptr; - static std::string get_instantiated_java_descriptor(); - static std::string get_instantiated_base_name(); - - /// Allocate a new array from Java heap, for passing as a JNI parameter or return value. - /// NOTE: if using as a return value, you want to call release() instead of get() on the - /// smart pointer. - static local_ref newArray(size_t count); - - /// Assign an object to the array. - /// Typically you will use the shorthand (*ref)[idx]=value; - void setElement(size_t idx, const T& value); - - /// Read an object from the array. - /// Typically you will use the shorthand - /// T value = (*ref)[idx]; - /// If you use auto, you'll get an ElementProxy, which may need to be cast. - local_ref getElement(size_t idx); - - /// EXPERIMENTAL SUBSCRIPT SUPPORT - /// This implementation of [] returns a proxy object which then has a bunch of specializations - /// (adopt_local free function, operator= and casting overloads on the ElementProxy) that can - /// make code look like it is dealing with a T rather than an obvious proxy. In particular, the - /// proxy in this iteration does not read a value and therefore does not create a LocalRef - /// until one of these other operators is used. There are certainly holes that you may find - /// by using idioms that haven't been tried yet. Consider yourself warned. On the other hand, - /// it does make for some idiomatic assignment code; see TestBuildStringArray in fbjni_tests - /// for some examples. - detail::ElementProxy operator[](size_t idx); -}; - -template -using jtypeArray = typename JArrayClass::javaobject; - -template -local_ref::javaobject> adopt_local_array(jobjectArray ref); - -template -local_ref adopt_local(detail::ElementProxy elementProxy) { - return static_cast>(elementProxy); -} - -template -class PinnedPrimitiveArray; - -template class PinnedArrayAlloc; -template class PinnedRegionAlloc; -template class PinnedCriticalAlloc; - -/// Wrapper to provide functionality to jarray references. -/// This is an empty holder by itself. Construct a PinnedPrimitiveArray to actually interact with -/// the elements of the array. -template -class JPrimitiveArray : - public JavaClass, detail::JArray, JArrayType> { - static_assert(is_jni_primitive_array(), ""); - public: - static constexpr const char* kJavaDescriptor = nullptr; - static std::string get_instantiated_java_descriptor(); - static std::string get_instantiated_base_name(); - - using T = typename jtype_traits::entry_type; - - static local_ref newArray(size_t count); - - void getRegion(jsize start, jsize length, T* buf); - std::unique_ptr getRegion(jsize start, jsize length); - void setRegion(jsize start, jsize length, const T* buf); - - /// Returns a view of the underlying array. This will either be a "pinned" - /// version of the array (in which case changes to one immediately affect the - /// other) or a copy of the array (in which cases changes to the view will take - /// affect when destroyed or on calls to release()/commit()). - PinnedPrimitiveArray> pin(); - - /// Returns a view of part of the underlying array. A pinned region is always - /// backed by a copy of the region. - PinnedPrimitiveArray> pinRegion(jsize start, jsize length); - - /// Returns a view of the underlying array like pin(). However, while the pin - /// is held, the code is considered within a "critical region". In a critical - /// region, native code must not call JNI functions or make any calls that may - /// block on other Java threads. These restrictions make it more likely that - /// the view will be "pinned" rather than copied (for example, the VM may - /// suspend garbage collection within a critical region). - PinnedPrimitiveArray> pinCritical(); - -private: - friend class PinnedArrayAlloc; - T* getElements(jboolean* isCopy); - void releaseElements(T* elements, jint mode); -}; - -local_ref make_boolean_array(jsize size); -local_ref make_byte_array(jsize size); -local_ref make_char_array(jsize size); -local_ref make_short_array(jsize size); -local_ref make_int_array(jsize size); -local_ref make_long_array(jsize size); -local_ref make_float_array(jsize size); -local_ref make_double_array(jsize size); - -using JArrayBoolean = JPrimitiveArray; -using JArrayByte = JPrimitiveArray; -using JArrayChar = JPrimitiveArray; -using JArrayShort = JPrimitiveArray; -using JArrayInt = JPrimitiveArray; -using JArrayLong = JPrimitiveArray; -using JArrayFloat = JPrimitiveArray; -using JArrayDouble = JPrimitiveArray; - -/// RAII class for pinned primitive arrays -/// This currently only supports read/write access to existing java arrays. You can't create a -/// primitive array this way yet. This class also pins the entire array into memory during the -/// lifetime of the PinnedPrimitiveArray. If you need to unpin the array manually, call the -/// release() or abort() functions. During a long-running block of code, you -/// should unpin the array as soon as you're done with it, to avoid holding up -/// the Java garbage collector. -template -class PinnedPrimitiveArray { - public: - static_assert(is_jni_primitive::value, - "PinnedPrimitiveArray requires primitive jni type."); - - using ArrayType = typename jtype_traits::array_type; - - PinnedPrimitiveArray(PinnedPrimitiveArray&&); - PinnedPrimitiveArray(const PinnedPrimitiveArray&) = delete; - ~PinnedPrimitiveArray() noexcept; - - PinnedPrimitiveArray& operator=(PinnedPrimitiveArray&&); - PinnedPrimitiveArray& operator=(const PinnedPrimitiveArray&) = delete; - - T* get(); - void release(); - /// Unpins the array. If the array is a copy, pending changes are discarded. - void abort(); - /// If the array is a copy, copies pending changes to the underlying java array. - void commit(); - - bool isCopy() const noexcept; - - const T& operator[](size_t index) const; - T& operator[](size_t index); - size_t size() const noexcept; - - private: - alias_ref array_; - size_t start_; - T* elements_; - jboolean isCopy_; - size_t size_; - - void allocate(alias_ref, jint start, jint length); - void releaseImpl(jint mode); - void clear() noexcept; - - PinnedPrimitiveArray(alias_ref, jint start, jint length); - - friend class JPrimitiveArray::array_type>; -}; - -struct JStackTraceElement : JavaClass { - static auto constexpr kJavaDescriptor = "Ljava/lang/StackTraceElement;"; - - static local_ref create(const std::string& declaringClass, const std::string& methodName, const std::string& file, int line); - - std::string getClassName() const; - std::string getMethodName() const; - std::string getFileName() const; - int getLineNumber() const; -}; - -/// Wrapper to provide functionality to jthrowable references -class JThrowable : public JavaClass { - public: - static constexpr const char* kJavaDescriptor = "Ljava/lang/Throwable;"; - - using JStackTrace = JArrayClass; - - local_ref initCause(alias_ref cause); - local_ref getStackTrace(); - void setStackTrace(alias_ref>); -}; - -#pragma push_macro("PlainJniRefMap") -#undef PlainJniRefMap -#define PlainJniRefMap(rtype, jtype) \ -namespace detail { \ -template<> \ -struct RefReprType { \ - using type = rtype; \ -}; \ -} - -PlainJniRefMap(JArrayBoolean, jbooleanArray); -PlainJniRefMap(JArrayByte, jbyteArray); -PlainJniRefMap(JArrayChar, jcharArray); -PlainJniRefMap(JArrayShort, jshortArray); -PlainJniRefMap(JArrayInt, jintArray); -PlainJniRefMap(JArrayLong, jlongArray); -PlainJniRefMap(JArrayFloat, jfloatArray); -PlainJniRefMap(JArrayDouble, jdoubleArray); -PlainJniRefMap(JObject, jobject); -PlainJniRefMap(JClass, jclass); -PlainJniRefMap(JString, jstring); -PlainJniRefMap(JThrowable, jthrowable); - -#pragma pop_macro("PlainJniRefMap") - -}} - -#include "CoreClasses-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Environment.h b/lib/fb/src/main/cpp/include/fbjni/detail/Environment.h deleted file mode 100644 index 5453e54b..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Environment.h +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once -#include -#include -#include - - -namespace facebook { -namespace jni { - -// Keeps a thread-local reference to the current thread's JNIEnv. -struct Environment { - // Throws a std::runtime_error if this thread isn't attached to the JVM - // TODO(T6594868) Benchmark against raw JNI access - static JNIEnv* current(); - static void initialize(JavaVM* vm); - - // There are subtle issues with calling the next functions directly. It is - // much better to always use a ThreadScope to manage attaching/detaching for - // you. - static JNIEnv* ensureCurrentThreadIsAttached(); -}; - -namespace detail { - -// This will return null the thread isn't attached to the VM, or if -// fbjni has never been initialized with a VM at all. You probably -// shouldn't be using this. -JNIEnv* currentOrNull(); - -/** - * If there's thread-local data, it's a pointer to one of these. The - * instance is a member of JniEnvCacher or ThreadScope, and lives on - * the stack. - */ -struct TLData { - // This is modified only by JniEnvCacher, and is guaranteed to be - // valid if set, and refer to an env which originated from a JNI - // call into C++. - JNIEnv* env; - // This is modified only by ThreadScope, and is set only if an - // instance of ThreadScope which attached is on the stack. - bool attached; -}; - -/** - * RAII object which manages a cached JNIEnv* value. A Value is only - * cached if it is guaranteed safe, which means when C++ is called - * from a registered fbjni function. - */ -class JniEnvCacher { -public: - JniEnvCacher(JNIEnv* env); - JniEnvCacher(JniEnvCacher&) = delete; - JniEnvCacher(JniEnvCacher&&) = default; - JniEnvCacher& operator=(JniEnvCacher&) = delete; - JniEnvCacher& operator=(JniEnvCacher&&) = delete; - ~JniEnvCacher(); - -private: - // If this flag is set, then, this object needs to clear the cache. - bool thisCached_; - - // The thread local pointer may point here. - detail::TLData data_; -}; - -} - -/** - * RAII Object that attaches a thread to the JVM. Failing to detach from a thread before it - * exits will cause a crash, as will calling Detach an extra time, and this guard class helps - * keep that straight. In addition, it remembers whether it performed the attach or not, so it - * is safe to nest it with itself or with non-fbjni code that manages the attachment correctly. - * - * Potential concerns: - * - Attaching to the JVM is fast (~100us on MotoG), but ideally you would attach while the - * app is not busy. - * - Having a thread detach at arbitrary points is not safe in Dalvik; you need to be sure that - * there is no Java code on the current stack or you run the risk of a crash like: - * ERROR: detaching thread with interp frames (count=18) - * (More detail at https://groups.google.com/forum/#!topic/android-ndk/2H8z5grNqjo) - * ThreadScope won't do a detach if the thread was already attached before the guard is - * instantiated, but there's probably some usage that could trip this up. - * - Newly attached C++ threads only get the bootstrap class loader -- i.e. java language - * classes, not any of our application's classes. This will be different behavior than threads - * that were initiated on the Java side. A workaround is to pass a global reference for a - * class or instance to the new thread; this bypasses the need for the class loader. - * (See http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html#attach_current_thread) - * If you need access to the application's classes, you can use ThreadScope::WithClassLoader. - * - If fbjni has never been initialized, there will be no JavaVM object to attach with. - * In that case, a std::runtime_error will be thrown. This is only likely to happen in a - * standalone C++ application, or if Environment::initialize is not used. - */ -class ThreadScope { - public: - ThreadScope(); - ThreadScope(ThreadScope&) = delete; - ThreadScope(ThreadScope&&) = default; - ThreadScope& operator=(ThreadScope&) = delete; - ThreadScope& operator=(ThreadScope&&) = delete; - ~ThreadScope(); - - /** - * This runs the closure in a scope with fbjni's classloader. This should be - * the same classloader as the rest of the application and thus anything - * running in the closure will have access to the same classes as in a normal - * java-create thread. - */ - static void WithClassLoader(std::function&& runnable); - - static void OnLoad(); - - private: - // If this flag is set, then this object needs to detach. - bool thisAttached_; - - // The thread local pointer may point here. - detail::TLData data_; -}; - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Exceptions.h b/lib/fb/src/main/cpp/include/fbjni/detail/Exceptions.h deleted file mode 100644 index 94b642b9..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Exceptions.h +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -/** - * @file Exceptions.h - * - * After invoking a JNI function that can throw a Java exception, the macro - * @ref FACEBOOK_JNI_THROW_PENDING_EXCEPTION() or @ref FACEBOOK_JNI_THROW_EXCEPTION_IF() - * should be invoked. - * - * IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT! IMPORTANT! - * To use these methods you MUST call initExceptionHelpers() when your library is loaded. - */ - -#pragma once - -#include -#include -#include - -#include - -#include "Common.h" -#include "References.h" -#include "CoreClasses.h" - -#if defined(__ANDROID__) && defined(__ARM_ARCH_5TE__) && !defined(FBJNI_NO_EXCEPTION_PTR) -// ARMv5 NDK does not support exception_ptr so we cannot use that when building for it. -#define FBJNI_NO_EXCEPTION_PTR -#endif - -namespace facebook { -namespace jni { - -class JThrowable; - -class JCppException : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Lcom/facebook/jni/CppException;"; - - static local_ref create(const char* str) { - return newInstance(make_jstring(str)); - } - - static local_ref create(const std::exception& ex) { - return newInstance(make_jstring(ex.what())); - } -}; - -// JniException //////////////////////////////////////////////////////////////////////////////////// - -/** - * This class wraps a Java exception into a C++ exception; if the exception is routed back - * to the Java side, it can be unwrapped and just look like a pure Java interaction. The class - * is resilient to errors while creating the exception, falling back to some pre-allocated - * exceptions if a new one cannot be allocated or populated. - * - * Note: the what() method of this class is not thread-safe (t6900503). - */ -class JniException : public std::exception { - public: - JniException(); - ~JniException() override; - - explicit JniException(alias_ref throwable); - - JniException(JniException &&rhs); - - JniException(const JniException &other); - - local_ref getThrowable() const noexcept; - - const char* what() const noexcept override; - - void setJavaException() const noexcept; - - private: - global_ref throwable_; - mutable std::string what_; - mutable bool isMessageExtracted_; - const static std::string kExceptionMessageFailure_; - - void populateWhat() const noexcept; -}; - -// Exception throwing & translating functions ////////////////////////////////////////////////////// - -// Functions that throw C++ exceptions - -static const int kMaxExceptionMessageBufferSize = 512; - -// These methods are the preferred way to throw a Java exception from -// a C++ function. They create and throw a C++ exception which wraps -// a Java exception, so the C++ flow is interrupted. Then, when -// translatePendingCppExceptionToJavaException is called at the -// topmost level of the native stack, the wrapped Java exception is -// thrown to the java caller. -template -[[noreturn]] void throwNewJavaException(const char* throwableName, const char* fmt, Args... args) { - int msgSize = snprintf(nullptr, 0, fmt, args...); - - char *msg = (char*) alloca(msgSize + 1); - snprintf(msg, kMaxExceptionMessageBufferSize, fmt, args...); - throwNewJavaException(throwableName, msg); -} - -// Identifies any pending C++ exception and throws it as a Java exception. If the exception can't -// be thrown, it aborts the program. -void translatePendingCppExceptionToJavaException(); - -#ifndef FBJNI_NO_EXCEPTION_PTR -local_ref getJavaExceptionForCppException(std::exception_ptr ptr); -#endif - -/*** - * The stack returned may include build ids. It may be beneficial to - * call lyra::setLibraryIdentifierFunction before calling this if - * build ids are desirable. - */ -local_ref getJavaExceptionForCppBackTrace(); - -local_ref getJavaExceptionForCppBackTrace(const char* msg); - -// For convenience, some exception names in java.lang are available here. -const char* const gJavaLangIllegalArgumentException = "java/lang/IllegalArgumentException"; - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Hybrid.h b/lib/fb/src/main/cpp/include/fbjni/detail/Hybrid.h deleted file mode 100644 index 430d7ca3..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Hybrid.h +++ /dev/null @@ -1,285 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include - -#include "CoreClasses.h" - -namespace facebook { -namespace jni { - -namespace detail { - -class BaseHybridClass { -public: - virtual ~BaseHybridClass() {} -}; - -struct HybridData : public JavaClass { - constexpr static auto kJavaDescriptor = "Lcom/facebook/jni/HybridData;"; - static local_ref create(); -}; - -class HybridDestructor : public JavaClass { - public: - static auto constexpr kJavaDescriptor = "Lcom/facebook/jni/HybridData$Destructor;"; - - detail::BaseHybridClass* getNativePointer(); - - void setNativePointer(std::unique_ptr new_value); -}; - -template -detail::BaseHybridClass* getNativePointer(T t) { - return getHolder(t)->getNativePointer(); -} - -template -void setNativePointer(T t, std::unique_ptr new_value) { - getHolder(t)->setNativePointer(std::move(new_value)); -} - -template -local_ref getHolder(T t) { - static auto holderField = t->getClass()->template getField("mDestructor"); - return t->getFieldValue(holderField); -} - -// JavaClass for HybridClassBase -struct HybridClassBase : public JavaClass { - constexpr static auto kJavaDescriptor = "Lcom/facebook/jni/HybridClassBase;"; - - static bool isHybridClassBase(alias_ref jclass) { - return HybridClassBase::javaClassStatic()->isAssignableFrom(jclass); - } -}; - -template -struct HybridTraits { - // This static assert should actually always fail if we don't use one of the - // specializations below. - static_assert( - std::is_base_of::value || - std::is_base_of::value, - "The base of a HybridClass must be either another HybridClass or derived from JObject."); -}; - -template <> -struct HybridTraits { - using CxxBase = BaseHybridClass; - using JavaBase = JObject; -}; - -template -struct HybridTraits< - Base, - typename std::enable_if::value>::type> { - using CxxBase = Base; - using JavaBase = typename Base::JavaPart; -}; - -template -struct HybridTraits< - Base, - typename std::enable_if::value>::type> { - using CxxBase = BaseHybridClass; - using JavaBase = Base; -}; - -// convert to HybridClass* from jhybridobject -template -struct Convert< - T, typename std::enable_if< - std::is_base_of::type>::value>::type> { - typedef typename std::remove_pointer::type::jhybridobject jniType; - static T fromJni(jniType t) { - if (t == nullptr) { - return nullptr; - } - return wrap_alias(t)->cthis(); - } - // There is no automatic return conversion for objects. -}; - -template -struct RefReprType::value, void>::type> { - static_assert(std::is_same::value, - "HybridFoo (where HybridFoo derives from HybridClass) is not supported in this context. " - "For an xxx_ref, you may want: xxx_ref or HybridFoo*."); - using Repr = T; -}; - - -} - -template -class HybridClass : public detail::HybridTraits::CxxBase { -public: - struct JavaPart : JavaClass::JavaBase> { - // At this point, T is incomplete, and so we cannot access - // T::kJavaDescriptor directly. jtype_traits support this escape hatch for - // such a case. - static constexpr const char* kJavaDescriptor = nullptr; - static std::string get_instantiated_java_descriptor(); - static std::string get_instantiated_base_name(); - - using HybridType = T; - - // This will reach into the java object and extract the C++ instance from - // the mHybridData and return it. - T* cthis(); - - friend class HybridClass; - friend T; - }; - - using jhybridobject = typename JavaPart::javaobject; - using javaobject = typename JavaPart::javaobject; - typedef detail::HybridData::javaobject jhybriddata; - - static alias_ref javaClassStatic() { - return JavaPart::javaClassStatic(); - } - - static local_ref javaClassLocal() { - std::string className(T::kJavaDescriptor + 1, strlen(T::kJavaDescriptor) - 2); - return findClassLocal(className.c_str()); - } - -protected: - typedef HybridClass HybridBase; - - // This ensures that a C++ hybrid part cannot be created on its own - // by default. If a hybrid wants to enable this, it can provide its - // own public ctor, or change the accessibility of this to public. - using detail::HybridTraits::CxxBase::CxxBase; - - static void registerHybrid(std::initializer_list methods) { - javaClassStatic()->registerNatives(methods); - } - - static local_ref makeHybridData(std::unique_ptr cxxPart) { - auto hybridData = detail::HybridData::create(); - setNativePointer(hybridData, std::move(cxxPart)); - return hybridData; - } - - template - static local_ref makeCxxInstance(Args&&... args) { - return makeHybridData(std::unique_ptr(new T(std::forward(args)...))); - } - - template - static void setCxxInstance(alias_ref o, Args&&... args) { - setNativePointer(o, std::unique_ptr(new T(std::forward(args)...))); - } - -public: - // Factory method for creating a hybrid object where the arguments - // are used to initialize the C++ part directly without passing them - // through java. This method requires the Java part to have a ctor - // which takes a HybridData, and for the C++ part to have a ctor - // compatible with the arguments passed here. For safety, the ctor - // can be private, and the hybrid declared a friend of its base, so - // the hybrid can only be created from here. - // - // Exception behavior: This can throw an exception if creating the - // C++ object fails, or any JNI methods throw. - template - static local_ref newObjectCxxArgs(Args&&... args) { - static bool isHybrid = detail::HybridClassBase::isHybridClassBase(javaClassStatic()); - auto cxxPart = std::unique_ptr(new T(std::forward(args)...)); - - local_ref result; - if (isHybrid) { - result = JavaPart::newInstance(); - setNativePointer(result, std::move(cxxPart)); - } - else { - auto hybridData = makeHybridData(std::move(cxxPart)); - result = JavaPart::newInstance(hybridData); - } - - return result; - } - - // TODO? Create reusable interface for Allocatable classes and use it to - // strengthen type-checking (and possibly provide a default - // implementation of allocate().) - template - static local_ref allocateWithCxxArgs(Args&&... args) { - auto hybridData = makeCxxInstance(std::forward(args)...); - static auto allocateMethod = - javaClassStatic()->template getStaticMethod("allocate"); - return allocateMethod(javaClassStatic(), hybridData.get()); - } - - // Factory method for creating a hybrid object where the arguments - // are passed to the java ctor. - template - static local_ref newObjectJavaArgs(Args&&... args) { - return JavaPart::newInstance(std::move(args)...); - } - - // If a hybrid class throws an exception which derives from - // std::exception, it will be passed to mapException on the hybrid - // class, or nearest ancestor. This allows boilerplate exception - // translation code (for example, calling throwNewJavaException on a - // particular java class) to be hoisted to a common function. If - // mapException returns, then the std::exception will be translated - // to Java. - static void mapException(const std::exception& ex) { - (void)ex; - } -}; - -template -inline T* HybridClass::JavaPart::cthis() { - detail::BaseHybridClass* result = 0; - static bool isHybrid = detail::HybridClassBase::isHybridClassBase(this->getClass()); - if (isHybrid) { - result = getNativePointer(this); - } else { - static auto field = - HybridClass::JavaPart::javaClassStatic()->template getField("mHybridData"); - auto hybridData = this->getFieldValue(field); - if (!hybridData) { - throwNewJavaException("java/lang/NullPointerException", "java.lang.NullPointerException"); - } - - result = getNativePointer(hybridData); - } - - // I'd like to use dynamic_cast here, but -fno-rtti is the default. - return static_cast(result); -}; - -template -/* static */ inline std::string HybridClass::JavaPart::get_instantiated_java_descriptor() { - return T::kJavaDescriptor; -} - -template -/* static */ inline std::string HybridClass::JavaPart::get_instantiated_base_name() { - auto name = get_instantiated_java_descriptor(); - return name.substr(1, name.size() - 2); -} - -// Given a *_ref object which refers to a hybrid class, this will reach inside -// of it, find the mHybridData, extract the C++ instance pointer, cast it to -// the appropriate type, and return it. -template -inline auto cthis(T jthis) -> decltype(jthis->cthis()) { - return jthis->cthis(); -} - -void HybridDataOnLoad(); - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Iterator-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/Iterator-inl.h deleted file mode 100644 index d0df3455..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Iterator-inl.h +++ /dev/null @@ -1,196 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -namespace facebook { -namespace jni { - -namespace detail { - -template -struct IteratorHelper : public JavaClass> { - constexpr static auto kJavaDescriptor = "Lcom/facebook/jni/IteratorHelper;"; - - typedef local_ref value_type; - typedef ptrdiff_t difference_type; - typedef value_type* pointer; - typedef value_type& reference; - typedef std::forward_iterator_tag iterator_category; - - typedef JavaClass> JavaBase_; - - bool hasNext() const { - static auto hasNextMethod = - JavaBase_::javaClassStatic()->template getMethod("hasNext"); - return hasNextMethod(JavaBase_::self()); - } - - value_type next() { - static auto elementField = - JavaBase_::javaClassStatic()->template getField("mElement"); - return dynamic_ref_cast>(JavaBase_::getFieldValue(elementField)); - } - - static void reset(value_type& v) { - v.reset(); - } -}; - -template -struct MapIteratorHelper : public JavaClass> { - constexpr static auto kJavaDescriptor = "Lcom/facebook/jni/MapIteratorHelper;"; - - typedef std::pair, local_ref> value_type; - - typedef JavaClass> JavaBase_; - - bool hasNext() const { - static auto hasNextMethod = - JavaBase_::javaClassStatic()->template getMethod("hasNext"); - return hasNextMethod(JavaBase_::self()); - } - - value_type next() { - static auto keyField = JavaBase_::javaClassStatic()->template getField("mKey"); - static auto valueField = JavaBase_::javaClassStatic()->template getField("mValue"); - return std::make_pair(dynamic_ref_cast(JavaBase_::getFieldValue(keyField)), - dynamic_ref_cast(JavaBase_::getFieldValue(valueField))); - } - - static void reset(value_type& v) { - v.first.reset(); - v.second.reset(); - } -}; - -template -class Iterator { - public: - typedef typename T::value_type value_type; - typedef ptrdiff_t difference_type; - typedef value_type* pointer; - typedef value_type& reference; - typedef std::input_iterator_tag iterator_category; - - // begin ctor - Iterator(global_ref&& helper) - : helper_(std::move(helper)) - , i_(-1) { - ++(*this); - } - - // end ctor - Iterator() - : i_(-1) {} - - bool operator==(const Iterator& it) const { return i_ == it.i_; } - bool operator!=(const Iterator& it) const { return !(*this == it); } - const value_type& operator*() const { assert(i_ != -1); return entry_; } - const value_type* operator->() const { assert(i_ != -1); return &entry_; } - Iterator& operator++() { // preincrement - bool hasNext = helper_->hasNext(); - if (hasNext) { - ++i_; - entry_ = helper_->next(); - } else { - i_ = -1; - helper_->reset(entry_); - } - return *this; - } - Iterator operator++(int) { // postincrement - Iterator ret; - ret.i_ = i_; - ret.entry_ = std::move(entry_); - ++(*this); - return ret; - } - - global_ref helper_; - // set to -1 at end - std::ptrdiff_t i_; - value_type entry_; -}; - -} - -template -struct JIterator::Iterator : public detail::Iterator> { - using detail::Iterator>::Iterator; -}; - -template -typename JIterator::Iterator JIterator::begin() const { - static auto ctor = detail::IteratorHelper::javaClassStatic()-> - template getConstructor::javaobject( - typename JIterator::javaobject)>(); - return Iterator( - make_global( - detail::IteratorHelper::javaClassStatic()->newObject(ctor, this->self()))); -} - -template -typename JIterator::Iterator JIterator::end() const { - return Iterator(); -} - -template -struct JIterable::Iterator : public detail::Iterator> { - using detail::Iterator>::Iterator; -}; - -template -typename JIterable::Iterator JIterable::begin() const { - static auto ctor = detail::IteratorHelper::javaClassStatic()-> - template getConstructor::javaobject( - typename JIterable::javaobject)>(); - return Iterator( - make_global( - detail::IteratorHelper::javaClassStatic()->newObject(ctor, this->self()))); -} - -template -typename JIterable::Iterator JIterable::end() const { - return Iterator(); -} - -template -size_t JCollection::size() const { - static auto sizeMethod = - JCollection::javaClassStatic()->template getMethod("size"); - return sizeMethod(this->self()); -} - -template -struct JMap::Iterator : public detail::Iterator> { - using detail::Iterator>::Iterator; -}; - -template -size_t JMap::size() const { - static auto sizeMethod = - JMap::javaClassStatic()->template getMethod("size"); - return sizeMethod(this->self()); -} - -template -typename JMap::Iterator JMap::begin() const { - static auto ctor = detail::MapIteratorHelper::javaClassStatic()-> - template getConstructor::javaobject( - typename JMap::javaobject)>(); - return Iterator( - make_global( - detail::MapIteratorHelper::javaClassStatic()->newObject(ctor, this->self()))); -} - -template -typename JMap::Iterator JMap::end() const { - return Iterator(); -} - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Iterator.h b/lib/fb/src/main/cpp/include/fbjni/detail/Iterator.h deleted file mode 100644 index 18afea6a..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Iterator.h +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include "CoreClasses.h" - -namespace facebook { -namespace jni { - -/** - * JavaClass which represents a reference to a java.util.Iterator instance. It - * provides begin()/end() methods to provide C++-style iteration over the - * underlying collection. The class has a template parameter for the element - * type, which defaults to jobject. For example: - * - * alias_ref::javaobject> my_iter = ...; - * - * In the simplest case, it can be used just as alias_ref::javaobject>, - * for example in a method declaration. - */ -template -struct JIterator : JavaClass> { - constexpr static auto kJavaDescriptor = "Ljava/util/Iterator;"; - - struct Iterator; - - /** - * To iterate: - * - * for (const auto& element : *jiter) { ... } - * - * The JIterator iterator value_type is local_ref, containing a reference - * to an element instance. - * - * If the Iterator returns objects whch are not convertible to the given - * element type, iteration will throw a java ClassCastException. - * - * For example, to convert an iterator over a collection of java strings to - * an std::vector of std::strings: - * - * std::vector vs; - * for (const auto& elem : *jiter) { - * vs.push_back(elem->toStdString()); - * } - * - * Or if you prefer using std algorithms: - * - * std::vector vs; - * std::transform(jiter->begin(), jiter->end(), std::back_inserter(vs), - * [](const local_ref& elem) { return elem->toStdString(); }); - * - * The iterator is a InputIterator. - */ - Iterator begin() const; - Iterator end() const; -}; - -/** - * Similar to JIterator, except this represents any object which implements the - * java.lang.Iterable interface. It will create the Java Iterator as a part of - * begin(). - */ -template -struct JIterable : JavaClass> { - constexpr static auto kJavaDescriptor = "Ljava/lang/Iterable;"; - - struct Iterator; - - Iterator begin() const; - Iterator end() const; -}; - -/** - * JavaClass types which represent Collection, List, and Set are also provided. - * These preserve the Java class heirarchy. - */ -template -struct JCollection : JavaClass, JIterable> { - constexpr static auto kJavaDescriptor = "Ljava/util/Collection;"; - - /** - * Returns the number of elements in the collection. - */ - size_t size() const; -}; - -template -struct JList : JavaClass, JCollection> { - constexpr static auto kJavaDescriptor = "Ljava/util/List;"; -}; - -template -struct JSet : JavaClass, JCollection> { - constexpr static auto kJavaDescriptor = "Ljava/util/Set;"; -}; - -/** - * JavaClass which represents a reference to a java.util.Map instance. It adds - * wrappers around Java methods, including begin()/end() methods to provide - * C++-style iteration over the Java Map. The class has template parameters - * for the key and value types, which default to jobject. For example: - * - * alias_ref::javaobject> my_map = ...; - * - * In the simplest case, it can be used just as alias_ref::javaobject>, - * for example in a method declaration. - */ -template -struct JMap : JavaClass> { - constexpr static auto kJavaDescriptor = "Ljava/util/Map;"; - - struct Iterator; - - /** - * Returns the number of pairs in the map. - */ - size_t size() const; - - /** - * To iterate over the Map: - * - * for (const auto& entry : *jmap) { ... } - * - * The JMap iterator value_type is std::pair, local_ref> - * containing references to key and value instances. - * - * If the Map contains objects whch are not convertible to the given key and - * value types, iteration will throw a java ClassCastException. - * - * The iterator is a InputIterator. - */ - Iterator begin() const; - Iterator end() const; -}; - -} -} - -#include "Iterator-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/JWeakReference.h b/lib/fb/src/main/cpp/include/fbjni/detail/JWeakReference.h deleted file mode 100644 index 4a95fd35..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/JWeakReference.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include "CoreClasses.h" - -namespace facebook { -namespace jni { - -/** - * Wrap Java's WeakReference instead of using JNI WeakGlobalRefs. - * A WeakGlobalRef can yield a strong reference even after the object has been - * finalized. See comment in the djinni library. - * https://github.com/dropbox/djinni/blob/master/support-lib/jni/djinni_support.hpp - */ -template -class JWeakReference : public JavaClass> { - - typedef JavaClass> JavaBase_; - - public: - static constexpr const char* kJavaDescriptor = "Ljava/lang/ref/WeakReference;"; - - static local_ref> newInstance(alias_ref object) { - return JavaBase_::newInstance(static_ref_cast(object)); - } - - local_ref get() const { - static const auto method = JavaBase_::javaClassStatic()->template getMethod("get"); - return static_ref_cast(method(JavaBase_::self())); - } -}; - -} -} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Log.h b/lib/fb/src/main/cpp/include/fbjni/detail/Log.h deleted file mode 100644 index 26b34aa3..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Log.h +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -/** @file ALog.h - * - * Very simple (android only) logging. Define LOG_TAG to enable the macros. - */ - -#pragma once - -#ifdef __ANDROID__ - -#include - -namespace facebook { -namespace jni { -namespace log_ { -// the weird name of this namespace is to avoid a conflict with the -// function named log. - -inline void loge(const char* tag, const char* msg) noexcept { - __android_log_write(ANDROID_LOG_ERROR, tag, msg); -} - -template -inline void loge(const char* tag, const char* msg, ARGS... args) noexcept { - __android_log_print(ANDROID_LOG_ERROR, tag, msg, args...); -} - -inline void logf(const char* tag, const char* msg) noexcept { - __android_log_write(ANDROID_LOG_FATAL, tag, msg); -} - -template -inline void logf(const char* tag, const char* msg, ARGS... args) noexcept { - __android_log_print(ANDROID_LOG_FATAL, tag, msg, args...); -} - -template -[[noreturn]] -inline void logassert(const char* tag, const char* msg, ARGS... args) noexcept { - __android_log_assert(0, tag, msg, args...); -} - - -#ifdef LOG_TAG -# define FBJNI_LOGE(...) ::facebook::jni::log_::loge(LOG_TAG, __VA_ARGS__) -# define FBJNI_LOGF(...) ::facebook::jni::log_::logf(LOG_TAG, __VA_ARGS__) -# define FBJNI_ASSERT(cond) do { if (!(cond)) ::facebook::jni::log_::logassert(LOG_TAG, "%s", #cond); } while(0) -#else -# define FBJNI_LOGE(...) ::facebook::jni::log_::loge("log", __VA_ARGS__) -# define FBJNI_LOGF(...) ::facebook::jni::log_::logf("log", __VA_ARGS__) -# define FBJNI_ASSERT(cond) do { if (!(cond)) ::facebook::jni::log_::logassert("log", "%s", #cond); } while(0) -#endif - -}}} - -#else -#include - -# define FBJNI_LOGE(...) ((void)0) -# define FBJNI_LOGF(...) (abort()) -# define FBJNI_ASSERT(cond) ((void)0) -#endif diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Meta-forward.h b/lib/fb/src/main/cpp/include/fbjni/detail/Meta-forward.h deleted file mode 100644 index e975f884..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Meta-forward.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -namespace facebook { -namespace jni { - -template -class JMethod; -template -class JStaticMethod; -template -class JNonvirtualMethod; -template -struct JConstructor; -template -class JField; -template -class JStaticField; - -/// Type traits for Java types (currently providing Java type descriptors) -template -struct jtype_traits; - -/// Type traits for Java methods (currently providing Java type descriptors) -template -struct jmethod_traits; - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Meta-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/Meta-inl.h deleted file mode 100644 index a2754374..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Meta-inl.h +++ /dev/null @@ -1,405 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -#include "Common.h" -#include "Exceptions.h" -#include "MetaConvert.h" -#include "References.h" -#include "Boxed.h" - -namespace facebook { -namespace jni { - -// JMethod ///////////////////////////////////////////////////////////////////////////////////////// - -inline JMethodBase::JMethodBase(jmethodID method_id) noexcept - : method_id_{method_id} -{} - -inline JMethodBase::operator bool() const noexcept { - return method_id_ != nullptr; -} - -inline jmethodID JMethodBase::getId() const noexcept { - return method_id_; -} - -namespace { - -template -struct ArgsArraySetter; - -template -struct ArgsArraySetter { - static void set(alias_ref::javaobject> array, Arg arg0, Args... args) { - // TODO(xxxxxxxx): Use Convert... to do conversions like the fast path. - (*array)[idx] = autobox(arg0); - ArgsArraySetter::set(array, args...); - } -}; - -template -struct ArgsArraySetter { - static void set(alias_ref::javaobject> array) { - (void)array; - } -}; - -template -local_ref::javaobject> makeArgsArray(Args... args) { - auto arr = JArrayClass::newArray(sizeof...(args)); - ArgsArraySetter<0, Args...>::set(arr, args...); - return arr; -} - -} - -template -inline void JMethod::operator()(alias_ref self, Args... args) const { - const auto env = Environment::current(); - env->CallVoidMethod( - self.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); -} - -#pragma push_macro("DEFINE_PRIMITIVE_CALL") -#undef DEFINE_PRIMITIVE_CALL -#define DEFINE_PRIMITIVE_CALL(TYPE, METHOD) \ -template \ -inline TYPE JMethod::operator()(alias_ref self, Args... args) const { \ - const auto env = Environment::current(); \ - auto result = env->Call ## METHOD ## Method( \ - self.get(), \ - getId(), \ - detail::callToJni(detail::Convert::type>::toCall(args))...); \ - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); \ - return result; \ -} - -DEFINE_PRIMITIVE_CALL(jboolean, Boolean) -DEFINE_PRIMITIVE_CALL(jbyte, Byte) -DEFINE_PRIMITIVE_CALL(jchar, Char) -DEFINE_PRIMITIVE_CALL(jshort, Short) -DEFINE_PRIMITIVE_CALL(jint, Int) -DEFINE_PRIMITIVE_CALL(jlong, Long) -DEFINE_PRIMITIVE_CALL(jfloat, Float) -DEFINE_PRIMITIVE_CALL(jdouble, Double) -#pragma pop_macro("DEFINE_PRIMITIVE_CALL") - -/// JMethod specialization for references that wraps the return value in a @ref local_ref -template -class JMethod : public JMethodBase { - public: - // TODO: static_assert is jobject-derived or local_ref jobject - using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "JniRet must be a JNI reference"); - using JMethodBase::JMethodBase; - JMethod() noexcept {}; - JMethod(const JMethod& other) noexcept = default; - - /// Invoke a method and return a local reference wrapping the result - local_ref operator()(alias_ref self, Args... args) const; - - friend class JClass; -}; - -template -inline auto JMethod::operator()(alias_ref self, Args... args) const -> local_ref { - const auto env = Environment::current(); - auto result = env->CallObjectMethod( - self.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); -} - -template -inline void JStaticMethod::operator()(alias_ref cls, Args... args) const { - const auto env = Environment::current(); - env->CallStaticVoidMethod( - cls.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); -} - -#pragma push_macro("DEFINE_PRIMITIVE_STATIC_CALL") -#undef DEFINE_PRIMITIVE_STATIC_CALL -#define DEFINE_PRIMITIVE_STATIC_CALL(TYPE, METHOD) \ -template \ -inline TYPE JStaticMethod::operator()(alias_ref cls, Args... args) const { \ - const auto env = Environment::current(); \ - auto result = env->CallStatic ## METHOD ## Method( \ - cls.get(), \ - getId(), \ - detail::callToJni(detail::Convert::type>::toCall(args))...); \ - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); \ - return result; \ -} - -DEFINE_PRIMITIVE_STATIC_CALL(jboolean, Boolean) -DEFINE_PRIMITIVE_STATIC_CALL(jbyte, Byte) -DEFINE_PRIMITIVE_STATIC_CALL(jchar, Char) -DEFINE_PRIMITIVE_STATIC_CALL(jshort, Short) -DEFINE_PRIMITIVE_STATIC_CALL(jint, Int) -DEFINE_PRIMITIVE_STATIC_CALL(jlong, Long) -DEFINE_PRIMITIVE_STATIC_CALL(jfloat, Float) -DEFINE_PRIMITIVE_STATIC_CALL(jdouble, Double) -#pragma pop_macro("DEFINE_PRIMITIVE_STATIC_CALL") - -/// JStaticMethod specialization for references that wraps the return value in a @ref local_ref -template -class JStaticMethod : public JMethodBase { - - public: - using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "T* must be a JNI reference"); - using JMethodBase::JMethodBase; - JStaticMethod() noexcept {}; - JStaticMethod(const JStaticMethod& other) noexcept = default; - - /// Invoke a method and return a local reference wrapping the result - local_ref operator()(alias_ref cls, Args... args) const { - const auto env = Environment::current(); - auto result = env->CallStaticObjectMethod( - cls.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); - } - - friend class JClass; -}; - -template -inline void -JNonvirtualMethod::operator()(alias_ref self, alias_ref cls, Args... args) const { - const auto env = Environment::current(); - env->CallNonvirtualVoidMethod( - self.get(), - cls.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); -} - -#pragma push_macro("DEFINE_PRIMITIVE_NON_VIRTUAL_CALL") -#undef DEFINE_PRIMITIVE_NON_VIRTUAL_CALL -#define DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(TYPE, METHOD) \ -template \ -inline TYPE \ -JNonvirtualMethod::operator()(alias_ref self, alias_ref cls, Args... args) const { \ - const auto env = Environment::current(); \ - auto result = env->CallNonvirtual ## METHOD ## Method( \ - self.get(), \ - cls.get(), \ - getId(), \ - detail::callToJni(detail::Convert::type>::toCall(args))...); \ - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); \ - return result; \ -} - -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jboolean, Boolean) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jbyte, Byte) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jchar, Char) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jshort, Short) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jint, Int) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jlong, Long) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jfloat, Float) -DEFINE_PRIMITIVE_NON_VIRTUAL_CALL(jdouble, Double) -#pragma pop_macro("DEFINE_PRIMITIVE_NON_VIRTUAL_CALL") - -/// JNonvirtualMethod specialization for references that wraps the return value in a @ref local_ref -template -class JNonvirtualMethod : public JMethodBase { - public: - using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "T* must be a JNI reference"); - using JMethodBase::JMethodBase; - JNonvirtualMethod() noexcept {}; - JNonvirtualMethod(const JNonvirtualMethod& other) noexcept = default; - - /// Invoke a method and return a local reference wrapping the result - local_ref operator()(alias_ref self, alias_ref cls, Args... args) const { - const auto env = Environment::current(); - auto result = env->CallNonvirtualObjectMethod( - self.get(), - cls.get(), - getId(), - detail::callToJni(detail::Convert::type>::toCall(args))...); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); - } - - friend class JClass; -}; - -template -local_ref slowCall(jmethodID method_id, alias_ref self, Args... args) { - static auto invoke = findClassStatic("java/lang/reflect/Method") - ->getMethod::javaobject)>("invoke"); - // TODO(xxxxxxx): Provide fbjni interface to ToReflectedMethod. - auto reflected = adopt_local(Environment::current()->ToReflectedMethod(self->getClass().get(), method_id, JNI_FALSE)); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - if (!reflected) throw std::runtime_error("Unable to get reflected java.lang.reflect.Method"); - auto argsArray = makeArgsArray(args...); - // No need to check for exceptions since invoke is itself a JMethod that will do that for us. - return invoke(reflected, self.get(), argsArray.get()); -} - - -// JField /////////////////////////////////////////////////////////////////////////////////////// - -template -inline JField::JField(jfieldID field) noexcept - : field_id_{field} -{} - -template -inline JField::operator bool() const noexcept { - return field_id_ != nullptr; -} - -template -inline jfieldID JField::getId() const noexcept { - return field_id_; -} - -#pragma push_macro("DEFINE_FIELD_PRIMITIVE_GET_SET") -#undef DEFINE_FIELD_PRIMITIVE_GET_SET -#define DEFINE_FIELD_PRIMITIVE_GET_SET(TYPE, METHOD) \ -template<> \ -inline TYPE JField::get(jobject object) const noexcept { \ - const auto env = Environment::current(); \ - return env->Get ## METHOD ## Field(object, field_id_); \ -} \ - \ -template<> \ -inline void JField::set(jobject object, TYPE value) noexcept { \ - const auto env = Environment::current(); \ - env->Set ## METHOD ## Field(object, field_id_, value); \ -} - -DEFINE_FIELD_PRIMITIVE_GET_SET(jboolean, Boolean) -DEFINE_FIELD_PRIMITIVE_GET_SET(jbyte, Byte) -DEFINE_FIELD_PRIMITIVE_GET_SET(jchar, Char) -DEFINE_FIELD_PRIMITIVE_GET_SET(jshort, Short) -DEFINE_FIELD_PRIMITIVE_GET_SET(jint, Int) -DEFINE_FIELD_PRIMITIVE_GET_SET(jlong, Long) -DEFINE_FIELD_PRIMITIVE_GET_SET(jfloat, Float) -DEFINE_FIELD_PRIMITIVE_GET_SET(jdouble, Double) -#pragma pop_macro("DEFINE_FIELD_PRIMITIVE_GET_SET") - -template -inline T JField::get(jobject object) const noexcept { - return static_cast(Environment::current()->GetObjectField(object, field_id_)); -} - -template -inline void JField::set(jobject object, T value) noexcept { - Environment::current()->SetObjectField(object, field_id_, static_cast(value)); -} - -// JStaticField ///////////////////////////////////////////////////////////////////////////////// - -template -inline JStaticField::JStaticField(jfieldID field) noexcept - : field_id_{field} -{} - -template -inline JStaticField::operator bool() const noexcept { - return field_id_ != nullptr; -} - -template -inline jfieldID JStaticField::getId() const noexcept { - return field_id_; -} - -#pragma push_macro("DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET") -#undef DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET -#define DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(TYPE, METHOD) \ -template<> \ -inline TYPE JStaticField::get(jclass jcls) const noexcept { \ - const auto env = Environment::current(); \ - return env->GetStatic ## METHOD ## Field(jcls, field_id_); \ -} \ - \ -template<> \ -inline void JStaticField::set(jclass jcls, TYPE value) noexcept { \ - const auto env = Environment::current(); \ - env->SetStatic ## METHOD ## Field(jcls, field_id_, value); \ -} - -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jboolean, Boolean) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jbyte, Byte) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jchar, Char) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jshort, Short) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jint, Int) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jlong, Long) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jfloat, Float) -DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET(jdouble, Double) -#pragma pop_macro("DEFINE_STATIC_FIELD_PRIMITIVE_GET_SET") - -template -inline T JStaticField::get(jclass jcls) const noexcept { - const auto env = Environment::current(); - return static_cast(env->GetStaticObjectField(jcls, field_id_)); -} - -template -inline void JStaticField::set(jclass jcls, T value) noexcept { - Environment::current()->SetStaticObjectField(jcls, field_id_, value); -} - - -// jmethod_traits ////////////////////////////////////////////////////////////////////////////////// - -// TODO(T6608405) Adapt this to implement a register natives method that requires no descriptor -namespace internal { - -template -inline std::string JavaDescriptor() { - return jtype_traits::descriptor(); -} - -template -inline std::string JavaDescriptor() { - return JavaDescriptor() + JavaDescriptor(); -} - -template -inline std::string JMethodDescriptor() { - return "(" + JavaDescriptor() + ")" + JavaDescriptor(); -} - -template -inline std::string JMethodDescriptor() { - return "()" + JavaDescriptor(); -} - -} // internal - -template -inline std::string jmethod_traits::descriptor() { - return internal::JMethodDescriptor(); -} - -template -inline std::string jmethod_traits::constructor_descriptor() { - return internal::JMethodDescriptor(); -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Meta.h b/lib/fb/src/main/cpp/include/fbjni/detail/Meta.h deleted file mode 100644 index b0e95fbe..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Meta.h +++ /dev/null @@ -1,337 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -/** @file meta.h - * - * Provides wrappers for meta data such as methods and fields. - */ - -#pragma once - -#include -#include - -#include - -#include "References-forward.h" - -#ifdef __ANDROID__ -# include -# define XLOG_TAG "fb-jni" -# define XLOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, XLOG_TAG, __VA_ARGS__) -# define XLOGD(...) __android_log_print(ANDROID_LOG_DEBUG, XLOG_TAG, __VA_ARGS__) -# define XLOGI(...) __android_log_print(ANDROID_LOG_INFO, XLOG_TAG, __VA_ARGS__) -# define XLOGW(...) __android_log_print(ANDROID_LOG_WARN, XLOG_TAG, __VA_ARGS__) -# define XLOGE(...) __android_log_print(ANDROID_LOG_ERROR, XLOG_TAG, __VA_ARGS__) -# define XLOGWTF(...) __android_log_print(ANDROID_LOG_FATAL, XLOG_TAG, __VA_ARGS__) -#endif - -namespace facebook { -namespace jni { - -// This will get the reflected Java Method from the method_id, get it's invoke -// method, and call the method via that. This shouldn't ever be needed, but -// Android 6.0 crashes when calling a method on a java.lang.Proxy via jni. -template -local_ref slowCall(jmethodID method_id, alias_ref self, Args... args); - -class JObject; - - -/// Wrapper of a jmethodID. Provides a common base for JMethod specializations -class JMethodBase { - public: - /// Verify that the method is valid - explicit operator bool() const noexcept; - - /// Access the wrapped id - jmethodID getId() const noexcept; - - protected: - /// Create a wrapper of a method id - explicit JMethodBase(jmethodID method_id = nullptr) noexcept; - - private: - jmethodID method_id_; -}; - - -/// Representation of a jmethodID -template -class JMethod; - -/// @cond INTERNAL -#pragma push_macro("DEFINE_PRIMITIVE_METHOD_CLASS") - -#undef DEFINE_PRIMITIVE_METHOD_CLASS - -// Defining JMethod specializations based on return value -#define DEFINE_PRIMITIVE_METHOD_CLASS(TYPE) \ -template \ -class JMethod : public JMethodBase { \ - public: \ - static_assert(std::is_void::value || IsJniPrimitive(), \ - "TYPE must be primitive or void"); \ - \ - using JMethodBase::JMethodBase; \ - JMethod() noexcept {}; \ - JMethod(const JMethod& other) noexcept = default; \ - \ - TYPE operator()(alias_ref self, Args... args) const; \ - \ - friend class JClass; \ -} - -DEFINE_PRIMITIVE_METHOD_CLASS(void); -DEFINE_PRIMITIVE_METHOD_CLASS(jboolean); -DEFINE_PRIMITIVE_METHOD_CLASS(jbyte); -DEFINE_PRIMITIVE_METHOD_CLASS(jchar); -DEFINE_PRIMITIVE_METHOD_CLASS(jshort); -DEFINE_PRIMITIVE_METHOD_CLASS(jint); -DEFINE_PRIMITIVE_METHOD_CLASS(jlong); -DEFINE_PRIMITIVE_METHOD_CLASS(jfloat); -DEFINE_PRIMITIVE_METHOD_CLASS(jdouble); - -#pragma pop_macro("DEFINE_PRIMITIVE_METHOD_CLASS") -/// @endcond - - -/// Convenience type representing constructors -/// These should only be used with JClass::getConstructor and JClass::newObject. -template -struct JConstructor : private JMethod { - using JMethod::JMethod; - private: - JConstructor(const JMethod& other) : JMethod(other.getId()) {} - friend class JClass; -}; - -/// Representation of a jStaticMethodID -template -class JStaticMethod; - -/// @cond INTERNAL -#pragma push_macro("DEFINE_PRIMITIVE_STATIC_METHOD_CLASS") - -#undef DEFINE_PRIMITIVE_STATIC_METHOD_CLASS - -// Defining JStaticMethod specializations based on return value -#define DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(TYPE) \ -template \ -class JStaticMethod : public JMethodBase { \ - static_assert(std::is_void::value || IsJniPrimitive(), \ - "T must be a JNI primitive or void"); \ - \ - public: \ - using JMethodBase::JMethodBase; \ - JStaticMethod() noexcept {}; \ - JStaticMethod(const JStaticMethod& other) noexcept = default; \ - \ - TYPE operator()(alias_ref cls, Args... args) const; \ - \ - friend class JClass; \ -} - -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(void); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jboolean); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jbyte); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jchar); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jshort); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jint); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jlong); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jfloat); -DEFINE_PRIMITIVE_STATIC_METHOD_CLASS(jdouble); - -#pragma pop_macro("DEFINE_PRIMITIVE_STATIC_METHOD_CLASS") -/// @endcond - - -/// Representation of a jNonvirtualMethodID -template -class JNonvirtualMethod; - -/// @cond INTERNAL -#pragma push_macro("DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS") - -#undef DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS - -// Defining JNonvirtualMethod specializations based on return value -#define DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(TYPE) \ -template \ -class JNonvirtualMethod : public JMethodBase { \ - static_assert(std::is_void::value || IsJniPrimitive(), \ - "T must be a JNI primitive or void"); \ - \ - public: \ - using JMethodBase::JMethodBase; \ - JNonvirtualMethod() noexcept {}; \ - JNonvirtualMethod(const JNonvirtualMethod& other) noexcept = default; \ - \ - TYPE operator()(alias_ref self, alias_ref cls, Args... args) const; \ - \ - friend class JClass; \ -} - -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(void); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jboolean); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jbyte); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jchar); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jshort); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jint); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jlong); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jfloat); -DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS(jdouble); - -#pragma pop_macro("DEFINE_PRIMITIVE_NON_VIRTUAL_METHOD_CLASS") -/// @endcond - - -/** - * JField represents typed fields and simplifies their access. Note that object types return - * raw pointers which generally should promptly get a wrap_local treatment. - */ -template -class JField { - static_assert(IsJniScalar(), "T must be a JNI scalar"); - - public: - /// Wraps an existing field id - explicit JField(jfieldID field = nullptr) noexcept; - - /// Verify that the id is valid - explicit operator bool() const noexcept; - - /// Access the wrapped id - jfieldID getId() const noexcept; - - private: - jfieldID field_id_; - - /// Get field value - /// @pre object != nullptr - T get(jobject object) const noexcept; - - /// Set field value - /// @pre object != nullptr - void set(jobject object, T value) noexcept; - - friend class JObject; -}; - - -/** - * JStaticField represents typed fields and simplifies their access. Note that object types - * return raw pointers which generally should promptly get a wrap_local treatment. - */ -template -class JStaticField { - static_assert(IsJniScalar(), "T must be a JNI scalar"); - - public: - /// Wraps an existing field id - explicit JStaticField(jfieldID field = nullptr) noexcept; - - /// Verify that the id is valid - explicit operator bool() const noexcept; - - /// Access the wrapped id - jfieldID getId() const noexcept; - - private: - jfieldID field_id_; - - /// Get field value - /// @pre object != nullptr - T get(jclass jcls) const noexcept; - - /// Set field value - /// @pre object != nullptr - void set(jclass jcls, T value) noexcept; - - friend class JClass; - friend class JObject; -}; - - -/// Template magic to provide @ref jmethod_traits -template -struct jmethod_traits { - static std::string descriptor(); - static std::string constructor_descriptor(); -}; - - -// jtype_traits //////////////////////////////////////////////////////////////////////////////////// - -template -struct jtype_traits { -private: - using Repr = ReprType; -public: - // The jni type signature (described at - // http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html). - static std::string descriptor() { - std::string descriptor; - if (Repr::kJavaDescriptor == nullptr) { - descriptor = Repr::get_instantiated_java_descriptor(); - } else { - descriptor = Repr::kJavaDescriptor; - } - return descriptor; - } - - // The signature used for class lookups. See - // http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getName(). - static std::string base_name() { - if (Repr::kJavaDescriptor != nullptr) { - std::string base_name = Repr::kJavaDescriptor; - return base_name.substr(1, base_name.size() - 2); - } - return Repr::get_instantiated_base_name(); - } -}; - -#pragma push_macro("DEFINE_FIELD_AND_ARRAY_TRAIT") -#undef DEFINE_FIELD_AND_ARRAY_TRAIT - -#define DEFINE_FIELD_AND_ARRAY_TRAIT(TYPE, DSC) \ -template<> \ -struct jtype_traits { \ - static std::string descriptor() { return std::string{#DSC}; } \ - static std::string base_name() { return descriptor(); } \ - using array_type = TYPE ## Array; \ -}; \ -template<> \ -struct jtype_traits { \ - static std::string descriptor() { return std::string{"[" #DSC}; } \ - static std::string base_name() { return descriptor(); } \ - using entry_type = TYPE; \ -}; - -// There is no voidArray, handle that without the macro. -template<> -struct jtype_traits { - static std::string descriptor() { return std::string{"V"}; }; -}; - -DEFINE_FIELD_AND_ARRAY_TRAIT(jboolean, Z) -DEFINE_FIELD_AND_ARRAY_TRAIT(jbyte, B) -DEFINE_FIELD_AND_ARRAY_TRAIT(jchar, C) -DEFINE_FIELD_AND_ARRAY_TRAIT(jshort, S) -DEFINE_FIELD_AND_ARRAY_TRAIT(jint, I) -DEFINE_FIELD_AND_ARRAY_TRAIT(jlong, J) -DEFINE_FIELD_AND_ARRAY_TRAIT(jfloat, F) -DEFINE_FIELD_AND_ARRAY_TRAIT(jdouble, D) - -#pragma pop_macro("DEFINE_FIELD_AND_ARRAY_TRAIT") - - -template -struct jmethod_traits_from_cxx; - -}} - -#include "Meta-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/MetaConvert.h b/lib/fb/src/main/cpp/include/fbjni/detail/MetaConvert.h deleted file mode 100644 index df15bfba..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/MetaConvert.h +++ /dev/null @@ -1,159 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -#include "Common.h" -#include "References.h" - -namespace facebook { -namespace jni { - -namespace detail { - -// In order to avoid potentially filling the jni locals table, -// temporary objects (right now, this is just jstrings) need to be -// released. This is done by returning a holder which autoconverts to -// jstring. -template -inline T callToJni(T&& t) { - return t; -} - -template -inline JniType callToJni(local_ref&& sref) { - return sref.get(); -} - -// Normally, pass through types unmolested. -template -struct Convert { - typedef T jniType; - static jniType fromJni(jniType t) { - return t; - } - static jniType toJniRet(jniType t) { - return t; - } - static jniType toCall(jniType t) { - return t; - } -}; - -// This is needed for return conversion -template <> -struct Convert { - typedef void jniType; -}; - -// jboolean is an unsigned char, not a bool. Allow it to work either way. -template<> -struct Convert { - typedef jboolean jniType; - static bool fromJni(jniType t) { - return t; - } - static jniType toJniRet(bool t) { - return t; - } - static jniType toCall(bool t) { - return t; - } -}; - -// Sometimes (64-bit Android) jlong is "long long", but int64_t is "long". -// Allow int64_t to work as jlong. -template -struct Convert::value || std::is_same::value) && !std::is_same::value - >::type> { - typedef jlong jniType; - static T fromJni(jniType t) { - return t; - } - static jniType toJniRet(T t) { - return t; - } - static jniType toCall(T t) { - return t; - } -}; - -// convert to alias_ref from T -template -struct Convert> { - typedef JniType jniType; - static alias_ref fromJni(jniType t) { - return wrap_alias(t); - } - static jniType toJniRet(alias_ref t) { - return t.get(); - } - static jniType toCall(alias_ref t) { - return t.get(); - } -}; - -// convert return from local_ref -template -struct Convert> { - typedef JniType jniType; - // No automatic synthesis of local_ref - static jniType toJniRet(local_ref t) { - return t.release(); - } - static jniType toCall(local_ref t) { - return t.get(); - } -}; - -// convert return from global_ref -template -struct Convert> { - typedef JniType jniType; - // No automatic synthesis of global_ref - static jniType toJniRet(global_ref&& t) { - // If this gets called, ownership the global_ref was passed in here. (It's - // probably a copy of a persistent global_ref made when a function was - // declared to return a global_ref, but it could moved out or otherwise not - // referenced elsewhere. Doesn't matter.) Either way, the only safe way - // to return it is to make a local_ref, release it, and return the - // underlying local jobject. - auto ret = make_local(t); - return ret.release(); - } - static jniType toJniRet(const global_ref& t) { - // If this gets called, the function was declared to return const&. We - // have a ref to a global_ref whose lifetime will exceed this call, so we - // can just get the underlying jobject and return it to java without - // needing to make a local_ref. - return t.get(); - } - static jniType toCall(global_ref t) { - return t.get(); - } -}; - -template struct jni_sig_from_cxx_t; -template -struct jni_sig_from_cxx_t { - using JniRet = typename Convert::type>::jniType; - using JniSig = JniRet(typename Convert::type>::jniType...); -}; - -template -using jni_sig_from_cxx = typename jni_sig_from_cxx_t::JniSig; - -} // namespace detail - -template -struct jmethod_traits_from_cxx : jmethod_traits> { -}; - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators-inl.h deleted file mode 100644 index e630a992..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators-inl.h +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include -#include - -#include "Environment.h" - -namespace facebook { -namespace jni { - -/// @cond INTERNAL -namespace internal { - -// Statistics mostly provided for test (only updated if FBJNI_DEBUG_REFS is defined) -struct ReferenceStats { - std::atomic_uint locals_created, globals_created, weaks_created, - locals_deleted, globals_deleted, weaks_deleted; - - void reset() noexcept; -}; - -extern ReferenceStats g_reference_stats; -} -/// @endcond - - -// LocalReferenceAllocator ///////////////////////////////////////////////////////////////////////// - -inline jobject LocalReferenceAllocator::newReference(jobject original) const { - internal::dbglog("Local new: %p", original); - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.locals_created; - #endif - auto ref = Environment::current()->NewLocalRef(original); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return ref; -} - -inline void LocalReferenceAllocator::deleteReference(jobject reference) const noexcept { - internal::dbglog("Local release: %p", reference); - - if (reference) { - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.locals_deleted; - #endif - assert(verifyReference(reference)); - Environment::current()->DeleteLocalRef(reference); - } -} - -inline bool LocalReferenceAllocator::verifyReference(jobject reference) const noexcept { - return isObjectRefType(reference, JNILocalRefType); -} - - -// GlobalReferenceAllocator //////////////////////////////////////////////////////////////////////// - -inline jobject GlobalReferenceAllocator::newReference(jobject original) const { - internal::dbglog("Global new: %p", original); - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.globals_created; - #endif - auto ref = Environment::current()->NewGlobalRef(original); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return ref; -} - -inline void GlobalReferenceAllocator::deleteReference(jobject reference) const noexcept { - internal::dbglog("Global release: %p", reference); - - if (reference) { - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.globals_deleted; - #endif - assert(verifyReference(reference)); - Environment::current()->DeleteGlobalRef(reference); - } -} - -inline bool GlobalReferenceAllocator::verifyReference(jobject reference) const noexcept { - return isObjectRefType(reference, JNIGlobalRefType); -} - - -// WeakGlobalReferenceAllocator //////////////////////////////////////////////////////////////////// - -inline jobject WeakGlobalReferenceAllocator::newReference(jobject original) const { - internal::dbglog("Weak global new: %p", original); - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.weaks_created; - #endif - auto ref = Environment::current()->NewWeakGlobalRef(original); - FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return ref; -} - -inline void WeakGlobalReferenceAllocator::deleteReference(jobject reference) const noexcept { - internal::dbglog("Weak Global release: %p", reference); - - if (reference) { - #ifdef FBJNI_DEBUG_REFS - ++internal::g_reference_stats.weaks_deleted; - #endif - assert(verifyReference(reference)); - Environment::current()->DeleteWeakGlobalRef(reference); - } -} - -inline bool WeakGlobalReferenceAllocator::verifyReference(jobject reference) const noexcept { - return isObjectRefType(reference, JNIWeakGlobalRefType); -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators.h b/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators.h deleted file mode 100644 index 55028ecd..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/ReferenceAllocators.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -/** - * @file ReferenceAllocators.h - * - * Reference allocators are used to create and delete various classes of JNI references (local, - * global, and weak global). - */ - -#pragma once - -#include "Common.h" - -namespace facebook { namespace jni { - -/// Allocator that handles local references -class LocalReferenceAllocator { - public: - jobject newReference(jobject original) const; - void deleteReference(jobject reference) const noexcept; - bool verifyReference(jobject reference) const noexcept; -}; - -/// Allocator that handles global references -class GlobalReferenceAllocator { - public: - jobject newReference(jobject original) const; - void deleteReference(jobject reference) const noexcept; - bool verifyReference(jobject reference) const noexcept; -}; - -/// Allocator that handles weak global references -class WeakGlobalReferenceAllocator { - public: - jobject newReference(jobject original) const; - void deleteReference(jobject reference) const noexcept; - bool verifyReference(jobject reference) const noexcept; -}; - -/** - * @return Helper based on GetObjectRefType. Since this isn't defined - * on all versions of Java or Android, if the type can't be - * determined, this returns true. If reference is nullptr, returns - * true. - */ -bool isObjectRefType(jobject reference, jobjectRefType refType); - -}} - -#include "ReferenceAllocators-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/References-forward.h b/lib/fb/src/main/cpp/include/fbjni/detail/References-forward.h deleted file mode 100644 index eab78de8..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/References-forward.h +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include "ReferenceAllocators.h" - -namespace facebook { -namespace jni { - -template -class JObjectWrapper; - -namespace detail { -struct JObjectBase { - jobject get() const noexcept; - void set(jobject reference) noexcept; - jobject this_; -}; - -// RefReprType maps a type to the representation used by fbjni smart references. -template -struct RefReprType; - -template -struct JavaObjectType; - -template -struct ReprAccess; -} - -// Given T, either a jobject-like type or a JavaClass-derived type, ReprType -// is the corresponding JavaClass-derived type and JniType is the -// jobject-like type. -template -using ReprType = typename detail::RefReprType::type; - -template -using JniType = typename detail::JavaObjectType::type; - -template -class base_owned_ref; - -template -class basic_strong_ref; - -template -class weak_ref; - -template -class alias_ref; - -/// A smart unique reference owning a local JNI reference -template -using local_ref = basic_strong_ref; - -/// A smart unique reference owning a global JNI reference -template -using global_ref = basic_strong_ref; - -}} // namespace facebook::jni diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/References-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/References-inl.h deleted file mode 100644 index eb2d834e..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/References-inl.h +++ /dev/null @@ -1,535 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include "CoreClasses.h" - -namespace facebook { -namespace jni { - -template -inline enable_if_t(), T> getPlainJniReference(T ref) { - return ref; -} - -template -inline JniType getPlainJniReference(alias_ref ref) { - return ref.get(); -} - -template -inline JniType getPlainJniReference(const base_owned_ref& ref) { - return ref.get(); -} - - -namespace detail { -template -struct ReprAccess { - using javaobject = JniType; - static void set(Repr& repr, javaobject obj) noexcept { - repr.JObjectBase::set(obj); - } - static javaobject get(const Repr& repr) { - return static_cast(repr.JObject::get()); - } -}; - -namespace { -template -void StaticAssertValidRepr() noexcept { - static_assert(std::is_base_of::value, - "A smart ref representation must be derived from JObject."); - static_assert(IsPlainJniReference>(), "T must be a JNI reference"); - static_assert(sizeof(Repr) == sizeof(JObjectBase), ""); - static_assert(alignof(Repr) == alignof(JObjectBase), ""); -} -} - -template -ReprStorage::ReprStorage(JniType obj) noexcept { - StaticAssertValidRepr(); - set(obj); -} - -template -void ReprStorage::set(JniType obj) noexcept { - new (&storage_) Repr; - ReprAccess::set(get(), obj); -} - -template -Repr& ReprStorage::get() noexcept { - return *reinterpret_cast(&storage_); -} - -template -const Repr& ReprStorage::get() const noexcept { - return *reinterpret_cast(&storage_); -} - -template -JniType ReprStorage::jobj() const noexcept { - ReprAccess::get(get()); - return ReprAccess::get(get()); -} - -template -void ReprStorage::swap(ReprStorage& other) noexcept { - StaticAssertValidRepr(); - using std::swap; - swap(get(), other.get()); -} - -inline void JObjectBase::set(jobject reference) noexcept { - this_ = reference; -} - -inline jobject JObjectBase::get() const noexcept { - return this_; -} - -template -enable_if_t(), plain_jni_reference_t> make_ref(const T& reference) { - auto old_reference = getPlainJniReference(reference); - if (!old_reference) { - return nullptr; - } - - auto ref = Alloc{}.newReference(old_reference); - if (!ref) { - // Note that we end up here if we pass a weak ref that refers to a collected object. - // Thus, it's hard to come up with a reason why this function should be used with - // weak references. - throw std::bad_alloc{}; - } - - return static_cast>(ref); -} - -} // namespace detail - -template -inline local_ref adopt_local(T ref) noexcept { - static_assert(IsPlainJniReference(), "T must be a plain jni reference"); - return local_ref{ref}; -} - -template -inline global_ref adopt_global(T ref) noexcept { - static_assert(IsPlainJniReference(), "T must be a plain jni reference"); - return global_ref{ref}; -} - -template -inline weak_ref adopt_weak_global(T ref) noexcept { - static_assert(IsPlainJniReference(), "T must be a plain jni reference"); - return weak_ref{ref}; -} - - -template -inline enable_if_t(), alias_ref> wrap_alias(T ref) noexcept { - return alias_ref(ref); -} - - -template -enable_if_t(), alias_ref> wrap_alias(T ref) noexcept; - - -template -enable_if_t(), local_ref>> -make_local(const T& ref) { - return adopt_local(detail::make_ref(ref)); -} - -template -enable_if_t(), global_ref>> -make_global(const T& ref) { - return adopt_global(detail::make_ref(ref)); -} - -template -enable_if_t(), weak_ref>> -make_weak(const T& ref) { - return adopt_weak_global(detail::make_ref(ref)); -} - -template -inline enable_if_t() && IsNonWeakReference(), bool> -operator==(const T1& a, const T2& b) { - return isSameObject(getPlainJniReference(a), getPlainJniReference(b)); -} - -template -inline enable_if_t() && IsNonWeakReference(), bool> -operator!=(const T1& a, const T2& b) { - return !(a == b); -} - -template -inline enable_if_t(), bool> -operator==(const T1& a, std::nullptr_t) { - return getPlainJniReference(a) == nullptr; -} - -template -inline enable_if_t(), bool> -operator==(std::nullptr_t, const T1& a) { - return nullptr == getPlainJniReference(a); -} - -template -inline enable_if_t(), bool> -operator!=(const T1& a, std::nullptr_t) { - return !(a == nullptr); -} - -template -inline enable_if_t(), bool> -operator!=(std::nullptr_t, const T1& a) { - return !(nullptr == getPlainJniReference(a)); -} - -// base_owned_ref /////////////////////////////////////////////////////////////////////// - -template -inline base_owned_ref::base_owned_ref() noexcept - : base_owned_ref(nullptr) -{} - -template -inline base_owned_ref::base_owned_ref(std::nullptr_t t) noexcept - : base_owned_ref(static_cast(nullptr)) -{ - (void)t; -} - -template -inline base_owned_ref::base_owned_ref(const base_owned_ref& other) - : storage_{static_cast(Alloc{}.newReference(other.get()))} -{} - -template -template -inline base_owned_ref::base_owned_ref(const base_owned_ref& other) - : storage_{static_cast(Alloc{}.newReference(other.get()))} -{ - static_assert(std::is_convertible, javaobject>::value, ""); -} - -template -inline facebook::jni::base_owned_ref::base_owned_ref( - javaobject reference) noexcept - : storage_(reference) { - assert(Alloc{}.verifyReference(reference)); - internal::dbglog("New wrapped ref=%p this=%p", get(), this); -} - -template -inline base_owned_ref::base_owned_ref( - base_owned_ref&& other) noexcept - : storage_(other.get()) { - internal::dbglog("New move from ref=%p other=%p", other.get(), &other); - internal::dbglog("New move to ref=%p this=%p", get(), this); - // JObject is a simple type and does not support move semantics so we explicitly - // clear other - other.set(nullptr); -} - -template -template -base_owned_ref::base_owned_ref(base_owned_ref&& other) noexcept - : storage_(other.get()) { - internal::dbglog("New move from ref=%p other=%p", other.get(), &other); - internal::dbglog("New move to ref=%p this=%p", get(), this); - // JObject is a simple type and does not support move semantics so we explicitly - // clear other - other.set(nullptr); -} - -template -inline base_owned_ref::~base_owned_ref() noexcept { - reset(); - internal::dbglog("Ref destruct ref=%p this=%p", get(), this); -} - -template -inline auto base_owned_ref::release() noexcept -> javaobject { - auto value = get(); - internal::dbglog("Ref release ref=%p this=%p", value, this); - set(nullptr); - return value; -} - -template -inline void base_owned_ref::reset() noexcept { - reset(nullptr); -} - -template -inline void base_owned_ref::reset(javaobject reference) noexcept { - if (get()) { - assert(Alloc{}.verifyReference(reference)); - Alloc{}.deleteReference(get()); - } - set(reference); -} - -template -inline auto base_owned_ref::get() const noexcept -> javaobject { - return storage_.jobj(); -} - -template -inline void base_owned_ref::set(javaobject ref) noexcept { - storage_.set(ref); -} - - -// weak_ref /////////////////////////////////////////////////////////////////////// - -template -inline weak_ref& weak_ref::operator=( - const weak_ref& other) { - auto otherCopy = other; - swap(*this, otherCopy); - return *this; -} - -template -inline weak_ref& weak_ref::operator=( - weak_ref&& other) noexcept { - internal::dbglog("Op= move ref=%p this=%p oref=%p other=%p", - get(), this, other.get(), &other); - reset(other.release()); - return *this; -} - -template -local_ref weak_ref::lockLocal() const { - return adopt_local( - static_cast(LocalReferenceAllocator{}.newReference(get()))); -} - -template -global_ref weak_ref::lockGlobal() const { - return adopt_global( - static_cast(GlobalReferenceAllocator{}.newReference(get()))); -} - -template -inline void swap( - weak_ref& a, - weak_ref& b) noexcept { - internal::dbglog("Ref swap a.ref=%p a=%p b.ref=%p b=%p", - a.get(), &a, b.get(), &b); - a.storage_.swap(b.storage_); -} - - -// basic_strong_ref //////////////////////////////////////////////////////////////////////////// - -template -inline basic_strong_ref& basic_strong_ref::operator=( - const basic_strong_ref& other) { - auto otherCopy = other; - swap(*this, otherCopy); - return *this; -} - -template -inline basic_strong_ref& basic_strong_ref::operator=( - basic_strong_ref&& other) noexcept { - internal::dbglog("Op= move ref=%p this=%p oref=%p other=%p", - get(), this, other.get(), &other); - reset(other.release()); - return *this; -} - -template -inline alias_ref basic_strong_ref::releaseAlias() noexcept { - return wrap_alias(release()); -} - -template -inline basic_strong_ref::operator bool() const noexcept { - return get() != nullptr; -} - -template -inline auto basic_strong_ref::operator->() noexcept -> Repr* { - return &storage_.get(); -} - -template -inline auto basic_strong_ref::operator->() const noexcept -> const Repr* { - return &storage_.get(); -} - -template -inline auto basic_strong_ref::operator*() noexcept -> Repr& { - return storage_.get(); -} - -template -inline auto basic_strong_ref::operator*() const noexcept -> const Repr& { - return storage_.get(); -} - -template -inline void swap( - basic_strong_ref& a, - basic_strong_ref& b) noexcept { - internal::dbglog("Ref swap a.ref=%p a=%p b.ref=%p b=%p", - a.get(), &a, b.get(), &b); - using std::swap; - a.storage_.swap(b.storage_); -} - - -// alias_ref ////////////////////////////////////////////////////////////////////////////// - -template -inline alias_ref::alias_ref() noexcept - : storage_{nullptr} -{} - -template -inline alias_ref::alias_ref(std::nullptr_t) noexcept - : storage_{nullptr} -{} - -template -inline alias_ref::alias_ref(const alias_ref& other) noexcept - : storage_{other.get()} -{} - -template -inline alias_ref::alias_ref(javaobject ref) noexcept - : storage_(ref) { - assert( - LocalReferenceAllocator{}.verifyReference(ref) || - GlobalReferenceAllocator{}.verifyReference(ref)); -} - -template -template -inline alias_ref::alias_ref(alias_ref other) noexcept - : storage_{other.get()} -{} - -template -template -inline alias_ref::alias_ref(const basic_strong_ref& other) noexcept - : storage_{other.get()} -{} - -template -inline alias_ref& alias_ref::operator=(alias_ref other) noexcept { - swap(*this, other); - return *this; -} - -template -inline alias_ref::operator bool() const noexcept { - return get() != nullptr; -} - -template -inline auto facebook::jni::alias_ref::get() const noexcept -> javaobject { - return storage_.jobj(); -} - -template -inline auto alias_ref::operator->() noexcept -> Repr* { - return &(**this); -} - -template -inline auto alias_ref::operator->() const noexcept -> const Repr* { - return &(**this); -} - -template -inline auto alias_ref::operator*() noexcept -> Repr& { - return storage_.get(); -} - -template -inline auto alias_ref::operator*() const noexcept -> const Repr& { - return storage_.get(); -} - -template -inline void alias_ref::set(javaobject ref) noexcept { - storage_.set(ref); -} - -template -inline void swap(alias_ref& a, alias_ref& b) noexcept { - a.storage_.swap(b.storage_); -} - -// Could reduce code duplication by using a pointer-to-function -// template argument. I'm not sure whether that would make the code -// more maintainable (DRY), or less (too clever/confusing.). -template -enable_if_t(), local_ref> -static_ref_cast(const local_ref& ref) noexcept -{ - T p = static_cast(ref.get()); - return make_local(p); -} - -template -enable_if_t(), global_ref> -static_ref_cast(const global_ref& ref) noexcept -{ - T p = static_cast(ref.get()); - return make_global(p); -} - -template -enable_if_t(), alias_ref> -static_ref_cast(const alias_ref& ref) noexcept -{ - T p = static_cast(ref.get()); - return wrap_alias(p); -} - -template -auto dynamic_ref_cast(const RefType& ref) -> -enable_if_t(), decltype(static_ref_cast(ref))> -{ - if (!ref) { - return decltype(static_ref_cast(ref))(); - } - - static alias_ref target_class = findClassStatic(jtype_traits::base_name().c_str()); - if (!target_class) { - throwNewJavaException("java/lang/ClassCastException", - "Could not find class %s.", - jtype_traits::base_name().c_str()); - - } - - local_ref source_class = ref->getClass(); - - if (!target_class->isAssignableFrom(source_class)) { - throwNewJavaException("java/lang/ClassCastException", - "Tried to cast from %s to %s.", - source_class->toString().c_str(), - jtype_traits::base_name().c_str()); - } - - return static_ref_cast(ref); -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/References.h b/lib/fb/src/main/cpp/include/fbjni/detail/References.h deleted file mode 100644 index 0984134e..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/References.h +++ /dev/null @@ -1,603 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ - -/** @file References.h - * - * Functionality similar to smart pointers, but for references into the VM. Four main reference - * types are provided: local_ref, global_ref, weak_ref, and alias_ref. All are generic - * templates that and refer to objects in the jobject hierarchy. The type of the referred objects - * are specified using the template parameter. All reference types except alias_ref own their - * underlying reference, just as a std smart pointer owns the underlying raw pointer. In the context - * of std smart pointers, these references behave like unique_ptr, and have basically the same - * interface. Thus, when the reference is destructed, the plain JNI reference, i.e. the underlying - * JNI reference (like the parameters passed directly to JNI functions), is released. The alias - * references provides no ownership and is a simple wrapper for plain JNI references. - * - * All but the weak references provides access to the underlying object using dereferencing, and a - * get() method. It is also possible to convert these references to booleans to test for nullity. - * To access the underlying object of a weak reference, the reference must either be released, or - * the weak reference can be used to create a local or global reference. - * - * An owning reference is created either by moving the reference from an existing owned reference, - * by copying an existing owned reference (which creates a new underlying reference), by using the - * default constructor which initialize the reference to nullptr, or by using a helper function. The - * helper function exist in two flavors: make_XXX or adopt_XXX. - * - * Adopting takes a plain JNI reference and wrap it in an owned reference. It takes ownership of the - * plain JNI reference so be sure that no one else owns the reference when you adopt it, and make - * sure that you know what kind of reference it is. - * - * New owned references can be created from existing plain JNI references, alias references, local - * references, and global references (i.e. non-weak references) using the make_local, make_global, - * and make_weak functions. - * - * Alias references can be implicitly initialized using global, local and plain JNI references using - * the wrap_alias function. Here, we don't assume ownership of the passed-in reference, but rather - * create a separate reference that we do own, leaving the passed-in reference to its fate. - * - * Similar rules apply for assignment. An owned reference can be copy or move assigned using a smart - * reference of the same type. In the case of copy assignment a new reference is created. Alias - * reference can also be assigned new values, but since they are simple wrappers of plain JNI - * references there is no move semantics involved. - * - * Alias references are special in that they do not own the object and can therefore safely be - * converted to and from its corresponding plain JNI reference. They are useful as parameters of - * functions that do not affect the lifetime of a reference. Usage can be compared with using plain - * JNI pointers as parameters where a function does not take ownership of the underlying object. - * - * The local, global, and alias references makes it possible to access methods in the underlying - * objects. A core set of classes are implemented in CoreClasses.h, and user defined wrappers are - * supported (see example below). The wrappers also supports inheritance so a wrapper can inherit - * from another wrapper to gain access to its functionality. As an example the jstring wrapper - * inherits from the jobject wrapper, so does the jclass wrapper. That means that you can for - * example call the toString() method using the jclass wrapper, or any other class that inherits - * from the jobject wrapper. - * - * Note that the wrappers are parameterized on the static type of your (jobject) pointer, thus if - * you have a jobject that refers to a Java String you will need to cast it to jstring to get the - * jstring wrapper. This also mean that if you make a down cast that is invalid there will be no one - * stopping you and the wrappers currently does not detect this which can cause crashes. Thus, cast - * wisely. - * - * @include WrapperSample.cpp - */ - -#pragma once - -#include -#include -#include - -#include - -#include "ReferenceAllocators.h" -#include "TypeTraits.h" -#include "References-forward.h" - -namespace facebook { -namespace jni { - -/// Convenience function to wrap an existing local reference -template -local_ref adopt_local(T ref) noexcept; - -/// Convenience function to wrap an existing global reference -template -global_ref adopt_global(T ref) noexcept; - -/// Convenience function to wrap an existing weak reference -template -weak_ref adopt_weak_global(T ref) noexcept; - - -/// Swaps two owning references of the same type -template -void swap(weak_ref& a, weak_ref& b) noexcept; - -/// Swaps two owning references of the same type -template -void swap(basic_strong_ref& a, basic_strong_ref& b) noexcept; - -/** - * Retrieve the plain reference from a plain reference. - */ -template -enable_if_t(), T> getPlainJniReference(T ref); - -/** - * Retrieve the plain reference from an alias reference. - */ -template -JniType getPlainJniReference(alias_ref ref); - -/** - * Retrieve the plain JNI reference from any reference owned reference. - */ -template -JniType getPlainJniReference(const base_owned_ref& ref); - -class JObject; -class JClass; - -namespace detail { - -template -struct HasJniRefRepr : std::false_type {}; - -template -struct HasJniRefRepr::value, void>::type> : std::true_type { - using type = typename T::JniRefRepr; -}; - -template -struct RefReprType { - using type = typename std::conditional::value, typename HasJniRefRepr::type, JObjectWrapper>::type; - static_assert(std::is_base_of::value, - "Repr type missing JObject base."); - static_assert(std::is_same::type>::value, - "RefReprType not idempotent"); -}; - -template -struct RefReprType::value, void>::type> { - using type = T; - static_assert(std::is_base_of::value, - "Repr type missing JObject base."); - static_assert(std::is_same::type>::value, - "RefReprType not idempotent"); -}; - -template -struct JavaObjectType { - using type = typename RefReprType::type::javaobject; - static_assert(IsPlainJniReference(), - "JavaObjectType not a plain jni reference"); - static_assert(std::is_same::type>::value, - "JavaObjectType not idempotent"); -}; - -template -struct JavaObjectType> { - using type = T; - static_assert(IsPlainJniReference(), - "JavaObjectType not a plain jni reference"); - static_assert(std::is_same::type>::value, - "JavaObjectType not idempotent"); -}; - -template -struct JavaObjectType { - using type = T*; - static_assert(IsPlainJniReference(), - "JavaObjectType not a plain jni reference"); - static_assert(std::is_same::type>::value, - "JavaObjectType not idempotent"); -}; - -template -struct ReprStorage { - explicit ReprStorage(JniType obj) noexcept; - - void set(JniType obj) noexcept; - - Repr& get() noexcept; - const Repr& get() const noexcept; - JniType jobj() const noexcept; - - void swap(ReprStorage& other) noexcept; - private: - ReprStorage() = delete; - ReprStorage(const ReprStorage&) = delete; - ReprStorage(ReprStorage&&) = delete; - ReprStorage& operator=(const ReprStorage&) = delete; - ReprStorage& operator=(ReprStorage&&) = delete; - - using Storage = typename std::aligned_storage::type; - Storage storage_; -}; - -} // namespace detail - -/** - * Create a new local reference from an existing reference - * - * @param ref a plain JNI, alias, or strong reference - * @return an owned local reference (referring to null if the input does) - * @throws std::bad_alloc if the JNI reference could not be created - */ -template -enable_if_t(), local_ref>> -make_local(const T& r); - -/** - * Create a new global reference from an existing reference - * - * @param ref a plain JNI, alias, or strong reference - * @return an owned global reference (referring to null if the input does) - * @throws std::bad_alloc if the JNI reference could not be created - */ -template -enable_if_t(), global_ref>> -make_global(const T& r); - -/** - * Create a new weak global reference from an existing reference - * - * @param ref a plain JNI, alias, or strong reference - * @return an owned weak global reference (referring to null if the input does) - * @throws std::bad_alloc if the returned reference is null - */ -template -enable_if_t(), weak_ref>> -make_weak(const T& r); - -/** - * Compare two references to see if they refer to the same object - */ -template -enable_if_t() && IsNonWeakReference(), bool> -operator==(const T1& a, const T2& b); - -/** - * Compare two references to see if they don't refer to the same object - */ -template -enable_if_t() && IsNonWeakReference(), bool> -operator!=(const T1& a, const T2& b); - -/** - * Compare references against nullptr - */ -template -enable_if_t(), bool> -operator==(const T1& a, std::nullptr_t); - -template -enable_if_t(), bool> -operator==(std::nullptr_t, const T1& a); - -template -enable_if_t(), bool> -operator!=(const T1& a, std::nullptr_t); - -template -enable_if_t(), bool> -operator!=(std::nullptr_t, const T1& a); - - -template -class base_owned_ref { - public: - using javaobject = JniType; - - /** - * Release the ownership and set the reference to null. Thus no deleter is invoked. - * @return Returns the reference - */ - javaobject release() noexcept; - - /** - * Reset the reference to refer to nullptr. - */ - void reset() noexcept; - - protected: - using Repr = ReprType; - detail::ReprStorage storage_; - - javaobject get() const noexcept; - void set(javaobject ref) noexcept; - - /* - * Wrap an existing reference and transfers its ownership to the newly created unique reference. - * NB! Does not create a new reference - */ - explicit base_owned_ref(javaobject reference) noexcept; - - /// Create a null reference - base_owned_ref() noexcept; - - /// Create a null reference - explicit base_owned_ref(std::nullptr_t) noexcept; - - /// Copy constructor (note creates a new reference) - base_owned_ref(const base_owned_ref& other); - template - base_owned_ref(const base_owned_ref& other); - - /// Transfers ownership of an underlying reference from one unique reference to another - base_owned_ref(base_owned_ref&& other) noexcept; - template - base_owned_ref(base_owned_ref&& other) noexcept; - - /// The delete the underlying reference if applicable - ~base_owned_ref() noexcept; - - - /// Assignment operator (note creates a new reference) - base_owned_ref& operator=(const base_owned_ref& other); - - /// Assignment by moving a reference thus not creating a new reference - base_owned_ref& operator=(base_owned_ref&& rhs) noexcept; - - void reset(javaobject reference) noexcept; - - friend javaobject jni::getPlainJniReference<>(const base_owned_ref& ref); - - template - friend class base_owned_ref; -}; - - -/** - * A smart reference that owns its underlying JNI reference. The class provides basic - * functionality to handle a reference but gives no access to it unless the reference is - * released, thus no longer owned. The API is stolen with pride from unique_ptr and the - * semantics should be basically the same. This class should not be used directly, instead use - * @ref weak_ref - */ -template -class weak_ref : public base_owned_ref { - public: - using javaobject = JniType; - - using Allocator = WeakGlobalReferenceAllocator; - - // This inherits non-default, non-copy, non-move ctors. - using base_owned_ref::base_owned_ref; - - /// Create a null reference - weak_ref() noexcept - : base_owned_ref{} {} - - /// Create a null reference - /* implicit */ weak_ref(std::nullptr_t) noexcept - : base_owned_ref{nullptr} {} - - /// Copy constructor (note creates a new reference) - weak_ref(const weak_ref& other) - : base_owned_ref{other} {} - - // This needs to be explicit to change its visibility. - template - weak_ref(const weak_ref& other) - : base_owned_ref{other} {} - - /// Transfers ownership of an underlying reference from one unique reference to another - weak_ref(weak_ref&& other) noexcept - : base_owned_ref{std::move(other)} {} - - - /// Assignment operator (note creates a new reference) - weak_ref& operator=(const weak_ref& other); - - /// Assignment by moving a reference thus not creating a new reference - weak_ref& operator=(weak_ref&& rhs) noexcept; - - // Creates an owned local reference to the referred object or to null if the object is reclaimed - local_ref lockLocal() const; - - // Creates an owned global reference to the referred object or to null if the object is reclaimed - global_ref lockGlobal() const; - - private: - // get/release/reset on weak_ref are not exposed to users. - using base_owned_ref::get; - using base_owned_ref::release; - using base_owned_ref::reset; - /* - * Wrap an existing reference and transfers its ownership to the newly created unique reference. - * NB! Does not create a new reference - */ - explicit weak_ref(javaobject reference) noexcept - : base_owned_ref{reference} {} - - template friend class weak_ref; - friend weak_ref adopt_weak_global(javaobject ref) noexcept; - friend void swap(weak_ref& a, weak_ref& b) noexcept; -}; - - -/** - * A class representing owned strong references to Java objects. This class - * should not be used directly, instead use @ref local_ref, or @ref global_ref. - */ -template -class basic_strong_ref : public base_owned_ref { - using typename base_owned_ref::Repr; - public: - using javaobject = JniType; - - using Allocator = Alloc; - - // This inherits non-default, non-copy, non-move ctors. - using base_owned_ref::base_owned_ref; - using base_owned_ref::release; - using base_owned_ref::reset; - - /// Create a null reference - basic_strong_ref() noexcept - : base_owned_ref{} {} - - /// Create a null reference - /* implicit */ basic_strong_ref(std::nullptr_t) noexcept - : base_owned_ref{nullptr} {} - - /// Copy constructor (note creates a new reference) - basic_strong_ref(const basic_strong_ref& other) - : base_owned_ref{other} {} - - // This needs to be explicit to change its visibility. - template - basic_strong_ref(const basic_strong_ref& other) - : base_owned_ref{other} {} - - /// Transfers ownership of an underlying reference from one unique reference to another - basic_strong_ref(basic_strong_ref&& other) noexcept - : base_owned_ref{std::move(other)} {} - - /// Assignment operator (note creates a new reference) - basic_strong_ref& operator=(const basic_strong_ref& other); - - /// Assignment by moving a reference thus not creating a new reference - basic_strong_ref& operator=(basic_strong_ref&& rhs) noexcept; - - /// Get the plain JNI reference - using base_owned_ref::get; - - /// Release the ownership of the reference and return the wrapped reference in an alias - alias_ref releaseAlias() noexcept; - - /// Checks if the reference points to a non-null object - explicit operator bool() const noexcept; - - /// Access the functionality provided by the object wrappers - Repr* operator->() noexcept; - - /// Access the functionality provided by the object wrappers - const Repr* operator->() const noexcept; - - /// Provide a reference to the underlying wrapper (be sure that it is non-null before invoking) - Repr& operator*() noexcept; - - /// Provide a const reference to the underlying wrapper (be sure that it is non-null - /// before invoking) - const Repr& operator*() const noexcept; - - private: - - using base_owned_ref::storage_; - - /* - * Wrap an existing reference and transfers its ownership to the newly created unique reference. - * NB! Does not create a new reference - */ - explicit basic_strong_ref(javaobject reference) noexcept - : base_owned_ref{reference} {} - - - friend local_ref adopt_local(T ref) noexcept; - friend global_ref adopt_global(T ref) noexcept; - friend void swap(basic_strong_ref& a, basic_strong_ref& b) noexcept; -}; - - -template -enable_if_t(), alias_ref> wrap_alias(T ref) noexcept; - -/// Swaps to alias reference of the same type -template -void swap(alias_ref& a, alias_ref& b) noexcept; - -/** - * A non-owning variant of the smart references (a dumb reference). These references still provide - * access to the functionality of the @ref JObjectWrapper specializations including exception - * handling and ease of use. Use this representation when you don't want to claim ownership of the - * underlying reference (compare to using raw pointers instead of smart pointers.) For symmetry use - * @ref alias_ref instead of this class. - */ -template -class alias_ref { - using Repr = ReprType; - - public: - using javaobject = JniType; - - /// Create a null reference - alias_ref() noexcept; - - /// Create a null reference - /* implicit */ alias_ref(std::nullptr_t) noexcept; - - /// Copy constructor - alias_ref(const alias_ref& other) noexcept; - - /// Wrap an existing plain JNI reference - /* implicit */ alias_ref(javaobject ref) noexcept; - - /// Wrap an existing smart reference of any type convertible to T - template< - typename TOther, - typename = enable_if_t< - IsConvertible, javaobject>(), T> - > - alias_ref(alias_ref other) noexcept; - - /// Wrap an existing alias reference of a type convertible to T - template< - typename TOther, - typename AOther, - typename = enable_if_t< - IsConvertible, javaobject>(), T> - > - alias_ref(const basic_strong_ref& other) noexcept; - - /// Assignment operator - alias_ref& operator=(alias_ref other) noexcept; - - /// Checks if the reference points to a non-null object - explicit operator bool() const noexcept; - - /// Converts back to a plain JNI reference - javaobject get() const noexcept; - - /// Access the functionality provided by the object wrappers - Repr* operator->() noexcept; - - /// Access the functionality provided by the object wrappers - const Repr* operator->() const noexcept; - - /// Provide a guaranteed non-null reference (be sure that it is non-null before invoking) - Repr& operator*() noexcept; - - /// Provide a guaranteed non-null reference (be sure that it is non-null before invoking) - const Repr& operator*() const noexcept; - - private: - void set(javaobject ref) noexcept; - - detail::ReprStorage storage_; - - friend void swap(alias_ref& a, alias_ref& b) noexcept; -}; - - -/** - * RAII object to create a local JNI frame, using PushLocalFrame/PopLocalFrame. - * - * This is useful when you have a call which is initiated from C++-land, and therefore - * doesn't automatically get a local JNI frame managed for you by the JNI framework. - */ -class JniLocalScope { -public: - JniLocalScope(JNIEnv* p_env, jint capacity); - ~JniLocalScope(); - -private: - JNIEnv* env_; - bool hasFrame_; -}; - -template -enable_if_t(), local_ref> -static_ref_cast(const local_ref& ref) noexcept; - -template -enable_if_t(), global_ref> -static_ref_cast(const global_ref& ref) noexcept; - -template -enable_if_t(), alias_ref> -static_ref_cast(const alias_ref& ref) noexcept; - -template -auto dynamic_ref_cast(const RefType& ref) -> -enable_if_t(), decltype(static_ref_cast(ref))> ; - -}} - -#include "References-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Registration-inl.h b/lib/fb/src/main/cpp/include/fbjni/detail/Registration-inl.h deleted file mode 100644 index d8e2b043..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Registration-inl.h +++ /dev/null @@ -1,166 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include "Exceptions.h" -#include "Hybrid.h" - -namespace facebook { -namespace jni { - -namespace detail { - -#ifdef __i386__ -// X86 ABI forces 16 byte stack allignment on calls. Unfortunately -// sometimes Dalvik chooses not to obey the ABI: -// - https://code.google.com/p/android/issues/detail?id=61012 -// - https://android.googlesource.com/platform/ndk/+/81696d2%5E!/ -// Therefore, we tell the compiler to re-align the stack on entry -// to our JNI functions. -#define JNI_ENTRY_POINT __attribute__((force_align_arg_pointer)) -#else -#define JNI_ENTRY_POINT -#endif - -template -struct CreateDefault { - static R create() { - return R{}; - } -}; - -template <> -struct CreateDefault { - static void create() {} -}; - -template -using Converter = Convert::type>; - -template -struct WrapForVoidReturn { - static typename Converter::jniType call(Args&&... args) { - return Converter::toJniRet(func(std::forward(args)...)); - } -}; - -template -struct WrapForVoidReturn { - static void call(Args&&... args) { - func(std::forward(args)...); - } -}; - -// registration wrapper for legacy JNI-style functions -template -struct BareJniWrapper { - JNI_ENTRY_POINT static R call(JNIEnv* env, jobject obj, Args... args) { - detail::JniEnvCacher jec(env); - try { - return (*func)(env, static_cast>(obj), args...); - } catch (...) { - translatePendingCppExceptionToJavaException(); - return CreateDefault::create(); - } - } -}; - -// registration wrappers for functions, with autoconversion of arguments. -template -struct FunctionWrapper { - using jniRet = typename Converter::jniType; - JNI_ENTRY_POINT static jniRet call(JNIEnv* env, jobject obj, typename Converter::jniType... args) { - detail::JniEnvCacher jec(env); - try { - return WrapForVoidReturn, Args...>::call( - static_cast>(obj), Converter::fromJni(args)...); - } catch (...) { - translatePendingCppExceptionToJavaException(); - return CreateDefault::create(); - } - } -}; - -// registration wrappers for non-static methods, with autoconvertion of arguments. -template -struct MethodWrapper { - using jhybrid = typename C::jhybridobject; - static R dispatch(alias_ref ref, Args&&... args) { - try { - // This is usually a noop, but if the hybrid object is a - // base class of other classes which register JNI methods, - // this will get the right type for the registered method. - auto cobj = static_cast(ref->cthis()); - return (cobj->*method)(std::forward(args)...); - } catch (const std::exception& ex) { - C::mapException(ex); - throw; - } - } - - JNI_ENTRY_POINT static typename Converter::jniType call( - JNIEnv* env, jobject obj, typename Converter::jniType... args) { - return FunctionWrapper, Args&&...), dispatch, jhybrid, R, Args...>::call(env, obj, args...); - } -}; - -template -inline NativeMethodWrapper* exceptionWrapJNIMethod(R (*)(JNIEnv*, C, Args... args)) { - // This intentionally erases the real type; JNI will do it anyway - return reinterpret_cast(&(BareJniWrapper::call)); -} - -template -inline NativeMethodWrapper* exceptionWrapJNIMethod(R (*)(alias_ref, Args... args)) { - // This intentionally erases the real type; JNI will do it anyway - return reinterpret_cast(&(FunctionWrapper::call)); -} - -template -inline NativeMethodWrapper* exceptionWrapJNIMethod(R (C::*method0)(Args... args)) { - (void)method0; - // This intentionally erases the real type; JNI will do it anyway - return reinterpret_cast(&(MethodWrapper::call)); -} - -template -inline std::string makeDescriptor(R (*)(JNIEnv*, C, Args... args)) { - return jmethod_traits::descriptor(); -} - -template -inline std::string makeDescriptor(R (*)(alias_ref, Args... args)) { - return jmethod_traits_from_cxx::descriptor(); -} - -template -inline std::string makeDescriptor(R (C::*)(Args... args)) { - return jmethod_traits_from_cxx::descriptor(); -} - -template -template -JNI_ENTRY_POINT R CriticalMethod::call(alias_ref, Args... args) noexcept { - static_assert( - IsJniPrimitive() || std::is_void(), - "Critical Native Methods may only return primitive JNI types, or void."); - static_assert( - AreJniPrimitives(), - "Critical Native Methods may only use primitive JNI types as parameters"); - - return func(std::forward(args)...); -} - -template -template -inline std::string CriticalMethod::desc() { - return makeDescriptor(call); -} - -} - -}} diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/Registration.h b/lib/fb/src/main/cpp/include/fbjni/detail/Registration.h deleted file mode 100644 index 0808a070..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/Registration.h +++ /dev/null @@ -1,167 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include -#include "References.h" - -namespace facebook { -namespace jni { - -namespace detail { - -// This uses the real JNI function as a non-type template parameter to -// cause a (static member) function to exist with the same signature, -// but with try/catch exception translation. -template -NativeMethodWrapper* exceptionWrapJNIMethod(R (*func0)(JNIEnv*, jobject, Args... args)); - -// Automatically wrap object argument, and don't take env explicitly. -template -NativeMethodWrapper* exceptionWrapJNIMethod(R (*func0)(alias_ref, Args... args)); - -// Extract C++ instance from object, and invoke given method on it, -template -NativeMethodWrapper* exceptionWrapJNIMethod(R (C::*method0)(Args... args)); - -// This uses deduction to figure out the descriptor name if the types -// are primitive or have JObjectWrapper specializations. -template -std::string makeDescriptor(R (*func)(JNIEnv*, C, Args... args)); - -// This uses deduction to figure out the descriptor name if the types -// are primitive or have JObjectWrapper specializations. -template -std::string makeDescriptor(R (*func)(alias_ref, Args... args)); - -// This uses deduction to figure out the descriptor name if the types -// are primitive or have JObjectWrapper specializations. -template -std::string makeDescriptor(R (C::*method0)(Args... args)); - -template -struct CriticalMethod; - -template -struct CriticalMethod { - template - static R call(alias_ref, Args... args) noexcept; - - template - inline static std::string desc(); -}; - -} - -// We have to use macros here, because the func needs to be used -// as both a decltype expression argument and as a non-type template -// parameter, since C++ provides no way for translateException -// to deduce the type of its non-type template parameter. -// The empty string in the macros below ensures that name -// is always a string literal (because that syntax is only -// valid when name is a string literal). -#define makeNativeMethod2(name, func) \ - { name "", ::facebook::jni::detail::makeDescriptor(&func), \ - ::facebook::jni::detail::exceptionWrapJNIMethod(&func) } - -#define makeNativeMethod3(name, desc, func) \ - { name "", desc, \ - ::facebook::jni::detail::exceptionWrapJNIMethod(&func) } - -// Variadic template hacks to get macros with different numbers of -// arguments. Usage instructions are in CoreClasses.h. -#define makeNativeMethodN(a, b, c, count, ...) makeNativeMethod ## count -#define makeNativeMethod(...) makeNativeMethodN(__VA_ARGS__, 3, 2)(__VA_ARGS__) - - -// FAST CALLS / CRITICAL CALLS -// Android up to and including v7 supports "fast calls" by prefixing the method -// signature with an exclamation mark. -// Android v8+ supports fast calls by annotating methods: -// https://source.android.com/devices/tech/dalvik/improvements#faster-native-methods -// -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// -// "Fast" calls are only on the order of a few dozen NANO-seconds faster than -// regular JNI calls. If your method does almost aaanything of consequence - if -// you loop, if you write to a log, if you call another method, if you even -// simply allocate or deallocate - then the method body will significantly -// outweigh the method overhead. -// -// The difference between a regular JNI method and a "FastJNI" method (as -// they're called inside the runtime) is that a FastJNI method doesn't mark the -// thread as executing native code, and by skipping that avoids the locking and -// thread state check overhead of interacting with the Garbage Collector. -// -// To understand why this is dangerous, you need to understand a bit about the -// GC. In order to perform its work the GC needs to have at least one (usually -// two in modern implementations) "stop the world" checkpoints where it can -// guarantee that all managed-code execution is paused. The VM performs these -// checkpoints at allocations, method boundaries, and each backward branch (ie -// anytime you loop). When the GC wants to run, it will signal to all managed -// threads that they should pause at the next checkpoint, and then it will wait -// for every thread in the system to transition from the "runnable" state into a -// "waiting" state. Once every thread has stopped, the GC thread can perform the -// work it needs to and then it will trigger the execution threads to resume. -// -// JNI methods fit neatly into the above paradigm: They're still methods, so -// they perform GC checkpoints at method entry and method exit. JNI methods also -// perform checkpoints at any JNI boundary crossing - ie, any time you call -// GetObjectField etc. Because access to managed objects from native code is -// tightly controlled, the VM is able to mark threads executing native methods -// into a special "native" state which the GC is able to ignore: It knows they -// can't touch managed objects (without hitting a checkpoint) so it doesn't care -// about them. -// -// JNI critical methods don't perform that "runnable" -> "native" thread state -// transition. Skipping that transition allows them to shave about 20ns off -// their total execution time, but it means that the GC has to wait for them to -// complete before it can move forward. If a critical method begins blocking, -// say on a long loop, or an I/O operation, or on perhaps a mutex, then the GC -// will also block, and because the GC is blocking the entire rest of the VM -// (which is waiting on the GC) will block. If the critical method is blocking -// on a mutex that's already held by the GC - for example, the VM's internal -// weak_globals_lock_ which guards modifications to the weak global reference -// table (and is required in order to create or free a weak_ref<>) - then you -// have a system-wide deadlock. - -// prefixes a JNI method signature as android "fast call". -#if defined(__ANDROID__) && defined(FBJNI_WITH_FAST_CALLS) -#define FBJNI_PREFIX_FAST_CALL(desc) (std::string{"!"} + desc) -#else -#define FBJNI_PREFIX_FAST_CALL(desc) (desc) -#endif - -#define makeCriticalNativeMethod3(name, desc, func) \ - makeNativeMethod3( \ - name, \ - FBJNI_PREFIX_FAST_CALL(desc), \ - ::facebook::jni::detail::CriticalMethod::call<&func>) - -#define makeCriticalNativeMethod2(name, func) \ - makeCriticalNativeMethod3( \ - name, \ - ::facebook::jni::detail::CriticalMethod::desc<&func>(), \ - func) - -#define makeCriticalNativeMethodN(a, b, c, count, ...) makeCriticalNativeMethod ## count - -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS. -// See above for an explanation. -#define makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(...) makeCriticalNativeMethodN(__VA_ARGS__, 3, 2)(__VA_ARGS__) - -}} - -#include "Registration-inl.h" diff --git a/lib/fb/src/main/cpp/include/fbjni/detail/TypeTraits.h b/lib/fb/src/main/cpp/include/fbjni/detail/TypeTraits.h deleted file mode 100644 index e5ffaed9..00000000 --- a/lib/fb/src/main/cpp/include/fbjni/detail/TypeTraits.h +++ /dev/null @@ -1,176 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. - */ -#pragma once - -#include - -#include "References-forward.h" - -namespace facebook { -namespace jni { - -/// Generic std::enable_if helper -template -using enable_if_t = typename std::enable_if::type; - -/// Generic std::is_convertible helper -template -constexpr bool IsConvertible() { - return std::is_convertible::value; -} - -template class TT, typename T> -struct is_instantiation_of : std::false_type {}; - -template class TT, typename... Ts> -struct is_instantiation_of> : std::true_type {}; - -template class TT, typename... Ts> -constexpr bool IsInstantiationOf() { - return is_instantiation_of::value; -} - -/// Metafunction to determine whether a type is a JNI reference or not -template -struct is_plain_jni_reference : - std::integral_constant::value && - std::is_base_of< - typename std::remove_pointer::type, - typename std::remove_pointer::type>::value> {}; - -/// Helper to simplify use of is_plain_jni_reference -template -constexpr bool IsPlainJniReference() { - return is_plain_jni_reference::value; -} - -/// Metafunction to determine whether a type is a primitive JNI type or not -template -struct is_jni_primitive : - std::integral_constant::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value> {}; - -/// Helper to simplify use of is_jni_primitive -template -constexpr bool IsJniPrimitive() { - return is_jni_primitive::value; -} - -/// Metafunction to determine whether a series of types are all primitive JNI types. -template -struct are_jni_primitives; - -template -struct are_jni_primitives : - std::integral_constant::value && are_jni_primitives::value> {}; - -template<> -struct are_jni_primitives<> : std::integral_constant {}; - -/// Helper to simplify use of are_jni_primitives -template -constexpr bool AreJniPrimitives() { - return are_jni_primitives::value; -} - - -/// Metafunction to determine whether a type is a JNI array of primitives or not -template -struct is_jni_primitive_array : - std::integral_constant::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value> {}; - -/// Helper to simplify use of is_jni_primitive_array -template -constexpr bool IsJniPrimitiveArray() { - return is_jni_primitive_array::value; -} - -/// Metafunction to determine if a type is a scalar (primitive or reference) JNI type -template -struct is_jni_scalar : - std::integral_constant::value || - is_jni_primitive::value> {}; - -/// Helper to simplify use of is_jni_scalar -template -constexpr bool IsJniScalar() { - return is_jni_scalar::value; -} - -// Metafunction to determine if a type is a JNI type -template -struct is_jni_type : - std::integral_constant::value || - std::is_void::value> {}; - -/// Helper to simplify use of is_jni_type -template -constexpr bool IsJniType() { - return is_jni_type::value; -} - -template -struct is_non_weak_reference : - std::integral_constant() || - IsInstantiationOf() || - IsInstantiationOf()> {}; - -template -constexpr bool IsNonWeakReference() { - return is_non_weak_reference::value; -} - -template -struct is_any_reference : - std::integral_constant() || - IsInstantiationOf() || - IsInstantiationOf() || - IsInstantiationOf()> {}; - -template -constexpr bool IsAnyReference() { - return is_any_reference::value; -} - -template -struct reference_traits { - using plain_jni_reference_t = JniType; - static_assert(IsPlainJniReference(), "Need a plain JNI reference"); -}; - -template