How to apply Single annotation on multiple variables?

前端 未结 2 1596
野性不改
野性不改 2021-01-21 14:09

I am rookie in Java Annotation and have been searching for applying single annotation on multiple variable simultaneously.

Code:



        
2条回答
  •  青春惊慌失措
    2021-01-21 14:26

    @NotNull annotation can be applied at element not at group of elements.

    JavaDoc: The annotated element must not be null. Accepts any type.

    If you really want to get away with boiler plate code, you can use frameworks like Lombok which can help you to certain extent.

    Link : http://projectlombok.org/features/Data.html

    OR you can use reflection to validate all the method.

    for (Field f : obj.getClass().getDeclaredFields()) {
      f.setAccessible(true); // optional
      if (f.get(obj) == null) {
         f.set(obj, getDefaultValueForType(f.getType()));
         // OR throw error
      }
    }
    

提交回复
热议问题