Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetClassMethods/libOverpassMethods.cpp
41153 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <string.h>27#include "jvmti.h"2829#ifdef __cplusplus30extern "C" {31#endif3233#define ACC_STATIC 0x00083435static jvmtiEnv *jvmti = NULL;3637JNIEXPORT38jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {39return JNI_VERSION_9;40}4142JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {43vm->GetEnv((void **)&jvmti, JVMTI_VERSION_11);4445if (options != NULL && strcmp(options, "maintain_original_method_order") == 0) {46printf("Enabled capability: maintain_original_method_order\n");47jvmtiCapabilities caps = {};48caps.can_maintain_original_method_order = 1;4950jvmtiError err = jvmti->AddCapabilities(&caps);51if (err != JVMTI_ERROR_NONE) {52printf("Agent_OnLoad: AddCapabilities failed with error: %d\n", err);53return JNI_ERR;54}55}56return JNI_OK;57}5859JNIEXPORT jobjectArray JNICALL Java_OverpassMethods_getJVMTIDeclaredMethods(JNIEnv *env, jclass static_klass, jclass klass) {60jint method_count = 0;61jmethodID* methods = NULL;62jvmtiError err = jvmti->GetClassMethods(klass, &method_count, &methods);63if (err != JVMTI_ERROR_NONE) {64printf("GetClassMethods failed with error: %d\n", err);65return NULL;66}6768jclass method_cls = env->FindClass("java/lang/reflect/Method");69if (method_cls == NULL) {70printf("FindClass (Method) failed\n");71return NULL;72}73jobjectArray array = env->NewObjectArray(method_count, method_cls, NULL);74if (array == NULL) {75printf("NewObjectArray failed\n");76return NULL;77}7879for (int i = 0; i < method_count; i++) {80jint modifiers = 0;81err = jvmti->GetMethodModifiers(methods[i], &modifiers);82if (err != JVMTI_ERROR_NONE) {83printf("GetMethodModifiers failed with error: %d\n", err);84return NULL;85}8687jobject m = env->ToReflectedMethod(klass, methods[i], (modifiers & ACC_STATIC) == ACC_STATIC);88if (array == NULL) {89printf("ToReflectedMethod failed\n");90return NULL;91}92env->SetObjectArrayElement(array, i, m);9394env->DeleteLocalRef(m);95}96jvmti->Deallocate((unsigned char *)methods);9798return array;99}100#ifdef __cplusplus101}102#endif103104105