Annotation and Marker Interface
These two are not related topics
But the intend of the both are the same
Both needs a consumer for it
and it only gives indication to its consumer
like
1-Annotation @Override - the consumer is JVM or the compiler and we "@Override" only provides the indication to JVM
2-Serializable marker interface - the consumer is JVM itself and by implementing this marker interface we only provides indication to JVM
rest is handled by JVM
Ques-
Can we create Annotation and marker interface
Yes we can create both Annotation and marker interface but we also needs to create its consumer
and we cant make JVM as the consumer, we needs to create its own consumer like Java Classes
You can create your own marker interface
1-marker interface
public interface AnyClass {
}
2-The consumer of your marker interface
if (anyObject instanceof AnyClass) {
youGotAWorkToDo((AnyClass) anyObject);
}
The more common practice now is to use annotations to provide the same metadata marker interfaces provide
Now how you will create your marker interface
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Means of each terms used in Annotation
@Documented – Whether to put the annotation in Javadocs
@Retention – When the annotation is needed
@Target? – Places the annotation can go - if you dont specify this annonation can be placed any where
@Inherited – Whether subclasses get the annotation.
RetentionPolicy.SOURCE – Discard during the compile. These annotations don’t make any sense after the compile has completed, so they aren’t written to the bytecode. Examples @Override, @SuppressWarnings
RetentionPolicy.CLASS – Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default.
RetentionPolicy.RUNTIME – Do not discard. The annotation should be available for reflection at runtime. This is what we generally use for our custom annotations.
@Target is inclusive that if you want it for all 7 types of element you need to mentioned for all except 1
elements are -
ElementType.TYPE (class, interface, enum)
ElementType.FIELD (instance variable)
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.ANNOTATION_TYPE (on another annotation)
ElementType.PACKAGE (remember package-info.java)
Annotation only supports primitives, String and Enum - no other data types or object goes inside the Annotation
There are two steps while creating the Annotation
1- Create the Annotation
2- Use the Annotation
3- Create the consumer of the Annotation - use the reflection to provide metadata information
Step -1
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Todo {
public enum Priority {LOW, MEDIUM, HIGH}
String author() default "Aks";
Priority priority() default Priority.LOW;
}
Step -2
@Todo(priority = Todo.Priority.MEDIUM, author = "Ankur")
public void incompleteMethod1() {
//Some business logic is written
//But it’s not complete yet
}
Step -3
Class businessLogicClass = BusinessLogic.class;
for(Method method : businessLogicClass.getMethods()) {
Todo todoAnnotation = (Todo)method.getAnnotation(Todo.class);
if(todoAnnotation != null) {
System.out.println(" Method Name : " + method.getName());
System.out.println(" Author : " + todoAnnotation.author());
System.out.println(" Priority : " + todoAnnotation.priority());
System.out.println(" Status : " + todoAnnotation.status());
}
}
Comments
Post a Comment