如何在Java中使用自定义类型注释

Bor*_*ška 9 java annotation-processing java-8 type-annotation

Java 8具有名为Type annotations(JSR 308)的功能.我想将它用于简单的Object to Object映射器框架.我想像这样定义注释@ExpectedType

@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpectedType {
    public Class<?> value();
}
Run Code Online (Sandbox Code Playgroud)

然后在我的代码中使用它,如下所示:

public class SomeServiceImpl() {
    public @ExpectedType(ObjectA_DTO.class) IObjectA doSomething(@ExpectedType(ObjectA_Entity.class) IObjectA obj) {
        return (ObjectA_Entity) obj; // it's correct
    }
}
Run Code Online (Sandbox Code Playgroud)

IObjectA是一个由类ObjectA_DTOObjectA_Entity.实现的接口.我想用这种方式服务:

// it's correct
assert someService.doSomething(new ObjectA_DTO()).getClass() == ObjectA_DTO.class;
Run Code Online (Sandbox Code Playgroud)

我想更改SomeServiceImpl方法的调用以使用Object mapper.它可以通过使用JSR 269或AOP 生成的代码来实现.

问题是我编写了简单的注释处理器,它根本不处理类型注释.简单注释处理器的来源如下所示:

@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class SimpleAnnotationsProcessor extends AbstractProcessor {

    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        Messager messager = processingEnv.getMessager();
        try {
            for (TypeElement e : annotations) {
                messager.printMessage(Diagnostic.Kind.NOTE, e.toString());
                for (Element elem : roundEnv.getElementsAnnotatedWith(e)) {
                    messager.printMessage(Diagnostic.Kind.NOTE, elem.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

有关SimpleAnnotationsProcessor如何使用或如何访问类型注释的任何想法?使用可插入注释处理API对我来说不是必需的我认为它会比Java反射具有更好的性能.无论如何,我也不知道如何通过Java Reflection访问类型注释.

Bal*_*der 3

我不确定我是否理解您想要实现的目标,但这里是一个如何使用 Java 反射 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

@test.TypeParameterTest$ExpectedType(value=class test.TypeParameterTest$ObjectA_DTO)
@test.TypeParameterTest$ExpectedType(value=class test.TypeParameterTest$ObjectA_Entity)
Run Code Online (Sandbox Code Playgroud)

但请注意,并非所有可能的类型注释都可以通过反射 api 访问,但如果需要,您始终可以从字节代​​码中读取它们(请参阅此处的我的答案)。