Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/shared/redefineClasses.cpp
41161 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include <jvmti.h>2627#include "jni_tools.h"28#include "nsk_tools.h"29#include "JVMTITools.h"30#include "jvmti_tools.h"3132extern "C" {333435static jvmtiEnv *test_jvmti = NULL;36static jvmtiCapabilities caps;3738/*39* Redefine a class with a new version (class file in byte array).40*41* native static public boolean redefineClassIntl(Class<?> clz, byte[] classFile);42*43* @param clz class for redefinition44* @param classFile new version as a byte array45* @return false if any errors occurred during class redefinition46*/47JNIEXPORT jboolean JNICALL48Java_vm_runtime_defmeth_shared_Util_redefineClassIntl(JNIEnv *env, jclass clazz, jclass clazzToRedefine, jbyteArray bytecodeArray) {49jvmtiClassDefinition classDef;50jboolean result = JNI_TRUE;5152if (!NSK_VERIFY(env != NULL) || !NSK_VERIFY(clazzToRedefine != NULL) || !NSK_VERIFY(bytecodeArray != NULL)) {53return JNI_FALSE;54}5556classDef.klass = clazzToRedefine;57if (!NSK_JNI_VERIFY(env,58(classDef.class_byte_count = /* jsize */ env->GetArrayLength(bytecodeArray)) > 0)) {59return JNI_FALSE;60}6162if (!NSK_JNI_VERIFY(env,63(classDef.class_bytes = (const unsigned char *) /* jbyte* */ env->GetByteArrayElements(bytecodeArray, NULL)) != NULL)) {64return JNI_FALSE;65}6667if (!NSK_JVMTI_VERIFY(68test_jvmti->RedefineClasses(1, &classDef))) {69result = JNI_FALSE;70}7172// Need to cleanup reference to byte[] whether RedefineClasses succeeded or not73if (!NSK_JNI_VERIFY_VOID(env,74env->ReleaseByteArrayElements(bytecodeArray, (jbyte*)classDef.class_bytes, JNI_ABORT))) {75return JNI_FALSE;76}7778return result;79}8081jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {82/* init framework and parse options */83if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))84return JNI_ERR;8586/* create JVMTI environment */87if (!NSK_VERIFY((test_jvmti =88nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))89return JNI_ERR;9091memset(&caps, 0, sizeof(jvmtiCapabilities));92caps.can_redefine_classes = 1;93if (!NSK_JVMTI_VERIFY(test_jvmti->AddCapabilities(&caps)))94return JNI_ERR;9596if (!NSK_JVMTI_VERIFY(test_jvmti->GetCapabilities(&caps)))97return JNI_ERR;9899if (!caps.can_redefine_classes)100printf("Warning: RedefineClasses is not implemented\n");101102return JNI_OK;103}104105JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {106return Agent_Initialize(jvm, options, reserved);107}108109JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {110return Agent_Initialize(jvm, options, reserved);111}112113}114115116