简体   繁体   中英

How to use lombok's @Delegate annotations in Java

I would like to use lombok's @Delegate annotations in the my code. Please check code snippet below and it throws an error saying: getAge() is already defined:

public interface I {
    String getName();
    int getAge();
}

@Data
public class Vo {
    private String name;
    private long age;
}

@AllArgsConstructor
public class Adapter implements I {

    @Delegate(types = I.class)
    private Vo vo;

    //I want to use my own code here,Because vo.getAge() returns a long,But I.getAge() expects a int
    public int getAge(){
        return (int) vo.getAge();
    }
}

From the lombok documentation :

To have very precise control over what is delegated and what isn't, write private inner interfaces with method signatures, then specify these private inner interfaces as types in

@Delegate(types=PrivateInnerInterfaceWithIncludesList.class, excludes=SameForExcludes.class).

Which means that to include everything in I , but exclude only getAge , you can declare an extra inner interface like this:

private interface Exclude {
    int getAge();
}

and pass it to exclude :

@Delegate(types = I.class, excludes = Exclude.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM