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

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

@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})

@Retention(RetentionPolicy.RUNTIME)

public @interface ExpectedType {

public Class> value();

}

And then use it in my code like this:

public class SomeServiceImpl() {

public @ExpectedType(ObjectA_DTO.class) IObjectA doSomething(@ExpectedType(ObjectA_Entity.class) IObjectA obj) {

return (ObjectA_Entity) obj; // it's correct

}

}

IObjectA is an interface implemented by classes ObjectA_DTO and ObjectA_Entity. The service I would like to use this way:

// it's correct

assert someService.doSomething(new ObjectA_DTO()).getClass() == ObjectA_DTO.class;

I would like change call of SomeServiceImpl methods to use Object mapper. It could be achieved by generated code using JSR 269 or by AOP.

The problem is I wrote simple annotations processor and it doesn't handle type annotations at all. The source of simple annotations processor looks like this:

@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;

}

}

Any ideas how to use or how to access type annotations by SimpleAnnotationsProcessor? Usage of Pluggable Annotation Processing API is not necessary for me I think it would has better performance than Java reflection. Anyway I don't know how to access type annotation via Java Reflection too.

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值