Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetClassFields/getclfld007/getclfld007.cpp
40955 views
/*1* Copyright (c) 2003, 2018, 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} fld_info;3940typedef struct {41const char *name;42jint fcount;43fld_info *flds;44} class_info;4546static jvmtiEnv *jvmti = NULL;47static jint result = PASSED;48static jboolean printdump = JNI_FALSE;4950static fld_info f0[] = {51{ "fld_1", "Ljava/lang/String;" }52};5354static fld_info f1[] = {55{ "fld_n1", "I" }56};5758static fld_info f2[] = {59{ "fld_n2", "I" }60};6162static fld_info f4[] = {63{ "fld_o2", "I" }64};6566static fld_info f5[] = {67{ "fld_o3", "I" }68};6970static fld_info f6[] = {71{ "fld_i1", "I" }72};7374static fld_info f7[] = {75{ "fld_i2", "I" }76};7778static fld_info f8[] = {79{ "fld_i2", "I" }80};8182static fld_info f9[] = {83{ "fld_i1", "I" }84};8586static class_info classes[] = {87{ "InnerClass1", 1, f0 },88{ "InnerInterface", 1, f1 },89{ "InnerClass2", 1, f2 },90{ "OuterClass1", 0, NULL },91{ "OuterClass2", 1, f4 },92{ "OuterClass3", 1, f5 },93{ "OuterInterface1", 1, f6 },94{ "OuterInterface2", 1, f7 },95{ "OuterClass4", 1, f8 },96{ "OuterClass5", 1, f9 }97};9899#ifdef STATIC_BUILD100JNIEXPORT jint JNICALL Agent_OnLoad_getclfld007(JavaVM *jvm, char *options, void *reserved) {101return Agent_Initialize(jvm, options, reserved);102}103JNIEXPORT jint JNICALL Agent_OnAttach_getclfld007(JavaVM *jvm, char *options, void *reserved) {104return Agent_Initialize(jvm, options, reserved);105}106JNIEXPORT jint JNI_OnLoad_getclfld007(JavaVM *jvm, char *options, void *reserved) {107return JNI_VERSION_1_8;108}109#endif110jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {111jint res;112113if (options != NULL && strcmp(options, "printdump") == 0) {114printdump = JNI_TRUE;115}116117res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);118if (res != JNI_OK || jvmti == NULL) {119printf("Wrong result of a valid call to GetEnv!\n");120return JNI_ERR;121}122123return JNI_OK;124}125126JNIEXPORT void JNICALL127Java_nsk_jvmti_GetClassFields_getclfld007_check(JNIEnv *env, jclass cls, jint i, jclass clazz) {128jvmtiError err;129jint fcount;130jfieldID *fields;131char *name, *sig, *generic;132int j;133134if (jvmti == NULL) {135printf("JVMTI client was not properly loaded!\n");136result = STATUS_FAILED;137return;138}139140if (printdump == JNI_TRUE) {141printf(">>> %s:\n", classes[i].name);142}143144err = jvmti->GetClassFields(clazz, &fcount, &fields);145if (err != JVMTI_ERROR_NONE) {146printf("(GetClassFields#%d) unexpected error: %s (%d)\n",147i, TranslateError(err), err);148result = STATUS_FAILED;149return;150}151152if (fcount != classes[i].fcount) {153printf("(%d) wrong number of fields: %d, expected: %d\n",154i, fcount, classes[i].fcount);155result = STATUS_FAILED;156}157for (j = 0; j < fcount; j++) {158if (fields[j] == NULL) {159printf("(%d:%d) fieldID = null\n", i, j);160} else {161err = jvmti->GetFieldName(clazz, fields[j],162&name, &sig, &generic);163if (err != JVMTI_ERROR_NONE) {164printf("(GetFieldName#%d:%d) unexpected error: %s (%d)\n",165i, j, TranslateError(err), err);166} else {167if (printdump == JNI_TRUE) {168printf(">>> [%d]: %s, sig = \"%s\"\n", j, name, sig);169}170if ((j < classes[i].fcount) &&171(name == NULL || sig == NULL ||172strcmp(name, classes[i].flds[j].name) != 0 ||173strcmp(sig, classes[i].flds[j].sig) != 0)) {174printf("(%d:%d) wrong field: \"%s%s\"", i, j, name, sig);175printf(", expected: \"%s%s\"\n",176classes[i].flds[j].name, classes[i].flds[j].sig);177result = STATUS_FAILED;178}179}180}181}182}183184JNIEXPORT int JNICALL185Java_nsk_jvmti_GetClassFields_getclfld007_getRes(JNIEnv *env, jclass cls) {186return result;187}188189}190191192