Is there a way to use annotations in Java to replace accessors?

前端 未结 7 1113
旧时难觅i
旧时难觅i 2020-12-14 22:50

I\'m a little new to the Java 5 annotations and I\'m curious if either of these are possible:

This annotation would generate a simple getter and setter for you.

7条回答
  •  春和景丽
    2020-12-14 23:24

    It's possible, just not where you're declaring them.

    Check out http://code.google.com/p/javadude/wiki/Annotations.

    I have a class annotated like

    package sample;
    import com.javadude.annotation.Bean;
    import com.javadude.annotation.Property;
    import com.javadude.annotation.PropertyKind; 
    
    @Bean(properties={
        @Property(name="name"),
        @Property(name="phone", bound=true),
        @Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
    })
    public class Person extends PersonGen {}
    

    And it generates the PersonGen class for you, containing getters/setters, etc

    The processor also does quite a bit more. Note that I'm working on a new version of it that has a little API breakage from the current version

提交回复
热议问题