annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/RedefineClasses/redefclass004/redefclass004.cpp @ 51870:cdfabab3413f

8210689: Remove the multi-line old C style for string literals Summary: Remove the multi-line old C style and prefer C++ multi-line Reviewed-by: amenkov, cjplummer
author jcbeyler
date Tue, 25 Sep 2018 09:34:51 -0700
parents 13a63d4a3f8d
children 9cfc8b0c45fd
rev   line source
iignatyev@50260 1 /*
iignatyev@50260 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
iignatyev@50260 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
iignatyev@50260 4 *
iignatyev@50260 5 * This code is free software; you can redistribute it and/or modify it
iignatyev@50260 6 * under the terms of the GNU General Public License version 2 only, as
iignatyev@50260 7 * published by the Free Software Foundation.
iignatyev@50260 8 *
iignatyev@50260 9 * This code is distributed in the hope that it will be useful, but WITHOUT
iignatyev@50260 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
iignatyev@50260 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
iignatyev@50260 12 * version 2 for more details (a copy is included in the LICENSE file that
iignatyev@50260 13 * accompanied this code).
iignatyev@50260 14 *
iignatyev@50260 15 * You should have received a copy of the GNU General Public License version
iignatyev@50260 16 * 2 along with this work; if not, write to the Free Software Foundation,
iignatyev@50260 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
iignatyev@50260 18 *
iignatyev@50260 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
iignatyev@50260 20 * or visit www.oracle.com if you need additional information or have any
iignatyev@50260 21 * questions.
iignatyev@50260 22 */
iignatyev@50260 23
iignatyev@50260 24 #include <stdio.h>
iignatyev@50260 25 #include <string.h>
iignatyev@50260 26 #include <jvmti.h>
iignatyev@50260 27 #include "jni_tools.h"
iignatyev@50260 28 #include "agent_common.h"
iignatyev@50260 29 #include "JVMTITools.h"
iignatyev@50260 30
iignatyev@50260 31 extern "C" {
iignatyev@50260 32
iignatyev@50260 33
iignatyev@50260 34 #define STATUS_FAILED 2
iignatyev@50260 35 #define PASSED 0
iignatyev@50260 36 #define NO_RESULTS 3
iignatyev@50260 37
iignatyev@50260 38 /* expected values of new instance fields */
iignatyev@50260 39 static jint INTFLD = 333;
iignatyev@50260 40 static jlong LONGFLD = 444;
iignatyev@50260 41 #define STRFLD "completely new String field"
iignatyev@50260 42
iignatyev@50260 43 static jvmtiEnv *jvmti = NULL;
iignatyev@50260 44 static jvmtiCapabilities caps;
iignatyev@50260 45
iignatyev@50260 46 #ifdef STATIC_BUILD
iignatyev@50260 47 JNIEXPORT jint JNICALL Agent_OnLoad_redefclass004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50260 48 return Agent_Initialize(jvm, options, reserved);
iignatyev@50260 49 }
iignatyev@50260 50 JNIEXPORT jint JNICALL Agent_OnAttach_redefclass004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50260 51 return Agent_Initialize(jvm, options, reserved);
iignatyev@50260 52 }
iignatyev@50260 53 JNIEXPORT jint JNI_OnLoad_redefclass004(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50260 54 return JNI_VERSION_1_8;
iignatyev@50260 55 }
iignatyev@50260 56 #endif
iignatyev@50260 57 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
iignatyev@50260 58 jint res;
iignatyev@50260 59 jvmtiError err;
iignatyev@50260 60
jcbeyler@51715 61 if ((res = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {
iignatyev@50260 62 printf("%s: Failed to call GetEnv: error=%d\n", __FILE__, res);
iignatyev@50260 63 return JNI_ERR;
iignatyev@50260 64 }
iignatyev@50260 65
iignatyev@51551 66 err = jvmti->GetPotentialCapabilities(&caps);
iignatyev@50260 67 if (err != JVMTI_ERROR_NONE) {
iignatyev@50260 68 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
iignatyev@50260 69 TranslateError(err), err);
iignatyev@50260 70 return JNI_ERR;
iignatyev@50260 71 }
iignatyev@50260 72
iignatyev@51551 73 err = jvmti->AddCapabilities(&caps);
iignatyev@50260 74 if (err != JVMTI_ERROR_NONE) {
iignatyev@50260 75 printf("(AddCapabilities) unexpected error: %s (%d)\n",
iignatyev@50260 76 TranslateError(err), err);
iignatyev@50260 77 return JNI_ERR;
iignatyev@50260 78 }
iignatyev@50260 79
iignatyev@51551 80 err = jvmti->GetCapabilities(&caps);
iignatyev@50260 81 if (err != JVMTI_ERROR_NONE) {
iignatyev@50260 82 printf("(GetCapabilities) unexpected error: %s (%d)\n",
iignatyev@50260 83 TranslateError(err), err);
iignatyev@50260 84 return JNI_ERR;
iignatyev@50260 85 }
iignatyev@50260 86
iignatyev@50260 87 if (!caps.can_redefine_classes) {
iignatyev@50260 88 printf("Warning: RedefineClasses is not implemented\n");
iignatyev@50260 89 }
iignatyev@50260 90
iignatyev@50260 91 return JNI_OK;
iignatyev@50260 92 }
iignatyev@50260 93
iignatyev@50260 94 JNIEXPORT jint JNICALL
iignatyev@50260 95 Java_nsk_jvmti_RedefineClasses_redefclass004_makeRedefinition(JNIEnv *env,
iignatyev@50260 96 jclass cls, jint vrb, jclass redefCls, jbyteArray classBytes) {
iignatyev@50260 97 jvmtiError err;
iignatyev@50260 98 jvmtiClassDefinition classDef;
iignatyev@50260 99 int no_results = 0;
iignatyev@50260 100
iignatyev@50260 101 if (jvmti == NULL) {
iignatyev@50260 102 printf("JVMTI client was not properly loaded!\n");
iignatyev@50260 103 return STATUS_FAILED;
iignatyev@50260 104 }
iignatyev@50260 105
iignatyev@50260 106 if (!caps.can_redefine_classes) {
iignatyev@50260 107 return PASSED;
iignatyev@50260 108 }
iignatyev@50260 109
iignatyev@50260 110 /* fill the structure jvmtiClassDefinition */
iignatyev@50260 111 classDef.klass = redefCls;
jcbeyler@51715 112 classDef.class_byte_count = env->GetArrayLength(classBytes);
jcbeyler@51715 113 classDef.class_bytes = (unsigned char *) env->GetByteArrayElements(classBytes, NULL);
iignatyev@50260 114
iignatyev@50260 115 if (vrb == 1)
iignatyev@50260 116 printf(">>>>>>>> Invoke RedefineClasses():\n\tnew class byte count=%d\n",
iignatyev@50260 117 classDef.class_byte_count);
iignatyev@51551 118 if ((err = (jvmti->RedefineClasses(1, &classDef))) != JVMTI_ERROR_NONE) {
iignatyev@50260 119 if (err == JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED) {
jcbeyler@51870 120 printf(
jcbeyler@51870 121 "Warning: unrestrictedly redefinition of classes is not implemented,\n"
jcbeyler@51870 122 "\tso the test has no results.\n");
iignatyev@50260 123 no_results = 1;
iignatyev@50260 124 }
iignatyev@50260 125 else {
iignatyev@50260 126 printf("%s: Failed to call RedefineClasses():\n\tthe function returned error %d: %s\n",
iignatyev@50260 127 __FILE__, err, TranslateError(err));
iignatyev@50260 128 printf("\tFor more info about this error see the JVMTI spec.\n");
iignatyev@50260 129 }
iignatyev@50260 130 if (no_results == 1) return NO_RESULTS;
iignatyev@50260 131 else return JNI_ERR;
iignatyev@50260 132 }
iignatyev@50260 133 if (vrb == 1)
iignatyev@50260 134 printf("<<<<<<<< RedefineClasses() is successfully done\n");
iignatyev@50260 135
iignatyev@50260 136 return PASSED;
iignatyev@50260 137 }
iignatyev@50260 138
iignatyev@50260 139 JNIEXPORT jint JNICALL
iignatyev@50260 140 Java_nsk_jvmti_RedefineClasses_redefclass004_checkNewFields(JNIEnv *env,
iignatyev@50260 141 jobject obj, jint vrb, jobject redefObj) {
jcbeyler@51715 142 jclass redefCls = env->GetObjectClass(redefObj);
iignatyev@50260 143 jfieldID fid;
iignatyev@50260 144 jint intFld;
iignatyev@50260 145 jlong longFld;
iignatyev@50260 146 jstring stringObj;
iignatyev@50260 147 const char *strFld = NULL;
iignatyev@50260 148
iignatyev@50260 149 /* get value of new instance field "intComplNewFld" */
jcbeyler@51715 150 if ((fid = env->GetFieldID(redefCls, "intComplNewFld", "I")) == NULL) {
iignatyev@50260 151 printf("%s: Failed to get the field ID for the field \"intComplNewFld\"\n",
iignatyev@50260 152 __FILE__);
iignatyev@50260 153 return STATUS_FAILED;
iignatyev@50260 154 }
jcbeyler@51715 155 intFld = env->GetIntField(redefObj, fid);
iignatyev@50260 156
iignatyev@50260 157 /* get value of new instance field "longComplNewFld" */
jcbeyler@51715 158 if ((fid = env->GetFieldID(redefCls, "longComplNewFld", "J")) == NULL) {
iignatyev@50260 159 printf("%s: Failed to get the field ID for the field \"longComplNewFld\"\n",
iignatyev@50260 160 __FILE__);
iignatyev@50260 161 return STATUS_FAILED;
iignatyev@50260 162 }
jcbeyler@51715 163 longFld = env->GetLongField(redefObj, fid);
iignatyev@50260 164
iignatyev@50260 165 /* get value of new instance field "stringComplNewFld" */
jcbeyler@51715 166 if ((fid = env->GetFieldID(redefCls, "stringComplNewFld", "Ljava/lang/String;")) == NULL) {
iignatyev@50260 167 printf("%s: Failed to get the field ID for the field \"stringComplNewFld\"\n",
iignatyev@50260 168 __FILE__);
iignatyev@50260 169 return STATUS_FAILED;
iignatyev@50260 170 }
jcbeyler@51715 171 stringObj = (jstring) env->GetObjectField(redefObj, fid);
jcbeyler@51715 172 strFld = env->GetStringUTFChars(stringObj, 0);
iignatyev@50260 173
iignatyev@50260 174 if (intFld != INTFLD || longFld != LONGFLD ||
iignatyev@50260 175 strcmp(strFld, STRFLD) != 0) {
iignatyev@50260 176 printf("Completely new fields have not assigned their default values:\n");
iignatyev@50260 177 printf("\tintComplNewFld = %d,\texpected %d\n",
iignatyev@50260 178 intFld, INTFLD);
iignatyev@51551 179 printf("\tlongComplNewFld = %" LL "d", longFld);
iignatyev@51551 180 printf(",\texpected %" LL "d\n", LONGFLD);
iignatyev@50260 181 printf("\tstringComplNewFld = \"%s\",\texpected \"%s\"\n",
iignatyev@50260 182 strFld, STRFLD);
jcbeyler@51715 183 env->ReleaseStringUTFChars(stringObj, strFld);
iignatyev@50260 184 return STATUS_FAILED;
iignatyev@50260 185 } else {
jcbeyler@51870 186 if (vrb == 1) {
jcbeyler@51870 187 printf(
jcbeyler@51870 188 "Completely new fields:\n"
jcbeyler@51870 189 "\tstringComplNewFld = \"%s\"\n"
jcbeyler@51870 190 "\tintComplNewFld = %d\n"
jcbeyler@51870 191 "\tlongComplNewFld = %" LL "d\n",
iignatyev@50260 192 strFld, intFld, longFld);
jcbeyler@51870 193 }
jcbeyler@51715 194 env->ReleaseStringUTFChars(stringObj, strFld);
iignatyev@50260 195 return PASSED;
iignatyev@50260 196 }
iignatyev@50260 197 }
iignatyev@50260 198
iignatyev@50260 199 }