帶你掌握不同平臺下,探索JDK源碼所需的native方法

摘要:要探索JDK的核心底層源碼,那必須掌握native用法。文章中會以“獲取系統的默認時區”爲例,介紹說明如何查看native對應方法的源碼。

本文分享自華爲雲社區《要探索JDK的核心底層源碼,那必須掌握native用法》,作者: 小虛竹 。

場景

有探索欲的同學,應該會跟我一樣,在看JDK源碼時,跟到最後,會出現native方法,類似下面這個方法

 /**
     * Gets the platform defined TimeZone ID.
     **/
    private static native String getSystemTimeZoneID(String javaHome);

看到這個native ,說明已經挖到核心了,到了這一步,還是不清楚是怎麼獲取系統的默認時區的,那怎麼辦,JDK代碼只能跟到這裏。

轉戰OpenJDK,源碼下載方式:https://gitee.com/mirrors/openjdk

什麼是native

native是一個計算機函數,一個Native Method就是一個Java調用非Java代碼的接口。方法的實現由非Java語言實現,比如C或C++。

native的源碼怎麼看呢

以**private static native String getSystemTimeZoneID(String javaHome)**爲例

getSystemTimeZoneID方法所在的package java.util.TimeZone;

如圖所示,找到TimeZone.c下的getSystemTimeZoneID方法

/*
 * Gets the platform defined TimeZone ID
 */
JNIEXPORT jstring JNICALL
Java_java_util_TimeZone_getSystemTimeZoneID(JNIEnv *env, jclass ign,
                                            jstring java_home, jstring country)
{
    const char *cname;
    const char *java_home_dir;
    char *javaTZ;

    if (java_home == NULL)
        return NULL;

    java_home_dir = JNU_GetStringPlatformChars(env, java_home, 0);
    if (java_home_dir == NULL)
        return NULL;

    if (country != NULL) {
        cname = JNU_GetStringPlatformChars(env, country, 0);
        /* ignore error cases for cname */
    } else {
        cname = NULL;
    }

    /*
     * Invoke platform dependent mapping function
     */
    javaTZ = findJavaTZ_md(java_home_dir, cname);

    free((void *)java_home_dir);
    if (cname != NULL) {
        free((void *)cname);
    }

    if (javaTZ != NULL) {
        jstring jstrJavaTZ = JNU_NewStringPlatform(env, javaTZ);
        free((void *)javaTZ);
        return jstrJavaTZ;
    }
    return NULL;
}

重點:調用不同平臺相關的映射函數

  /*
     * Invoke platform dependent mapping function
     */
    javaTZ = findJavaTZ_md(java_home_dir, cname);

去查找findJavaTZ_md方法時,發現存在分別在solaris和windows兩個目錄下。

查了下這兩個目錄的差別:

因爲OpenJDK裏,Java標準庫和部分工具的源碼repo(jdk目錄)裏,BSD和Linux的平臺相關源碼都是在solaris目錄裏的。
原本Sun JDK的源碼裏平臺相關的目錄就是從solaris和windows這兩個目錄開始的,後來Unix系的平臺相關代碼全都放在solaris目錄下了,共用大部分代碼。

作者:RednaxelaFX
鏈接:https://www.zhihu.com/question/58982441/answer/170264788
來源:知乎

簡單的理解就是:

  • window系統下,使用windows目錄下編譯的JDK代碼
  • unix系的平臺下,使用solaris目錄下編譯的JDK代碼

瞭解不同系統下findJavaTZ_md方法執行

windows系統

/*
 * Detects the platform time zone which maps to a Java time zone ID.
 */
char *findJavaTZ_md(const char *java_home_dir, const char *country)
{
    char winZoneName[MAX_ZONE_CHAR];
    char winMapID[MAX_MAPID_LENGTH];
    char *std_timezone = NULL;
    int  result;

    winMapID[0] = 0;
    result = getWinTimeZone(winZoneName, winMapID);

    if (result != VALUE_UNKNOWN) {
        if (result == VALUE_GMTOFFSET) {
            std_timezone = _strdup(winZoneName);
        } else {
            std_timezone = matchJavaTZ(java_home_dir, result,
                                       winZoneName, winMapID, country);
        }
    }

    return std_timezone;
}

註釋寫得很清楚,獲取“Time Zones”註冊表中的當前時區

/*
 * Gets the current time zone entry in the "Time Zones" registry.
 */
static int getWinTimeZone(char *winZoneName, char *winMapID)
{
...
}

時區的設置方式:

那時區上的選擇值是從哪取到的,上面有說了,是在註冊表中取值

打開註冊表 :Regedit–>

計算機\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\

unix系的平臺

findJavaTz_md()方法的註釋上寫得很清楚了:將平臺時區ID映射爲Java時區ID

/*
 * findJavaTZ_md() maps platform time zone ID to Java time zone ID
 * using <java_home>/lib/tzmappings. If the TZ value is not found, it
 * trys some libc implementation dependent mappings. If it still
 * can't map to a Java time zone ID, it falls back to the GMT+/-hh:mm
 * form. `country', which can be null, is not used for UNIX platforms.
 */
/*ARGSUSED1*/
char *
findJavaTZ_md(const char *java_home_dir, const char *country)
{
    char *tz;
    char *javatz = NULL;
    char *freetz = NULL;

    tz = getenv("TZ");

#ifdef __linux__
    if (tz == NULL) {
#else
#ifdef __solaris__
    if (tz == NULL || *tz == '\0') {
#endif
#endif
        tz = getPlatformTimeZoneID();
        freetz = tz;
    }

    /*
     * Remove any preceding ':'
     */
    if (tz != NULL && *tz == ':') {
        tz++;
    }

#ifdef __solaris__
    if (strcmp(tz, "localtime") == 0) {
        tz = getSolarisDefaultZoneID();
        freetz = tz;
    }
#endif

    if (tz != NULL) {
#ifdef __linux__
        /*
         * Ignore "posix/" prefix.
         */
        if (strncmp(tz, "posix/", 6) == 0) {
            tz += 6;
        }
#endif
        javatz = strdup(tz);
        if (freetz != NULL) {
            free((void *) freetz);
        }
    }
    return javatz;
}

步驟:

1、使用< Java home>/lib/tzmappings,。如果沒有找到"TZ"變量,就進行第2步

2、 tz = getPlatformTimeZoneID(); 執行Linux特定的映射,如果找到,返回一個時區ID,否則返回null

【Linux】Centos7修改系統時區timezone方式:

timedatectl

修改時區

timedatectl  set-timezone Asia/Shanghai

3、對比/etc/localtime與"/usr/share/zoneinfo目錄下的文件,如果一致,就返回時區ID,沒有則到第4步

4、返回到GMT

 

點擊關注,第一時間瞭解華爲雲新鮮技術~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章