java反射获得对象内的方法.属性.注解

先声明两个注解

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

@Retention(RetentionPolicy.RUNTIME)
//注解放在属性上面
@Target({ElementType.FIELD})
public @interface MyFieldAnnotation {
}

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

@Retention(RetentionPolicy.RUNTIME)
//注解放在方法上面
@Target({ElementType.METHOD})
public @interface MyMethodAnnotation {


    String value();

    String other();
}

再声明一个类 A



public class A {

    @MyFieldAnnotation
    private String name ="张三";
    public  String sex ="男";

    public A() {
    }

    public A(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    @MyMethodAnnotation(value = "注解的值",other = "另一个值")
    public String methodA(String msg){
        return msg;
    }

    private int methodB(int i){
        return i;
    }


}

开始反射 A 对象


  A a = new A();
        /***
         *  反射获得类的三种方法
         *  Class cla = Class.forName("类路径名");
         *  Class aClass =  A.class;
         *  Class clazz =  a.getClass();
         */
        Class clazz =  a.getClass();

        /***
         * 获得类所在的包路径
         */
        Package clazzPackage =  clazz.getPackage();
        /**
         * 获得类名称以及路劲
         */
        String clazzName =  clazz.getName();

        /***
         * 获得当前类的所有属性 仅仅是公有
         */
        Field[] publicFields =  clazz.getFields();

        /***
         * 获得当前类的所有属性 包括私有的
         */
        Field[] allFields =  clazz.getDeclaredFields();
        /**
         * 获得具体公有属性
         */
        Field sex =  clazz.getField("sex");
        /**
         * 获得具体属性 可以是私有
         */
        Field name =  clazz.getDeclaredField("name");
        /**
         * setAccessible 为 true 代表可以操作私有属性
         * set 方法需要的参数 1 :类实例 2 :新的值
         */
        name.setAccessible(true);
        A newA = (A)clazz.newInstance();
        name.set(newA,"李四");

        /***
         * 获得所有 public 修饰的方法 包括父类的
         */
        Method[] methods =  clazz.getMethods();
        /***
         * 获得当前类的所有方法 包括私有的
         */
        Method[] declaredMethods = clazz.getDeclaredMethods();
        /**
         *  获取当前类的指定方法
         *  该操作无法获取私有方法  但可以获得父类公用方法
         *  参数 1:方法名称  参数 2 :方法参数   如果没有参数可以不填
         */
        Method methodA = clazz.getMethod("methodA", String.class);
        /**
         * 反射调用方法
         * invoke 需要的参数 1 :对象实例 2 :方法参数
         */
        A newA2 = (A)clazz.newInstance();
        Object result = methodA.invoke(newA2,"执行方法");
        System.out.println(result);// 输出 : 执行方法
        /***
         * 获取当前类的指定方法
         * 该操作可以获取私有方法 但无法获得父类方法
         */
        Method methodB = clazz.getDeclaredMethod("methodB",int.class);

        /**
         * 获得属性上面的所有注解
         */
        Annotation[] fieldAnnotations = name.getAnnotations();
        /**
         * 获得属性上具体某个注解
         */
        Annotation myFieldAnnotation = name.getAnnotation(MyFieldAnnotation.class);
        /**
         * 获得方法上面的所有注解
         */
        Annotation[] methodBAnnotations = methodA.getAnnotations();
        /**
         * 获得方法上具体某个注解
         */
        MyMethodAnnotation myMethodAnnotation = methodA.getAnnotation(MyMethodAnnotation.class);
        /**
         * 获得注解的值
         * 属性上的注解也是一样操作
         */
        String value =  myMethodAnnotation.value();
        System.out.println(value);//注解的值
        String other = myMethodAnnotation.other();
        System.out.println(other);//另一个值


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值