Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetClassMethods/getclmthd007/getclmthd007.cpp
40955 views
/*1* Copyright (c) 2003, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"26#include "agent_common.h"27#include "JVMTITools.h"2829extern "C" {303132#define PASSED 033#define STATUS_FAILED 23435typedef struct {36const char *name;37const char *sig;38} meth_info;3940typedef struct {41const char *name;42jint mcount;43meth_info *meths;44} class_info;4546static jvmtiEnv *jvmti = NULL;47static jint result = PASSED;48static jboolean printdump = JNI_FALSE;4950static meth_info m0[] = {51{ "<init>", "(Lnsk/jvmti/GetClassMethods/getclmthd007;)V" },52{ "meth_1", "(Ljava/lang/String;)V" }53};5455static meth_info m1[] = {56{ "meth_n1", "()V" },57{ "meth_def1", "()V" }58};5960static meth_info m2[] = {61{ "<init>", "()V" },62{ "meth_n1", "()V" },63{ "meth_n2", "()I" },64{ "<clinit>", "()V" }65};6667static meth_info m3[] = {68{ "<init>", "()V" }69};7071static meth_info m4[] = {72{ "<init>", "()V" },73{ "meth_o2", "()V" }74};7576static meth_info m5[] = {77{ "<init>", "()V" },78{ "meth_o3", "()I" }79};8081static meth_info m6[] = {82{ "meth_i1", "()I" }83};8485static meth_info m7[] = {86{ "meth_i2", "()I" }87};8889static meth_info m8[] = {90{ "<init>", "()V" },91{ "meth_i2", "()I" }92};9394static meth_info m9[] = {95{ "<init>", "()V" },96{ "meth_i1", "()I" }97};9899static class_info classes[] = {100{ "InnerClass1", 2, m0 },101{ "InnerInterface", 2, m1 },102{ "InnerClass2", 4, m2 },103{ "OuterClass1", 1, m3 },104{ "OuterClass2", 2, m4 },105{ "OuterClass3", 2, m5 },106{ "OuterInterface1", 1, m6 },107{ "OuterInterface2", 1, m7 },108{ "OuterClass4", 2, m8 },109{ "OuterClass5", 2, m9 }110};111112#ifdef STATIC_BUILD113JNIEXPORT jint JNICALL Agent_OnLoad_getclmthd007(JavaVM *jvm, char *options, void *reserved) {114return Agent_Initialize(jvm, options, reserved);115}116JNIEXPORT jint JNICALL Agent_OnAttach_getclmthd007(JavaVM *jvm, char *options, void *reserved) {117return Agent_Initialize(jvm, options, reserved);118}119JNIEXPORT jint JNI_OnLoad_getclmthd007(JavaVM *jvm, char *options, void *reserved) {120return JNI_VERSION_1_8;121}122#endif123jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {124jint res;125126if (options != NULL && strcmp(options, "printdump") == 0) {127printdump = JNI_TRUE;128}129130res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);131if (res != JNI_OK || jvmti == NULL) {132printf("Wrong result of a valid call to GetEnv!\n");133return JNI_ERR;134}135136return JNI_OK;137}138139JNIEXPORT void JNICALL140Java_nsk_jvmti_GetClassMethods_getclmthd007_check(JNIEnv *env,141jclass cls, jint i, jclass clazz) {142jvmtiError err;143jint mcount;144jmethodID *methods;145char *name, *sig, *generic;146int j, k;147148int failed = JNI_FALSE; // enable debugging on failure149if (jvmti == NULL) {150printf("JVMTI client was not properly loaded!\n");151result = STATUS_FAILED;152return;153}154155if (printdump == JNI_TRUE) {156printf(">>> %s:\n", classes[i].name);157}158159err = jvmti->GetClassMethods(clazz, &mcount, &methods);160if (err != JVMTI_ERROR_NONE) {161printf("(GetClassMethods#%d) unexpected error: %s (%d)\n",162i, TranslateError(err), err);163result = STATUS_FAILED;164return;165}166167if (mcount != classes[i].mcount) {168printf("(%d) wrong number of methods: %d, expected: %d\n",169i, mcount, classes[i].mcount);170result = STATUS_FAILED;171failed = JNI_TRUE; // show the methods found172printf(">>> %s:\n", classes[i].name);173}174for (k = 0; k < mcount; k++) {175if (methods[k] == NULL) {176printf("(%d:%d) methodID = null\n", i, k);177result = STATUS_FAILED;178} else if (printdump == JNI_TRUE || failed == JNI_TRUE) {179err = jvmti->GetMethodName(methods[k],180&name, &sig, &generic);181if (err == JVMTI_ERROR_NONE) {182printf(">>> [%d]: %s%s\n", k, name, sig);183}184}185}186for (j = 0; j < classes[i].mcount; j++) {187/* search the returned table for each expected entry */188for (k = 0; k < mcount; k++) {189if (methods[k] != NULL) {190err = jvmti->GetMethodName(methods[k],191&name, &sig, &generic);192if (err != JVMTI_ERROR_NONE) {193printf("(GetMethodName#%d:%d) unexpected error: %s (%d)\n",194i, k, TranslateError(err), err);195result = STATUS_FAILED;196} else {197if (name != NULL && sig != NULL &&198strcmp(name, classes[i].meths[j].name) == 0 &&199strcmp(sig, classes[i].meths[j].sig) == 0) break;200}201}202}203if (k == mcount) {204printf("(%d:%d) method not found: \"%s%s\"", i, j,205classes[i].meths[j].name, classes[i].meths[j].sig);206result = STATUS_FAILED;207}208}209}210211JNIEXPORT int JNICALL212Java_nsk_jvmti_GetClassMethods_getclmthd007_getRes(JNIEnv *env, jclass cls) {213return result;214}215216}217218219