rem: jbyteArray newBytes = (jbyteArray) jni->CallStaticObjectMethod(clp, clpm, jni->NewStringUTF("org.codehaus.aspectwerkz.hook.impl.ClassLoaderPreProcessorImpl"));
add: jclass sysC = jni->FindClass("java/lang/System"); jmethodID sysM = jni->GetStaticMethodID(sysC, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;"); jstring ppName = (jstring) jni->CallStaticObjectMethod(sysC, sysM, jni->NewStringUTF("aspectwerkz.classloader.clpreprocessor")); jbyteArray newBytes; if (ppName != NULL) { fprintf(stdout, "AspectWerkz> using ClassLoaderPreProcessor %s\n", jni->GetStringUTFChars(ppName, JNI_FALSE)); newBytes = (jbyteArray) jni->CallStaticObjectMethod(clp, clpm, ppName); } else { newBytes = (jbyteArray) jni->CallStaticObjectMethod(clp, clpm, jni->NewStringUTF("org.codehaus.aspectwerkz.hook.impl.ClassLoaderPreProcessorImpl")); }
context: JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *jvm, char *options, void *reserved) {	fprintf(stdout, "AspectWerkz> initializing .....\n", options);	//*********************************************************************	// prepare the JVMPI	if ((jvm->GetEnv((void **)&jvmpi_interface, JVMPI_VERSION_1)) < 0) {		fprintf(stderr, "AspectWerkz> JVMPI prepare failed\n");		return JNI_ERR;	} 	// initialize jvmpi interface with the function notifyEvent	jvmpi_interface->NotifyEvent = notifyEvent;	// debug - enable class load event notification	//jvmpi_interface->EnableEvent(JVMPI_EVENT_CLASS_LOAD, NULL);	// debug - enable class load hook	//jvmpi_interface->EnableEvent(JVMPI_EVENT_CLASS_LOAD_HOOK, NULL);		// debug VM init	//jvmpi_interface->EnableEvent(JVMPI_EVENT_JVM_INIT_DONE, NULL);		//*********************************************************************	// prepare the JNI	JNIEnv *jni;	if ((jvm->GetEnv((void**)&jni, JNI_VERSION_1_1)) < 0) {		fprintf(stderr, "AspectWerkz> JNI prepare failed\n");		return JNI_ERR;	}	/*if ((jvm->AttachCurrentThread((void **)&jni, NULL)) < 0) {		fprintf(stderr, "AspectWerkz> JNI prepare failed\n");		return JNI_ERR;	}*/	//*********************************************************************	// prepare the JVMDI	JVMDI_Interface_1 *jvmdi;	jint res = jvm->GetEnv((void**)&jvmdi, JVMDI_VERSION_1);	if (res < 0) {		fprintf(stderr, "AspectWerkz> JVMDI prepare failed [code %d %d]\n", res, JVMDI_ERROR_ACCESS_DENIED);		return JNI_ERR;	}	/*if ( ! jvmdi::enabled() ) {		fprintf(stderr, "AspectWerkz> JVMDI NOT enabled");		return JNI_ERR;	}*/		/*  Thread* thread = ThreadLocalStorage::thread();  if (thread != NULL && thread->is_Java_thread()) {    if (Threads::is_supported_jni_version_including_1_1(version)) {      *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment();      return JNI_OK;    } else if (version == JVMPI_VERSION_1 || version == JVMPI_VERSION_1_1) {      *penv = (void* )jvmpi::GetInterface_1(version);	// version 1.X support      return JNI_OK;     } else if (version == JVMDI_VERSION_1) {      if (jvmdi::enabled()) {        *penv = (void *)jvmdi::GetInterface_1(vm);        return JNI_OK;      } else {        return JVMDI_ERROR_ACCESS_DENIED;      }    } else {      *penv = NULL;      return JNI_EVERSION;    }  } else {    *penv = NULL;    return JNI_EDETACHED;  }	*/					// debug - prints the JVMDI version	//jint version;	//jvmdiError err = jvmdi->GetVersionNumber(&version);	//fprintf(stderr, "JVMDI> %d\n", version);	// check for HotSwap capabilities without schema change	JVMDI_capabilities capabilities;	jvmdi->GetCapabilities(&capabilities);	if (capabilities.can_redefine_classes != 1) {		fprintf(stderr, "AspectWerkz> HotSwap not supported by JVM\n");		return JNI_ERR;	}	JVMDI_class_definition target;	target.clazz = jni->FindClass("java/lang/ClassLoader");	if (options != NULL) {		fprintf(stdout, "AspectWerkz> loading bytecode from file %s\n", options);		FILE *fin = fopen(options, "rb");		if (fin == NULL) {			fprintf(stderr, "AspectWerkz> could not read boot file %s\n", options);			return JNI_ERR;		}		unsigned char data[MAX];		int data_len = fread(data, sizeof(unsigned char), MAX, fin);		//fprintf(stdout, "JVMDI> read %d\n", data_len);		fclose(fin);			target.class_bytes = (jbyte*)data;		target.class_byte_count = data_len;	} else {		jthrowable ex = NULL;		jclass clp = jni->FindClass("org/codehaus/aspectwerkz/hook/ClassLoaderPatcher");		if (jni->ExceptionOccurred() != NULL) {			fprintf(stderr, "AspectWerkz> error in JNI call:\n");			jni->ExceptionDescribe();			return JNI_ERR;		}		jmethodID clpm = jni->GetStaticMethodID(clp, "getPatchedClassLoader", "(Ljava/lang/String;)[B");		if (jni->ExceptionOccurred() != NULL) {			fprintf(stderr, "AspectWerkz> error in JNI call:\n");			jni->ExceptionDescribe();			return JNI_ERR;		}		jbyteArray newBytes = (jbyteArray) jni->CallStaticObjectMethod(clp, clpm, jni->NewStringUTF("org.codehaus.aspectwerkz.hook.impl.ClassLoaderPreProcessorImpl"));		target.class_bytes = jni->GetByteArrayElements(newBytes, JNI_FALSE);		target.class_byte_count = jni->GetArrayLength(newBytes);	}		// HotSwap	jvmdiError err = jvmdi->RedefineClasses(1, &target);	if (err == JVMDI_ERROR_NONE) {		fprintf(stdout, "AspectWerkz> ..... done.\n");	} else {		fprintf(stderr, "AspectWerkz> HotSwap failed [code %d]\n", err);		return JNI_ERR;	}		fflush(stdout);	fflush(stderr);	return JNI_OK;}