David Brazdil | 12f70c8 | 2018-02-19 11:44:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "jni.h" |
| 18 | |
| 19 | class ScopedUtfChars { |
| 20 | public: |
| 21 | ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) { |
| 22 | if (s == NULL) { |
| 23 | utf_chars_ = NULL; |
| 24 | } else { |
| 25 | utf_chars_ = env->GetStringUTFChars(s, NULL); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | ~ScopedUtfChars() { |
| 30 | if (utf_chars_) { |
| 31 | env_->ReleaseStringUTFChars(string_, utf_chars_); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | const char* c_str() const { |
| 36 | return utf_chars_; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | JNIEnv* env_; |
| 41 | jstring string_; |
| 42 | const char* utf_chars_; |
| 43 | }; |
| 44 | |
| 45 | extern "C" JNIEXPORT void JNICALL |
| 46 | Java_android_signature_cts_api_HiddenApiTest_getField_1JNI( |
| 47 | JNIEnv* env, jclass, jclass klass, jstring name, jstring type) { |
| 48 | ScopedUtfChars utf_name(env, name); |
| 49 | ScopedUtfChars utf_type(env, type); |
| 50 | // Attempt to access the given instance field. It will succeed if it exists, |
| 51 | // and throw NoSuchFieldError if not. |
| 52 | env->GetFieldID(klass, utf_name.c_str(), utf_type.c_str()); |
| 53 | } |
| 54 | |
| 55 | extern "C" JNIEXPORT void JNICALL |
| 56 | Java_android_signature_cts_api_HiddenApiTest_getStaticField_1JNI( |
| 57 | JNIEnv* env, jclass, jclass klass, jstring name, jstring type) { |
| 58 | ScopedUtfChars utf_name(env, name); |
| 59 | ScopedUtfChars utf_type(env, type); |
| 60 | // Attempt to access the given static field. It will succeed if it exists, |
| 61 | // and throw NoSuchFieldError if not. |
| 62 | env->GetStaticFieldID(klass, utf_name.c_str(), utf_type.c_str()); |
| 63 | } |
| 64 | |
| 65 | extern "C" JNIEXPORT void JNICALL |
| 66 | Java_android_signature_cts_api_HiddenApiTest_getMethod_1JNI( |
| 67 | JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) { |
| 68 | ScopedUtfChars utf_name(env, name); |
| 69 | ScopedUtfChars utf_signature(env, signature); |
| 70 | // Attempt to access the given instance method. It will succeed if it exists, |
| 71 | // and throw NoSuchMethodError if not. |
| 72 | env->GetMethodID(klass, utf_name.c_str(), utf_signature.c_str()); |
| 73 | } |
| 74 | |
| 75 | extern "C" JNIEXPORT void JNICALL |
| 76 | Java_android_signature_cts_api_HiddenApiTest_getStaticMethod_1JNI( |
| 77 | JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) { |
| 78 | ScopedUtfChars utf_name(env, name); |
| 79 | ScopedUtfChars utf_signature(env, signature); |
| 80 | // Attempt to access the given static method. It will succeed if it exists, |
| 81 | // and throw NoSuchMethodError if not. |
| 82 | env->GetStaticMethodID(klass, utf_name.c_str(), utf_signature.c_str()); |
| 83 | } |