Skip to content

Commit

Permalink
Params no longer need to be provided if there are none.
Browse files Browse the repository at this point in the history
#2

PiperOrigin-RevId: 618473835
  • Loading branch information
jwhpryor authored and copybara-github committed Mar 25, 2024
1 parent f7a9908 commit 228cdae
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ constexpr Class kBuilder {
Method{"setTwo", Return{Self{}}, Params<int>{}},
// FAILS:
// Method{"setTwo", Return{Class{"com/jnibind/test/BuilderTest$Builder"}}, Params<int>{}},
Method{"build", Return{kObjectTestHelperClass}, Params{}},
Method{"build", Return{kObjectTestHelperClass}},
};

LocalObject<kBuilder>{}("setOne", 111)("setTwo", 222)("build")
Expand Down
4 changes: 2 additions & 2 deletions class_defs/java_lang_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ namespace jni {

inline constexpr Class kJavaLangClass{
"java/lang/Class",
Method{"getClassLoader", Return{ Class { "java/lang/ClassLoader" } }, Params{}},
Method{"getClassLoader", Return{ Class { "java/lang/ClassLoader" } }},
};

inline constexpr Class kJavaLangObject{
"java/lang/Object",
Method{"getClass", Return{kJavaLangClass}, Params{}},
Method{"getClass", Return{kJavaLangClass}},
};

inline constexpr Class kJavaLangClassLoader{
Expand Down
5 changes: 4 additions & 1 deletion implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,10 @@ cc_library(
cc_library(
name = "method",
hdrs = ["method.h"],
deps = [":params"],
deps = [
":params",
":return",
],
)

cc_library(
Expand Down
3 changes: 1 addition & 2 deletions implementation/array_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ TEST_F(JniTest, ArrayView_ShallowObjectsAreIterable) {
}

TEST_F(JniTest, ArrayView_RichObjectsAreIterable) {
static constexpr Class kClass{"kClass",
Method{"Foo", Return<int>{}, Params<>{}}};
static constexpr Class kClass{"kClass", Method{"Foo", Return<int>{}}};

EXPECT_CALL(*env_, GetArrayLength(Fake<jobjectArray>()))
.WillOnce(testing::Return(3));
Expand Down
7 changes: 3 additions & 4 deletions implementation/class_loader_ref_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ using ::testing::Eq;
using ::testing::InSequence;
using ::testing::StrEq;

static constexpr Class kClass1{
"Class1", Constructor{}, Constructor<jint>{},
Method{"Foo", Return{Class{"Class2"}}, Params{}}};
static constexpr Class kClass1{"Class1", Constructor{}, Constructor<jint>{},
Method{"Foo", Return{Class{"Class2"}}}};

static constexpr Class kClass2{
"Class2", Constructor{}, Constructor{kClass1},
Expand Down Expand Up @@ -259,7 +258,7 @@ TEST_F(JniTestWithNoDefaultJvmRef,
ClassLoaderRefTest_ClassesOfDifferentClassLoadersAreUnique) {
static constexpr Class class_under_test{
"com/google/ARCore",
Method{"Foo", jni::Return{}, Params<>{}},
Method{"Foo", jni::Return{}},
};
static constexpr ClassLoader class_loader{
kNullClassLoader, SupportedClassSet{class_under_test}};
Expand Down
14 changes: 7 additions & 7 deletions implementation/id_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ static constexpr Class kClass1{
Constructor{jfloat{}, jboolean{}},
Constructor{Array{jint{}}},

Method{"m0", jni::Return<void>{}, Params{}},
Method{"m0", jni::Return<void>{}},
Method{"m1", jni::Return<jshort>{}, Params<jint>{}},
Method{"m2", jni::Return{Class{"kClass2"}}, Params<jfloat, jboolean>{}},
Method{
"m3",
Overload{jni::Return<void>{}, Params{}},
Overload{jni::Return<void>{}},
Overload{jni::Return<jint>{}, Params<jboolean>{}},
Overload{jni::Return<jfloat>{}, Params<jshort, jdouble>{}},
Overload{jni::Return{Array{Class{"kClass2"}}},
Expand All @@ -69,9 +69,9 @@ static constexpr Class kClass1{
Overload{jni::Return{Array<jboolean, 2>{}}, Params{Array<jfloat, 2>{}}},
Overload{jni::Return{Array<jboolean, 3>{}}, Params{Array<jshort, 3>{}}},
},
Method{"m5", jni::Return{Array{Class{"kClass2"}}}, Params<>{}},
Method{"m6", jni::Return{Array{Class{"kClass2"}, Rank<2>{}}}, Params<>{}},
Method{"m7", jni::Return{LoadedBy{kClassLoader, kOtherClass}}, Params<>{}},
Method{"m5", jni::Return{Array{Class{"kClass2"}}}},
Method{"m6", jni::Return{Array{Class{"kClass2"}, Rank<2>{}}}},
Method{"m7", jni::Return{LoadedBy{kClassLoader, kOtherClass}}},

Field{"f0", int{}},
Field{"f1", Class{"kClass2"}}};
Expand Down Expand Up @@ -217,13 +217,13 @@ static constexpr Class kStaticClass{
"kStaticClass",

Static{
Method{"sm0", jni::Return<void>{}, Params{}},
Method{"sm0", jni::Return<void>{}},
Method{"sm1", jni::Return<jshort>{}, Params<jint>{}},
Method{"sm2", jni::Return{Class{"kClass2"}},
Params<jfloat, jboolean>{}},
Method{
"sm3",
Overload{jni::Return<void>{}, Params{}},
Overload{jni::Return<void>{}},
Overload{jni::Return<jint>{}, Params<jboolean>{}},
Overload{jni::Return<jfloat>{}, Params<jshort, jdouble>{}},
},
Expand Down
7 changes: 3 additions & 4 deletions implementation/local_array_method_multidimensional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static constexpr Class kClass2{"kClass2"};
// As Return.
////////////////////////////////////////////////////////////////////////////////
TEST_F(JniTest, V_2I) {
static constexpr Class kClass{
"kClass", Method{"I", jni::Return{Array<jint, 2>{}}, Params{}}};
static constexpr Class kClass{"kClass",
Method{"I", jni::Return{Array<jint, 2>{}}}};

EXPECT_CALL(*env_, GetMethodID(_, StrEq("I"), StrEq("()[[I")));

Expand All @@ -52,8 +52,7 @@ TEST_F(JniTest, V_2I) {

TEST_F(JniTest, V_2LkClass) {
static constexpr Class kClass{
"kClass",
Method{"Foo", jni::Return{Array{kClass2, Rank<2>{}}}, Params{}}};
"kClass", Method{"Foo", jni::Return{Array{kClass2, Rank<2>{}}}}};

EXPECT_CALL(*env_, GetMethodID(_, StrEq("Foo"), StrEq("()[[LkClass2;")));

Expand Down
18 changes: 9 additions & 9 deletions implementation/local_array_method_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ static constexpr Class kClass2{"kClass2"};
TEST_F(JniTest, ReturnSmokeTest) {
static constexpr Class kClass{
"kClass",
Method{"BooleanArray", Return{Array{jboolean{}}}, Params{}},
Method{"ByteArray", Return{Array{jbyte{}}}, Params{}},
Method{"CharArray", Return{Array{jchar{}}}, Params{}},
Method{"ShortArray", Return{Array{jshort{}}}, Params{}},
Method{"IntArray", Return{Array{jint{}}}, Params{}},
Method{"FloatArray", Return{Array{jfloat{}}}, Params{}},
Method{"DoubleArray", Return{Array{jdouble{}}}, Params{}},
Method{"LongArray", Return{Array{jlong{}}}, Params{}},
Method{"ObjectArray", Return{Array{kClass2}}, Params{}},
Method{"BooleanArray", Return{Array{jboolean{}}}},
Method{"ByteArray", Return{Array{jbyte{}}}},
Method{"CharArray", Return{Array{jchar{}}}},
Method{"ShortArray", Return{Array{jshort{}}}},
Method{"IntArray", Return{Array{jint{}}}},
Method{"FloatArray", Return{Array{jfloat{}}}},
Method{"DoubleArray", Return{Array{jdouble{}}}},
Method{"LongArray", Return{Array{jlong{}}}},
Method{"ObjectArray", Return{Array{kClass2}}},
};

EXPECT_CALL(*env_, CallObjectMethodV)
Expand Down
2 changes: 1 addition & 1 deletion implementation/local_array_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TEST_F(JniTest, Array_LocalVanillaObjectRValuesCanBeSet) {
TEST_F(JniTest, Array_CorrectReturnSignatureForStrings) {
static constexpr Class kClass{
"kClass",
Method{"StringArray", jni::Return{Array{jstring{}}}, Params{}},
Method{"StringArray", jni::Return{Array{jstring{}}}},
};

LocalObject<kClass> obj{Fake<jobject>()};
Expand Down
3 changes: 1 addition & 2 deletions implementation/local_object_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ TEST_F(JniTest, LocalObject_SupportsReturningAString) {
}

jobject ReturnOutputOfMethod() {
static constexpr Class kClass2{
"Class2", Method{"Foo", jni::Return{kClass}, Params<>{}}};
static constexpr Class kClass2{"Class2", Method{"Foo", jni::Return{kClass}}};

return LocalObject<kClass2>{}("Foo").Release();
}
Expand Down
28 changes: 25 additions & 3 deletions implementation/method.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JNI_BIND_METHOD_H_
#define JNI_BIND_METHOD_H_

#include <tuple>
#include <type_traits>

#include "params.h"
#include "implementation/params.h"
#include "implementation/return.h"

namespace jni {

Expand All @@ -33,10 +33,18 @@ struct Overload : OverloadBase {
const ReturnT_ return_;
const Params_ params_;

// `Return`, no `Params`.
constexpr Overload(ReturnT_ return_type)
: return_(return_type), params_(Params{}) {}

// `Return` and `Params`.
constexpr Overload(ReturnT_ return_type, Params_ params)
: return_(return_type), params_(params) {}
};

template <typename ReturnT_>
Overload(ReturnT_) -> Overload<ReturnT_, Params<>>;

template <typename ReturnT_, typename Params_>
Overload(ReturnT_, Params_) -> Overload<ReturnT_, Params_>;

Expand All @@ -50,15 +58,29 @@ struct Method<std::tuple<Returns...>, std::tuple<Params_...>>
const char* name_;
const std::tuple<Overload<Returns, Params_>...> invocations_;

// `Return`, no `Params`.
template <typename ReturnT_,
std::enable_if_t<std::is_base_of_v<ReturnBase, ReturnT_>, int> = 0>
constexpr Method(const char* name, ReturnT_ return_type)
: name_(name), invocations_(Overload{return_type}) {}

// `Return` and `Params`.
template <typename ReturnT_, typename ParamsT_,
std::enable_if_t<std::is_base_of_v<ParamsBase, ParamsT_>, int> = 0>
std::enable_if_t<std::is_base_of_v<ReturnBase, ReturnT_>, int> = 0>
constexpr Method(const char* name, ReturnT_ return_type, ParamsT_ params)
: name_(name), invocations_(Overload{return_type, params}) {}

// `Overload` Set.
constexpr Method(const char* name, Overload<Returns, Params_>... invocations)
: name_(name), invocations_(invocations...) {}
};

// CTAD for Non-overloaded form, no Params.
template <typename ReturnT, typename = std::enable_if_t<
!std::is_base_of_v<OverloadBase, ReturnT>>>
Method(const char*,
ReturnT) -> Method<std::tuple<ReturnT>, std::tuple<Params<>>>;

// CTAD for Non-overloaded form.
template <
typename ReturnT, typename ParamsT,
Expand Down
22 changes: 11 additions & 11 deletions implementation/method_ref_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TEST_F(JniTest, MethodRef_DoesntStaticCrossTalkWithTagUse) {
}

TEST_F(JniTest, MethodRef_CallsGetMethodCorrectlyForSingleMethod) {
static constexpr Method m1{"FooV", Return<void>{}, Params<>{}};
static constexpr Method m1{"FooV", Return<void>{}};
static constexpr Class c{"SimpleClass", m1};

InSequence seq;
Expand All @@ -72,7 +72,7 @@ TEST_F(JniTest, MethodRef_CallsGetMethodCorrectlyForSingleMethod) {

TEST_F(JniTest, MethodRef_ReturnWithObject) {
static constexpr Class c2{"someClass2"};
static constexpr Method m1{"FooV", Return{c2}, Params<>{}};
static constexpr Method m1{"FooV", Return{c2}};
static constexpr Class c{"someClass", m1};

InSequence seq;
Expand All @@ -87,7 +87,7 @@ TEST_F(JniTest, MethodRef_ReturnWithObject) {

TEST_F(JniTest, MethodRef_ReturnWithRank1Object) {
static constexpr Class c2{"someClass2"};
static constexpr Method m1{"FooV", Return{Array{c2}}, Params<>{}};
static constexpr Method m1{"FooV", Return{Array{c2}}};
static constexpr Class c{"someClass", m1};

InSequence seq;
Expand Down Expand Up @@ -117,9 +117,9 @@ TEST_F(JniTest, MethodRef_ReturnWithRank2Object) {
}

TEST_F(JniTest, MethodRef_ReturnWithNoParams) {
static constexpr Method m1{"FooV", Return<void>{}, Params<>{}};
static constexpr Method m2{"BarI", Return<jint>{}, Params<>{}};
static constexpr Method m3{"BazF", Return<jfloat>{}, Params<>{}};
static constexpr Method m1{"FooV", Return<void>{}};
static constexpr Method m2{"BarI", Return<jint>{}};
static constexpr Method m3{"BazF", Return<jfloat>{}};
static constexpr Class c{"someClass", m1, m2, m3};

InSequence seq;
Expand Down Expand Up @@ -233,16 +233,16 @@ TEST_F(JniTest, MethodRef_SupportsForwardDefines) {
"kClass1",
Method{"m1", Return<void>{}, Params{Class{"kClass1"}}},
Method{"m2", Return<void>{}, Params{Class{"kClass2"}}},
Method{"m3", Return{Class{"kClass1"}}, Params{}},
Method{"m4", Return{Class{"kClass2"}}, Params{}},
Method{"m3", Return{Class{"kClass1"}}},
Method{"m4", Return{Class{"kClass2"}}},
};

static constexpr Class kClass2{
"kClass2",
Method{"m1", Return<void>{}, Params{Class{"kClass1"}}},
Method{"m2", Return<void>{}, Params{Class{"kClass2"}}},
Method{"m3", Return{Class{"kClass1"}}, Params{}},
Method{"m4", Return{Class{"kClass2"}}, Params{}},
Method{"m3", Return{Class{"kClass1"}}},
Method{"m4", Return{Class{"kClass2"}}},
};

LocalObject<kClass1> c1_obj1{Fake<jobject>(1)};
Expand Down Expand Up @@ -273,7 +273,7 @@ TEST_F(JniTest, MethodRef_SupportsStrings) {
"com/google/SupportsStrings",
Method{"Foo", Return<void>{}, Params<jstring>{}},
Method{"Bar", Return<void>{}, Params<jstring, jstring>{}},
Method{"Baz", Return<jstring>{}, Params<>{}},
Method{"Baz", Return<jstring>{}},
};

LocalObject<class_under_test> obj1{Fake<jobject>()};
Expand Down
6 changes: 3 additions & 3 deletions implementation/method_selection_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using ::jni::Params;
using ::jni::Rank;
using ::jni::Return;

static constexpr Class kClass1{"class1", Method{"Foo", Return{}, Params{}}};
static constexpr Class kClass1{"class1", Method{"Foo", Return{}}};
static constexpr Class kClass1NoDefinition{"class1"};
static constexpr Class kClass2{"class2"};
static constexpr Class kUnusedClass{"invalidClass"};
Expand All @@ -56,7 +56,7 @@ using GlobalInvalidObj = GlobalObject<kUnusedClass>;
////////////////////////////////////////////////////////////////////////////////
constexpr Class c1{
"c1",
Method{"0", Return<void>{}, Params{}},
Method{"0", Return<void>{}},
Method{"1", Return<void>{}, Params<jboolean>{}},
Method{"2", Return<void>{}, Params<jchar>{}},
Method{"3", Return<void>{}, Params<jshort>{}},
Expand Down Expand Up @@ -210,7 +210,7 @@ static_assert(MthdId_t<c2, 3>::ArgSetViable<jboolean, jintArray>());
constexpr Class c3{
"c3", Method{
"0",
Overload{Return<void>{}, Params{}},
Overload{Return<void>{}},
Overload{Return<void>{}, Params<int>{}},
Overload{Return<void>{}, Params<int, int>{}},
Overload{Return<void>{}, Params<int, int, float, int>{}},
Expand Down
2 changes: 1 addition & 1 deletion implementation/multi_type_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST_F(JniTest, MultiTypeTest_SimpleSmokeTestForSingleObject) {
static constexpr Class object{
"ARCore",
Method{"Foo", jni::Return<jint>{}, Params<jint, jfloat>{}},
Method{"Bar", jni::Return{jint{}}, Params<>{}},
Method{"Bar", jni::Return{jint{}}},
Method{"Baz", jni::Return<void>{}, Params<jfloat>{}},
Field{"SomeField", jint{}},
};
Expand Down
2 changes: 0 additions & 2 deletions implementation/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include <tuple>
#include <utility>

#include "object.h"

namespace jni {

struct ParamsBase {};
Expand Down
6 changes: 4 additions & 2 deletions implementation/return.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace jni {

struct ReturnBase {};

template <typename Raw_>
struct Return {
struct Return : ReturnBase {
const Raw_ raw_ = {};

using Raw = Raw_;
Expand All @@ -36,7 +38,7 @@ struct Return {
};

template <>
struct Return<void> {
struct Return<void> : ReturnBase {
using Raw = void;
const Void raw_{};

Expand Down
3 changes: 1 addition & 2 deletions implementation/self_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ using ::testing::_;
using ::testing::AnyNumber;
using ::testing::StrEq;

static constexpr Class kClass{"Builder",
Method{"build", Return{Self{}}, Params{}},
static constexpr Class kClass{"Builder", Method{"build", Return{Self{}}},
Method{"takesBuilder", Return{}, Params{Self{}}}};

TEST_F(JniTest, SelfCanBeUsedAsAReturnValue) {
Expand Down
Loading

0 comments on commit 228cdae

Please sign in to comment.