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:
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:
  • 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
The deprecated code was compiled as Deprecated.class, located at /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:


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

Popular posts from this blog

What is Java Synthetic Class (ACC_SYNTHETIC)? with the example of BoundMethodHandle$1.class in Java package java.lang.invoke

Java Native Methods Essentials, from Binary code point of view, with example of java.lang.Runtime

What is Java Bridge Method (ACC_BRIDGE)?