JNIEXPORT jclass JNICALL Java_java_lang_ClassLoader_defineClass1(JNIEnv *env, jobject loader, jstring name, jbyteArray data, jint offset, jint length, jobject pd, jstring source, jboolean verify) { jbyte *body; char *utfName; jclass result = 0; char buf[128]; char* utfSource; char sourceBuf[1024]; if (data == NULL) { JNU_ThrowNullPointerException(env, 0); return 0; } /* Work around 4153825. malloc crashes on Solaris when passed a * negative size. */ if (length < 0) { JNU_ThrowArrayIndexOutOfBoundsException(env, 0); return 0; } body = (jbyte *)malloc(length); if (body == 0) { JNU_ThrowOutOfMemoryError(env, 0); return 0; } (*env)->GetByteArrayRegion(env, data, offset, length, body); if ((*env)->ExceptionOccurred(env)) goto free_body; if (name != NULL) { utfName = getUTF(env, name, buf, sizeof(buf)); if (utfName == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); goto free_body; } VerifyFixClassname(utfName); } else { utfName = NULL; } if (source != NULL) { utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf)); if (utfSource == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); goto free_utfName; } } else { utfSource = NULL; } result = JVM_DefineClassWithSourceCond(env, utfName, loader, body, length, pd, utfSource, verify); if (utfSource && utfSource != sourceBuf) free(utfSource); free_utfName: if (utfName && utfName != buf) free(utfName); free_body: free(body); return result; }
/* * Class: java_lang_reflect_Proxy * Method: defineClass0 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BII)Ljava/lang/Class; * * The implementation of this native static method is a copy of that of * the native instance method Java_java_lang_ClassLoader_defineClass0() * with the implicit "this" parameter becoming the "loader" parameter. */ JNIEXPORT jclass JNICALL Java_java_lang_reflect_Proxy_defineClass0(JNIEnv *env, jclass ignore, jobject loader, jstring name, jbyteArray data, jint offset, jint length) { jbyte *body; char *utfName; jclass result = 0; char buf[128]; if (data == NULL) { JNU_ThrowNullPointerException(env, 0); return 0; } /* Work around 4153825. malloc crashes on Solaris when passed a * negative size. */ if (length < 0) { JNU_ThrowArrayIndexOutOfBoundsException(env, 0); return 0; } body = (jbyte *)malloc(length); if (body == 0) { JNU_ThrowOutOfMemoryError(env, 0); return 0; } (*env)->GetByteArrayRegion(env, data, offset, length, body); if ((*env)->ExceptionOccurred(env)) goto free_body; if (name != NULL) { jsize len = (*env)->GetStringUTFLength(env, name); jsize unicode_len = (*env)->GetStringLength(env, name); if (len >= (jsize)sizeof(buf)) { utfName = malloc(len + 1); if (utfName == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); goto free_body; } } else { utfName = buf; } (*env)->GetStringUTFRegion(env, name, 0, unicode_len, utfName); VerifyFixClassname(utfName); } else { utfName = NULL; } result = (*env)->DefineClass(env, utfName, loader, body, length); if (utfName && utfName != buf) free(utfName); free_body: free(body); return result; }
JNIEXPORT jclass JNICALL Java_java_lang_ClassLoader_defineClass2(JNIEnv *env, jobject loader, jstring name, jobject data, jint offset, jint length, jobject pd, jstring source, jboolean verify) { jbyte *body; char *utfName; jclass result = 0; char buf[128]; char* utfSource; char sourceBuf[1024]; assert(data != NULL); // caller fails if data is null. assert(length >= 0); // caller passes ByteBuffer.remaining() for length, so never neg. // caller passes ByteBuffer.position() for offset, and capacity() >= position() + remaining() assert((*env)->GetDirectBufferCapacity(env, data) >= (offset + length)); body = (*env)->GetDirectBufferAddress(env, data); if (body == 0) { JNU_ThrowNullPointerException(env, 0); return 0; } body += offset; if (name != NULL) { utfName = getUTF(env, name, buf, sizeof(buf)); if (utfName == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); return result; } VerifyFixClassname(utfName); } else { utfName = NULL; } if (source != NULL) { utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf)); if (utfSource == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); goto free_utfName; } } else { utfSource = NULL; } result = JVM_DefineClassWithSourceCond(env, utfName, loader, body, length, pd, utfSource, verify); if (utfSource && utfSource != sourceBuf) free(utfSource); free_utfName: if (utfName && utfName != buf) free(utfName); return result; }