วันพุธที่ 13 มีนาคม พ.ศ. 2556

Test clear field by configure on Java Annotation

บทความนี้เป็นบทความตัวอย่าง ที่ทำการเขียน Method ที่ใช้ในการ Clear ค่าตัวแปรที่อยู่ใน Class ที่เรากำลังใช้ทำงาน โดยใช้ Annotation เป็นตัวกำหนดว่าค่าตัวแปรใน Class ชื่ออะไรบ้างที่ต้องการทำการ Clear ค่าโดยค่าก็จะสามารถกำหนดได้ว่า จะทำการ Clear ค่าให้เป็นค่าว่างเปล่า => Null หรือค่าเริ่มต้น => New Instance ซึ่งจากตัวอย่างอาจจะนำไป Apply ไปใช้กับการทำงานอื่น ๆ ได้ เราจะกำหนด Code ไว้ดังนี้ TestBean.java < ใช้เป็นตัวแปรที่ทำการเก็บค่า และทดสอบการ Clear ข้อมูล TestAnnotation.java < ใช้เป็น Class ที่ทำการทดสอบการดำเนินการ และเก็บตัวแปร TestBean.java ไว้ เราจะกำหนด Annotation ไว้ดังนี้ FormBeanReset.java < เป็น Annotation ที่ใช้ดำเนินการ FormBeanResetType.java < เป็น Class ที่ไว้กำหนดประเภทการดำเนินการ ตัวอย่าง Code FormBeanReset.java
package test.annotation;

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FormBeanReset {
    FormBeanResetType resetType() default FormBeanResetType.NEW_INSTANCE;
}
ตัวอย่าง Code FormBeanResetType.java
package test.annotation;

public enum FormBeanResetType {
 NULL, NEW_INSTANCE;
}
ตัวอย่าง Code TestBean.java
package test.bean;

public class TestBean {

    private String code;
 
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
ตัวอย่าง Code TestAnnotation.java
package test.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import test.bean.TestBean;

public class TestAnnotation {

    @FormBeanReset(resetType=FormBeanResetType.NULL)
    public TestBean testBean;
 
    public TestBean getTestBean() {
        return testBean;
    }

    public void setTestBean(TestBean testBean) {
        this.testBean = testBean;
    }

    public static void main(String[] args) throws Exception {
        testResetBean();
    }

    private static void testResetBean() throws Exception {
        TestAnnotation test = new TestAnnotation();
        test.setTestBean(new TestBean());
        test.getTestBean().setCode("TEST_CODE_001");

        System.out.println("[testResetBean TestBean] : " + test.getTestBean().getCode());
  
        Field[] fs = test.getClass().getFields();
        for(Field f : fs){
            System.out.println("[testResetBean FieldName]:" + f.getName());

            if(f.isAnnotationPresent(FormBeanReset.class)){
                System.out.println("[testResetBean Annotation FieldName]:" + f.getName());
                FormBeanReset fb = f.getAnnotation(FormBeanReset.class);
                FormBeanResetType ft = fb.resetType();
                
                if(FormBeanResetType.NULL.name().equalsIgnoreCase(ft.name())){
                    f.set(test, null);
                    continue;
                }

                Class clazz = f.getType();
                System.out.println("[testResetBean return clazz] : " + clazz.getName());
                Method[] ms = test.getClass().getMethods();
                for(Method m : ms){
                    System.out.println("[testResetBean Method] : " + m.getName());
      
                    if(("set"+f.getName()).equalsIgnoreCase(m.getName())){
                        System.out.println("[testResetBean Map Method] : " + m.getName());
                        m.invoke(test, clazz.newInstance());
                        break;
                    }
                }
            }
        } 
        System.out.println("[testResetBean TestBean] : " + test.getTestBean());
        System.out.println("[testResetBean TestBean.Code] : " + test.getTestBean().getCode());
 }
จากตัวอย่าง Code นี้ จะทำการ Clear testBean ให้มีค่าเป็น Null เมื่อ Run จะเห็น Log ดังนี้
[testResetBean TestBean] : TEST_CODE_001
[testResetBean FieldName]:testBean
[testResetBean Annotation FieldName]:testBean
[testResetBean TestBean] : null
Exception in thread "main" java.lang.NullPointerException
    at test.annotation.TestAnnotation.testResetBean(TestAnnotation.java:81)
    at test.annotation.TestAnnotation.main(TestAnnotation.java:23)
แต่หากเราไม่ต้องการให้ค่าเป็น Null เราจะสามารถเปลี่ยนในส่วน Configure ได้ดังนี้
    ...

    @FormBeanReset(resetType=FormBeanResetType.NEW_INSTANCE)
    public TestBean testBean;

    ...
เมื่อแก้ดังนี้แล้ว ค่าที่เคยได้ เป็น Null ก็จะกลายเป็น New Instance เมื่อ Run จะเห็น Log ดังนี้
[testResetBean TestBean] : TEST_CODE_001
[testResetBean FieldName]:testBean
[testResetBean Annotation FieldName]:testBean
[testResetBean return clazz] : test.bean.TestBean
[testResetBean Method] : getTestBean
[testResetBean Method] : setTestBean
[testResetBean Map Method] : setTestBean
[testResetBean TestBean] : test.bean.TestBean@18eb9e6
[testResetBean TestBean.Code] : null
ในกรณีที่ได้แก้ไข Constructor ไว้ก่อนหน้านี้แล้ว ค่าก็จะเป็นไปตาม ที่ Constructor กำหนด เพราะในส่วน Code ที่เขียนกำหนดนี้มันไปทำการเรียก Default Constructor คือ method ชื่อเดียวกับ Class ที่ไม่ทำการใส่ argument นั่นเอง ประโยชน์ที่ได้หลักจากนี้คือ เมื่อเราต้องมีการเพิ่ม ตัวแปรใน Class ที่กำหนด และ มีการ Clear ค่าตัวแปรทุกครั้งที่มีการทำงาน เราก็แค่เพิ่ม Annotation กำหนดเข้าไป โดยไม่ต้องเขียน Code เพิ่มเลย บทความนี้ก็มีเท่านี้แหละ จบ

ไม่มีความคิดเห็น: