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 extern "C" {
  35 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t002/MyClass"
  36 #define DIR_NAME "newclass"
  37 #define PATH_FORMAT "%s%02d/%s"
  38 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t002/MyClass"
  39 
  40 static jvmtiEnv * jvmti;
  41 
  42 #ifdef STATIC_BUILD
  43 JNIEXPORT jint JNICALL Agent_OnLoad_hs301t002(JavaVM *jvm, char *options, void *reserved) {
  44     return Agent_Initialize(jvm, options, reserved);
  45 }
  46 JNIEXPORT jint JNICALL Agent_OnAttach_hs301t002(JavaVM *jvm, char *options, void *reserved) {
  47     return Agent_Initialize(jvm, options, reserved);
  48 }
  49 JNIEXPORT jint JNI_OnLoad_hs301t002(JavaVM *jvm, char *options, void *reserved) {
  50     return JNI_VERSION_1_8;
  51 }
  52 #endif
  53 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
  54     nsk_printf(" Agent:: VM Started.\n");
  55     if ( ! NSK_VERIFY ( JNI_OK == NSK_CPP_STUB3(GetEnv, vm,
  56                     (void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
  57         nsk_printf(" Agent ::Agent failed to get jvmti env.\n");
  58         return JNI_ERR;
  59     } else {
  60         jvmtiCapabilities caps;
  61         if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
  62             nsk_printf(" Agent:: ## error agent Failed to parse options.\n");
  63             return JNI_ERR;
  64         }
  65         memset(&caps, 0, sizeof(caps));
  66         caps.can_redefine_classes = 1;
  67         if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2(AddCapabilities, jvmti, &caps) ) ) {
  68             nsk_printf(" Agent:: Error occured while adding capabilities.\n");
  69             return JNI_ERR;
  70         }
  71     }
  72     return JNI_OK;
  73 }
  74 
  75 /**
  76  * fixed JNI  signature, and droped jclass clsa (paramter from here as it was not used).
  77  *
  78  */
  79 JNIEXPORT jboolean JNICALL
  80 Java_nsk_jvmti_scenarios_hotswap_HS301_hs301t002_hs301t002_redefine(JNIEnv * jni, jobject obj) {
  81     jclass cls;
  82     jboolean ret;
  83     char fileName[512];
  84     int redefineNumber ;
  85 
  86     redefineNumber=0;
  87     ret = JNI_FALSE;
  88     if (!NSK_JNI_VERIFY(jni, (cls = NSK_CPP_STUB2(FindClass, jni, SEARCH_NAME)) != NULL)) {
  89         nsk_printf("Agent:: (*JNI)->FindClass(jni, %s) returns `null`.\n", SEARCH_NAME);
  90         return NSK_FALSE;
  91     }
  92     nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName,
  93                         sizeof(fileName)/sizeof(char));
  94     if ( nsk_jvmti_redefineClass(jvmti, cls, fileName) == NSK_TRUE) {
  95         nsk_printf("Agent:: MyClass :: Successfully redefined.\n");
  96         ret = JNI_TRUE;
  97     } else {
  98         nsk_printf("Agent:: MyClass :: Failed to redefine.\n");
  99     }
 100     return ret;
 101 }
 102 
 103 }