I would like to get rid of NullPointerException and other errors using annotations for example there is a class
class A{ public List<@NonNull String> list = new ArrayList();//здесь надо запретить чтобы в коллекцию попадали null значения public Long compute(@Positive Integer num){//здесь разрешить только положит числа // calculation ... return num; } create your annotations
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE_PARAMETER}) public @interface NonNull {} @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.PARAMETER }) public @interface Positive{} ok now in the code i use this class
A a = new A(); String test = null; Integer random = new Random().nextInt(); if ( random < 0 ){ a.compute(random); a.list.add(test); } else{ test = "NonNull"; a.list.add(test); } I understand how using Reflection you can scan class A before initialization and find these annotations in it, but it is not clear what to do next how you can install the check already in the working code ????
if (num.compareTo(0) <= 0) throw new IllegalArgumentException()- Roman Bortnikov