@Annotation 註解的使用,使用註解實現屬性NotNull校驗,實現爲方法賦值。

註解基礎的知識我就不多做說明了。直接通過代碼實現,更爲直觀。

package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * <p>NotNULL</p>
 *  對對象屬性使用註解不爲空,爲空拋出空指針異常。
 *  @Target(ElementType.FIELD) ElementType.FIELD標識該註解使用在屬性上
 *  @Retention(RetentionPolicy.RUNTIME) RetentionPolicy.RUNTIME表示運行時註解生效
 * @version V1.0
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNULL {
}

定義一個NotNULL註解,用於校驗對象屬性是否爲空;

package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * <p>MyTag</p>
 * 自定義註解給方法賦值
 * @version V1.0
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTag {
    String name();
}

定義一個方法上的註解,用於方法註解值。

public class PersonBean {
    @NotNULL
    private String name;
    @MyTag(name = "瓦片的")
    public void info(){
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

定義一個POJO類

package com.annotation;
import org.springframework.beans.BeanUtils;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
 * <p>AnnotationUtils</p>
 * @version V1.0
 */
public class AnnotationUtils {
    public static void fieldNotNullAnnotion(Object o){
        Class<?> claz = o.getClass();
        try {
            Field[] fields = claz.getDeclaredFields();
            for ( Field field : fields ) {
                field.setAccessible(true);
                Annotation[] annotations = field.getDeclaredAnnotations();
                for ( Annotation annotation : annotations ) {
                    if ( annotation instanceof NotNULL ) {
                        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(claz, field.getName());
                        Method readMethod = propertyDescriptor.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(o);
                        if ( value == null ) {
                            throw new NullPointerException();
                        }
                    }
                }
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }
    public static String methodMyTagAnnotation (Object o){
        Class<?> claz = o.getClass();
        Method[] methods = claz.getDeclaredMethods();
        for ( Method method : methods ) {
            for ( Annotation annotation : method.getAnnotations() ) {
                if ( annotation instanceof MyTag ) {
                    MyTag myTag = (MyTag) annotation;
                    return myTag.name();
                }
            }
        }
        return null;
    }
}

讀取對象註解的工具類

    public static void main(String[] args){
        PersonBean personBean = new PersonBean();
        AnnotationUtils.fieldNotNullAnnotion(personBean);
        String name = AnnotationUtils.methodMyTagAnnotation(personBean);
        System.out.println(name);
    }

測試

java.lang.NullPointerException
	at com.annotation.AnnotationUtils.fieldNotNullAnnotion(AnnotationUtils.java:35)
	at com.annotation.AnnotationTest.main(AnnotationTest.java:13)
瓦片的

輸出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章