Get the fields name that contain @Size annotation along with their max length

I have this entity -

@Entity
public class Employee{
@Id
@NotNull
@Size(max=5)
private Integer employeeId;

@NotNull
@Size(max=40)
private String employeeName;

private Long employeeSalary;
}

I want to get the name of the fields along with their maximum length allowed. That is, for above case the output should be like

employeeId - 5
employeeName - 40

I have created this following which returns the name of the fields that contain @Size

public boolean hasSize() {
        return Arrays.stream(this.getClass().getDeclaredFields())
                .anyMatch(field -> field.isAnnotationPresent(Size.class));
    }
public List<String> getSizeFields(){
        if(hasSize()) {
            Stream<Field> filter = Arrays.stream(this.getClass().getDeclaredFields())
                    .filter(field -> field.isAnnotationPresent(Size.class));
            return filter.map(obj -> obj.getName()).collect(Collectors.toList());
        }
        else
            return null;
    }

Suggest me how can I get the max length of the fields as well.



Read more here: https://stackoverflow.com/questions/66327324/get-the-fields-name-that-contain-size-annotation-along-with-their-max-length

Content Attribution

This content was originally published by Bhumika at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: