偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

開發(fā)利器Hutool之MapProxy的妙用

開發(fā) 前端
目前公司項(xiàng)目中主要采用Hutool作為項(xiàng)目的工具包,相對(duì)于google的guava, hutool的工具類采用中文注釋,更加符合國(guó)人使用。所謂知己知彼,我們需要了解Hutool都具有什么樣的功能,才能夠最大化發(fā)揮它的價(jià)值。

概述

Hutool是一個(gè)小而全的Java工具類庫(kù),通過(guò)靜態(tài)方法封裝,降低相關(guān)API的學(xué)習(xí)成本,提高工作效率,使Java擁有函數(shù)式語(yǔ)言般的優(yōu)雅,讓Java語(yǔ)言也可以“甜甜的”。

目前公司項(xiàng)目中主要采用Hutool作為項(xiàng)目的工具包,相對(duì)于google的guava, hutool的工具類采用中文注釋,更加符合國(guó)人使用。所謂知己知彼,我們需要了解Hutool都具有什么樣的功能,才能夠最大化發(fā)揮它的價(jià)值。

本文主要就hutool 5.8.8版本中MapProxy的使用。

場(chǎng)景引入

其實(shí)Map在get的時(shí)候是比較危險(xiǎn)的,你可能不知道它是什么類型,需要進(jìn)行強(qiáng)制,舉個(gè)例子如下:

@Test
public void testMapProxy1() {
Map<String, Object> userMap = MapUtil.newHashMap(16);
userMap.put("username", "alvin");
userMap.put("age", 20);

// 使用map的時(shí)候, 需要進(jìn)行強(qiáng)轉(zhuǎn),一旦類型錯(cuò)誤,會(huì)報(bào)錯(cuò)
String age = (String)userMap.get("age");
}

運(yùn)行結(jié)果:

圖片

那有什么更好的解決方案嗎?Hutool提供了一種解決方案給我們。

MapProxy使用

依賴引入

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>

定義一個(gè)可訪問(wèn)接口

interface MapUser {
String getUsername();

Integer getAge();

MapUser setAge(Integer age);
}

通過(guò)MapProxy訪問(wèn)

@Test
public void testMapProxy2() {
Map<String, Object> userMap = MapUtil.newHashMap(16);
userMap.put("username", "alvin");
userMap.put("age", 20);

MapProxy mapProxy = MapProxy.create(userMap);
Integer age = mapProxy.getInt("age", 18);
Assert.assertTrue(age == 20);

// 通過(guò)代理的方式
MapUser mapUser = mapProxy.toProxyBean(MapUser.class);
// 后續(xù)訪問(wèn)會(huì)變的更加安全
Assert.assertTrue(mapUser.getAge() == 20);

mapUser.setAge(30);
Assert.assertTrue(mapUser.getAge() == 30);
}

MapProxy源碼解析

Map代理,提供各種getXXX方法,并提供默認(rèn)值支持,它的類結(jié)構(gòu)圖如下:

圖片

  • 實(shí)現(xiàn)了OptNullBasicTypeFromObjectGetter接口, 提供了基本類型的get, 在不提供默認(rèn)值的情況下, 如果值不存在或獲取錯(cuò)誤,返回null, 比如:mapProxy.getInt("age", 18)。
  • 實(shí)現(xiàn)了InvocationHandler接口,支持jdk的動(dòng)態(tài)代理,生成代理對(duì)象。
public <T> T toProxyBean(Class<T> interfaceClass) {
return (T) Proxy.newProxyInstance(ClassLoaderUtil.getClassLoader(), new Class<?>[]{interfaceClass}, this);
}
  • toProxyBean方法就是生成代理對(duì)象,最終會(huì)調(diào)用代理類的invoke方法,這里的代理類就是MapProxy本身。
public Object invoke(Object proxy, Method method, Object[] args) {
final Class<?>[] parameterTypes = method.getParameterTypes();
// 如果調(diào)用方法參數(shù)為空
if (ArrayUtil.isEmpty(parameterTypes)) {
final Class<?> returnType = method.getReturnType();
// 方法返回值不是void
if (void.class != returnType) {
// 匹配Getter
final String methodName = method.getName();
String fieldName = null;
if (methodName.startsWith("get")) {
// 匹配getXXX
fieldName = StrUtil.removePreAndLowerFirst(methodName, 3);
} else if (BooleanUtil.isBoolean(returnType) && methodName.startsWith("is")) {
// 匹配isXXX
fieldName = StrUtil.removePreAndLowerFirst(methodName, 2);
}else if ("hashCode".equals(methodName)) {
return this.hashCode();
} else if ("toString".equals(methodName)) {
return this.toString();
}

if (StrUtil.isNotBlank(fieldName)) {
if (false == this.containsKey(fieldName)) {
// 駝峰不存在轉(zhuǎn)下劃線嘗試
fieldName = StrUtil.toUnderlineCase(fieldName);
}
return Convert.convert(method.getGenericReturnType(), this.get(fieldName));
}
}

// 如果方法參數(shù)不為空
} else if (1 == parameterTypes.length) {
// 匹配Setter
final String methodName = method.getName();
if (methodName.startsWith("set")) {
final String fieldName = StrUtil.removePreAndLowerFirst(methodName, 3);
if (StrUtil.isNotBlank(fieldName)) {
this.put(fieldName, args[0]);
final Class<?> returnType = method.getReturnType();
// 判斷返回類型是不是代理類的實(shí)例
if(returnType.isInstance(proxy)){
return proxy;
}
}
} else if ("equals".equals(methodName)) {
return this.equals(args[0]);
}
}

throw new UnsupportedOperationException(method.toGenericString());
}

總結(jié)

本文主要講解了Hutool中的MapProxy類的使用,希望對(duì)大家有幫助。

責(zé)任編輯:武曉燕 來(lái)源: JAVA旭陽(yáng)
相關(guān)推薦

2019-10-18 16:05:32

框架開發(fā)Java

2022-09-21 08:16:18

緩存框架

2024-03-19 07:00:00

C++編程pragma

2022-10-26 09:57:52

VectorRustC++

2018-11-26 07:04:59

神經(jīng)網(wǎng)絡(luò)優(yōu)化函數(shù)

2023-07-19 12:24:48

C++constexpr?語(yǔ)句

2023-10-16 16:05:44

PythonPyCharm編程語(yǔ)言

2021-09-02 07:04:44

Go 開發(fā)利器

2010-03-10 17:57:54

Python編程語(yǔ)言

2012-06-25 17:21:15

天天記事

2011-11-14 09:17:14

Linux運(yùn)維ClusterShel

2024-03-05 10:41:51

Rollup前端開發(fā)

2024-03-22 09:45:34

大型語(yǔ)言模型Unity引擎游戲開發(fā)

2024-04-25 08:22:43

AndroidlargeHeap屬性

2021-12-09 06:59:24

FlinkSQL 開發(fā)

2023-09-28 21:39:26

HutoolJava工具包

2010-09-08 16:26:26

SQL循環(huán)語(yǔ)句

2023-08-01 09:46:57

虛擬鍵盤API

2021-08-30 10:19:05

PyFlink 開發(fā)環(huán)境Zeppelin No

2010-09-26 09:50:36

SQL Where子句
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)