0
public class Investor {

final String  name;
int id ;

public Investor(String name, int id) {
    super();
    this.name = name;
    this.id = id;
}

@MaskName
public  String getName() {
    return name;
}



public int getId() {
    return id;
}

this is my POJO.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MaskName {


}

this is my annotation

what I want to do is :place a mask annotation on Getter method of my pojo and want the getter to return some different value than the object's field , say my investor name is "Mike" , but i have put a MaskName annotation on it , so whenever i would call the getInvestorName() method it should return me a masked value (xxxx) .

How can I achieve this , is it possible to achieve this using custom annotation

  • 3
    Annotations can't do magic. With annotations, if you have a program that uses reflection on the class, it can do things based on the values of the annotation. So if you have a program that invokes that method through reflection, you can tell it to return a different value if it finds that annotation on the method. But if you invoke the method without reflection, the annotations don't "do" anything. – RealSkeptic Jun 22 '17 at 11:58
  • 1
    And there’s no sense in that either. Adding an annotation implies modifying the source code, so if you are able to modify the source code, why not change the method to `public String getName() { return "xxxx"; }` in the first place… – Holger Jun 22 '17 at 12:51

0 Answers0