proxygen
JniUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
10 #include <glog/logging.h>
11 
12 namespace fizz {
13 namespace jni {
14 
15 namespace {
16 JavaVM* vm;
17 }
18 
19 void setVM(JavaVM* jvm) {
20  vm = jvm;
21 }
22 
23 JNIEnv* getEnv(bool* shouldDetach) {
24  *shouldDetach = false;
25 
26  JNIEnv* env;
27  auto status = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
28  CHECK(status == JNI_OK || status == JNI_EDETACHED);
29 
30  if (status == JNI_EDETACHED) {
31  status = vm->AttachCurrentThread(
32  reinterpret_cast<void**>(&env), nullptr /*args*/);
33  CHECK_EQ(status, JNI_OK);
34  *shouldDetach = true;
35  }
36 
37  return env;
38 }
39 
40 void releaseEnv(bool shouldDetach) {
41  if (shouldDetach) {
42  vm->DetachCurrentThread();
43  }
44 }
45 
46 jclass getClass(JNIEnv* env, const std::string& name) {
47  auto clazz =
48  reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass(name.c_str())));
49  CHECK(clazz);
50  return clazz;
51 }
52 
53 jmethodID getMethodID(
54  JNIEnv* env,
55  jclass clazz,
56  const std::string& name,
57  const std::string& signature) {
58  auto methodId = env->GetMethodID(clazz, name.c_str(), signature.c_str());
59  CHECK(methodId);
60  return methodId;
61 }
62 
63 void maybeThrowException(JNIEnv* env, bool shouldDetach) {
64  if (!env->ExceptionCheck()) {
65  return;
66  }
67  env->ExceptionDescribe();
68  releaseEnv(shouldDetach);
69  throw std::runtime_error("JNI exception");
70 }
71 
72 jbyteArray createByteArray(JNIEnv* env, folly::ByteRange byteRange) {
73  auto byteArray = env->NewByteArray(byteRange.size());
74  env->SetByteArrayRegion(
75  byteArray,
76  0 /*start*/,
77  byteRange.size(),
78  reinterpret_cast<const jbyte*>(byteRange.data()));
79  return byteArray;
80 }
81 
82 jbyteArray createByteArray(JNIEnv* env, Buf buf) {
83  return createByteArray(env, buf->coalesce());
84 }
85 
86 } // namespace jni
87 } // namespace fizz
jclass getClass(JNIEnv *env, const std::string &name)
Definition: JniUtils.cpp:46
constexpr size_type size() const
Definition: Range.h:431
const char * name
Definition: http_parser.c:437
void maybeThrowException(JNIEnv *env, bool shouldDetach)
Definition: JniUtils.cpp:63
void setVM(JavaVM *jvm)
Definition: JniUtils.cpp:19
void releaseEnv(bool shouldDetach)
Definition: JniUtils.cpp:40
constexpr Iter data() const
Definition: Range.h:446
Definition: Actions.h:16
JNIEnv * getEnv(bool *shouldDetach)
Definition: JniUtils.cpp:23
jmethodID getMethodID(JNIEnv *env, jclass clazz, const std::string &name, const std::string &signature)
Definition: JniUtils.cpp:53
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
jbyteArray createByteArray(JNIEnv *env, folly::ByteRange byteRange)
Definition: JniUtils.cpp:72