Creating Custom Annotation in Java to force UPPER or lower Case

后端 未结 2 1128
猫巷女王i
猫巷女王i 2021-01-28 03:59

Appreciate any help for creating custom annotation in JAVA by forcing Upper or Lower case for a pojo field. Like to get something like below

// CaseMode enum wou         


        
相关标签:
2条回答
  • 2021-01-28 04:13

    Annotations are passive elements in Java; they can not have any behavior. As @maba said, you can not write any code inside an annotation declaration.

    In order to have something similar to what you are trying to do, you need something which detects the annotation and performs some work. You have a few choices.

    • Compile-time annotation processing
    • Aspect-oriented programming
    • Dynamic proxies / Code generation libraries

    Writing a custom compile-time annotation processor allows you to preprocess the source code before compilation.

    Aspect oriented programming can work both at compile and at run time. It's relatively easy to understand, needs however some tooling to be set up together with your project, depending where you deploy it.

    Dynamic proxies are simple as well, but you need to change the way your code accesses the objects, probably by declaring some interfaces and using those instead of the classes, etc.

    Code generation libraries fall on the complex side of the spectrum, giving lots of flexibility (they are for instance how Hibernate does its magic with your POJO objects).

    0 讨论(0)
  • 2021-01-28 04:24

    Your statement // NEED HELP HERE should be according to the following:

    @Target({ElementType.FIELD, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Case {
        CaseMode value();
    }
    

    If you think that you can have some code in there then you are wrong.

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