-1

In Java we can use custom annotation to validate the fields but apart from this is there any way we can set the value of variable based upon logic using custom annotation in java or spring boot. See example below

@CustomAnnotation
private String name;

if setter method set the value to name is "$123See" our custom annotation automatically modify the value to name "see" by remove some characters or based upon our own logic.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 2
    It would be possible, yes. But to be honest: hiding something like this behind an annotation (and its processor) seems unnecessary complex. Why not have a service that parses the `String` and extracts the value? – Turing85 Dec 19 '21 at 20:46

1 Answers1

0

This is not possible directly and without a background process. Basically, attribute annotations are performed and populated by class analysis.

#1 way: You create a setter method for private attributes and define annotate on it. So, you have to define (example) @Around AOP to your annotation. https://www.baeldung.com/spring-aop-annotation

#2 way: You add your annotation to your attribute and you have to analyse it with Java reflection, direct after setting the attribute or before setting it with another logic. https://www.baeldung.com/java-custom-annotation

(You can write private attr. in reflection temporary: Set private field value with reflection)

Numichi
  • 926
  • 6
  • 14