Home | History | Annotate | Download | only in 907-get-loaded-classes
      1 /*
      2  * Copyright (C) 2013 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 <pthread.h>
     18 
     19 #include <cstdio>
     20 #include <iostream>
     21 #include <vector>
     22 
     23 #include "android-base/macros.h"
     24 
     25 #include "jni.h"
     26 #include "jvmti.h"
     27 #include "scoped_local_ref.h"
     28 #include "scoped_utf_chars.h"
     29 
     30 // Test infrastructure
     31 #include "jni_helper.h"
     32 #include "test_env.h"
     33 
     34 namespace art {
     35 namespace Test907GetLoadedClasses {
     36 
     37 static jstring GetClassName(JNIEnv* jni_env, jclass cls) {
     38   ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls));
     39   jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;");
     40   return reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid));
     41 }
     42 
     43 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test907_getLoadedClasses(
     44     JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
     45   jint count = -1;
     46   jclass* classes = nullptr;
     47   jvmtiError result = jvmti_env->GetLoadedClasses(&count, &classes);
     48   if (result != JVMTI_ERROR_NONE) {
     49     char* err;
     50     jvmti_env->GetErrorName(result, &err);
     51     printf("Failure running GetLoadedClasses: %s\n", err);
     52     jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
     53     return nullptr;
     54   }
     55 
     56   auto callback = [&](jint i) {
     57     jstring class_name = GetClassName(env, classes[i]);
     58     env->DeleteLocalRef(classes[i]);
     59     return class_name;
     60   };
     61   jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
     62 
     63   // Need to Deallocate.
     64   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes));
     65 
     66   return ret;
     67 }
     68 
     69 }  // namespace Test907GetLoadedClasses
     70 }  // namespace art
     71