Lombok 使用教程-@Delegate | 将其他类方法注入到当前类

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

一、实验性功能说明

@Delegate是在lombok v0.10中作为特性引入的(当时实验包还不存在)。

lombok v1.14中,它被移到了实验包中;lombok主包中的旧版本现在已被废弃。

实验因为:

  • 不常用。
  • 难以支持边缘情况,如递归委托。
  • API相当不友好;如果你能简单地实现一些方法,并让@Delegate为你没有手动实现的东西生成委托,那就更好了,但由于泛型擦除的问题,这也无法做到没有注意事项。

当前状态:否定-目前我们认为该功能不会很快脱离实验状态,如果未来版本的javacecj难以继续维护该功能,则可能会放弃对该功能的支持。

二、简介

任何字段或无参数方法都可以用@Delegate来注解,让lombok生成转发对这个字段的调用(或调用这个方法的结果)的委托方法。

Lombok委托该字段类型(或方法的返回类型)的所有公共方法,以及其父类型的方法,但所有以java.lang.Object声明的方法除外。

你可以在@Delegate注解的类型参数中传递任意数量的类。如果你这样做,那么lombok将委托这些类型(以及它们的父类型,除了java.lang.Object)中的所有公共方法,而不是看字段/方法的类型。

所有属于计算类型的公共非Object方法都被复制,无论你是否也为这些方法写了实现。因此,这将导致重复的方法错误。你可以通过使用@Delegate(excludes=SomeType.class)参数来排除被排除类型中的所有公共方法,以及它们的超类型来避免这些错误。

为了非常精确地控制哪些被委托,哪些不被委托,可以用方法签名编写私有内部接口,然后在@Delegate(types=PrivateInnerInterfaceWithIncludesList.class, excludes=SameForExcludes.class)中指定这些私有内部接口作为类型。

三、示例比较

1. Lombok 写法

    import java.util.ArrayList;
    import java.util.Collection;
    
    import lombok.experimental.Delegate;
    
    public class DelegationExample {
      private interface SimpleCollection {
        boolean add(String item);
        boolean remove(Object item);
      }
      
      @Delegate(types=SimpleCollection.class)
      private final Collection<String> collection = new ArrayList<String>();
    }
    
    
    class ExcludesDelegateExample {
      long counter = 0L;
      
      private interface Add {
        boolean add(String x);
        boolean addAll(Collection<? extends String> x);
      }
      
      @Delegate(excludes=Add.class)
      private final Collection<String> collection = new ArrayList<String>();
      
      public boolean add(String item) {
        counter++;
        return collection.add(item);
      }
      
      public boolean addAll(Collection<? extends String> col) {
        counter += col.size();
        return collection.addAll(col);
      }
    }

2. Java 标准写法

    import java.util.ArrayList;
    import java.util.Collection;
    
    public class DelegationExample {
      private interface SimpleCollection {
        boolean add(String item);
        boolean remove(Object item);
      }
      
      private final Collection<String> collection = new ArrayList<String>();
      
      @java.lang.SuppressWarnings("all")
      public boolean add(final java.lang.String item) {
        return this.collection.add(item);
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean remove(final java.lang.Object item) {
        return this.collection.remove(item);
      }
    }
    
    class ExcludesDelegateExample {
      long counter = 0L;
      
      private interface Add {
        boolean add(String x);
        boolean addAll(Collection<? extends String> x);
      }
      
      private final Collection<String> collection = new ArrayList<String>();
      
      public boolean add(String item) {
        counter++;
        return collection.add(item);
      }
      
      public boolean addAll(Collection<? extends String> col) {
        counter += col.size();
        return collection.addAll(col);
      }
      
      @java.lang.SuppressWarnings("all")
      public int size() {
        return this.collection.size();
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean isEmpty() {
        return this.collection.isEmpty();
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean contains(final java.lang.Object arg0) {
        return this.collection.contains(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public java.util.Iterator<java.lang.String> iterator() {
        return this.collection.iterator();
      }
      
      @java.lang.SuppressWarnings("all")
      public java.lang.Object[] toArray() {
        return this.collection.toArray();
      }
      
      @java.lang.SuppressWarnings("all")
      public <T extends .java.lang.Object>T[] toArray(final T[] arg0) {
        return this.collection.<T>toArray(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean remove(final java.lang.Object arg0) {
        return this.collection.remove(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean containsAll(final java.util.Collection<?> arg0) {
        return this.collection.containsAll(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean removeAll(final java.util.Collection<?> arg0) {
        return this.collection.removeAll(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public boolean retainAll(final java.util.Collection<?> arg0) {
        return this.collection.retainAll(arg0);
      }
      
      @java.lang.SuppressWarnings("all")
      public void clear() {
        this.collection.clear();
      }
    }

四、支持的配置项

lombok.delegate.flagUsage = [warning | error] (默认: not set)
Lombok@Delegate的任何用法标记为警告或错误(如果已配置)。

五、附属说明

当向注解的typestypes参数传递类时,你不能包括泛型。这是java的一个限制。使用私有的内部接口或扩展预定类型的类,包括泛型参数来解决这个问题。

当向注解传递类时,这些类不需要是字段的超类型。请看例子

@Delegate不能用在静态字段或方法上。

当要委托/排除的计算类型本身包含@Delegate注释时,不能使用@Delegate;换句话说,如果你试图递归使用它,@Delegate会出错。

参考文献

【1】@Delegate | Don’t lose your composition.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值