最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How do i check a custom annotation? - Stack Overflow

programmeradmin1浏览0评论

today i created a hello world app. It's not a simple hello world app. It uses classes, custom exceptions, threads, generics, custom annotation and more. The problem here is that the AnnotationApplyer class which checks if the annotations is well applyed does not do anything.

I tried to create an annotation that says that the class is a user-defined exception (extends java.lang.Exception class). This does not work, do i made a mistake at checking or something else?

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);
    }
}


today i created a hello world app. It's not a simple hello world app. It uses classes, custom exceptions, threads, generics, custom annotation and more. The problem here is that the AnnotationApplyer class which checks if the annotations is well applyed does not do anything.

I tried to create an annotation that says that the class is a user-defined exception (extends java.lang.Exception class). This does not work, do i made a mistake at checking or something else?

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);
    }
}


Share Improve this question asked Jan 29 at 16:52 iasonasiasonas 175 bronze badges 7
  • Keep in mind that using reflection in your own code is usually a sign of poor design. – mr mcwolf Commented Jan 29 at 16:55
  • @mrmcwolf how else would you check your own code for your own annotation? – f1sh Commented Jan 29 at 16:56
  • Why are you using annotations? – markspace Commented Jan 29 at 16:58
  • I'm looking at UserDefined and it's applied to a static method. That might through the code you have for a loop. Double check how you are testing for annotations and check that static methods are included in your checks. – markspace Commented Jan 29 at 16:59
  • annotations break all the rules of OOP. :) Otherwise your code won't work because you need to test real objects. The AnnotationApplyer::check method needs to accept an object instance to work with. – mr mcwolf Commented Jan 29 at 17:02
 |  Show 2 more comments

1 Answer 1

Reset to default 0

You are missing a

@Retention(RetentionPolicy.RUNTIME)

on your UserException annotation. The default value does not ensure that the annotation is still available at runtime.

发布评论

评论列表(0)

  1. 暂无评论