Validate with Custom Annotations in Java


Create package annotations and create new annotations as below:

Create new java file named NotEmpty 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 NotEmpty {

}

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.METHOD })
public @interface MinLength {

	int value();

}




Create new java file named MaxLength 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 MaxLength {

	int value();

}

Create new java file named Min 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 Min {

	int value();

}

Create new java file named Max 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 Max {

	int value();

}




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

package entities;

import annotations.Max;
import annotations.MaxLength;
import annotations.Min;
import annotations.MinLength;
import annotations.NotEmpty;

public class Employee {

	private String id;
	private String name;
	private int age;

	@NotEmpty
	@MinLength(3)
	@MaxLength(10)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@NotEmpty
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Min(18)
	@Max(120)
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Employee(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Employee() {
	}

}




package annotations;

import java.lang.reflect.Method;
import entities.Employee;

public class Main {

	public static boolean isValid(Employee employee) {
		boolean result = true;
		try {
			Class employeeClass = employee.getClass();
			for (Method method : employeeClass.getMethods()) {
				if (method.getName().startsWith("get") && !method.getName().equalsIgnoreCase("getClass")) {
					if (method.isAnnotationPresent(NotEmpty.class)) {
						String value = method.invoke(employee, new Object[] {}).toString();
						if (value.isEmpty()) {
							result = false;
							break;
						}
					}
					if (method.isAnnotationPresent(MinLength.class)) {
						MinLength minLength = (MinLength) method.getAnnotation(MinLength.class);
						String value = method.invoke(employee, new Object[] {}).toString();
						if (value.length() < minLength.value()) {
							result = false;
							break;
						}
					}
					if (method.isAnnotationPresent(MaxLength.class)) {
						MaxLength maxLength = (MaxLength) method.getAnnotation(MaxLength.class);
						String value = method.invoke(employee, new Object[] {}).toString();
						if (value.length() > maxLength.value()) {
							result = false;
							break;
						}
					}
					if (method.isAnnotationPresent(Min.class)) {
						Min min = (Min) method.getAnnotation(Min.class);
						int value = Integer.parseInt(method.invoke(employee, new Object[] {}).toString());
						if (value < min.value()) {
							result = false;
							break;
						}
					}
					if (method.isAnnotationPresent(Min.class)) {
						Max max = (Max) method.getAnnotation(Max.class);
						int value = Integer.parseInt(method.invoke(employee, new Object[] {}).toString());
						if (value > max.value()) {
							result = false;
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public static void main(String[] args) {

		Employee employee = new Employee();
		employee.setId("p01");
		employee.setName("Name 1");
		employee.setAge(2); // Invalid
		System.out.println("Valid: " + isValid(employee));

	}

}
Valid: false