< prev index next >

test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/shared/redefineClasses.cpp

Print this page
rev 51840 : [mq]: refactor


  39 /*
  40  * Redefine a class with a new version (class file in byte array).
  41  *
  42  * native static public boolean redefineClassIntl(Class<?> clz, byte[] classFile);
  43  *
  44  * @param clz class for redefinition
  45  * @param classFile new version as a byte array
  46  * @return false if any errors occurred during class redefinition
  47  */
  48 JNIEXPORT jboolean JNICALL
  49 Java_vm_runtime_defmeth_shared_Util_redefineClassIntl(JNIEnv *env, jclass clazz, jclass clazzToRedefine, jbyteArray bytecodeArray) {
  50     jvmtiClassDefinition classDef;
  51     jboolean result = JNI_TRUE;
  52 
  53     if (!NSK_VERIFY(env != NULL) || !NSK_VERIFY(clazzToRedefine != NULL) || !NSK_VERIFY(bytecodeArray != NULL)) {
  54         return JNI_FALSE;
  55     }
  56 
  57     classDef.klass = clazzToRedefine;
  58     if (!NSK_JNI_VERIFY(env,
  59             (classDef.class_byte_count = /* jsize */ NSK_CPP_STUB2(GetArrayLength, env, bytecodeArray)) > 0)) {
  60         return JNI_FALSE;
  61     }
  62 
  63     if (!NSK_JNI_VERIFY(env,
  64             (classDef.class_bytes = (const unsigned char *) /* jbyte* */ NSK_CPP_STUB3(GetByteArrayElements, env, bytecodeArray, NULL)) != NULL)) {
  65         return JNI_FALSE;
  66     }
  67 
  68     if (!NSK_JVMTI_VERIFY(
  69             NSK_CPP_STUB3(RedefineClasses, test_jvmti, 1, &classDef))) {
  70         result = JNI_FALSE;
  71     }
  72 
  73     // Need to cleanup reference to byte[] whether RedefineClasses succeeded or not
  74     if (!NSK_JNI_VERIFY_VOID(env,
  75             NSK_CPP_STUB4(ReleaseByteArrayElements, env, bytecodeArray, (jbyte*)classDef.class_bytes, JNI_ABORT))) {
  76         return JNI_FALSE;
  77     }
  78 
  79     return result;
  80 }
  81 
  82 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  83     /* init framework and parse options */
  84     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
  85         return JNI_ERR;
  86 
  87     /* create JVMTI environment */
  88     if (!NSK_VERIFY((test_jvmti =
  89             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
  90         return JNI_ERR;
  91 
  92     memset(&caps, 0, sizeof(jvmtiCapabilities));
  93     caps.can_redefine_classes = 1;
  94     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities,
  95             test_jvmti, &caps)))
  96         return JNI_ERR;
  97 
  98     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities,
  99             test_jvmti, &caps)))
 100         return JNI_ERR;
 101 
 102     if (!caps.can_redefine_classes)
 103         printf("Warning: RedefineClasses is not implemented\n");
 104 
 105     return JNI_OK;
 106 }
 107 
 108 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 109     return Agent_Initialize(jvm, options, reserved);
 110 }
 111 
 112 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
 113     return Agent_Initialize(jvm, options, reserved);
 114 }
 115 
 116 }


  39 /*
  40  * Redefine a class with a new version (class file in byte array).
  41  *
  42  * native static public boolean redefineClassIntl(Class<?> clz, byte[] classFile);
  43  *
  44  * @param clz class for redefinition
  45  * @param classFile new version as a byte array
  46  * @return false if any errors occurred during class redefinition
  47  */
  48 JNIEXPORT jboolean JNICALL
  49 Java_vm_runtime_defmeth_shared_Util_redefineClassIntl(JNIEnv *env, jclass clazz, jclass clazzToRedefine, jbyteArray bytecodeArray) {
  50     jvmtiClassDefinition classDef;
  51     jboolean result = JNI_TRUE;
  52 
  53     if (!NSK_VERIFY(env != NULL) || !NSK_VERIFY(clazzToRedefine != NULL) || !NSK_VERIFY(bytecodeArray != NULL)) {
  54         return JNI_FALSE;
  55     }
  56 
  57     classDef.klass = clazzToRedefine;
  58     if (!NSK_JNI_VERIFY(env,
  59             (classDef.class_byte_count = /* jsize */ env->GetArrayLength(bytecodeArray)) > 0)) {
  60         return JNI_FALSE;
  61     }
  62 
  63     if (!NSK_JNI_VERIFY(env,
  64             (classDef.class_bytes = (const unsigned char *) /* jbyte* */ env->GetByteArrayElements(bytecodeArray, NULL)) != NULL)) {
  65         return JNI_FALSE;
  66     }
  67 
  68     if (!NSK_JVMTI_VERIFY(
  69             test_jvmti->RedefineClasses(1, &classDef))) {
  70         result = JNI_FALSE;
  71     }
  72 
  73     // Need to cleanup reference to byte[] whether RedefineClasses succeeded or not
  74     if (!NSK_JNI_VERIFY_VOID(env,
  75             env->ReleaseByteArrayElements(bytecodeArray, (jbyte*)classDef.class_bytes, JNI_ABORT))) {
  76         return JNI_FALSE;
  77     }
  78 
  79     return result;
  80 }
  81 
  82 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  83     /* init framework and parse options */
  84     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
  85         return JNI_ERR;
  86 
  87     /* create JVMTI environment */
  88     if (!NSK_VERIFY((test_jvmti =
  89             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
  90         return JNI_ERR;
  91 
  92     memset(&caps, 0, sizeof(jvmtiCapabilities));
  93     caps.can_redefine_classes = 1;
  94     if (!NSK_JVMTI_VERIFY(test_jvmti->AddCapabilities(&caps)))

  95         return JNI_ERR;
  96 
  97     if (!NSK_JVMTI_VERIFY(test_jvmti->GetCapabilities(&caps)))

  98         return JNI_ERR;
  99 
 100     if (!caps.can_redefine_classes)
 101         printf("Warning: RedefineClasses is not implemented\n");
 102 
 103     return JNI_OK;
 104 }
 105 
 106 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 107     return Agent_Initialize(jvm, options, reserved);
 108 }
 109 
 110 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
 111     return Agent_Initialize(jvm, options, reserved);
 112 }
 113 
 114 }
< prev index next >