Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetClassMethods/libOverpassMethods.cpp
66645 views
/*1* Copyright (c) 2020, 2022, 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;48memset(&caps, 0, sizeof(caps));49caps.can_maintain_original_method_order = 1;5051jvmtiError err = jvmti->AddCapabilities(&caps);52if (err != JVMTI_ERROR_NONE) {53printf("Agent_OnLoad: AddCapabilities failed with error: %d\n", err);54return JNI_ERR;55}56}57return JNI_OK;58}5960JNIEXPORT jobjectArray JNICALL Java_OverpassMethods_getJVMTIDeclaredMethods(JNIEnv *env, jclass static_klass, jclass klass) {61jint method_count = 0;62jmethodID* methods = NULL;63jvmtiError err = jvmti->GetClassMethods(klass, &method_count, &methods);64if (err != JVMTI_ERROR_NONE) {65printf("GetClassMethods failed with error: %d\n", err);66return NULL;67}6869jclass method_cls = env->FindClass("java/lang/reflect/Method");70if (method_cls == NULL) {71printf("FindClass (Method) failed\n");72return NULL;73}74jobjectArray array = env->NewObjectArray(method_count, method_cls, NULL);75if (array == NULL) {76printf("NewObjectArray failed\n");77return NULL;78}7980for (int i = 0; i < method_count; i++) {81jint modifiers = 0;82err = jvmti->GetMethodModifiers(methods[i], &modifiers);83if (err != JVMTI_ERROR_NONE) {84printf("GetMethodModifiers failed with error: %d\n", err);85return NULL;86}8788jobject m = env->ToReflectedMethod(klass, methods[i], (modifiers & ACC_STATIC) == ACC_STATIC);89if (array == NULL) {90printf("ToReflectedMethod failed\n");91return NULL;92}93env->SetObjectArrayElement(array, i, m);9495env->DeleteLocalRef(m);96}97jvmti->Deallocate((unsigned char *)methods);9899return array;100}101#ifdef __cplusplus102}103#endif104105106