十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

创新互联-专业网站定制、快速模板网站建设、高性价比定襄网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式定襄网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖定襄地区。费用合理售后完善,十余年实体公司更值得信赖。
目前公司项目中主要采用Hutool作为项目的工具包,相对于google的guava, hutool的工具类采用中文注释,更加符合国人使用。所谓知己知彼,我们需要了解Hutool都具有什么样的功能,才能够最大化发挥它的价值。
本文主要就hutool 5.8.8版本中MapProxy的使用。
其实Map在get的时候是比较危险的,你可能不知道它是什么类型,需要进行强制,举个例子如下:
@Test
public void testMapProxy1() {
MapuserMap = MapUtil.newHashMap(16); 
userMap.put("username", "alvin");
userMap.put("age", 20);
// 使用map的时候, 需要进行强转,一旦类型错误,会报错
String age = (String)userMap.get("age");
}
运行结果:
那有什么更好的解决方案吗?Hutool提供了一种解决方案给我们。
依赖引入
cn.hutool 
hutool-all 
5.8.8 
定义一个可访问接口
interface MapUser {
    String getUsername();
    Integer getAge();
    MapUser setAge(Integer age);
}
通过MapProxy访问
@Test
public void testMapProxy2() {
MapuserMap = 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);
// 通过代理的方式
MapUser mapUser = mapProxy.toProxyBean(MapUser.class);
// 后续访问会变的更加安全
Assert.assertTrue(mapUser.getAge() == 20);
mapUser.setAge(30);
Assert.assertTrue(mapUser.getAge() == 30);
}
Map代理,提供各种getXXX方法,并提供默认值支持,它的类结构图如下:
mapProxy.getInt("age", 18)。publicT toProxyBean(Class interfaceClass) { 
return (T) Proxy.newProxyInstance(ClassLoaderUtil.getClassLoader(), new Class>[]{interfaceClass}, this);
}
public Object invoke(Object proxy, Method method, Object[] args) {
    final Class>[] parameterTypes = method.getParameterTypes();
    // 如果调用方法参数为空
    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)) {
                    // 驼峰不存在转下划线尝试
                    fieldName = StrUtil.toUnderlineCase(fieldName);
                }
                return Convert.convert(method.getGenericReturnType(), this.get(fieldName));
            }
        }
        // 如果方法参数不为空
    } 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();
                // 判断返回类型是不是代理类的实例
                if(returnType.isInstance(proxy)){
                    return proxy;
                }
            }
        } else if ("equals".equals(methodName)) {
            return this.equals(args[0]);
        }
    }
    throw new UnsupportedOperationException(method.toGenericString());
}
本文主要讲解了Hutool中的MapProxy类的使用,希望对大家有帮助。