重复注解与获取方法返回值泛型类型

Repeatable元注解,标记了此注解的注解。

能够在注解声明的地方多次使用。

此注解需要新增一个其它注解来配合使用。例如我有一个验证注解:

Repeatable.值为ValidateMapping。ValidateMapping中包含Validate注解的返回值 声明。

/**
 *
 * 验证标记接口,标记这个动作要做走哪些验证方法
 * @author caomin
 * @version 1.0
 * @Date 2017-09-04 13:47
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(ValidateMapping.class)
public @interface Validate {
    /**
     * 执行动作
     * @return
     */
    Action value();

    /**
     * 执行顺序,从1开始
     * @return
     */
    int order();
}

 

声明一个注解ValidateMapping,其返回值为要重复使用的那个注解的数组类型。

/**
 *重复注解
 * Created by caomin on 2017/9/8.
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateMapping {
    Validate[] value() default {};
}

 

 

在获取值时,需要这样获取:

Validate annotation = method.getAnnotation(Validate.class);
ValidateMapping validateMapping = method.getAnnotation(ValidateMapping.class);
if (annotation!=null){
    if (action==annotation.value()){
        methodList[annotation.order()]=method;
    }
}else if (validateMapping!=null){//重复注解
    for (Validate validate : validateMapping.value()) {
        if (action==validate.value()){
            methodList[validate.order()]=method;
        }
    }
}

如果一个方法只打了一个此种类型的注解,那么直接获取即可。如果打了多个此种类型的注解。需要获取声明的那个注解类型。然后通过其中的方法,获取实际的值。

 

 

 

获取某个方法,返回值的泛型的具体类型:

需要注意,必须获取对象的class对象,而不是代理对象。如果是代理对象,则获取不到他的真实对象

private String getReturnName(Class clazz,Method method) throws IllegalAccessException, InstantiationException, NoSuchMethodException {
    Class<?>[] parameterTypes = method.getParameterTypes();
    Method declaredMethod = clazz.getMethod(method.getName(), parameterTypes);
    Type genericReturnType = declaredMethod.getGenericReturnType();
    Class<?> returnType = declaredMethod.getReturnType();
    if (genericReturnType instanceof ParameterizedType) {//如果包含泛型 ParameterizedType parameterizedType = (ParameterizedType) genericReturnType; //获取真实类型 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); String returnName = returnType.getSimpleName();
        //泛型名字
        String fanName = "";
        if (returnType.newInstance() instanceof Map) {
            if (actualTypeArguments.length == 2) {
                fanName = ((Class) actualTypeArguments[1]).getSimpleName().toLowerCase();
            }
        } else if (returnType.newInstance() instanceof Collection) {
            if (actualTypeArguments.length != 0) {
                fanName = ((Class) actualTypeArguments[0]).getSimpleName().toLowerCase();
            }
        }
        return fanName + returnName;
    } else {//不是泛型
        return returnType.getSimpleName().toLowerCase();
    }
}

 

转载于:https://my.oschina.net/begin01running/blog/1532301

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值