0
点赞
收藏
分享

微信扫一扫

java反射常用api

书坊尚 2022-02-15 阅读 98

1案例的类

import com.example.demo3.fanshe.MyAnnotation;
import lombok.Data;

/**
 * @author zw
 * @Description 描述
 * @create /2022/2/13
 */
@MyAnnotation("Dog class Annotation")
@Data
public class Dog {
    @MyAnnotation("Dog field-> name Annotation")
    private String name="dog旺旺";
    public String age="dog5岁";
    protected String test="test岁";
      String tesat="test岁";
    //无参构造
    public  Dog(){
        System.err.println("我是无参构造");
    }
    public void Dog(String age) {
        this.age = age;
        System.out.println("方法Dog 入参age"+age);
    }
    public void dog1(String age){
        System.err.println("我是方法dog1:"+age);
    }
    public void dog1(String age,String name){
        System.err.println("我是方法dog1==="+age+name);
    }
    //有参构造
    public Dog(String age) {
        this.age = age;
        System.out.println("有参构造age"+age);
    }
    //有参构造
    public Dog(String name, String age) {
        this.name = name;
        this.age = age;
        System.out.println("有参构造name+age"+name+age);
    }
   @MyAnnotation(value = "Dog method Annotation")
    public void testMyAnnotation(String name, String age) {
        System.out.println("testMyAnnotation:"+name+age);
    }

}


/**
 * @author zw
 * @Description 描述
 * @create /2022/2/6
 */
//value 可以自动省略
//@Target(ElementType.METHOD)
@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented //表示把注解生成在Javadoc中
@Inherited //表示可以被继承
public @interface MyAnnotation {
    String value();
}

2反射方法

        //1三种获取反射方法
        Dog dog = new Dog();
        Class c1 = dog.getClass();//一般不用 因为反射效率低 都能new 对象了就不用反射了
         System.out.println(c1.getName()); //  com.example.demo3.fanseh.Dog
        Class c2 =Dog.class;
        Class c3 = Class.forName("com.example.demo3.fanseh.Dog");
        //2 反射获取所有方法
        //获取所有的方法(包含private修饰的方法)
        Method[] declaredMethods = c2.getDeclaredMethods();
        for (Method method : declaredMethods) {
            method.getName();//方法名
            method.invoke(c2.newInstance(),"参数1","参数2");//执行方法
        }
        //获取指定的方法 参数一是方法名,后面是可变参,是参数类型
        Method method1 = c2.getDeclaredMethod("dog1", String.class);
        //参数一 类的实例 参数二 是参数    invoke执行这个方法
        method1.invoke(c2.newInstance(),"参数");
        
        //3 获取所有的属性(包含private修饰的方法)
        Field[] declaredFields = c2.getDeclaredFields();
        Dog dog1 = new Dog();//需要赋值的对象
        String str="反射赋值";
        for (Field field : declaredFields) {
            field.setAccessible(true);//解除私有限定 要不私有属性设置值等会报错
            //获取属性名称
            String name = field.getName();
            String type = field.getType().toString();
            if (type.endsWith("String")) {
                field.set(dog1,  str); // 给属性设值
            } else if (type.endsWith("int") || type.endsWith("Integer")&&null!=str) {
                field.set(dog1, new Integer((String) str)); // 给属性设值
            }  else if (type.endsWith("BigDecimal")&&null!=str) {
                field.set(dog1, new BigDecimal((String) str)); // 给属性设值
            } else if (type.endsWith("List")&&null!=str) {
                field.set(dog1, str); // 给属性设值
            } else {
                field.set(dog1, str);
            }
        }
       //获取注解
        // Dog类上是否含有这个注解
        boolean annotation = c2.isAnnotationPresent(MyAnnotation.class);
        System.err.println("Dog类是否含有注解:"+annotation);//是否含有注解:true
        Method[] declaredMethods1 = c2.getDeclaredMethods();
        for (Method method2 : declaredMethods1) {
            //Dog类中 方法是否含有注解
            boolean methodAnnotation = method2.isAnnotationPresent(MyAnnotation.class);
            if(methodAnnotation){
                MyAnnotation annotation2 = method2.getAnnotation(MyAnnotation.class);
                System.out.println("注解的值"+annotation2.value());//注解的值Dog method Annotation
            }
        }
        Field[] declaredFields1 = c2.getDeclaredFields();
        Dog dog2 = new Dog();//需要赋值的对象
        dog2.setName("反射获取值");
        for (Field field : declaredFields1) {
            boolean annotationPresent = field.isAnnotationPresent(MyAnnotation.class);
            if(annotationPresent){
                MyAnnotation annotation1 = field.getAnnotation(MyAnnotation.class);
                System.out.println(annotation1.value());//Dog field-> name Annotation
                field.setAccessible(true);//设置私有属性也能获取值
                System.out.println(field.get(c2.newInstance()));//dog旺旺
                System.out.println(field.get(dog2));//反射获取值
            }
        }

3 java反射内存图

引用别人内存图 Java基础之—反射(非常重要)_sinat_38259539的博客-CSDN博客_jvav

 

举报

相关推荐

0 条评论