Skip to content

Commit

Permalink
Add proxy_convenience_aliases.h and add release header testing.
Browse files Browse the repository at this point in the history
This was causing breakages for missing CDecl when arrays were introduced, but only for the compacted inline release header (vs. using through Bazel).

Also, add a build_test so this doesn't happen in the future. :jni_test now conditionally allows the usage of the monolithic header.

Lastly, rev the release header.

Test:'bazel test  --cxxopt='\''-std=c++17'\'' --repo_env=CC=clang' ...'
PiperOrigin-RevId: 453236675
Change-Id: I2871a3c03c78f54701300727b679838f495fc44b
  • Loading branch information
jwhpryor committed Jun 6, 2022
1 parent 931684c commit 12deff9
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 46 deletions.
45 changes: 43 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ filegroup(
name = "headers_for_export",
srcs = glob(
["**/*.h"],
exclude = ["jni_bind_release.h"],
exclude = [
"jni_bind_release.h",
"jni_bind_release_for_testing.h",
":jni_bind_release_target",
],
),
)

Expand Down Expand Up @@ -64,6 +68,7 @@ cc_library(
################################################################################
# Testing Targets.
################################################################################

cc_library(
name = "jni_test",
testonly = 1,
Expand Down Expand Up @@ -101,6 +106,28 @@ cc_library(
# Release targets.
################################################################################

# Copy of the release header (above) that uses a different filename.
# Strangely, if a file is present with the same name as what's in outs blaze
# will ignore the genrule and depend on the real header, but it will fail
# because there is no cc_library providing the header. jni_bind_release.h is
# checked in, so the above *will* fail.
genrule(
name = "gen_jni_bind_release_for_testing",
outs = ["jni_bind_release_for_testing.h"],
cmd = "./$(location build_jni_bind_release.sh) $(location jni_bind_release_input) jni_wrapper $(locations :jni_bind_decorative_text)>$@",
tools = [
":build_jni_bind_release.sh",
":headers_for_export",
":jni_bind_decorative_text",
":jni_bind_release_input",
"//class_defs:headers_for_export",
"//implementation:headers_for_export",
"//implementation/jni_helper:headers_for_export",
"//metaprogramming:headers_for_export",
],
visibility = ["//visibility:private"],
)

filegroup(
name = "jni_bind_decorative_text",
srcs = [
Expand All @@ -116,6 +143,20 @@ filegroup(
# Use :jni_bind, this is solely for build validation of the release header.
cc_library(
name = "jni_bind_release_target",
hdrs = ["jni_bind_release.h"],
hdrs = ["jni_bind_release_for_testing.h"],
visibility = ["//visibility:private"],
)

cc_test(
name = "release_header_smoke_test",
srcs = [
"release_header_smoke_test.cc",
":jni_bind_release_for_testing.h",
],
copts = ["-DJNI_BIND_USE_FOR_TESTING_RELEASE_HEADER=1"],
deps = [
":jni_test",
"//third_party/java/jdk:jni",
"@googletest//:gtest_main",
],
)
6 changes: 6 additions & 0 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ cc_library(
":jvm",
":local_array",
":object",
":proxy_convenience_aliases",
":ref_base",
":string_ref",
"//:jni_dep",
Expand All @@ -530,6 +531,11 @@ cc_library(
],
)

cc_library(
name = "proxy_convenience_aliases",
hdrs = ["proxy_convenience_aliases.h"],
)

cc_test(
name = "proxy_test",
srcs = ["proxy_test.cc"],
Expand Down
21 changes: 1 addition & 20 deletions implementation/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ref_base.h"
#include "implementation/class.h"
#include "implementation/class_loader.h"
#include "implementation/proxy_convenience_aliases.h"
#include "jni_dep.h"
#include "metaprogramming/cartesian_product.h"
#include "metaprogramming/combine.h"
Expand Down Expand Up @@ -115,26 +116,6 @@ struct ProxyHelper {
using AsDecl_t = typename Proxy_t::AsDecl;
};

// See jni::Proxy.
template <typename T>
using Proxy_t = typename ProxyHelper<T>::Proxy_t;

template <typename T>
using Index_t = typename ProxyHelper<T>::Index;

template <typename T, typename Overload = void>
using CDecl_t = typename ProxyHelper<T>::template CDecl<Overload>;

template <typename T, typename OverloadSelection>
using Return_t =
typename ProxyHelper<T>::template AsReturn_t<OverloadSelection>;

template <typename T, typename ParamSelection>
using Arg_t = typename ProxyHelper<T>::template AsArg_t<ParamSelection>;

template <typename T>
using AsDecl_t = typename ProxyHelper<T>::AsDecl_t;

////////////////////////////////////////////////////////////////////////////////
// MetaFunction Helpers.
////////////////////////////////////////////////////////////////////////////////
Expand Down
47 changes: 47 additions & 0 deletions implementation/proxy_convenience_aliases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 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_PROXY_CONVENIENCE_ALIASES_H_
#define JNI_BIND_IMPLEMENTATION_PROXY_CONVENIENCE_ALIASES_H_

namespace jni {

template <typename TUndecayed>
struct ProxyHelper;

// See jni::Proxy.
template <typename T>
using Proxy_t = typename ProxyHelper<T>::Proxy_t;

template <typename T>
using Index_t = typename ProxyHelper<T>::Index;

template <typename T, typename Overload = void>
using CDecl_t = typename ProxyHelper<T>::template CDecl<Overload>;

template <typename T, typename OverloadSelection>
using Return_t =
typename ProxyHelper<T>::template AsReturn_t<OverloadSelection>;

template <typename T, typename ParamSelection>
using Arg_t = typename ProxyHelper<T>::template AsArg_t<ParamSelection>;

template <typename T>
using AsDecl_t = typename ProxyHelper<T>::AsDecl_t;

} // namespace jni

#endif // JNI_BIND_IMPLEMENTATION_PROXY_CONVENIENCE_ALIASES_H_
62 changes: 40 additions & 22 deletions jni_bind_release.h
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,18 @@ inline constexpr Jvm kDefaultJvm{kDefaultClassLoader};

} // namespace jni

namespace jni {

template <typename SpanType>
class ArrayView {
public:
ArrayView(ArrayView&&) = delete;
ArrayView(const ArrayView&) = delete;

ArrayView(jarray array, bool copy_on_completion)
: array_(array),
get_array_elements_result_(
JniArrayHelper<SpanType>::GetArrayElements(array)),
copy_on_completion_(copy_on_completion) {}

~ArrayView() {
Expand All @@ -2202,7 +2214,6 @@ ArrayView(ArrayView<SpanType>&&) -> ArrayView<SpanType>;

} // namespace jni


#include <type_traits>

namespace jni {
Expand Down Expand Up @@ -3294,6 +3305,33 @@ struct SelectorStaticInfo {
} // namespace jni


namespace jni {

template <typename TUndecayed>
struct ProxyHelper;

// See jni::Proxy.
template <typename T>
using Proxy_t = typename ProxyHelper<T>::Proxy_t;

template <typename T>
using Index_t = typename ProxyHelper<T>::Index;

template <typename T, typename Overload = void>
using CDecl_t = typename ProxyHelper<T>::template CDecl<Overload>;

template <typename T, typename OverloadSelection>
using Return_t =
typename ProxyHelper<T>::template AsReturn_t<OverloadSelection>;

template <typename T, typename ParamSelection>
using Arg_t = typename ProxyHelper<T>::template AsArg_t<ParamSelection>;

template <typename T>
using AsDecl_t = typename ProxyHelper<T>::AsDecl_t;

} // namespace jni

namespace jni {

// Represents and possibly builds a runtime Java String object.
Expand Down Expand Up @@ -4211,26 +4249,6 @@ struct ProxyHelper {
using AsDecl_t = typename Proxy_t::AsDecl;
};

// See jni::Proxy.
template <typename T>
using Proxy_t = typename ProxyHelper<T>::Proxy_t;

template <typename T>
using Index_t = typename ProxyHelper<T>::Index;

template <typename T, typename Overload = void>
using CDecl_t = typename ProxyHelper<T>::template CDecl<Overload>;

template <typename T, typename OverloadSelection>
using Return_t =
typename ProxyHelper<T>::template AsReturn_t<OverloadSelection>;

template <typename T, typename ParamSelection>
using Arg_t = typename ProxyHelper<T>::template AsArg_t<ParamSelection>;

template <typename T>
using AsDecl_t = typename ProxyHelper<T>::AsDecl_t;

////////////////////////////////////////////////////////////////////////////////
// MetaFunction Helpers.
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4942,7 +4960,7 @@ struct PermutationSelectionForArgs {
typename MethodSelectionForArgs::template FindPermutation<Args...>;

static constexpr bool kIsValidArgSet =
MethodSelectionForArgs::template ArgSetViable<Args...>;
MethodSelectionForArgs::template ArgSetViable<Args...>();

using PermutationRef = PermutationRef<MethodSelectionForArgs, OverloadSelectionForArgs,
PermutationForArgs>;
Expand Down
2 changes: 1 addition & 1 deletion jni_bind_release_leader.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/*******************************************************************************
* JNI Bind Version 0.1.
* JNI Bind Version 0.3.
* Alpha Public Release.
********************************************************************************
* This header is the single header version which you can use to quickly test or
Expand Down
7 changes: 6 additions & 1 deletion jni_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "jni_bind.h"
#include "mock_jni_env.h"
#include "mock_jvm.h"

#ifdef JNI_BIND_USE_FOR_TESTING_RELEASE_HEADER
#include "jni_bind_release_for_testing.h"
#else
#include "jni_bind.h"
#endif

namespace jni::test {

// "Translates" a fake local object into its global counterpart.
Expand Down
37 changes: 37 additions & 0 deletions release_header_smoke_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef JNI_BIND_RELEASE_HEADER_SMOKE_TEST_H_
#define JNI_BIND_RELEASE_HEADER_SMOKE_TEST_H_

#include <gtest/gtest.h>
#include "jni_bind_release_for_testing.h"
#include "jni_test.h"

namespace {

using ::jni::Class;
using ::jni::Field;
using ::jni::Method;
using ::jni::Params;
using ::jni::test::JniTest;

TEST_F(JniTest, SmokeTest_SimplyRun) {
static constexpr Class object{
"ARCore",
Method{"Foo", jni::Return<jint>{}, Params<jint, jfloat>{}},
Method{"Bar", jni::Return{jint{}}, Params<>{}},
Method{"Baz", jni::Return<void>{}, Params<jfloat>{}},
Field{"SomeField", jint{}},
};

jni::GlobalObject<object> obj{};
obj("Foo", 1, 2.f);
obj("Baz", 1.f);
obj("Baz", 1.f);
obj("Baz", 2.f);
obj("Baz", 3.f);
obj("Bar");
obj["SomeField"].Get();
}

} // namespace

#endif // JNI_BIND_RELEASE_HEADER_SMOKE_TEST_H_

0 comments on commit 12deff9

Please sign in to comment.