Skip to content

Experiment with Java annotation processor and annotation retention policy (SOURCE, CLASS, RUNTIME)

Notifications You must be signed in to change notification settings

devbith/source-annotation-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

COMPILE Time @Annotation Processing

Compile the annotation processor class.

javac src/FinalMethodAnnotationProcessor.java src/FinalMethod.java -d classes

Compile the source code with -processor key specifiing the annotation processor class

javac -cp classes/ -processor FinalMethodAnnotationProcessor -d classes src/Sample.java

Experiment

with FinalMethodAnnotationProcessor :
  • Remove the final modifer of close method from the class Sample.java

  • Compile the annoation processor, then compile the source code with preceding command.

with RetentionPolicy
  • Change the retenion policy to (SOURCE, CLASS, RUNTIME) in the class FinalMethod.java.

  • Compile the annotation processor, then compile the source code with preceding command.

  • Run the command java -v classes/Sample then output may vary according to retention policy.

Example:

  • When RetentionPolicy.CLASS the output will be:

    RuntimeInvisibleAnnotations:
    0: #27()
      FinalMethod
Note
Annotations with CLASS retention policy are included in the class file but are not accessible at runtime.
  • When RetentionPolicy.RUNTIME the output will be:

    RuntimeVisibleAnnotations:
      0: #27()
        FinalMethod
Note
Annotations with RUNTIME retention policy are included in the class file and are accessible at runtime via reflection.
  • When RetentionPolicy.SOURCE annotation is not visible in class file.

Note
Annotations with SOURCE retention policy are discarded by the compiler and are not included in the class file.

About

Experiment with Java annotation processor and annotation retention policy (SOURCE, CLASS, RUNTIME)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages