How do I obtain annotations at runtime using reflection?

This example demonstrate how to obtain annotations of a class and methods. We use the reflection API to get class and method information from where we can read information about annotation attached to the class or the method.

package org.kodejava.lang.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@HelloAnnotation(value = "Hello", greetTo = "Universe")
public class GettingAnnotation {
    public static void main(String[] args) {
        GettingAnnotation demo = new GettingAnnotation();

        Class<? extends GettingAnnotation> clazz = demo.getClass();
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("Annotation Type: " + annotation.annotationType());
        }

        HelloAnnotation annotation = clazz.getAnnotation(HelloAnnotation.class);
        System.out.println("Value  : " + annotation.value());
        System.out.println("GreetTo: " + annotation.greetTo());

        try {
            Method m = clazz.getMethod("sayHi");

            annotation = m.getAnnotation(HelloAnnotation.class);
            System.out.println("Value  : " + annotation.value());
            System.out.println("GreetTo: " + annotation.greetTo());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        demo.sayHello();
    }

    @HelloAnnotation(value = "Hi", greetTo = "Alice")
    public void sayHi() {
    }

    @HelloAnnotation(value = "Hello", greetTo = "Bob")
    public void sayHello() {
        try {
            Method method = getClass().getMethod("sayHello");
            HelloAnnotation annotation = method.getAnnotation(HelloAnnotation.class);

            System.out.println(annotation.value() + " " + annotation.greetTo());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

You can find the HelloAnnotation annotation that we use above on the following example: How do I create a simple annotation?.

The result of our program is:

Annotation Type: interface org.kodejava.lang.annotation.HelloAnnotation
Value  : Hello
GreetTo: Universe
Value  : Hi
GreetTo: Alice
Hello Bob

How do I annotate a class or method?

This example show you how to use the HelloAnnotation annotation on the previous example code, How do I create a simple annotation?. We add the HelloAnnotation annotation to our class and its methods.

package org.kodejava.lang.annotation;

@HelloAnnotation(value = "Good Morning", greetTo = "Universe")
public class HelloAnnotationExample {
    public static void main(String[] args) {
        HelloAnnotationExample hello = new HelloAnnotationExample();
        hello.sayHi();
        hello.sayHello();
    }

    @HelloAnnotation(value = "Hi there", greetTo = "Alice")
    private void sayHi() {
    }

    @HelloAnnotation(value = "Hello there", greetTo = "Bob")
    private void sayHello() {
    }
}

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.

How do I get the name of current executed method?

package org.kodejava.lang;

public class GetCurrentMethodName {
    public static void main(String[] args) {
        // Get the current executing method name
        String methodName =
                Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println("methodName = " + methodName);

        GetCurrentMethodName obj = new GetCurrentMethodName();
        obj.executeAMethod();
    }

    private void executeAMethod() {
        // Get the current executing method name
        String methodName =
                Thread.currentThread().getStackTrace()[1].getMethodName();
        System.out.println("methodName = " + methodName);
    }
}

This program will print out the following string:

methodName = main
methodName = executeAMethod

How do I execute external command and obtain the result?

This example demonstrate how to execute an external command from Java and obtain the result of the command. Here we simply execute a Linux ls -al command on the current working directory and display the result.

package org.kodejava.lang;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcessResult {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("ls -al");

            // Wait for this process to finish or terminated
            process.waitFor();

            // Get process exit value
            int exitValue = process.exitValue();
            System.out.println("exitValue = " + exitValue);

            // Read the result of the ls -al command by reading the
            // process's input stream
            InputStreamReader is = new InputStreamReader(process.getInputStream());
            BufferedReader reader = new BufferedReader(is);
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Below is our program result:

exitValue = 0
total 64
drwxr-xr-x  20 wsaryada  staff   680 Aug  7 14:13 .
drwxr-xr-x   4 wsaryada  staff   136 Jun 18 00:33 ..
-rw-r--r--@  1 wsaryada  staff  6148 Jun 29 14:04 .DS_Store
-rw-r--r--   1 wsaryada  staff   267 May 19 20:47 .editorconfig
drwxr-xr-x  16 wsaryada  staff   544 Aug 10 08:34 .git
-rw-r--r--   1 wsaryada  staff  1535 May 19 20:47 .gitignore
drwxr-xr-x  15 wsaryada  staff   510 Aug 10 08:34 .idea
-rw-r--r--   1 wsaryada  staff  1313 May 19 20:47 LICENSE
-rw-r--r--   1 wsaryada  staff   101 May 19 20:47 README.md
drwxr-xr-x   5 wsaryada  staff   170 Jul 28 23:11 kodejava-basic
drwxr-xr-x   6 wsaryada  staff   204 Jun 30 14:22 kodejava-commons
drwxr-xr-x   6 wsaryada  staff   204 Jul 27 15:32 kodejava-lang-package
drwxr-xr-x@  5 wsaryada  staff   170 Jun 16 20:49 kodejava-mail
drwxr-xr-x   6 wsaryada  staff   204 Aug  7 17:22 kodejava-mybatis
drwxr-xr-x   5 wsaryada  staff   170 Jul 20 10:36 kodejava-poi
-rw-r--r--   1 wsaryada  staff   669 Jun  2 14:29 kodejava-project.iml
drwxr-xr-x   7 wsaryada  staff   238 Jun 26 21:52 kodejava-swing
drwxr-xr-x   6 wsaryada  staff   204 Aug  3 15:06 kodejava-util-package
drwxr-xr-x   6 wsaryada  staff   204 Jun 30 15:09 maven-helloworld
-rw-r--r--   1 wsaryada  staff  1622 Aug  7 14:13 pom.xml