Java基础系列:获取加了注解的方法

1 注解

package com.company.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Name注解
 * @author xindaqi
 * @since 2020-12-26
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NameAnnotation {
    String name() default "xiaoxiao";
}

2 实体类

package com.company.common;

import com.company.annotation.NameAnnotation;

import java.io.Serializable;

/**
 * 公共测试类
 * @author xindaqi
 * @since 2020-12-26
 */

public class TestClass implements Serializable {
    private static final long serialVersionUID = 1377068656512767294L;

    private String name;

    private String address;

    @NameAnnotation(name = "one")
    public String annotationOne() {
        return "annotation one";
    }

    @NameAnnotation(name = "two")
    public String annotationTwo() {
        return "annotation two";
    }



    static {
        System.out.println("我是静态代码");
    }
    {
        System.out.println("我是动态代码");
    }

    public TestClass() {
        System.out.println("我是构造代码");
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "TestClass{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

3 获取加了指定注解的方法

package com.company.reflect;

import com.company.annotation.NameAnnotation;
import com.company.common.TestClass;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 反射获取类的方法
 * @author xindaqi
 * @since 2020-12-26
 */

public class ReflectMethod {

    public static void main(String[] args) {
        
        /**
         * 获取声明的方法名
         */
        Method[] declearedMethods = clzzUnderClassName.getDeclaredMethods();
        List<String> declearedMethodsList = Stream.of(declearedMethods).map(Method::getName).collect(Collectors.toList());
        System.out.println("声明的方法名:" + declearedMethodsList);
        List<String> annotationDeclaredMethods = Stream.of(declearedMethods).filter(method -> method.isAnnotationPresent(NameAnnotation.class)).map(Method::getName).collect(Collectors.toList());
        System.out.println("加了NameAnnotation注解的方法:" + annotationDeclaredMethods);

    }
}

4 小结

  • 获取加了注解的方法,通过isAnnotationPresent()方法判断当前方法是否使用了注解
  • 应用场景:通过指定注解,执行对应的方法,@XxlJob注解使用了该种方式,通过调度中心,完成执行中心的定时任务
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值