代码之家  ›  专栏  ›  技术社区  ›  iasonas

如何检查自定义注释?

  •  0
  • iasonas  · 技术社区  · 4 月前

    今天我创建了一个hello world应用程序。 这不是一个简单的hello world应用程序。 它使用类、自定义异常、线程、泛型、自定义注释等。 这里的问题是,检查注释是否应用良好的AnnotationApplyer类什么也不做。

    我试图创建一个注释,说明该类是用户定义的异常(扩展了java.lang.exception类)。 这不起作用,我在检查时犯了错误还是别的什么?

    import java.lang.annotation.*;
    
    @UserException // wrong placed annotation for testing
    @UserDefined(user = "iasonas")
    public abstract class Main {
    
        public static void main(String[] args) {
            // check annotations
            AnnotationApplyer.check();
    
            // start the program
            new Greeter().greet();
        }
        
        @UserDefined(user = "iasonas")
        private static class Greeter {
            private final String MSG = "Hello, World!";
            private Printer ms = new MessageService<String>(MSG);
            
            public Greeter () {
            }
            
            public void greet () {
                try {
                    ms.print();
                } catch (InvalidMessageException e) {
                    System.out.println("Cannot greet. "+e.toString());
                }
            }
        };
    };
    
    @UserException // wrong placed annotation for testing
    @UserDefined(user = "iasonas")
    class AnnotationApplyer {
        public static void check () {
            checkUserException(Main.class, AnnotationApplyer.class, MessageService.class, UserDefined.class);
            checkUserException(UserException.class, Printer.class, InvalidMessageException.class);
            
        }
    
        public static void checkUserException (Class<?>... classes) {
            for (Class<?> clazz : classes) {
                if (!clazz.isAnnotationPresent(UserException.class)) {
                    continue;
                }
    
                if (!Exception.class.isAssignableFrom(clazz)) {
                    System.out.println("Error: "+clazz.getName()+" does not extend java.lang.Exception");
                } else {
                    System.out.println("Class "+clazz.getName()+" extends java.lang.Exception!");
                }
            }
        }
    }
    
    @Target(ElementType.TYPE)
    @interface UserException {
    
    }
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @interface UserDefined {
        String user() default "Not specified";
    }
    
    class MessageService <T> implements Runnable, Printer {
        private T message;
        private final Thread thread = new Thread(this);
        private int printCounter;
        
        public MessageService () {   
        }
        
        public MessageService (T message) {
            this.message = message;
        }
    
        public T getMessage () {
            return message;
        }
        
        public void setMessage (T aMsg) {
            this.message = aMsg;
        }
    
        public int getTimes () {
            return printCounter;
        }
        
        @Override
        public void run () {
            System.out.println(message);
            printCounter++;
        }
        
        @Override
        public synchronized void print () throws InvalidMessageException {
            if (message==null) {
                throw new InvalidMessageException("message is null");
            }
            thread.start();
        }
    };
    
    interface Printer {
        void print() throws InvalidMessageException;
    };
    
    @UserException // well placed annotation for checking
    class InvalidMessageException extends Exception {
        public InvalidMessageException (String m) {
            super(m);
            System.exit(0);
        }
    }
    
    
    
    1 回复  |  直到 4 月前
        1
  •  0
  •   f1sh    4 月前

    你缺少一个

    @Retention(RetentionPolicy.RUNTIME)
    

    在你的 UserException 注释。默认值不能确保注释在运行时仍然可用。