Creating Custom annotation to modify attributes in an object

Viewed 335

I want to create a class level annotation that will check each and every attribute in class and based on type of the attribute it should do some defined modification, Like below

@Entity
@Getter @Setter
@Table(name = "BASIC", schema = "AXLES")
public class CoAxle {

    private int cAx;
    private String cAxOrigin;
    private int pAx;
    private String pAxOrigin;

}

here in above code snippet I have to perform few Operation, like if the attribute is Integer and attribute value is Negative then it should return 0 instead negative number, and if the type of Attribute is String then it just have to trim the String and return Trimmed String.

So far I have tried to achieve this using AttributeConverter interface Like below

@Converter
public class StringTrimConverter implements AttributeConverter<String, String> {
    @Override
    public String convertToDatabaseColumn(String attribute) {
        return attribute;
    }
    @Override
    public String convertToEntityAttribute(String dbData) {
        return dbData.trim();
    }
}

@Entity
@Table(name = "ViewAddress")
public class PostalAddress extends DbObject { 
    @Convert(converter = StringTrimConverter.class)
    private String street;
    @Convert(converter = StringTrimConverter.class)
    private String number;
    (...)

But the challenge here is I have to mention Annotation on top of each attribute, and in my code the count of attribute will increase to 100 or more, so I want to avoid such repetitive annotation, instead if I can declare on class level annotation that can just check the class and its attribute and type of attribute and modify accordingly like Getter annotation in Project Lombok

0 Answers
Related