通过反射获取类的完整结构

Field、Method、Constructor、Superclass、Interface、Annotation

//父类
package com.ztc.reflect;

public class Creature {
    public double weight;
    
    public void breath(){
        System.out.println("呼吸");
    }
}


//子类
package com.ztc.reflect;

@MyAnnotation(value="ztc")
public class Person extends Creature implements Comparable,MyInterface{
    public String name;
    private int age;
    int id;
    
    public Person() {
        super();
    }
    public Person(String name) {
        super();
        this.name = name;
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    
    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    @MyAnnotation(value="show") 
    public void show(){
        System.out.println("我是一个人");
    }
    public void display(String nation) throws Exception{
        System.out.println("我的国籍是:"+nation);
    }
    
    class Brid{
        
    }
    
}

Field:

package com.ztc.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.junit.Test;


public class TestField {
    //获得对应的运行时类的属性
    @Test
    public void test1(){
        Class clazz=Person.class;
        //1.getFields()只能申请到运行时类中声明为public的属性(包含父类中的属性)
        Field[] fields=clazz.getFields();
//      for(int i=0;i

Method:

package com.ztc.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Test;

public class TestMethod {
    //获取运行时类的方法
    @Test
    public void test1(){
        Class clazz=Person.class;
        //getMethods获得该类或父类中的public方法
        Method[] m1=clazz.getMethods();
        for(Method m:m1){
            System.out.println(m);
        }
        System.out.println("-----------------------------------------------------------");
        //getDeclaredMethods获得类本身的所有方法
        Method[] m2=clazz.getDeclaredMethods();
        for(Method m:m2){
            System.out.println(m);
        }
        
    }
    
    
    //获取方法的注解、权限修饰符、返回值类型、变量名、形参列表、异常
    @Test
    public void test2(){
        Class clazz=Person.class;
        Method[] m2=clazz.getDeclaredMethods();
        for(Method m:m2){
            //1.注解
            Annotation[] a1 = m.getAnnotations();
            for(Annotation a:a1){
                System.out.println("Method Annotation"+a);
            }
            //2.权限修饰符
            String str=Modifier.toString(m.getModifiers());
            System.out.println("Method Modifier: "+str);
            //3.返回值类型
            Class returnType=m.getReturnType();
            System.out.println("Method ReturnType: "+returnType.getName());
            //4.变量名
            String name=m.getName();
            System.out.println("Method name: "+name);
            //5.形参类型列表
            Class[] params=m.getParameterTypes();
            for(Class p:params){
                System.out.println("Method param: "+p.getName());
            }
            //6.异常
            Class[] exTypes=m.getExceptionTypes();
            for(Class e:exTypes){
                System.out.println("Method exType: "+e.getName());
            }
            
        }
    }
}

Constructor:

package com.ztc.reflect;

import java.lang.reflect.Constructor;

import org.junit.Test;

public class TestConstructor {
    @Test
    public void test1() throws Exception{
        String className="com.ztc.reflect.Person";
        Class clazz=Class.forName(className);
        Object obj=clazz.newInstance();
        Person p=(Person) obj;
        System.out.println(p);
    }
    
    //获取构造器
    @Test
    public void test2() throws Exception{
        String className="com.ztc.reflect.Person";
        Class clazz=Class.forName(className);
        Constructor[] cons=clazz.getDeclaredConstructors();
        for(Constructor c:cons){
            System.out.println(c);
        }
    }
}

其他:

package com.ztc.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.junit.Test;

public class TestOthers {

    //获取运行时类的父类
    @Test
    public void test1(){
        Class clazz=Person.class;
        Class superClass=clazz.getSuperclass();
        System.out.println(superClass);
    }
    
    //获取带泛型的父类
    @Test
    public void test2(){
        Class clazz=Person.class;
        Type type=clazz.getGenericSuperclass();
        System.out.println(type);
    }
    
    //父类的泛型
    @Test
    public void test3(){
        Class clazz=Person.class;
        Type type=clazz.getGenericSuperclass();
        //强转成子类
        ParameterizedType param=(ParameterizedType) type;
        Type[] ars=param.getActualTypeArguments();
        System.out.println(ars[0]);
        
    }
    
    //获取实现的接口(接口的父接口是获取不到的)
    @Test
    public void test4(){
        Class clazz=Person.class;
        Class[] interfaces=clazz.getInterfaces();
        for(Class i:interfaces){
            System.out.println(i);
        }
    }
    
    //获取所在的包
    @Test
    public void test5(){
        Class clazz=Person.class;
        Package pack=clazz.getPackage();
        System.out.println(pack);
    }
    
    //获取类的注解
    @Test
    public void test6(){
        Class clazz=Person.class;
        Annotation[] anns=clazz.getAnnotations();
        for(Annotation a:anns){
            System.out.println(a);
        }
    }
    
    
    
    
}

你可能感兴趣的:(通过反射获取类的完整结构)