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:
ScopedUtfChars(JNIEnv * env,jstring s)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 
~ScopedUtfChars()29   ~ScopedUtfChars() {
30     if (utf_chars_) {
31       env_->ReleaseStringUTFChars(string_, utf_chars_);
32     }
33   }
34 
c_str() const35   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 jobject JNICALL
Java_android_signature_cts_DexMemberChecker_getField_1JNI(JNIEnv * env,jclass,jclass klass,jstring name,jstring type)46 Java_android_signature_cts_DexMemberChecker_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   jfieldID fid = env->GetFieldID(klass, utf_name.c_str(), utf_type.c_str());
51   if (env->ExceptionCheck()) {
52     // GetFieldID could have thrown either NoSuchFieldError or ExceptionInInitializerError.
53     // Do not clear the exception, let Java code handle it.
54     return nullptr;
55   }
56   return env->ToReflectedField(klass, fid, /* static */ false);
57 }
58 
59 extern "C" JNIEXPORT jobject JNICALL
Java_android_signature_cts_DexMemberChecker_getStaticField_1JNI(JNIEnv * env,jclass,jclass klass,jstring name,jstring type)60 Java_android_signature_cts_DexMemberChecker_getStaticField_1JNI(
61     JNIEnv* env, jclass, jclass klass, jstring name, jstring type) {
62   ScopedUtfChars utf_name(env, name);
63   ScopedUtfChars utf_type(env, type);
64   jfieldID fid = env->GetStaticFieldID(klass, utf_name.c_str(), utf_type.c_str());
65   if (env->ExceptionCheck()) {
66     // GetStaticFieldID could have thrown either NoSuchFieldError or ExceptionInInitializerError.
67     // Do not clear the exception, let Java code handle it.
68     return nullptr;
69   }
70   return env->ToReflectedField(klass, fid, /* static */ true);
71 }
72 
73 extern "C" JNIEXPORT jobject JNICALL
Java_android_signature_cts_DexMemberChecker_getMethod_1JNI(JNIEnv * env,jclass,jclass klass,jstring name,jstring signature)74 Java_android_signature_cts_DexMemberChecker_getMethod_1JNI(
75     JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) {
76   ScopedUtfChars utf_name(env, name);
77   ScopedUtfChars utf_signature(env, signature);
78   jmethodID mid = env->GetMethodID(klass, utf_name.c_str(), utf_signature.c_str());
79   if (env->ExceptionCheck()) {
80     // GetMethodID could have thrown either NoSuchMethodError or ExceptionInInitializerError.
81     // Do not clear the exception, let Java code handle it.
82     return nullptr;
83   }
84   return env->ToReflectedMethod(klass, mid, /* static */ false);
85 }
86 
87 extern "C" JNIEXPORT jobject JNICALL
Java_android_signature_cts_DexMemberChecker_getStaticMethod_1JNI(JNIEnv * env,jclass,jclass klass,jstring name,jstring signature)88 Java_android_signature_cts_DexMemberChecker_getStaticMethod_1JNI(
89     JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) {
90   ScopedUtfChars utf_name(env, name);
91   ScopedUtfChars utf_signature(env, signature);
92   jmethodID mid = env->GetStaticMethodID(klass, utf_name.c_str(), utf_signature.c_str());
93   if (env->ExceptionCheck()) {
94     // GetStaticMethodID could have thrown either NoSuchMethodError or ExceptionInInitializerError.
95     // Do not clear the exception, let Java code handle it.
96     return nullptr;
97   }
98   return env->ToReflectedMethod(klass, mid, /* static */ true);
99 }
100