Java 没有 getter/setter/toString/constructors 的清晰代码的 Lombok 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21741166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lombok alternatives for clear code without getters/setters/toString/constructors
提问by
Do you know any Lombok alternatives ?
Using Lombok we can forget about messing classes with getters and setters and toString
. I want to use it in my project, however I wonder if there are any better alternatives?
你知道龙目岛的任何替代品吗?使用 Lombok 我们可以忘记使用 getter 和 setter 以及toString
. 我想在我的项目中使用它,但是我想知道是否有更好的选择?
I am using Java 1.7 and cannot change to 1.8.
我使用的是 Java 1.7,无法更改为 1.8。
回答by Louis Wasserman
"Better" is fairly contentious here, but one alternative is AutoValue, available here. This presentationexplains some of the differences between AutoValue and e.g. Lombok.
“更好”在这里相当有争议,但另一种选择是AutoValue,可在此处获得。 本演示文稿解释了 AutoValue 和例如 Lombok 之间的一些差异。
In particular, AutoValue emphasizes immutability (a good thing, generally!) and a minimum of extralinguistic magic (very different from Lombok).
特别是,AutoValue 强调不变性(通常是一件好事!)和最少的超语言魔法(与 Lombok 非常不同)。
回答by maaartinus
I'm afraid, there's no alternative unless you want to switch to something like Scalaor are happy with a smaller set of features like those provided by AutoValue.
恐怕别无选择,除非您想切换到Scala 之类的东西,或者对AutoValue提供的较小功能集感到满意。
While AutoValue
is probably the best you can get with pure Java, it offers
虽然AutoValue
可能是使用纯 Java 所能获得的最好的结果,但它提供了
- @Getter
- @AllArgsConstructor
- @EqualsAndHashCode
- @ToString
- @Builder
- @Getter
- @AllArgsConstructor
- @EqualsAndHashCode
- @ToString
- @Builder
but it misses
但它错过了
- @Wither
- toBuilder
- @Setter
- @Delegate
- @ExtensionMethod
- and some more features I don't use.
- @枯萎
- 建造者
- @Setter
- @代表
- @扩展方法
- 还有一些我不使用的功能。
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
虽然我强烈同意不变性是一种美德,但它有时并不适用。无论如何,Lombok 努力支持不变性,它甚至集成了 Guava 的不可变集合,你可以写
@Builder @Getter public final class Sentence {
private final boolean truthValue;
@Singular private final ImmutableList<String> words;
}
and use it like
并像使用它一样
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.
注意:我不是作者,所以我可以说它很酷。
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:
对于immutables,@Wither
并toBuilder
很酷。前者允许您创建一个与单个字段不同的副本,后者为您提供一个从当前值开始并适合更改多个字段的构建器。以下两行是等效的:
o.withA(1).withB(2)
o.toBuilder().a(1).b(2).build()
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.
龙目岛和AutoValue
使用一些魔法。后者的神奇之处在于标准的注释处理,所以它非常健壮。它有一些缺点,如第 27 页所列。我要补充一个事实,即生成了一些我没有订购的 AutoValue_foo。
Lombok uses some black magic and thus is much more fragile, but it offers more and works pretty well.
Lombok 使用了一些黑魔法,因此更加脆弱,但它提供了更多功能并且效果很好。