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