Skip to content

Commit e655a33

Browse files
authored
🎨 #3752 修复 Gson 在 Java 9+ 环境下反射访问 java.io.File#path 字段失败的问题
1 parent 9940546 commit e655a33

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxGsonBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package me.chanjar.weixin.common.util.json;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
46
import com.google.gson.GsonBuilder;
57
import me.chanjar.weixin.common.bean.WxAccessToken;
68
import me.chanjar.weixin.common.bean.WxNetCheckResult;
79
import me.chanjar.weixin.common.bean.menu.WxMenu;
810
import me.chanjar.weixin.common.error.WxError;
911
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
12+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
13+
14+
import java.io.File;
1015
import java.util.Objects;
1116

1217
/**
@@ -25,6 +30,17 @@ public class WxGsonBuilder {
2530
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxMediaUploadResultAdapter());
2631
INSTANCE.registerTypeAdapter(WxNetCheckResult.class, new WxNetCheckResultGsonAdapter());
2732

33+
INSTANCE.setExclusionStrategies(new ExclusionStrategy() {
34+
@Override
35+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
36+
return false;
37+
}
38+
39+
@Override
40+
public boolean shouldSkipClass(Class<?> aClass) {
41+
return aClass == File.class || aClass == ApacheHttpClientBuilder.class;
42+
}
43+
});
2844
}
2945

3046
public static Gson create() {

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package me.chanjar.weixin.cp.util.json;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
46
import com.google.gson.GsonBuilder;
57
import me.chanjar.weixin.common.bean.menu.WxMenu;
68
import me.chanjar.weixin.common.error.WxError;
9+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
710
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
811
import me.chanjar.weixin.cp.bean.WxCpChat;
912
import me.chanjar.weixin.cp.bean.WxCpDepart;
1013
import me.chanjar.weixin.cp.bean.WxCpTag;
1114
import me.chanjar.weixin.cp.bean.WxCpUser;
1215
import me.chanjar.weixin.cp.bean.kf.WxCpKfGetCorpStatisticResp;
1316

17+
import java.io.File;
1418
import java.util.Objects;
1519

1620
/**
@@ -32,6 +36,18 @@ public class WxCpGsonBuilder {
3236
INSTANCE.registerTypeAdapter(WxMenu.class, new WxCpMenuGsonAdapter());
3337
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter());
3438
INSTANCE.registerTypeAdapter(WxCpKfGetCorpStatisticResp.StatisticList.class, new StatisticListAdapter());
39+
40+
INSTANCE.setExclusionStrategies(new ExclusionStrategy() {
41+
@Override
42+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
43+
return false;
44+
}
45+
46+
@Override
47+
public boolean shouldSkipClass(Class<?> aClass) {
48+
return aClass == File.class || aClass == ApacheHttpClientBuilder.class;
49+
}
50+
});
3551
}
3652

3753
/**

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/json/WxMaGsonBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
1010
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
1111
import cn.binarywang.wx.miniapp.json.adaptor.*;
12+
import com.google.gson.ExclusionStrategy;
13+
import com.google.gson.FieldAttributes;
1214
import com.google.gson.Gson;
1315
import com.google.gson.GsonBuilder;
16+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
17+
18+
import java.io.File;
1419
import java.util.Objects;
1520

1621
/**
@@ -30,6 +35,18 @@ public class WxMaGsonBuilder {
3035
INSTANCE.registerTypeAdapter(WxMaRetainInfo.class, new WxMaRetainInfoGsonAdapter());
3136
INSTANCE.registerTypeAdapter(WxMaUserPortrait.class, new WxMaUserPortraitGsonAdapter());
3237
INSTANCE.registerTypeAdapter(WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson.class, new WxMaSubscribeMsgEventJsonAdapter());
38+
39+
INSTANCE.setExclusionStrategies(new ExclusionStrategy() {
40+
@Override
41+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
42+
return false;
43+
}
44+
45+
@Override
46+
public boolean shouldSkipClass(Class<?> aClass) {
47+
return aClass == File.class || aClass == ApacheHttpClientBuilder.class;
48+
}
49+
});
3350
}
3451

3552
public static Gson create() {

weixin-java-open/src/main/java/me/chanjar/weixin/open/util/json/WxOpenGsonBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package me.chanjar.weixin.open.util.json;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
46
import com.google.gson.GsonBuilder;
7+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
58
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
69
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
710
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
811
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo;
912
import me.chanjar.weixin.open.bean.result.*;
13+
14+
import java.io.File;
1015
import java.util.Objects;
1116

1217
/**
@@ -27,6 +32,18 @@ public class WxOpenGsonBuilder {
2732
INSTANCE.registerTypeAdapter(WxOpenAuthorizerInfoResult.class, new WxOpenAuthorizerInfoResultGsonAdapter());
2833
INSTANCE.registerTypeAdapter(WxOpenAuthorizerOptionResult.class, new WxOpenAuthorizerOptionResultGsonAdapter());
2934
INSTANCE.registerTypeAdapter(WxOpenAuthorizerListResult.class, new WxOpenAuthorizerListResultGsonAdapter());
35+
36+
INSTANCE.setExclusionStrategies(new ExclusionStrategy() {
37+
@Override
38+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
39+
return false;
40+
}
41+
42+
@Override
43+
public boolean shouldSkipClass(Class<?> aClass) {
44+
return aClass == File.class || aClass == ApacheHttpClientBuilder.class;
45+
}
46+
});
3047
}
3148

3249
public static Gson create() {

weixin-java-qidian/src/main/java/me/chanjar/weixin/qidian/util/json/WxQidianGsonBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package me.chanjar.weixin.qidian.util.json;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
46
import com.google.gson.GsonBuilder;
7+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
8+
9+
import java.io.File;
510

611
/**
712
* @author someone
@@ -12,6 +17,18 @@ public class WxQidianGsonBuilder {
1217

1318
static {
1419
INSTANCE.disableHtmlEscaping();
20+
21+
INSTANCE.setExclusionStrategies(new ExclusionStrategy() {
22+
@Override
23+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
24+
return false;
25+
}
26+
27+
@Override
28+
public boolean shouldSkipClass(Class<?> aClass) {
29+
return aClass == File.class || aClass == ApacheHttpClientBuilder.class;
30+
}
31+
});
1532
}
1633

1734
public static Gson create() {

0 commit comments

Comments
 (0)