Annotations in Java


Create package annotations and create new annotations as below:

Create new java file named MyClass as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface MyClass {

	String name();

	String author();

	String dateCreation();

	String description();

}

Create new java file named MyField as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface MyField {

	String name();

	String author();

	String dateCreation();

}




Create new java file named MyField as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface MyField {

	String name();

	String author();

	String dateCreation();

}

Create new java file named MyMethod as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MyMethod {

	String name();

	String author();

	String dateCreation();

	String[] parameters();

}

Create new java file named MinLength as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface MinLength {

	int value();

}

Create new enum named RoleType contains roles for this annotation

package annotations;

public enum RoleType {

	Role1, Role2, Role3;

}

Create new java file named MyRoles as below:

package annotations;

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MyRoles {

	RoleType value();

}




Create entities package and create new class named Account as below:

package entities;

import annotations.MinLength;
import annotations.MyClass;
import annotations.MyField;
import annotations.MyMethod;
import annotations.MyRoles;
import annotations.RoleType;

@MyClass(name = "Account", author = "PMK Lab", dateCreation = "2018-03-18", description = "Description of Account class")
public class Account {

	private String username;
	private String role;

	@MyField(name = "username", author = "PMK Lab", dateCreation = "2018-03-18")
	public String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@MyMethod(name = "getUsername", author = "PMK Lab", dateCreation = "2018-03-18", paramters = {})
	@MinLength(3)
	public String getUsername() {
		return username;
	}

	@MyRoles(RoleType.Role1)
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@MyMethod(name = "display", author = "PMK Lab", dateCreation = "2018-03-18", parameters = { "fullName" })
	public String display(String fullName) {
		return "Hello " + fullName;
	}

}
package annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import entities.Account;

public class Main {

	private static void displayAnnotationsOfClass(Account account) {
		Class accountClass = account.getClass();
		Annotation[] annotations = accountClass.getAnnotations();
		System.out.println("\tCount Annotations: " + annotations.length);
		if (annotations.length > 0) {
			for (Annotation annotation : annotations) {
				System.out.println("\tAnnotation Name: " + annotation.annotationType().getName());
				System.out.println("\tAnnotation Simple Name: " + annotation.annotationType().getSimpleName());
				System.out.println("\tAnnotation Modifier: " + Modifier.toString(annotation.annotationType().getModifiers()));
			}
		}
		System.out.println("------------------------------------------------------------");

	}

	private static void displayAnnotationsOfMethods(Account account) {
		Class accountClass = account.getClass();
		for (Method method : accountClass.getDeclaredMethods()) {
			System.out.println("\tMethod Name: " + method.getName());
			Annotation[] annotations = method.getAnnotations();
			System.out.println("\tCount Annotations: " + annotations.length);
			if (annotations.length > 0) {
				for (Annotation annotation : annotations) {
					System.out.println("\tAnnotation Name: " + annotation.annotationType().getName());
					System.out.println("\tAnnotation Simple Name: " + annotation.annotationType().getSimpleName());
					System.out.println("\tAnnotation Modifier: " + Modifier.toString(annotation.annotationType().getModifiers()));
				}
			}
			System.out.println("------------------------------------------------------------");
		}
	}

	private static void displayAnnotationsOfFields(Account account) {
		Class accountClass = account.getClass();
		for (Field field : accountClass.getDeclaredFields()) {
			System.out.println("\tField Name: " + field.getName());
			Annotation[] annotations = field.getAnnotations();
			System.out.println("\tCount Annotations: " + annotations.length);
			if (annotations.length > 0) {
				for (Annotation annotation : annotations) {
					System.out.println("\tAnnotation Name: " + annotation.annotationType().getName());
					System.out.println("\tAnnotation Simple Name: " + annotation.annotationType().getSimpleName());
					System.out.println("\tAnnotation Modifier: " + Modifier.toString(annotation.annotationType().getModifiers()));
				}
			}
			System.out.println("------------------------------------------------------------");
		}
	}

	public static void main(String[] args) {

		Account account = new Account();

		System.out.println("Annotations of Class");
		displayAnnotationsOfClass(account);

		System.out.println("Annotations of Fields");
		displayAnnotationsOfFields(account);

		System.out.println("Annotations of Methods");
		displayAnnotationsOfMethods(account);

	}

}




Annotations of Class
	Count Annotations: 1
	Annotation Name: annotations.MyClass
	Annotation Simple Name: MyClass
	Annotation Modifier: public abstract interface
------------------------------------------------------------------------
Annotations of Fields
	Field Name: username
	Count Annotations: 0
------------------------------------------------------------------------
	Field Name: role
	Count Annotations: 0
------------------------------------------------------------------------
	Field Name: address
	Count Annotations: 1
	Annotation Name: annotations.MyField
	Annotation Simple Name: MyField
	Annotation Modifier: public abstract interface
------------------------------------------------------------------------
Annotations of Methods
	Method Name: getAddress
	Count Annotations: 0
------------------------------------------------------------------------
	Method Name: setRole
	Count Annotations: 0
------------------------------------------------------------------------
	Method Name: display
	Count Annotations: 1
	Annotation Name: annotations.MyMethod
	Annotation Simple Name: MyMethod
	Annotation Modifier: public abstract interface
------------------------------------------------------------------------
	Method Name: getUsername
	Count Annotations: 2
	Annotation Name: annotations.MyMethod
	Annotation Simple Name: MyMethod
	Annotation Modifier: public abstract interface
	Annotation Name: annotations.MinLength
	Annotation Simple Name: MinLength
	Annotation Modifier: public abstract interface
------------------------------------------------------------------------
	Method Name: setAddress
	Count Annotations: 0
------------------------------------------------------------------------
	Method Name: setUsername
	Count Annotations: 0
------------------------------------------------------------------------
	Method Name: getRole
	Count Annotations: 1
	Annotation Name: annotations.MyRoles
	Annotation Simple Name: MyRoles
	Annotation Modifier: public abstract interface
------------------------------------------------------------------------