• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /** JNI utils for nativehelper-internal use. */
18 
19 #ifndef SRC_ANDROID_SDK_NATIVEHELPER_NATIVEHELPER_UTILS_H_
20 #define SRC_ANDROID_SDK_NATIVEHELPER_NATIVEHELPER_UTILS_H_
21 
22 // Copied from
23 // https://cs.android.com/android/platform/superproject/main/+/main:libnativehelper/header_only_include/nativehelper/nativehelper_utils.h;drc=4be05051ef76b2c24d8385732a892401eb45d911
24 
25 #include <jni.h>
26 
27 #if defined(__cplusplus)
28 
29 #if !defined(DISALLOW_COPY_AND_ASSIGN)
30 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes
31 // in the private: declarations in a class.
32 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
33   TypeName(const TypeName&) = delete;      \
34   void operator=(const TypeName&) = delete
35 #endif  // !defined(DISALLOW_COPY_AND_ASSIGN)
36 
37 // This seems a header-only include. Provide NPE throwing.
jniThrowNullPointerException(JNIEnv * env)38 static inline int jniThrowNullPointerException(JNIEnv* env) {
39   if (env->ExceptionCheck()) {
40     // Drop any pending exception.
41     env->ExceptionClear();
42   }
43 
44   jclass e_class = env->FindClass("java/lang/NullPointerException");
45   if (e_class == nullptr) {
46     return -1;
47   }
48 
49   if (env->ThrowNew(e_class, nullptr) != JNI_OK) {
50     env->DeleteLocalRef(e_class);
51     return -1;
52   }
53 
54   env->DeleteLocalRef(e_class);
55   return 0;
56 }
57 
58 #endif  // defined(__cplusplus)
59 
60 #endif  // SRC_ANDROID_SDK_NATIVEHELPER_NATIVEHELPER_UTILS_H_
61