Annotation: ClassAnnotation.java
package com.pkm.annotation;
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) //can use in class only.
public @interface ClassAnnotation {
public enum Priority {
LOW, MEDIUM, HIGH
}
Priority priority() default Priority.HIGH;
String[] tags() default "";
String name() default "Pritom";
String date() default "02/05/2014";
}
Annotation: MethodAnnotation.java
package com.pkm.annotation;
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) //can use in method only.
public @interface MethodAnnotation {
public boolean enabled() default false;
}
Annotation: FieldAnnotation.java
package com.pkm.annotation;
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) //can use in field only.
public @interface FieldAnnotation {
public boolean required() default false;
public boolean blank() default true;
public int min() default 0;
public int max() default 255;
}
Example Usage Of Custom Annotation: Example.java
package com.pkm.annotation;
@ClassAnnotation(name = "Pritom Kumar Mondal", priority = ClassAnnotation.Priority.MEDIUM,
tags = {"Pritom", "Kumar", "Mondal"})
public class Example {
@FieldAnnotation(required = true, blank = false, min = 10, max = 15)
String name;
@MethodAnnotation(enabled = true)
void method1() {
if(true) {
System.out.println("True");
} else {
System.out.println("False");
}
}
@MethodAnnotation(enabled = false)
void method2() {
if(true) {
System.out.println("True");
} else {
System.out.println("False");
}
}
@MethodAnnotation()
void method3() {
if(true) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}
Example Runner Class: RunExample.java
package com.pkm.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class RunExample {
public static void main(String[] args) throws Exception {
RunExample runExample = new RunExample();
Example example = new Example();
example.name = "Pritom Kumar Mondal";
try {
RunExample.validate(example);
System.out.println("\n\nInfo: Validation successful");
} catch (Exception ex) {
System.out.println("\n\nError: " + ex.getMessage());
}
}
public static boolean validate(Object object) throws Exception {
Class obj = object.getClass();
/* Process @ClassAnnotation */
if (obj.isAnnotationPresent(ClassAnnotation.class)) {
Annotation annotation = obj.getAnnotation(ClassAnnotation.class);
ClassAnnotation classAnnotation = (ClassAnnotation) annotation;
System.out.printf("%nPriority :%s", classAnnotation.priority());
System.out.printf("%nCreatedBy :%s", classAnnotation.name());
System.out.printf("%nTags :");
int tagLength = classAnnotation.tags().length;
for (String tag : classAnnotation.tags()) {
if (tagLength > 1) {
System.out.print(tag + " ||| ");
} else {
System.out.print(tag);
}
tagLength--;
}
System.out.printf("%nDate :%s%n%n", classAnnotation.date());
}
/* Process @MethodAnnotation */
for (Method method : obj.getDeclaredMethods()) {
if (method.isAnnotationPresent(MethodAnnotation.class)) {
Annotation annotation = method.getAnnotation(MethodAnnotation.class);
MethodAnnotation methodAnnotation = (MethodAnnotation) annotation;
if(methodAnnotation.enabled()) {
System.out.println(method.getName() +" is enabled");
} else {
System.out.println(method.getName() +" is not enabled");
}
}
}
/* Process @FieldAnnotation */
for (Field field : obj.getDeclaredFields()) {
if (field.isAnnotationPresent(FieldAnnotation.class)) {
Annotation annotation = field.getAnnotation(FieldAnnotation.class);
FieldAnnotation methodAnnotation = (FieldAnnotation) annotation;
field.setAccessible(true);
Object value = field.get(object);
/* Checking: required */
if(methodAnnotation.required()) {
System.out.println(field.getName() +" is required with value: " + value);
if(value == null) {
throw new Exception(field.getName() + " is required");
}
} else {
System.out.println(field.getName() +" is not required");
}
/* Checking: blank */
if(!methodAnnotation.blank()) {
System.out.println(field.getName() +" not blank with value: " + value);
if(value == null || value.toString().trim().length() == 0) {
throw new Exception(field.getName() + " not blank");
}
} else {
System.out.println(field.getName() +" can blank");
}
/* Checking: min */
System.out.println(field.getName() +" min length: " + methodAnnotation.min());
if(value != null && value.toString().trim().length() < methodAnnotation.min()) {
throw new Exception(field.getName() + " must min length: " + methodAnnotation.min());
}
/* Checking: max */
System.out.println(field.getName() +" max length: " + methodAnnotation.max());
if(value != null && value.toString().trim().length() > methodAnnotation.max()) {
throw new Exception(field.getName() + " must max length: " + methodAnnotation.max());
}
}
}
return true;
}
}
Example Output:
Priority :MEDIUM
CreatedBy :Pritom Kumar Mondal
Tags :Pritom ||| Kumar ||| Mondal
Date :02/05/2014
method1 is enabled
method2 is not enabled
method3 is not enabled
name is required with value: Pritom Kumar Mondal
name not blank with value: Pritom Kumar Mondal
name min length: 10
name max length: 15
Error: name must max length: 15
No comments:
Post a Comment