How do I get all annotations?

To obtains all annotations for classes, methods, constructors, or fields we use the getAnnotations()method. This method returns an array of Annotation

In the following example we tried to read all annotations from the sayHi() method. First we need to obtain the method object it self. Because the sayHi() method has parameters, we need to pass not only the method name to the getMethod() method, but we also need to pass the parameter’s type.

The getAnnotations() method returns only annotation that has a RetentionPolicy.RUNTIME. Because other retention policy doesn’t allow the annotation to available at runtime.

package org.kodejava.example.lang.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class GetAllAnnotation {
    private Map<String, String> data = new HashMap<>();

    @SuppressWarnings("unchecked")
    public static void main(String args[]) {
        GetAllAnnotation demo = new GetAllAnnotation();
        demo.sayHi("001", "Alice");
        demo.sayHi("004", "Malory");

        try {
            Class clazz = demo.getClass();

            // To get the sayHi() method we need to pass not only the method
            // name but also its parameters type so the the getMethod() method
            // return the correct method for us to use.
            Method method = clazz.getMethod("sayHi", String.class, String.class);

            // Get all annotations from the sayHi() method. But this actually
            // will only return one annotation. Because only the HelloAnnotation
            // annotation that has RUNTIME retention policy, which means that
            // the other annotations associated with sayHi() method is not
            // available at runtime.
            Annotation[] annotations = method.getAnnotations();
            for (Annotation anno : annotations) {
                System.out.println("Type: " + anno.annotationType());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    @MyAnnotation("Hi")
    @HelloAnnotation(value = "Hello", greetTo = "Everyone")
    public void sayHi(String dataId, String name) {
        Map data = getData();
        if (data.containsKey(dataId)) {
            System.out.println("Hello " + data.get(dataId));
        } else {
            data.put(dataId, name);
        }
    }

    private Map<String, String> getData() {
        data.put("001", "Alice");
        data.put("002", "Bob");
        data.put("003", "Carol");
        return data;
    }
}
package org.kodejava.example.lang.annotation;

public @interface MyAnnotation {
    String value();
}

Check the HelloAnnotation on the following link How do I create a simple annotation?.

The result of this code snippet:

Hello Alice
Type: interface org.kodejava.example.lang.annotation.HelloAnnotation

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 simple execute a Linux ls -al command on the current working directory and display the result.

package org.kodejava.example.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

How do I know the memory size in virtual machine?

If you want to know the free memory and the total memory available in the Java runtime environment then you can use the following code snippet to check.

package org.kodejava.example.lang;

public class MemoryExample {
    public static void main(String[] args) {
        long freeMemory = Runtime.getRuntime().freeMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();

        System.out.println("Free Memory  = " + freeMemory);
        System.out.println("Total Memory = " + totalMemory);
    }
}

Here is the result of the code snippet above:

Free Memory  = 252054480
Total Memory = 257425408