要探索JDK的核心底层源码,那必须掌握native用法 场景

小知识,大挑战!本文正在参与“ 程序员必备小知识 ”创作活动

本文同时参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金

❤️作者简介:Java领域优质创作者🏆,CSDN博客专家认证🏆,华为云享专家认证🏆
❤️技术活,该赏
❤️点赞 👍 收藏 ⭐再看,养成习惯

场景

有探索欲的同学,应该会跟我一样,在看JDK源码时,跟到最后,会出现native方法,类似下面这个方法

1
2
3
4
java复制代码 /**
* Gets the platform defined TimeZone ID.
**/
private static native String getSystemTimeZoneID(String javaHome);

看到这个native ,说明已经挖到核心了,到了这一步,还是不清楚是怎么获取系统的默认时区的,那怎么办,JDK代码只能跟到这里。

转战OpenJDK,源码下载方式:gitee.com/mirrors/ope…

什么是native

native是一个计算机函数,一个Native Method就是一个Java调用非Java代码的接口。方法的实现由非Java语言实现,比如C或C++。

native的源码怎么看呢

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

1
go复制代码getSystemTimeZoneID方法所在的package java.util.TimeZone;

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

image-20210706233905997

image-20210706234052425

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
c复制代码/*
* 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;
}

重点:调用不同平台相关的映射函数

1
2
3
4
scss复制代码  /*
* Invoke platform dependent mapping function
*/
javaTZ = findJavaTZ_md(java_home_dir, cname);

去查找findJavaTZ_md方法时,发现存在分别在solaris和windows两个目录下。

image-20210706234905448

查了下这两个目录的差别:

1
2
3
4
5
6
bash复制代码因为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系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
c复制代码/*
* 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”注册表中的当前时区

1
2
3
4
5
6
7
c复制代码/*
* Gets the current time zone entry in the "Time Zones" registry.
*/
static int getWinTimeZone(char *winZoneName, char *winMapID)
{
...
}

时区的设置方式:

image-202107086550950

那时区上的选择值是从哪取到的,上面有说了,是在注册表中取值

打开注册表 :Regedit–>

1
css复制代码计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\

unix系的平台

findJavaTz_md()方法的注释上写得很清楚了:将平台时区ID映射为Java时区ID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
c复制代码/*
* 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方式:

1
复制代码timedatectl

image-202107086455780

修改时区

1
arduino复制代码timedatectl  set-timezone Asia/Shanghai

image-2021070864438866

3、对比/etc/localtime与”/usr/share/zoneinfo目录下的文件,如果一致,就返回时区ID,没有则到第4步

4、返回到GMT

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%