Having a field, we can fetch its annotations via getDeclaredAnnotations():
Here it is the code:
Class<Melon> clazz = Melon.class;
Field weightField = clazz.getDeclaredField("weight");
Annotation[] fieldAnnotations = weightField.getDeclaredAnnotations();
Getting the value of the @Unit annotation can be done as follows:
Unit unitFieldAnnotation = (Unit) fieldAnnotations[0];
System.out.println("@Unit value: " + unitFieldAnnotation.value());
Or, use the getDeclaredAnnotation() method to fetch the right type directly:
Unit unitFieldAnnotation
= weightField.getDeclaredAnnotation(Unit.class);