通过反射获取类的结构信息

第一组 api0_1 Java.lang.Class 类

第二组 api0_2 Java.lang.reflect.Field 类

  1. getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
  2. getType:以 Class 形式返回类型
  3. getName:返回属性名

第三组 api0_3 Java.lang.reflect.Method 类

  1. getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
  2. getReturnType:以 Class 形式获取返回类型
  3. getName:返回方法名
  4. getParameterTypes:以 Class[ ] 形式获取参数类型数组

第四组 api_04 Java.lang.reflect.Constructor 类

  1. getModifiers:以 int 形式返回修饰符(默认修饰符是0,public 是1,private 是2,protected 是4,static 是8,final 是16)
  2. getName:返回构造器名(全类名)
  3. getParameterTypes:以 Class[ ] 形式获取参数类型数组
package com.ftn.reflection.question;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//通过反射获取类的结构信息
public class ReflectionUtils {
    public static void main(String[] args) {

    }

    @Test
    public void api_01() throws ClassNotFoundException {
        //获取Class对象
        Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
        //getName:获取全类名
        System.out.println(personClass.getName());
        System.out.println("--------------");

        //getSimpleName:获取简单类名
        System.out.println(personClass.getSimpleName());
        System.out.println("--------------");

        //getFields:获取所有 public 修饰的属性,包含本类以及父类的
        Field[] fields = personClass.getFields();
        for (Field field : fields) {
            System.out.println("本类及父类的属性=" + field.getName());
        }
        System.out.println("--------------");

        //getDeclaredFields:获取本类中所有属性
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("本类中所有属性="+declaredField.getName());
        }
        System.out.println("--------------");

        //getMethods:获取所有 public 修饰的方法,包含本类以及父类的
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println("本类及父类的方法="+method.getName());
        }
        System.out.println("--------------");

        //getDeclaredMethods:获取本类中所有方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("本类中所有方法="+declaredMethod.getName());
        }
        System.out.println("--------------");

        //getConstructors:获取所有 public 修饰的构造器,仅本类
        Constructor<?>[] constructors = personClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println("本类及父类中所有构造器="+constructor.getName());
        }
        System.out.println("--------------");

        //getDeclaredConstructors:获取本类中所有构造器
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println("本类中所有构造器="+declaredConstructor.getName());
        }
        System.out.println("--------------");

        //getPackage:以 Package 形式返回 包信息
        System.out.println(personClass.getPackage());
        System.out.println("--------------");

        //getSuperClass:以 Class 形式返回父类信息
        Class<?> superclass = personClass.getSuperclass();
        System.out.println("父类的Class对象="+superclass);
        System.out.println("--------------");

        //getInterfaces:以 Class[] 形式返回接口信息
        Class<?>[] interfaces = personClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("接口信息="+anInterface.getName());
        }
        System.out.println("--------------");

        //getAnnotations:以 Annotation[] 形式返回注解信息
        Annotation[] annotations = personClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("注解信息="+annotation);
        }
    }

    @Test
    public void api_02() throws ClassNotFoundException {
        //有关 Java.lang.reflect.Field 类的方法
        Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("本类的属性=" + declaredField.getName() + "\t修饰符的int值=" + declaredField.getModifiers());
            System.out.println( "本类属性类型=" + declaredField.getType());
        }
    }

    @Test
    public void api_03() throws ClassNotFoundException {
        //有关 Java.lang.reflect.Method 类的方法
        Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("本类中所有方法="+declaredMethod.getName() + "\t修饰符的int值=" + declaredMethod.getModifiers());
            System.out.println("返回值类型=" + declaredMethod.getReturnType());
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("参数类型=" + parameterType);
            }
            System.out.println("------------------");
        }
    }

    @Test
    public void api_04() throws ClassNotFoundException {
        //有关 Java.lang.reflect.Constructor 类的方法
        Class<?> personClass = Class.forName("com.ftn.reflection.question.Person");
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println("本类中所有构造器="+declaredConstructor.getName() + "\t修饰符的int值=" + declaredConstructor.getModifiers());
            Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("构造器参数类型=" + parameterType);
            }
            System.out.println("-----------------------");
        }
    }
}

class A{
    public String hobby;
    public void hi(){

    }
    public A(){

    }
}

interface IA{}

interface IB{}

@Deprecated
class Person extends A implements IA,IB{
    //属性
    public String name;
    protected int age;
    String job;
    private double sal;

    //构造器
    public Person(){

    }

    public Person(String name){

    }

    private Person(String name,int age){

    }

    //方法
    public void m1(){

    }
    protected void m2(String name){

    }
    void m3(String name,int age){

    }
    private void m4(){

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值