Skip to content

Commit

Permalink
Shatter JvmRefBase and ThreadGuard into separate files.
Browse files Browse the repository at this point in the history
This change has no effect.

PiperOrigin-RevId: 595901454
  • Loading branch information
jwhpryor authored and copybara-github committed Jan 8, 2024
1 parent 176f908 commit b0b2305
Show file tree
Hide file tree
Showing 29 changed files with 1,094 additions and 402 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cc_library(
"//implementation:corpus_tag",
"//implementation:default_class_loader",
"//implementation:field",
"//implementation:find_class_fallback",
"//implementation:forward_declarations",
"//implementation:global_class_loader",
"//implementation:global_object",
Expand Down
1 change: 1 addition & 0 deletions class_defs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cc_library(
"//:jni_dep",
"//implementation:array",
"//implementation:class",
"//implementation:constructor",
"//implementation:method",
"//implementation:params",
"//implementation:return",
Expand Down
26 changes: 17 additions & 9 deletions class_defs/java_lang_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "implementation/array.h"
#include "implementation/class.h"
#include "implementation/constructor.h"
#include "implementation/method.h"
#include "implementation/params.h"
#include "implementation/return.h"
Expand All @@ -27,23 +28,30 @@
namespace jni {

// clang-format off
inline constexpr Class kJavaLangClass{"java/lang/Class"};

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

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

inline constexpr Class kJavaLangClassLoader{
"java/lang/ClassLoader",
Method{"loadClass", Return{kJavaLangClass}, Params<jstring>{}},
Method{"toString", Return{jstring{}}, Params<>{}},
"java/lang/ClassLoader",
Method{"loadClass", Return{kJavaLangClass}, Params<jstring>{}},
Method{"toString", Return{jstring{}}, Params<>{}},
};

static constexpr Class kJavaLangString{
"java/lang/String",
"java/lang/String",

Constructor{jstring{}},
Constructor{Array{jbyte{}}},
Constructor{jstring{}},
Constructor{Array{jbyte{}}},

Method{"toString", Return{jstring{}}, Params<>{}},
Method{"toString", Return{jstring{}}, Params<>{}},
};
// clang-format on

Expand Down
75 changes: 67 additions & 8 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ cc_library(
":global_object",
":id",
":jni_type",
":jvm_ref",
":local_object",
":local_string",
":method",
Expand Down Expand Up @@ -390,6 +389,19 @@ cc_library(
],
)

cc_library(
name = "find_class_fallback",
hdrs = ["find_class_fallback.h"],
deps = [
":default_class_loader",
":global_class_loader",
":local_object",
":promotion_mechanics_tags",
"//:jni_dep",
"//implementation/jni_helper",
],
)

cc_library(
name = "forward_declarations",
hdrs = ["forward_declarations.h"],
Expand All @@ -407,6 +419,7 @@ cc_library(
deps = [
":class_loader",
":class_loader_ref",
":default_class_loader",
":jvm",
":promotion_mechanics",
"//:jni_dep",
Expand Down Expand Up @@ -533,6 +546,18 @@ cc_library(
],
)

cc_test(
name = "jvm_test",
srcs = ["jvm_test.cc"],
deps = [
"//:jni_bind",
"//:jni_test",
"//implementation/jni_helper",
"//implementation/jni_helper:jni_env",
"@googletest//:gtest_main",
],
)

cc_library(
name = "jvm_ref",
hdrs = ["jvm_ref.h"],
Expand All @@ -543,25 +568,38 @@ cc_library(
":default_class_loader",
":field_ref",
":forward_declarations",
":global_class_loader",
":jni_type",
":jvm",
":method_ref",
":jvm_ref_base",
":promotion_mechanics_tags",
":ref_storage",
":thread_guard",
"//:jni_dep",
"//class_defs:java_lang_classes",
"//implementation/jni_helper",
"//implementation/jni_helper:lifecycle_object",
"//metaprogramming:double_locked_value",
"//metaprogramming:function_traits",
],
)

cc_library(
name = "jvm_ref_base",
hdrs = ["jvm_ref_base.h"],
deps = [
":forward_declarations",
"//:jni_dep",
],
)

cc_test(
name = "jvm_test",
srcs = ["jvm_test.cc"],
name = "jvm_ref_test",
srcs = ["jvm_ref_test.cc"],
deps = [
":jvm_ref",
"//:jni_bind",
"//:jni_test",
"//implementation/jni_helper",
"//implementation/jni_helper:jni_env",
"@googletest//:gtest_main",
],
)
Expand Down Expand Up @@ -658,6 +696,7 @@ cc_library(
":class",
":class_loader",
":class_loader_ref",
":default_class_loader",
":forward_declarations",
":jvm",
":jvm_ref",
Expand All @@ -679,7 +718,6 @@ cc_library(
":forward_declarations",
":jni_type",
":jvm",
":jvm_ref",
":object_ref",
":promotion_mechanics",
":promotion_mechanics_tags",
Expand Down Expand Up @@ -851,7 +889,6 @@ cc_library(
":default_class_loader",
":field_ref",
":jni_type",
":jvm_ref",
":method_ref",
":method_selection",
":proxy",
Expand Down Expand Up @@ -1175,6 +1212,28 @@ cc_test(
],
)

cc_library(
name = "thread_guard",
hdrs = ["thread_guard.h"],
deps = [
":forward_declarations",
":jvm_ref_base",
"//:jni_dep",
"//metaprogramming:function_traits",
],
)

cc_test(
name = "thread_guard_test",
srcs = ["thread_guard_test.cc"],
deps = [
":thread_guard",
"//:jni_bind",
"//:jni_test",
"@googletest//:gtest_main",
],
)

cc_library(
name = "void",
hdrs = ["void.h"],
Expand Down
5 changes: 0 additions & 5 deletions implementation/class_loader_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "implementation/jni_helper/jni_env.h"
#include "implementation/jni_helper/lifecycle_object.h"
#include "implementation/jni_type.h"
#include "implementation/jvm_ref.h"
#include "implementation/local_object.h"
#include "implementation/local_string.h"
#include "implementation/method.h"
Expand Down Expand Up @@ -64,10 +63,6 @@ class ClassLoaderRef : public ClassLoaderImpl<lifecycleType> {
using Base = ClassLoaderImpl<lifecycleType>;
using Base::Base;

static_assert(class_loader_v_ != kDefaultClassLoader,
"Custom class loaders should not use the default class loader,"
"objects will automatically use the default.");

template <const auto& class_v, typename... Params>
[[nodiscard]] auto BuildLocalObject(Params&&... params) {
using JniClassT = JniT<jobject, class_v>;
Expand Down
50 changes: 50 additions & 0 deletions implementation/find_class_fallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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_FIND_CLASS_FALLBACK_H_
#define JNI_BIND_IMPLEMENTATION_FIND_CLASS_FALLBACK_H_

#include "implementation/default_class_loader.h"
#include "implementation/global_class_loader.h"
#include "implementation/jni_helper/jni_helper.h"
#include "implementation/local_object.h"
#include "implementation/promotion_mechanics_tags.h"
#include "jni_dep.h"

namespace jni {

// This is currently using JNI Bind itself and so cannot be directly included.
// By delaying linkage for this we can use a bootstrapped JNI Bind. This could
// be rewritten in porcelain JNI, but this flow ought to be only on
// secondary threads with previously unused `jclass`'s (which is mostly rare),
// and this is simpler to reason about than porcelain JNI.
inline jclass FindClassFallback(const char* class_name) {
// The loader will be primed by the JVM, however, it needs to be accessible
// from the jni_helper layer. See `JvmRef` for how this is primed.
GlobalClassLoader<kDefaultClassLoader> loader{AdoptGlobal{},
FallbackLoader()};

jni::LocalObject loaded_class = loader("loadClass", class_name);
jclass ret{static_cast<jclass>(static_cast<jobject>(loaded_class.Release()))};

loader.Release();

return ret;
}

} // namespace jni

#endif // JNI_BIND_IMPLEMENTATION_FIND_CLASS_FALLBACK_H_
4 changes: 4 additions & 0 deletions implementation/forward_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ template <LifecycleType lifecycleType, const auto& jvm_v_,
const auto& class_loader_v_>
class ClassLoaderRef;

// Jvm.
template <const auto& jvm_v_>
class JvmRef;

// Thread Guards.
class ThreadGuard;
class ThreadLocalGuardDestructor;
Expand Down
4 changes: 3 additions & 1 deletion implementation/global_class_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
#include "class_defs/java_lang_classes.h"
#include "implementation/class_loader.h"
#include "implementation/class_loader_ref.h"
#include "implementation/default_class_loader.h"
#include "implementation/jni_helper/lifecycle_object.h"
#include "implementation/jvm.h"
#include "implementation/promotion_mechanics.h"
#include "jni_dep.h"

namespace jni {

template <const auto& class_loader_v_, const auto& jvm_v_ = kDefaultJvm>
template <const auto& class_loader_v_ = kDefaultClassLoader,
const auto& jvm_v_ = kDefaultJvm>
class GlobalClassLoader
: public ClassLoaderRef<LifecycleType::GLOBAL, class_loader_v_, jvm_v_> {
public:
Expand Down
16 changes: 14 additions & 2 deletions implementation/jni_helper/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_library(
hdrs = ["field_value.h"],
deps = [
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand All @@ -27,6 +28,7 @@ cc_library(
hdrs = ["jni_array_helper.h"],
deps = [
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand Down Expand Up @@ -55,6 +57,7 @@ cc_library(
hdrs = ["jni_helper.h"],
deps = [
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand Down Expand Up @@ -95,6 +98,7 @@ cc_library(
hdrs = ["invoke.h"],
deps = [
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand All @@ -118,6 +122,7 @@ cc_library(
deps = [
":invoke",
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand All @@ -127,6 +132,7 @@ cc_library(
hdrs = ["lifecycle.h"],
deps = [
":jni_env",
":trace",
"//:jni_dep",
],
)
Expand All @@ -135,9 +141,8 @@ cc_library(
name = "lifecycle_object",
hdrs = ["lifecycle_object.h"],
deps = [
":jni_env",
":lifecycle",
"//:jni_dep",
":trace",
],
)

Expand All @@ -158,6 +163,7 @@ cc_library(
deps = [
":lifecycle",
":lifecycle_object",
":trace",
],
)

Expand All @@ -178,6 +184,12 @@ cc_library(
deps = [
":field_value_getter",
":jni_env",
":trace",
"//:jni_dep",
],
)

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

0 comments on commit b0b2305

Please sign in to comment.