Annotations are type of metadata ( data that provides information about other data) provide data about a program that is not part of the program itself. They don’t have any effect on operations of the program.

implemented two custom annotation one for Class level named ClassLevelAnnotation and another one is Field level named FieldLevelAnnotation

1
2
3
4
5
6
7
8
9
	@Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @interface ClassLevelAnnotation {
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @interface FieldLevelAnnotation {
    }

Retention:

It declares the availability of the annotation. if its given RetentionPolicy.RUNTIME that means it will be available in the runtime. If no retention policy is there then it takes the default retention policy which is RetentionPolicy.CLASS that means the annotation will be stored in the class file but not be available at the runtime.

Target:

It gives the option to declare which java elements the custom annotation can be used to annotate. ElementType.TYPE means it targets a class type and can be used to annotate class. ElementType.FIELD means it targets a class’s field and can be used to annotate it.

Here in this code there is another type of annotation that is ElementType.ANNOTATION_TYPE which is used to annotate another annotation. For example in the implementation the @Target and @Retention

Now the annotations are placed on a class

1
2
3
4
5
    @ClassLevelAnnotation
    static class AnotherClass {
        @FieldLevelAnnotation
        public int firstNumber;
    }

now in the main class there are two methods implemented to check if the class is annotated with a class level annotation and a field is annotated with a field level annotation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15

class Day42 {
    public static void main(String[] args) throws NoSuchFieldException {
        System.out.println(isAnnotatedClass(AnotherClass.class));
        System.out.println(isAnnotatedField(AnotherClass.class,"firstNumber"));
    }
}

private static boolean isAnnotatedField(Class<?> clasz, String fieldName) throws NoSuchFieldException {
      return clasz.getDeclaredField(fieldName).isAnnotationPresent(FieldLevelAnnotation.class);
    }

private static boolean isAnnotatedClass(Class<?> clasz) {
      return clasz.isAnnotationPresent(ClassLevelAnnotation.class);
    }

in isAnnotatedField() it takes a class and the fieldName that needs to be checked if it had annotation on it. isAnnotatedClass() takes a class and checks if any annotations are present on that class using the java.lang.Class