Inspecting annotations of interfaces

For inspecting the annotations of the implemented interfaces, we need to call the getAnnotatedInterfaces() method:

This method returns the interfaces types that are annotated:

Class<Melon> clazz = Melon.class;
AnnotatedType[] interfacesTypes = clazz.getAnnotatedInterfaces();

The returned array printed via Arrays.toString() reveals a single result:

[@modern.challenge.ByWeight() java.lang.Comparable]

Extracting the first interface type can be accomplished as follows:

// interface java.lang.Comparable
System.out.println("First interface type: "
+ interfacesTypes[0].getType());

Moreover, extracting the annotations of the first interface type can be done as follows:

// [@modern.challenge.ByWeight()]
System.out.println("Annotations of the first exception type: "
+ Arrays.toString(interfacesTypes[0].getAnnotations()));

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset