Home | History | Annotate | Download | only in dex-checker
      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 jobject JNICALL
     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     env->ExceptionClear();
     53     return nullptr;
     54   }
     55   return env->ToReflectedField(klass, fid, /* static */ false);
     56 }
     57 
     58 extern "C" JNIEXPORT jobject JNICALL
     59 Java_android_signature_cts_DexMemberChecker_getStaticField_1JNI(
     60     JNIEnv* env, jclass, jclass klass, jstring name, jstring type) {
     61   ScopedUtfChars utf_name(env, name);
     62   ScopedUtfChars utf_type(env, type);
     63   jfieldID fid = env->GetStaticFieldID(klass, utf_name.c_str(), utf_type.c_str());
     64   if (env->ExceptionCheck()) {
     65     env->ExceptionClear();
     66     return nullptr;
     67   }
     68   return env->ToReflectedField(klass, fid, /* static */ true);
     69 }
     70 
     71 extern "C" JNIEXPORT jobject JNICALL
     72 Java_android_signature_cts_DexMemberChecker_getMethod_1JNI(
     73     JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) {
     74   ScopedUtfChars utf_name(env, name);
     75   ScopedUtfChars utf_signature(env, signature);
     76   jmethodID mid = env->GetMethodID(klass, utf_name.c_str(), utf_signature.c_str());
     77   if (env->ExceptionCheck()) {
     78     env->ExceptionClear();
     79     return nullptr;
     80   }
     81   return env->ToReflectedMethod(klass, mid, /* static */ false);
     82 }
     83 
     84 extern "C" JNIEXPORT jobject JNICALL
     85 Java_android_signature_cts_DexMemberChecker_getStaticMethod_1JNI(
     86     JNIEnv* env, jclass, jclass klass, jstring name, jstring signature) {
     87   ScopedUtfChars utf_name(env, name);
     88   ScopedUtfChars utf_signature(env, signature);
     89   jmethodID mid = env->GetStaticMethodID(klass, utf_name.c_str(), utf_signature.c_str());
     90   if (env->ExceptionCheck()) {
     91     env->ExceptionClear();
     92     return nullptr;
     93   }
     94   return env->ToReflectedMethod(klass, mid, /* static */ true);
     95 }
     96