简体   繁体   中英

Can I inject a annotation into a pojo

Lets say I have a POJO that looks like this.

public class SomeDataClass {

    @SomeProperty1
    String myField1;

    @SomeProperty1
    String myField2;

    ...

    getters()... 
    setters()... // you know the drill

}

I want to be able to synthetically inject an annotation to an instantiated object. The new POJO, if I were to inspect it using reflection, would actually look like this:

public class SomeDataClass {

    @SomeProperty1
    String myField1;

    @SomeProperty1
--> @SomeProperty2 <--- the logic flow would change depending on whether this annotation is present
    String myField2;

    ...

    getters()... 
    setters()... // you know the drill

}

Is this possible? How could I accomplish something like this?

Annotations are just syntactic metadata providers...

so you should get by reflection the name of the declared fields in the given class and use the desired criteria to discriminate which one of those fields needs the info that @SomeProperty2 is providing, after that adding the info from @SomeProperty2 at run time is just a normal use of setter or getters...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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