java反射获取类的所有信息(方法,属性,构造,注解)

import java.lang.annotation.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * TODO
 *
 * @author syl
 */
public class TestReflect {
    public static void main(String[] args) {
        try {
            //获取字节码对象
            Class<?> r= Class.forName("com.tedu.test.Reflect");
            //类操作
            //class1(r);

            //属性操作
            //field(r);


            //方法操作
            method(r);

            //构造方法方法
            //constructor(r);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

类的相关方法

   /**
     * 类操作
     * @param r
     */
    private static void class1(Class<?> r) {
        //获取全限定类名
        r.getName();
        //获取包名
        r.getPackage().getName();
        //获取类上所有注解
        Annotation[] annotations = r.getAnnotations();
        //获取类上指定的注解
        ClassAnnotation classAnnotation =r.getAnnotation(ClassAnnotation.class);
        //注解的全限定类名
        String aClass = classAnnotation.annotationType().getName();
        //判断是否有指定注解
        boolean b = r.isAnnotationPresent(ClassAnnotation.class);
    }

属性的相关方法

  /**
     * 属性操作
     * @param r
     */
    private static void field(Class<?> r) {
        try {
            //获取指定属性
            Field id = r.getDeclaredField("id");
            //获取属性名
            System.out.println(id.getName());
            //获取属性类型
            System.out.println(id.getType().getName());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        //获取所有属性
        Field[] fields = r.getDeclaredFields();
        for (Field field : fields) {
            //获取属性上面指定的指定的注解
            FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class);
            //获取注解属性值
            System.out.println(fieldAnnotation.value());
        }
    }

构造方法相关操作

 /**
     * 构造方法操作
     * @param r
     */
    private static void constructor(Class<?> r) {
        //获取所有构造方法
        Constructor<?>[] constructors = r.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            //获取构造方法上面指定的注解
            ConstructorAnnotation constructorAnnotation = constructor.getAnnotation(ConstructorAnnotation.class);
            //获取注解的属性值
            if ("含参构造".equals(constructorAnnotation.value())){
                //获取各个参数名
                Parameter[] parameters = constructor.getParameters();
                for (Parameter parameter : parameters) {
                    System.out.println(parameter.getName());
                }
                //获取各个参数类型
                Class<?>[] parameterTypes = constructor.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    System.out.println(parameterType.getName());
                }
                //获取参数数量
                System.out.println(constructor.getParameterCount());
            }
        }
    }

方法的操作

   /**
     *方法操作
     * @param r
     */
    private static void method(Class<?> r) {
        //获取所有方法
        Method[] methods = r.getDeclaredMethods();
        for (Method method : methods) {
            //获取方法上指定的注解
            MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class);
            System.out.println(methodAnnotation.value());
            //获取方法名
            System.out.println(method.getName());
            //获取返回值类型
            System.out.println(method.getReturnType().getName());
            //获取各个形参数名及类型
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                System.out.println(parameter.getName());
                System.out.println(parameter.getType().getName());
                //获取参数的注解
                System.out.println(parameter.getAnnotation(ParameterAnnotation.class).value());
            }
        }
    }
}

用于反射的类

@ClassAnnotation("反射")
class Reflect{
    @FieldAnnotation("姓名")
    private String name;

    @FieldAnnotation("id")
    private Integer id;

    @ConstructorAnnotation("无参构造")
    private Reflect(){

    }

    @ConstructorAnnotation("含参构造")
    private Reflect(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

  @MethodAnnotation("公共方法获取实例")
//    public static Reflect getReflect(){
//        return new Reflect();
//    }
//
//    @MethodAnnotation("获取name")
//    public String getName() {
//        return name;
//    }

    @MethodAnnotation("设置name")
    public void setName(@ParameterAnnotation("姓名") String name) {
        this.name = name;
    }

//    @MethodAnnotation("获取id")
//    public Integer getId() {
//        return id;
//    }
//
//    @MethodAnnotation("设置id")
//    public void setId(Integer id) {
//        this.id = id;
//    }
}

定义各种注解

/**
 * 类注解
 * @author syl
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface ClassAnnotation{
    String value() default "";
}

/**
 * 属性注解
 * @author syl
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface FieldAnnotation{
    String value() default "";
}

/**
 * 方法注解
 * @author syl
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MethodAnnotation{
    String value() default "";
}

/**
 * 构造方法注解
 * @author syl
 */
@Target({ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@interface ConstructorAnnotation{
    String value() default "";
}

/**
 * 参数注解
 * @author syl
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface ParameterAnnotation{
    String value() default "";
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值