zoukankan      html  css  js  c++  java
  • java——注解处理器

    Java提供了两种方式来处理注解:第一种是利用运行时反射机制;另一种是使用Java提供的API来处理编译期的注解。

    运行时通过反射:仅当定义的注解的@Retention为RUNTIME时,才能够通过运行时的反射机制来处理注解。

    https://www.jb51.net/article/132038.htm

    FruitName.java

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
        String value() default "";
    }

    FruitColor.java

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitColor {
        /**
         * 颜色枚举
         * @author peida
         *
         */
        public enum Color{
            BULE,RED,GREEN
        }
        ;
        /**
         * 颜色属性
         * @return
         */
        Color fruitColor() default Color.GREEN;
    }

    FruitProvider.java

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitProvider {
        /**
         * 供应商编号
         * @return
         */
        public int id() default -1;
        /**
         * 供应商名称
         * @return
         */
        public String name() default "";
        /**
         * 供应商地址
         * @return
         */
        public String address() default "";
    }

    Apple.java

    public class Apple {
        @FruitName("Apple")
        private String appleName;
        @FruitColor(fruitColor=FruitColor.Color.RED)
        private String appleColor;
        @FruitProvider(id=1,name="陕西红富士集团",address="陕西省西安市延安路89号红富士大厦")
        private String appleProvider;
        public void setAppleColor(String appleColor) {
            this.appleColor = appleColor;
        }
        public String getAppleColor() {
            return appleColor;
        }
        public void setAppleName(String appleName) {
            this.appleName = appleName;
        }
        public String getAppleName() {
            return appleName;
        }
        public void setAppleProvider(String appleProvider) {
            this.appleProvider = appleProvider;
        }
        public String getAppleProvider() {
            return appleProvider;
        }
        public void displayName(){
            System.out.println("水果的名字是:苹果");
        }
    }

    FruitInfoUtil.java

    public class FruitInfoUtil {
        public static void getFruitInfo(Class<?> clazz){
            String strFruitName=" 水果名称:";
            String strFruitColor=" 水果颜色:";
            String strFruitProvicer="供应商信息:";
            Field[] fields = clazz.getDeclaredFields();
            for (Field field :fields){
                if(field.isAnnotationPresent(FruitName.class)){
                    FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
                    strFruitName=strFruitName+fruitName.value();
                    System.out.println(strFruitName);
                } else if(field.isAnnotationPresent(FruitColor.class)){
                    FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class);
                    strFruitColor=strFruitColor+fruitColor.fruitColor().toString();
                    System.out.println(strFruitColor);
                } else if(field.isAnnotationPresent(FruitProvider.class)){
                    FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class);
                    strFruitProvicer=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+fruitProvider.address();
                    System.out.println(strFruitProvicer);
                }
            }
        }
    }

    FruitRun.java

    public class FruitRun {
        /**
         * @param args
         */
        public static void main(String[] args) {
            FruitInfoUtil.getFruitInfo(Apple.class);
        }
    }

    编译时:

    https://blog.csdn.net/qfanmingyiq/article/details/75432854

  • 相关阅读:
    git创建版本库
    DataSet的加密解密
    在InstallShield中加密字符串,在C#中解密
    asp.net后台长时间操作时,向前台输出“请等待"信息的方法
    DataSet的加密解密(续)
    XXTEA加密算法的InstallShield 脚本实现
    c#如何监视文件或者文件夹的变化
    wpf制作毛玻璃效果按钮的代码
    WPF中用于Path的Geometry MiniLanguage
    如何在非英文环境中正确显示数字
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/10290892.html
Copyright © 2011-2022 走看看