test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/hotswap/HS301/hs301t002/hs301t002.c
changeset 50260 46c67f5e27c2
equal deleted inserted replaced
50259:01d27ae7a84e 50260:46c67f5e27c2
       
     1 /*
       
     2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 /**
       
    24  * Please see the ./hs301t002.README file for detailed explanation of this testcase.
       
    25  *
       
    26  */
       
    27 #include <jni.h>
       
    28 #include <jvmti.h>
       
    29 #include "agent_common.h"
       
    30 #include <string.h>
       
    31 #include "jvmti_tools.h"
       
    32 #include "jni_tools.h"
       
    33 
       
    34 #ifdef __cplusplus
       
    35 extern "C" {
       
    36 #endif
       
    37 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t002/MyClass"
       
    38 #define DIR_NAME "newclass"
       
    39 #define PATH_FORMAT "%s%02d/%s"
       
    40 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t002/MyClass"
       
    41 
       
    42 static jvmtiEnv * jvmti;
       
    43 
       
    44 #ifdef STATIC_BUILD
       
    45 JNIEXPORT jint JNICALL Agent_OnLoad_hs301t002(JavaVM *jvm, char *options, void *reserved) {
       
    46     return Agent_Initialize(jvm, options, reserved);
       
    47 }
       
    48 JNIEXPORT jint JNICALL Agent_OnAttach_hs301t002(JavaVM *jvm, char *options, void *reserved) {
       
    49     return Agent_Initialize(jvm, options, reserved);
       
    50 }
       
    51 JNIEXPORT jint JNI_OnLoad_hs301t002(JavaVM *jvm, char *options, void *reserved) {
       
    52     return JNI_VERSION_1_8;
       
    53 }
       
    54 #endif
       
    55 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
       
    56     nsk_printf(" Agent:: VM Started.\n");
       
    57     if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB3(GetEnv, vm,
       
    58                     (void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
       
    59         nsk_printf(" Agent ::Agent failed to get jvmti env.\n");
       
    60         return JNI_ERR;
       
    61     } else {
       
    62         jvmtiCapabilities caps;
       
    63         if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
       
    64             nsk_printf(" Agent:: ## error agent Failed to parse options.\n");
       
    65             return JNI_ERR;
       
    66         }
       
    67         memset(&caps, 0, sizeof(caps));
       
    68         caps.can_redefine_classes = 1;
       
    69         if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2(AddCapabilities, jvmti, &caps) ) ) {
       
    70             nsk_printf(" Agent:: Error occured while adding capabilities.\n");
       
    71             return JNI_ERR;
       
    72         }
       
    73     }
       
    74     return JNI_OK;
       
    75 }
       
    76 
       
    77 /**
       
    78  * fixed JNI  signature, and droped jclass clsa (paramter from here as it was not used).
       
    79  *
       
    80  */
       
    81 JNIEXPORT jboolean JNICALL
       
    82 Java_nsk_jvmti_scenarios_hotswap_HS301_hs301t002_hs301t002_redefine(JNIEnv * jni, jobject obj) {
       
    83     jclass cls;
       
    84     jboolean ret;
       
    85     char fileName[512];
       
    86     int redefineNumber ;
       
    87 
       
    88     redefineNumber=0;
       
    89     ret = JNI_FALSE;
       
    90     if (!NSK_JNI_VERIFY(jni, (cls = NSK_CPP_STUB2(FindClass, jni, SEARCH_NAME)) != NULL)) {
       
    91         nsk_printf("Agent:: (*JNI)->FindClass(jni, %s) returns `null`.\n", SEARCH_NAME);
       
    92         return NSK_FALSE;
       
    93     }
       
    94     nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName,
       
    95                         sizeof(fileName)/sizeof(char));
       
    96     if ( nsk_jvmti_redefineClass(jvmti, cls, fileName) == NSK_TRUE) {
       
    97         nsk_printf("Agent:: MyClass :: Successfully redefined.\n");
       
    98         ret = JNI_TRUE;
       
    99     } else {
       
   100         nsk_printf("Agent:: MyClass :: Failed to redefine.\n");
       
   101     }
       
   102     return ret;
       
   103 }
       
   104 
       
   105 #ifdef __cplusplus
       
   106 }
       
   107 #endif