Java Annotation Essentials, from Binary code point of view, with example of java.lang.Deprecated
Java Annotations provides a mechanism to document and extend the code in Java programming language. Like we can annotation an API as deprecated, mapping the Database table and field to Java class and fields, define the REST API access points, asking JVM to do injections, etc.
Binary Code is always the best document.
Let's take an example from OpenJDK of the very popular annotation java.lang.Deprecated over the Java method java.lang.Class.newInstance() method.
The Deprecated annotation is a widely used in Java source code, to a Java Object has been deprecated, and it could be removed in later release.
Here is the source code of the Deprecated annotation:
The Deprecated type has the following annotations:
From the screen shot bellow, we can validate the source code defined elements are included in the Deprecated.class:
The java.lang.Class.newInstance() method is tagged with the Deprecated annotation. Here is the source code of the method:
The @Deprecated(since="9") annotaion means the method Class.newInstance() method was deprecated since Java 9.
Here is the Class.class file, located at
The Java system property java.version contains the Java platform version:
So, this is how the Java annoation works in binary world.
Related Links
Binary Code is always the best document.
Let's take an example from OpenJDK of the very popular annotation java.lang.Deprecated over the Java method java.lang.Class.newInstance() method.
The Deprecated annotation is a widely used in Java source code, to a Java Object has been deprecated, and it could be removed in later release.
Here is the source code of the Deprecated annotation:
80 @Documented
81 @Retention(RetentionPolicy.RUNTIME)
82 @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
83 public @interface Deprecated {
93 String since() default "";
102 boolean forRemoval() default false;
103 }
The Deprecated type has the following annotations:
- @Documented means the Deprecated annotation could exist in the javadoc of the Java object it annotated to
- @Retention(RetentionPolicy.RUNTIME) means the Deprecated annotation is to be recorded in the class file by the compiler and retained by the VM at run time
- @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE}) means the Deprecated annotation can be tagged on a Java constructor, field, local variable, method, package, module, parameter or class/interface ype
- since() adds a parameter to Deprecated annotation to indicate the Java version in which the annotation element became deprecated
- forRemoval() adds a parameter to Deprecated annotation to indicate whether annotated element is subject to removal in a future version
/usr/lib/jvm/default-java/jmods/java.base.jmod/classes/java/lang/Deprecated.class
for Ubuntu Linux for example.From the screen shot bellow, we can validate the source code defined elements are included in the Deprecated.class:
- These two methods since() and forRemoval() are defined in the methods section of the classfile
- These three annotation @Documented, @Retention and @Target are defined in the attributes section of RuntimeVisibleAnnotations.
java.lang.Deprecated.class on OpenJDK, via Java Class Viewer |
The java.lang.Class.newInstance() method is tagged with the Deprecated annotation. Here is the source code of the method:
569 @Deprecated(since="9")
570 public T newInstance()
571 throws InstantiationException, IllegalAccessException
572 {
573 ...
614 }
The @Deprecated(since="9") annotaion means the method Class.newInstance() method was deprecated since Java 9.
Here is the Class.class file, located at
/usr/lib/jvm/default-java/jmods/java.base.jmod/classes/java/lang/Class.class
for Ubuntu Linux for example. We can validate the @Deprecated annoation did contained in this binary .class file for Java Virtual Machine to handle, as requried by @Retention(RetentionPolicy.RUNTIME) definiton.The Java system property java.version contains the Java platform version:
- When java compiler javac found the current java.version is higher than 9, it may show an warning saying this API has been deprecatedn and please do not call it; the same warning could be shown in Java IDE like Eclipse, IntelliJ IDEA, or Apache NetBeans.
So, this is how the Java annoation works in binary world.
Related Links
Comments
Post a Comment