Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No public description #339

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ cc_library(
"//implementation:global_string",
"//implementation:id",
"//implementation:id_type",
"//implementation:inheritance",
"//implementation:jni_type",
"//implementation:jvm",
"//implementation:jvm_ref",
Expand Down
23 changes: 23 additions & 0 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,28 @@ cc_library(
],
)

################################################################################
# Inheritance.
################################################################################
cc_library(
name = "inheritance",
hdrs = ["inheritance.h"],
deps = [
":id_type",
],
)

cc_test(
name = "inheritance_test",
srcs = ["inheritance_test.cc"],
deps = [
"//:jni_bind",
"//:jni_test",
"//testing/base/public:gunit_for_library",
"@googletest//:gtest_main",
],
)

################################################################################
# LocalArray.
################################################################################
Expand Down Expand Up @@ -920,6 +942,7 @@ cc_library(
":id_type",
":jni_type",
":method_selection",
":no_class_specified",
":no_idx",
":ref_base",
"//:jni_dep",
Expand Down
46 changes: 46 additions & 0 deletions implementation/inheritance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JNI_BIND_IMPLEMENTATION_INHERITANCE_H_
#define JNI_BIND_IMPLEMENTATION_INHERITANCE_H_

// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"

#include <type_traits>

#include "implementation/id_type.h"

namespace jni {

struct Inheritance {
template <typename IdT, typename Enable = void>
struct Helper {
static_assert(false,
"You must use an overload set or field for `Inheritance`.");
};

template <typename IdT>
struct Helper<IdT, std::enable_if_t<IdT::kIDType == IdType::OVERLOAD_SET>> {
using type = void;
};

template <typename IdT>
using type = typename Helper<IdT>::type;
};

} // namespace jni

#endif // JNI_BIND_IMPLEMENTATION_INHERITANCE_H_
24 changes: 24 additions & 0 deletions implementation/inheritance_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "jni_bind.h"
#include "jni_test.h"

namespace {

using ::jni::test::JniTest;

constexpr jni::Class c1{
"c1", jni::Method{"Foo", jni::Return<int>{}, jni::Params<int>{}}};

constexpr jni::Class c2{
"c2", jni::Extends{c1},
jni::Method{"Bar", jni::Return<int>{}, jni::Params<int>{}}};

TEST_F(JniTest, Inhertance_CallsSuper) {
// jni::LocalObject<c1> local_object{};
// jni::LocalObject<c2> local_object{};
// local_object("Bar", 123);
// local_object("Foo", 123);
}

} // namespace
4 changes: 4 additions & 0 deletions implementation/jni_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct JniT<SpanType_, class_v_, class_loader_v_, jvm_v_, kRank_, class_idx_,
// `kDepthInAncestors` will recursively calculate depth, but always starts
// with a base value of 1, which this -1 will counterbalance.
static constexpr std::size_t kDepthInAncestors = -1;

static constexpr bool kIsRoot = true;
};

// Represents some JNI type, possibly as an index into a classloader, or
Expand All @@ -66,6 +68,8 @@ struct JniT<SpanType_, class_v_, class_loader_v_, jvm_v_, kRank_, class_idx_,
class_loader_idx_,
std::enable_if_t<!std::is_same_v<std::decay_t<decltype(class_v_)>,
RootObject>>> {
static constexpr bool kIsRoot = false;

static constexpr std::size_t kRank = kRank_;
static_assert(kRank != -1);

Expand Down
14 changes: 13 additions & 1 deletion implementation/jni_type_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,24 @@ using GrandparentJniT = JniT<jobject, kParent>;
using ParentJniT = JniT<jobject, kChild>;
using ChildJniT = JniT<jobject, kGrandchild>;

static_assert(GrandparentJniT::ParentJniT::kIsRoot);
static_assert(!GrandparentJniT::kIsRoot);
static_assert(!ParentJniT::kIsRoot);
static_assert(!ChildJniT::kIsRoot);

static_assert(JniTEqual_v<ChildJniT, ChildJniT>);
static_assert(JniTEqual_v<ParentJniT, ChildJniT::ParentJniT>);
static_assert(JniTEqual_v<GrandparentJniT, ChildJniT::ParentJniT::ParentJniT>);

// Why const differs on localstring?
static_assert(
std::is_same_v<decltype(GrandparentJniT::kParent), const jni::RootObject>);

static_assert(!JniT<jstring, jni::kJavaLangString>::kIsRoot);
static_assert(JniT<jstring, jni::kJavaLangString>::ParentJniT::kIsRoot);

static_assert(
std::is_same_v<std::decay_t<decltype(jni::kObject)>, jni::RootObject>);
;

static_assert(GrandparentJniT::kDepthInAncestors == 0);
static_assert(ParentJniT::kDepthInAncestors == 1);
Expand Down
35 changes: 35 additions & 0 deletions implementation/object_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "implementation/jni_helper/lifecycle.h"
#include "implementation/jni_type.h"
#include "implementation/method_selection.h"
#include "implementation/no_class_specified.h"
#include "implementation/no_idx.h"
#include "implementation/ref_base.h"
#include "jni_dep.h"
Expand All @@ -39,6 +40,34 @@

namespace jni {

template <typename JniT>
class ObjectRef;

template <typename JniT_, typename ParentJniT, typename Enable = void>
struct InheritanceProxy;

// non root
template <typename JniT_, typename ParentJniT>
struct InheritanceProxy<JniT_, ParentJniT,
std::enable_if_t<!ParentJniT::kIsRoot>> {
static_assert(std::is_same_v<typename JniT_::StorageType,
typename ParentJniT::StorageType>);
using type = ObjectRef<ParentJniT>;
// using type = RootObject;
};

// root
template <typename JniT_, typename ParentJniT>
struct InheritanceProxy<JniT_, ParentJniT,
std::enable_if_t<ParentJniT::kIsRoot>> {
static_assert(std::is_same_v<typename JniT_::StorageType,
typename ParentJniT::StorageType>);
using type = RefBase<typename JniT_::StorageType>;
};

template <typename JniT>
class ObjectRef;

// Represents a runtime instance of a JNI Object. Instead of using this class
// directly, instead the more specialised types such as LocalObject,
// GlobalObject, etc.
Expand All @@ -53,7 +82,13 @@ class ObjectRef
public metaprogramming::QueryableMap_t<
ObjectRef<JniT>, JniT::stripped_class_v, &JniT::ClassT::fields_>,
public RefBase<typename JniT::StorageType> {
// public InheritanceProxy<JniT, typename JniT::ParentJniT>::type {
protected:
static_assert(std::is_same_v<typename JniT::StorageType, jstring> ||
// std::is_same_v< typename JniT::ParentJniT::StorageType,
// jobject> std::is_same_v< void, typename JniT::ParentJniT>
std::is_same_v<void, decltype(JniT::GetClass().parent_)>);

static_assert(
JniT::class_loader_v
.template SupportedDirectlyOrIndirectly<JniT::class_v>(),
Expand Down
2 changes: 1 addition & 1 deletion implementation/ref_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class RefBase : public RefBaseBase {

explicit operator StorageType() const { return object_ref_; }

protected:
// protected:
StorageType object_ref_ = nullptr;
};

Expand Down
12 changes: 9 additions & 3 deletions javatests/com/jnibind/test/global_object_test_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

static std::unique_ptr<jni::JvmRef<jni::kDefaultJvm>> jvm;

constexpr jni::Class kBase{
"kBase", jni::Method{"toString", jni::Return<jstring>{}, jni::Params{}}};

constexpr jni::Class kGlobalObjectTestClass{
"com/jnibind/test/GlobalObjectTest",
jni::Extends{kBase},
jni::Method{"methodTakesGlobalObjectReturnsNewObject",
jni::Return{kObjectTestHelperClass},
jni::Params{kObjectTestHelperClass}},
Expand Down Expand Up @@ -63,9 +67,11 @@ Java_com_jnibind_test_GlobalObjectTest_jniBuildNewObjectsFromExistingObjects(
JNIEnv* env, jclass, jobject test_helper_object, jobject object_to_mutate) {
jni::LocalObject<kObjectTestHelperClass> helper_obj{object_to_mutate};

return jni::LocalObject<kGlobalObjectTestClass>{test_helper_object}(
"methodTakesGlobalObjectReturnsNewObject", helper_obj)
.Release();
auto a = jni::LocalObject<kGlobalObjectTestClass>{test_helper_object}(
"methodTakesGlobalObjectReturnsNewObject", helper_obj);
// a("toString");

return a.Release();
}

JNIEXPORT jobject JNICALL
Expand Down
1 change: 1 addition & 0 deletions jni_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static constexpr Configuration kConfig{
#include "implementation/forward_declarations.h"
#include "implementation/id.h"
#include "implementation/id_type.h"
#include "implementation/inheritance.h"
#include "implementation/jni_type.h"
#include "implementation/jvm.h"
#include "implementation/loaded_by.h"
Expand Down
Loading