How do I get information regarding class name?

package org.kodejava.lang.reflect;

import java.util.Date;

public class ClassNameDemo {
    public static void main(String[] args) {
        Date date = new Date();

        // Gets the Class of the date instance.
        Class<?> clazz = date.getClass();

        // Gets the name of the class.
        String name = clazz.getName();
        System.out.println("Class name     : " + name);

        // Gets the canonical name of the class.
        String canonical = clazz.getCanonicalName();
        System.out.println("Canonical name : " + canonical);

        // Gets the simple name of the class.
        String simple = clazz.getSimpleName();
        System.out.println("Simple name    : " + simple);
    }
}

Here are the information printed out by the program:

Class name     : java.util.Date
Canonical name : java.util.Date
Simple name    : Date

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 itself. 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.lang.annotation;

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

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

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

        try {
            Class<? extends GetAllAnnotation> clazz = demo.getClass();

            // To get the sayHi() method we need to pass not only the method
            // name but also its parameters type so 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 annotation : annotations) {
                System.out.println("Type: " + annotation.annotationType());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @MyAnnotation("Hi")
    @HelloAnnotation(value = "Hello", greetTo = "Everyone")
    public void sayHi(String dataId, String name) {
        Map<String, String> 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.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.lang.annotation.HelloAnnotation

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 get modifiers of a class object?

package org.kodejava.lang.reflect;

import java.lang.reflect.Modifier;

public class ClassModifier {
    public static void main(String[] args) {
        getClassModifier(String.class);
        getClassModifier(TestA.class);
        getClassModifier(TestB.class);
    }

    private static void getClassModifier(Class<?> clazz) {
        int modifier = clazz.getModifiers();

        // Return true if the integer argument includes the public modifier,
        // false otherwise.
        if (Modifier.isPublic(modifier)) {
            System.out.println(clazz.getName() + " class modifier is public");
        }

        // Return true if the integer argument includes the protected modifier,
        // false otherwise.
        if (Modifier.isProtected(modifier)) {
            System.out.println(clazz.getName() + " class modifier is protected");
        }

        // Return true if the integer argument includes the private modifier,
        // false otherwise.
        if (Modifier.isPrivate(modifier)) {
            System.out.println(clazz.getName() + " class modifier is private");
        }

        // Return true if the integer argument includes the static modifier,
        // false otherwise.
        if (Modifier.isStatic(modifier)) {
            System.out.println(clazz.getName() + " class modifier is static");
        }

        // Return true if the integer argument includes the final modifier,
        // false otherwise.
        if (Modifier.isFinal(modifier)) {
            System.out.println(clazz.getName() + " class modifier is final");
        }

        // Return true if the integer argument includes the abstract modifier,
        // false otherwise.
        if (Modifier.isAbstract(modifier)) {
            System.out.println(clazz.getName() + " class modifier is abstract");
        }
    }

    protected static final class TestA {
    }

    private abstract class TestB {
    }
}

The code snippet prints the following output:

java.lang.String class modifier is public
java.lang.String class modifier is final
org.kodejava.lang.reflect.ClassModifier$TestA class modifier is protected
org.kodejava.lang.reflect.ClassModifier$TestA class modifier is static
org.kodejava.lang.reflect.ClassModifier$TestA class modifier is final
org.kodejava.lang.reflect.ClassModifier$TestB class modifier is private
org.kodejava.lang.reflect.ClassModifier$TestB class modifier is abstract

How do I create object using Constructor object?

The example below using a constructor reflection to create a string object by calling String(String) and String(StringBuilder) constructors.

package org.kodejava.lang.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CreateObjectDemo {
    public static void main(String[] args) {
        Class<String> clazz = String.class;

        try {
            Constructor<String> constructor = clazz.getConstructor(String.class);

            String object = constructor.newInstance("Hello World!");
            System.out.println("String = " + object);

            constructor = clazz.getConstructor(StringBuilder.class);
            object = constructor.newInstance(new StringBuilder("Hello Universe!"));
            System.out.println("String = " + object);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}