How do I create a simple annotation?

Metadata is a way to add some supplement information to the source code. This information is called annotation will not change how the program runs. This metadata can be used by other tools such as source code generator for instance to generate additional code at the runtime. Or it will be used by a dependency injection framework such as the Spring Framework.

The annotation can be attached to classes, methods, etc. To create an annotation we use the interface keyword and add an @ symbol in front of it. The @ symbol will tell the compiler that it is an annotation.

So now let us see the code for a simple annotation, a HelloAnnotation.

package org.kodejava.lang.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface HelloAnnotation {
    String value();

    String greetTo();
}

All annotations extend the java.lang.annotation.Annotation interface, which means that java.lang.annotation.Annotation is the super-interface of all annotations

An annotation need to have a RetentionPolicy that will be the scope of the annotation where at this point the annotation will be ignored or discarded. The values are RetentionPolicy.SOURCE, RetentionPolicy.CLASS and RetentionPolicy.RUNTIME. When no retention policy defined it will use the default retention policy which is the RetentionPolicy.CLASS.

Annotation with RetentionPolicy.SOURCE retention policy will be retained only in the source code, it is available to the compiler when it compiles the class and will be discarded after that. The RetentionPolicy.CLASS retention policy will make the annotation stored in the class file during compilation, but will not available during the runtime. And the RetentionPolicy.RUNTIME retention policy will store the annotation in the class file during compilation, and it is also available to JVM at runtime.

In the example above you also see that the HelloAnnotation have two members value() and greetTo(). Annotations only have method declaration in it with no implementation body.

What is @SuppressWarnings annotation?

The @SuppressWarnings annotation tells the compiler to suppress the warning messages it normally shows during compilation time. It has some level of suppression to be added to the code, these level including: all, deprecation, fallthrough, finally, path, serial and unchecked.

package org.kodejava.basic;

import java.util.Calendar;
import java.util.Date;

public class SuppressWarningsExample {
    @SuppressWarnings(value={"deprecation"})
    public static void main(String[] args) {
        Date date = new Date(2021, Calendar.OCTOBER, 3);

        System.out.println("date = " + date);
    }
}

In the example above if we don’t use @SuppressWarnings annotation the compiler will report that the constructor of the Date class called above has been deprecated.

How do I use @Override annotation?

We use the @Override annotation as part of method declaration. The @Override annotation is used when we want to override methods and want to make sure have overridden the correct methods.

As the annotation name we know that there should be the same method signature in the parent class to override. That means using this annotation let us know earlier when we are mistakenly override method that doesn’t exist in the base class.

package org.kodejava.basic;

public class OverrideExample {
    private String field;
    private String attribute;

    @Override
    public int hashCode() {
        return field.hashCode() + attribute.hashCode();
    }

    @Override
    public String toString() {
        return field + " " + attribute;
    }
}

How do I mark method as @deprecated?

To mark a method as deprecated we can use the JavaDoc @deprecated tag. This is what we did since the beginning of Java. But when a new metadata support introduced to the Java language we can also use annotation. The annotation for marking method as deprecated is @Depreated.

The difference between these two that the @deprecated is place in the JavaDoc comment block while the @Deprecated is placed as a source code element.

package org.kodejava.basic;

import java.util.Date;
import java.util.Calendar;

public class DeprecatedExample {
    public static void main(String[] args) {
        DeprecatedExample de = new DeprecatedExample();
        de.getDate();
        System.out.println(de.getMonthFromDate());
    }

    /**
     * Get current system date.
     *
     * @return current system date.
     * @deprecated This method will be removed in the near future.
     */
    @Deprecated
    public Date getDate() {
        return new Date();
    }

    public int getMonthFromDate() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }
}