Top


定義場所(file name)

jdk/src/share/native/java/lang/ClassLoader.c

名前(function name)

JNIEXPORT jclass JNICALL
Java_java_lang_ClassLoader_defineClass1(JNIEnv *env,
                                        jobject loader,
                                        jstring name,
                                        jbyteArray data,
                                        jint offset,
                                        jint length,
                                        jobject pd,
                                        jstring source)
{

本体部(body)

  {- -------------------------------------------
  (1) (変数宣言など)
      ---------------------------------------- -}

        jbyte *body;
        char *utfName;
        jclass result = 0;
        char buf[128];
        char* utfSource;
        char sourceBuf[1024];

  {- -------------------------------------------
  (1) data 引数が不正な場合は NullPointerException.
      ---------------------------------------- -}

        if (data == NULL) {
            JNU_ThrowNullPointerException(env, 0);
            return 0;
        }

  {- -------------------------------------------
  (1) length 引数が不正な場合は ArrayIndexOutOfBoundsException.
      ---------------------------------------- -}

        /* Work around 4153825. malloc crashes on Solaris when passed a
         * negative size.
         */
        if (length < 0) {
            JNU_ThrowArrayIndexOutOfBoundsException(env, 0);
            return 0;
        }

  {- -------------------------------------------
  (1) 作業用のバッファの確保.
      (なお, 確保できなかった場合は OutOfMemoryError)
      ---------------------------------------- -}

        body = (jbyte *)malloc(length);

        if (body == 0) {
            JNU_ThrowOutOfMemoryError(env, 0);
            return 0;
        }

  {- -------------------------------------------
  (1) JNI の GetByteArrayRegion() により, data 引数のデータを作業用バッファ(body)にコピーする.
      (例外が出た場合は free_body までジャンプ)
      ---------------------------------------- -}

        (*env)->GetByteArrayRegion(env, data, offset, length, body);

        if ((*env)->ExceptionOccurred(env))
            goto free_body;

  {- -------------------------------------------
  (1) utfName 局所変数を初期化
      (なお, 確保できなかった場合は OutOfMemoryError)
      ---------------------------------------- -}

        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;
        }

  {- -------------------------------------------
  (1) utfSource 局所変数を初期化
      (なお, 確保できなかった場合は OutOfMemoryError)
      ---------------------------------------- -}

        if (source != NULL) {
            utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf));
            if (utfSource == NULL) {
                JNU_ThrowOutOfMemoryError(env, NULL);
                goto free_utfName;
            }
        } else {
            utfSource = NULL;
        }

  {- -------------------------------------------
  (1) JVM_DefineClassWithSource() を呼んで, クラスのロード処理を行う.
      ---------------------------------------- -}

        result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource);

  {- -------------------------------------------
  (1) 確保したメモリを解放しておく.
      ---------------------------------------- -}

        if (utfSource && utfSource != sourceBuf)
            free(utfSource);

     free_utfName:
        if (utfName && utfName != buf)
            free(utfName);

     free_body:
        free(body);

  {- -------------------------------------------
  (1) 結果をリターン
      ---------------------------------------- -}

        return result;
    }

This document is available under the GNU GENERAL PUBLIC LICENSE Version 2.