目標:經過自定義註解 @Ignore 註解,以爲是否讀取指定類的屬性。html
自定義註解:基礎詳見 註解Annotation實現原理與自定義註解例子java
運行結果:ide
[main] INFO util.FruitInfoUtil - 水果的名字爲:entity.Apple [main] INFO util.FruitInfoUtil - 水果的顏色爲:RED [main] INFO util.FruitInfoUtil - 水果的供應商名稱爲:紅富士供應商,供應商地址爲:山西紅富士產業園 [main] INFO main.MainTest - ==================== [main] INFO util.FieldsUtil - fieldName:fruitColor's ignore is null [main] INFO util.FieldsUtil - fieldName:fruitProvider's ignore is false
[main] INFO util.FieldsUtil - fields: [main] INFO util.FieldsUtil - fruitColor [main] INFO util.FieldsUtil - fruitProvider [main] INFO util.FieldsUtil - END
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitName { String value() default ""; }
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitColor { public enum Color {GREND, RED, BLUE} Color fruitColor() default Color.GREND; }
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitProvider { int id() default -1; String name() default ""; String addr() default ""; }
import java.lang.annotation.*; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Ignore { boolean ignore() default false; }
import annotation.FruitColor; import annotation.FruitName; import annotation.FruitProvider; import annotation.Ignore; public class Apple { @FruitName("entity.Apple") @Ignore(ignore = true) String fruitName; @FruitColor(fruitColor = FruitColor.Color.RED) // @Ignore(ignore = true) String fruitColor; @FruitProvider(id = 1, name = "紅富士供應商", addr = "山西紅富士產業園") @Ignore String fruitProvider; public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } public String getFruitColor() { return fruitColor; } public void setFruitColor(String fruitColor) { this.fruitColor = fruitColor; } public String getFruitProvider() { return fruitProvider; } public void setFruitProvider(String fruitProvider) { this.fruitProvider = fruitProvider; } public void getAppleName(){ System.out.println("水果的名稱爲:蘋果。"); } }
import annotation.FruitColor; import annotation.FruitName; import annotation.FruitProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; public class FruitInfoUtil { final static Logger logger = LoggerFactory.getLogger(FruitInfoUtil.class); public static void getAnnotationInfo(Class clazz){ Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if(field.isAnnotationPresent(FruitName.class)){ FruitName fruitName = field.getAnnotation(FruitName.class); logger.info("水果的名字爲:"+fruitName.value()); } else if(field.isAnnotationPresent(FruitColor.class)) { FruitColor fruitColor = field.getAnnotation(FruitColor.class); logger.info("水果的顏色爲:" + fruitColor.fruitColor()); } else if(field.isAnnotationPresent(FruitProvider.class)){ FruitProvider fruitProvider = field.getAnnotation(FruitProvider.class); logger.info("水果的供應商名稱爲:" + fruitProvider.name()+",供應商地址爲:"+fruitProvider.addr()); } } } }
import annotation.Ignore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class FieldsUtil { final static Logger logger = LoggerFactory.getLogger(FieldsUtil.class); /** * 本類的全部 Ignore = false的字段集合 * @param clazz */ public static void getOneSelfActiveFieldsUtil(Class clazz){ Field[] fields = clazz.getDeclaredFields(); List<String> fileNames = new ArrayList<>(); for (Field field : fields){ String fieldName = field.getName(); Ignore ignoreField = field.getAnnotation(Ignore.class); if(field.isAnnotationPresent(Ignore.class) && !ignoreField.ignore()){ fileNames.add(field.getName()); logger.info("fieldName:"+fieldName+"'s ignore is false"); } else if(!field.isAnnotationPresent(Ignore.class)){ fileNames.add(field.getName()); logger.info("fieldName:"+fieldName+"'s ignore is null"); } } logger.info("fields:"); if(fileNames != null && fileNames.size() > 0){ // fileNames.stream().forEach(System.out::println); fileNames.stream().forEach(y -> { logger.info(y); }); }else{ logger.info("Size() == 0 "); } logger.info("END"); } /** * 包括 父類的全部Ignore = false(默認值)的字段信息 * @param clazz */ public static List<Field> getAllActiveFieldsUtil(Class clazz){ List<String> fileNames = new ArrayList<>(); List<Field> fieldList = null; Field[] fields; do{ fields = clazz.getDeclaredFields(); if(fields != null && fields.length > 0){ for (Field field : fields){ String fieldName = field.getName(); Ignore ignoreField = field.getAnnotation(Ignore.class); if(field.isAnnotationPresent(Ignore.class) && !ignoreField.ignore()){ fileNames.add(field.getName()); logger.info("fieldName:"+fieldName+"'s ignore is false"); fieldList.add(field); } else if(!field.isAnnotationPresent(Ignore.class)){ fileNames.add(field.getName()); logger.info("fieldName:"+fieldName+"'s ignore is null"); fieldList.add(field); } } if(fileNames != null && fileNames.size() > 0){ fileNames.stream().forEach(System.out::println); }else{ logger.info("Size() == 0 "); } logger.info("END"); } clazz = clazz.getSuperclass(); }while (clazz != Object.class && clazz != null); return fieldList; } }
import entity.Apple; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import util.FieldsUtil; import util.FruitInfoUtil; public class MainTest { final static Logger logger = LoggerFactory.getLogger(MainTest.class); public static void main(String[] args) { FruitInfoUtil.getAnnotationInfo(Apple.class); logger.info("===================="); FieldsUtil.getOneSelfActiveFieldsUtil(Apple.class); } }
參考資源:post