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