Lombok替代清除代码而没有getter / setter / toString / constructors [英] Lombok alternatives for clear code without getters/setters/toString/constructors

查看:148
本文介绍了Lombok替代清除代码而没有getter / setter / toString / constructors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道任何龙目岛的替代品吗?
使用Lombok我们可以忘记使用getter和setter以及toString来搞乱这个类,我想在我的项目中使用它但是我想知道是否还有更好的选择?

Do you know any Lombok alternatives ? Using Lombok we can forget about messing class with getters and setters and toString, I want use it in my project however I wonder if there are any better alternatives?

我使用的是Java 1.7 ..并且无法更改为1.8。

I am using Java 1.7.. and cannot change to 1.8.

推荐答案

我担心,除非你想要,否则别无选择切换到 Scala 之类的内容,或者对 AutoValue

I'm afraid, there's no alternative unless you want to switch to something like Scala or are happy with a smaller set of features like those provided by AutoValue.

虽然 AutoValue 可能是纯Java最好的,它提供

While AutoValue is probably the best you can get with pure Java, it offers


  • @Getter

  • @AllArgsConstructor

  • @EqualsAndHashCode

  • @ToString

  • @Builder

  • @Getter
  • @AllArgsConstructor
  • @EqualsAndHashCode
  • @ToString
  • @Builder

但它错过了


  • @Wither

  • toBuilder

  • @Setter

  • @Deleg ate

  • @ExtensionMethod

  • 以及我不使用的更多功能。

  • @Wither
  • toBuilder
  • @Setter
  • @Delegate
  • @ExtensionMethod
  • and some more features I don't use.

虽然我非常同意不变性是一种美德,但它有时不适用。无论如何,龙目岛努力支持不变性,它甚至与Guava的不可变集合集成,你可以写

While I strongly agree that immutability is a virtue, it sometimes isn't applicable. Anyway, Lombok tries hard to support immutability, it even integrates with Guava's immutable collections, and you can write

@Builder @Getter public final class Sentence {
    private final boolean truthValue;
    @Singular private final ImmutableList<String> words;    
}

并使用它

Sentence s = Sentence.builder().truthValue(true)
    .word("Lombok").word("is").word("cool").build();
assertEquals(3, s.getWords().size());

注意:我不是作者,所以我可以说它很酷。

Note: I'm not the author, so I can say it's cool.

对于不可变项, @Wither toBuilder 非常酷。前者允许您创建一个不同于单个字段的副本,后者为您提供一个以当前值开头并适合更改多个字段的构建器。以下两行是等价的:

For immutables, @Wither and toBuilder are pretty cool. The former allows you to create a copy differing by a single field and the latter gives you a builder starting with the current values and suitable for changing multiple fields. The following two lines are equivalent:

o.withA(1).withB(2)
o.toBuilder().a(1).b(2).build()






Lombok和 AutoValue 都使用了一些魔法。后者的神奇之处在于标准的注释处理,所以它非常强大。它有一些缺点,如第27页中所列。我添加了一些事实,我生成了一些我没有订购的AutoValue_foo。


Both Lombok and AutoValue use some magic. The magic of the latter is the standard annotation processing, so it's pretty robust. It has some disadvantages as listed on page 27. I'd add the fact that some AutoValue_foo gets generated which I didn't order.

龙目岛使用了一些黑魔法因此更加脆弱,但它提供更多和效果很好。

Lombok uses some black magic and thus is much more fragile, but it offers more and works pretty well.

这篇关于Lombok替代清除代码而没有getter / setter / toString / constructors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆