How to use custom type annotations in Java

前端 未结 3 1056
[愿得一人]
[愿得一人] 2021-02-14 13:21

Java 8 has feature called Type annotations (JSR 308). I would like to use it for simple Object to Object mapper framework. I would like define annotation @ExpectedType like this

相关标签:
3条回答
  • 2021-02-14 13:31

    I think you're mixing up the use of annotations at run time versus the use of same at "compile" time by various tools. Processor interface is for use in tools (compiler, javadoc generator), not in your runtime code.

    0 讨论(0)
  • 2021-02-14 13:31
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface SearchDefinition {
    
        public String identifier() default "";
    
    }
    

    @SearchDefinition - can be used anywhere

    0 讨论(0)
  • 2021-02-14 13:51

    I'm not sure I understand what you try to achieve, but here is an example how you can access your annotations with the Java reflection api:

    package test;
    
    import java.lang.annotation.Annotation;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.AnnotatedType;
    import java.lang.reflect.Method;
    
    public class TypeParameterTest {
    
        @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
        @Retention(RetentionPolicy.RUNTIME)
        public @interface ExpectedType {
            public Class<?> value();
        }
    
        public static interface IObjectA {}
    
        public static class ObjectA_DTO implements IObjectA {}
    
        public static class ObjectA_Entity implements IObjectA {}
    
        public static class SomeServiceImpl {
            public @ExpectedType(ObjectA_DTO.class) IObjectA doSomething(@ExpectedType(ObjectA_Entity.class) IObjectA obj) {
                return (ObjectA_Entity) obj;
            }
        }
    
        public static void main(String[] args) throws NoSuchMethodException, SecurityException {
            Method m = SomeServiceImpl.class.getMethod("doSomething", IObjectA.class);
            AnnotatedType returnType = m.getAnnotatedReturnType();
            Annotation returnTypeAnnotation = returnType.getAnnotation(ExpectedType.class);
            System.out.println(returnTypeAnnotation);
    
            AnnotatedType[] parameters = m.getAnnotatedParameterTypes();
            for (AnnotatedType p : parameters) {
                Annotation parameterAnnotation = p.getAnnotation(ExpectedType.class);
                System.out.println(parameterAnnotation);
            }
        }
    }
    

    The output looks like this:

    @test.TypeParameterTest$ExpectedType(value=class test.TypeParameterTest$ObjectA_DTO)
    @test.TypeParameterTest$ExpectedType(value=class test.TypeParameterTest$ObjectA_Entity)
    

    Note though, that not all possible type annotations are accessible through the reflection api, but you can always read them from the byte code if necessary (see my answer here).

    0 讨论(0)
提交回复
热议问题