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

前端 未结 7 1109
旧时难觅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:21

    There is project named OVal and i think it does what you want. http://oval.sourceforge.net/

    If i remeber right for advance thing AspectJ is needed, however simple check work without AspectJ. check it out.

    There is also Hibernate Validator, check it out too :P

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 23:32

    Although they are sometimes useful, if you are seriously missing attributes then you are probably a little unsteady on the whole OO design thing.

    The idea of OO Design is that methods are "Actions" where you are asking the object to do something for you--not just setting or getting a value--that's not an object, it's a structure.

    If you think that the lack of attributes is a serious problem, I highly recommend that you try coding for a while without ever using setters and resisting getters.

    It might be hard at first, but I bet at the end you'll have a firmer grasp on OO design principles.

    (Note: There are rare situations where you must do "setter" type operations after the construction of an object--most commonly when you must create two objects that refer to each other. In this case I recommend something like the builder pattern where your setters must be called once and are protected from being called twice)

    0 讨论(0)
  • 2020-12-14 23:33

    Annotation processing occurs on the abstract syntax tree. This is a structure that the parser creates and the compiler manipulates.

    The current specification (link to come) says that annotation processors cannot alter the abstract syntax tree. One of the consequences of this is that it is not suitable to do code generation.

    If you'd like this sort of functionality, then have a look at XDoclet. This should give you the code generation preprocessing I think you are looking for.

    For your @NonNull example, JSR-305 is a set of annotations to enhance software defect detection, and includes @NonNull and @CheckForNull and a host of others.

    Edit: Project Lombok solves exactly the problem of getter and setter generation.

    0 讨论(0)
  • 2020-12-14 23:36

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

    In short, no, the Java 5/6 compiler does not support this and it would be difficult for third parties to add such support in a compiler-agnostic manner.

    To get a better handle on annotations, I'd start with JUnit. If you write code for versions 3 (pre-annotations) and 4 (annotation-based), you quickly get a handle on how the framework replaced a contract based on naming patterns with one that was annotation-based.

    For a more dramatic example, compare EJB 2 with EJB 3.

    0 讨论(0)
  • 2020-12-14 23:38

    Getters/Setters: Yes, it is possible. The Project Lombok (http://projectlombok.org/index.html) defines annotations for generating getters/setters and more.

    So for example

    @lombok.Data;
    public class Person {
       private final String name;
       private int age;
    }
    

    Will generate getName (no setter since the variable is final) and getAge/setAge. It will also generate equals, hashCode, toString and aconstructor initializing the required fields (name in this case). Adding @AllArgsConstructor would generate a constructor initializing both fields. Using @Value instead of @Data will make the objects immutable.

    There are other annotations and parameters giving you control over access rights (should your getter be protected or public), names (getName() or name()?), etc. And there is more. For example, I really like the annotations for builders and extension methods.

    Lombok is very easy to use:

    • Just download the jar and use the annotations, the automatically generated getter/setters can be used in your code without actually being spelled out.
    • The annotations are used only during compilation not during runtime, so you don't distribute lombok with your jar's.
    • Most IDE's support this, so that you see the getter/setter in code completion, navigation, etc. In Netbeans this works out of the box. In IntelliJ IDEA use the Lombok plugin.

    NotNull: This is supported by findbugs and IDEA IDE, maybe others

    0 讨论(0)
提交回复
热议问题