1. 程式人生 > 其它 >帶你掌握不同平臺下,探索JDK原始碼所需的native方法

帶你掌握不同平臺下,探索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

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