简体   繁体   English

Java:Lombok和unwrapping属性

[英]Java: Lombok and unwrapping properties

I am working with JavaFx properties and Lombok 我正在使用JavaFx属性和Lombok

I started recently using Lombok, it makes my code much simpler and readable, but I have the issue with JavaFx properties, it doesn't unwrap them like I would generate them with IntelliJ I get a getter for the property itself and a getter for the value. 我最近开始使用Lombok,它使我的代码更简单和可读,但我有JavaFx属性的问题,它没有解开它们就像我用IntelliJ生成它我得到了属性本身的getter和一个getter for the值。 Here is a simple example with explanation what I want to do. 这是一个简单的例子,解释了我想做什么。

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;

    }

}

I know that I can use like: model.getStringProperty().get() to obtain the String value, but I would prefer the direct way if it exists. 我知道我可以使用: model.getStringProperty().get()来获取String值,但我更喜欢直接的方式,如果它存在。

Does any solution exist for this? 这有什么解决方案吗?

I've found a way: 我找到了一种方法:

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;
        }
    }
}

With @Accessor annotation you can manipulate your getter name, while @Delegate gives you a delegation pattern. 使用@Accessor注释,您可以操纵您的getter名称,而@Delegate为您提供委托模式。 You can find more here and here . 你可以在这里这里找到更多。 However notice two things: first, those annotations are marked as "experimental" by Lombok team. 但请注意两件事:首先,这些注释被Lombok团队标记为“实验性”。 Second, to me this is a quite confusing API, so use it carefully. 其次,对我来说这是一个非常混乱的API,所以要小心使用它。

If this solution is too complicated, I would suggest to adopt only @Accessor and creating your own delegate method: 如果这个解决方案过于复杂,我建议只采用@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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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