Java: Lombok 和展开属性
Java: Lombok and unwrapping properties
我正在使用 JavaFx 属性和 Lombok
我最近开始使用 Lombok,它使我的代码更加简单和可读,但是我遇到了 JavaFx 属性的问题,它不会像我用 IntelliJ 生成它们那样解开它们我得到一个 getter对于 属性 本身和 getter 作为值。这是一个简单的例子,解释了我想做什么。
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // returns the StringProperty instead of String
model.stringProperty(); // doesn't exist -> doesn't compile
// Expectation:
// model.getStringProperty() <- return the String that is stringProperty.get()
// model.stringProperty() <- return the StringProperty itself
}
@Getter
private static class Model{
private StringProperty stringProperty;
}
}
我知道我可以使用 like: model.getStringProperty().get()
来获取 String
值,但如果它存在,我更喜欢直接的方式。
有解决办法吗?
我找到了一个方法:
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // <- return the String that is stringProperty.getStringProperty()
model.stringProperty(); // <- return the StringProperty itself
}
private static class Model{
private interface DelegateExample {
String getStringProperty();
}
@Accessors(fluent = true)
@Getter
@Delegate(types = DelegateExample.class)
private StringProperty stringProperty = new StringProperty();
}
private static class StringProperty {
String property = "p";
public String getStringProperty(){
return property;
}
}
}
使用 @Accessor
注释,您可以操纵您的 getter 名称,而 @Delegate
为您提供委托模式。您可以找到更多 here and here。但是请注意两件事:首先,这些注释被 Lombok 团队标记为 "experimental"。其次,对我来说这是一个相当混乱的API,所以请谨慎使用它。
如果这个解决方案太复杂,我建议只采用 @Accessor
并创建自己的委托方法:
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // <- return the String that is stringProperty.get()
model.stringProperty(); // <- return the StringProperty itself
}
private static class Model{
@Accessors(fluent = true)
@Getter
private StringProperty stringProperty;
public String getStringProperty(){
return stringProperty.get();
}
}
}
我正在使用 JavaFx 属性和 Lombok
我最近开始使用 Lombok,它使我的代码更加简单和可读,但是我遇到了 JavaFx 属性的问题,它不会像我用 IntelliJ 生成它们那样解开它们我得到一个 getter对于 属性 本身和 getter 作为值。这是一个简单的例子,解释了我想做什么。
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // returns the StringProperty instead of String
model.stringProperty(); // doesn't exist -> doesn't compile
// Expectation:
// model.getStringProperty() <- return the String that is stringProperty.get()
// model.stringProperty() <- return the StringProperty itself
}
@Getter
private static class Model{
private StringProperty stringProperty;
}
}
我知道我可以使用 like: model.getStringProperty().get()
来获取 String
值,但如果它存在,我更喜欢直接的方式。
有解决办法吗?
我找到了一个方法:
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // <- return the String that is stringProperty.getStringProperty()
model.stringProperty(); // <- return the StringProperty itself
}
private static class Model{
private interface DelegateExample {
String getStringProperty();
}
@Accessors(fluent = true)
@Getter
@Delegate(types = DelegateExample.class)
private StringProperty stringProperty = new StringProperty();
}
private static class StringProperty {
String property = "p";
public String getStringProperty(){
return property;
}
}
}
使用 @Accessor
注释,您可以操纵您的 getter 名称,而 @Delegate
为您提供委托模式。您可以找到更多 here and here。但是请注意两件事:首先,这些注释被 Lombok 团队标记为 "experimental"。其次,对我来说这是一个相当混乱的API,所以请谨慎使用它。
如果这个解决方案太复杂,我建议只采用 @Accessor
并创建自己的委托方法:
public class LombokAndProperties {
public static void main(String[] args) {
Model model = new Model();
model.getStringProperty(); // <- return the String that is stringProperty.get()
model.stringProperty(); // <- return the StringProperty itself
}
private static class Model{
@Accessors(fluent = true)
@Getter
private StringProperty stringProperty;
public String getStringProperty(){
return stringProperty.get();
}
}
}