获取运行时类的完整结构

       获取运行时类的完整结构:

package Reflection2;
@MyAnntation(value = "hi")
public class Person extends Creature<String> implements Comparable<String>,MyInterface
{
   private String name;
   int age;
   public int id;

   public Person()
  {

  }
   @MyAnntation(value = "abc")
   private Person(String name)
  {
       this.name=name;
  }
   public Person(String name,int age)
  {
        this.name=name;
        this.age=age;
  }
   @MyAnntation   //默认的
   private String show(String nation)
  {
       System.out.println("我的国籍是:"+nation);
       return nation;
  }
   public String display(String interests,int age)
  {
       return interests+age;
  }

   @Override
   public void info()
  {
       System.out.println("我是一个人");
  }

   @Override
   public int compareTo(String o)
  {
       return 0;
  }
}
package Reflection2;
//Person的父类
import java.io.Serializable;

public class Creature<T> implements Serializable
{
  private char gendr;
  public double weight;

  private void breath()
  {
      System.out.println("生物呼吸");
  }
  public void eat()
  {
      System.out.println("生物进食");
  }
}
package Reflection2;
//Porson类的接口
public interface MyInterface
{
   void info();
}
package Reflection2;
//自定义注解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
/*
注解
*/

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnntation
{
   String value()default "hello";
}

 

package Reflection3;

import Reflection2.Person;

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

/*
获取当前运行时类的属性结构
*/
public class FieldTest
{
   public void test1()
  {
       Class clazz= Person.class;
       //获取属性结构
       //getFields():获取当前运行时类及其父类中声明为public访问权限的属性
       Field[] fields = clazz.getFields();//获取所有丰富的属性
       for (Field f:fields)
      {
           System.out.println(f);
      }
       System.out.println();
    //getDeclaredFields(): 获取当前运行时类及其父类中声明所有属性。(不包含父类中声明的属性)
       Field[] declaredFields = clazz.getDeclaredFields();
       for (Field f:declaredFields)
      {
           System.out.println(f);
      }
  }
   //权限修饰符 数据类型 变量名
 public void test2()
{
     Class clazz= Person.class;
     Field[] declaredFields = clazz.getDeclaredFields();
     for (Field f:declaredFields)
    {
         //权限修饰符
         int modifiers = f.getModifiers();
         System.out.print(Modifier.toString(modifiers)+"\t");

         //数据类型
              Class type=f.getType();
         System.out.print(type.getName()+"\t");

         //变量名

         String name = f.getName();
         System.out.print(name);

         System.out.println();

    }
}

   public static void main(String[] args)
  {
       new FieldTest().test1();
       new FieldTest().test2();
  }
}
package Reflection3;

import Reflection2.Person;

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

/*
获取运行时类的方法结构
*/
public class MethodTest
{
   public void test1()
  {
       Class clzz= Person.class;

       //getMethods():获取当前运行时类及其所有父类中声明为public权限的方法
       Method[] methods = clzz.getMethods();
       for (Method method : methods)
      {
           System.out.println(method);
      }
       System.out.println();
       //getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)
       Method[] declaredMethods = clzz.getDeclaredMethods();
       for (Method m: declaredMethods)
      {
           System.out.println(m);
      }

  }

   /*
    @Xxxx
    权限修饰符 返回值类型 方法名(参数类型1 形参1,....)throws xxxException{}
     */
   public void test2()
  {
       //获取方法声明的注解
       Class clzz= Person.class;
       Method[] declaredMethods = clzz.getDeclaredMethods();
       for (Method m: declaredMethods)
      {
           Annotation[] annotations = m.getAnnotations();
           for (Annotation a:annotations)//真假for循环
          {
               System.out.println(a);
          }
           //2.权限修饰符
           System.out.print(Modifier.toString(m.getModifiers())+"\t");

           //返回类型
           System.out.print(m.getReturnType().getName()+"\t");
           System.out.println();

           //方法名
           System.out.println(m.getName());
           System.out.print("(");
           
           //形参列表
           Class[] parameterTypes = m.getParameterTypes();
           if (!(parameterTypes==null && parameterTypes.length==0))//判断方法参数是否为空
          {
               for (int i=0;i<parameterTypes.length;i++)
              {
                   if (i==parameterTypes.length-1)
                  {
                       System.out.print(parameterTypes[i].getName()+"args_"+i);
                       break;
                  }
                   System.out.print(parameterTypes[i].getName()+"args_"+i+",");
              }
          }
           System.out.print("(");
           
           //6.抛出的异常
           Class<?>[] exceptionTypes = m.getExceptionTypes();
           if (!(exceptionTypes==null&&exceptionTypes.length==0))
          {
               System.out.print("throws");
               for (int i=0;i<exceptionTypes.length;i++)
              {
                   if (i==exceptionTypes.length-1)
                  {
                       System.out.print(exceptionTypes[i].getName());
                       break;
                  }

              }
          }
           System.out.println();
      }

  }


   public static void main(String[] args)
  {
       new MethodTest().test1();
      new MethodTest().test2();
  }
}
package Reflection3;

import Reflection2.Person;

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

public class OtherTest
{
public void test1()
{
    /*
   获取构造器结构
    */
   Class clazz= Person.class;
   //getConstructors():获取当前运行时类中声明为public的构造器
   Constructor[] constructors = clazz.getConstructors();

   for (Constructor c : constructors)
  {
       System.out.println(c);
  }
   System.out.println();
//getDeclaredConstructors():获取当前运行时类中声明中所有的构造器
   Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
   for (Constructor c : declaredConstructors)
  {
       System.out.println(c);
  }
}

/*
获取运行时类的父类
*/
public void test2()
{
   Class clazz= Person.class;
   Class superclass = clazz.getSuperclass();
   System.out.println(superclass);
}

/*
获取运行时类的带泛型的父类
*/
public void test3()
{
   Class clazz=Person.class;
   Type genericSuperclass = clazz.getGenericSuperclass();
   System.out.println(genericSuperclass);
}

/*
获取运行时类的带泛型的父类的泛型
*/
public void Test4()
{
   Class clazz=Person.class;
   Type genericSuperclass = clazz.getGenericSuperclass();
   ParameterizedType paramtype=(ParameterizedType) genericSuperclass;
   Type[] actualTypeArguments = paramtype.getActualTypeArguments();//获取泛型(类型)参数
   System.out.println(actualTypeArguments[0].getTypeName());
}

/*
获取运行时类实现的接口
*/
   public void Test5()
  {
       Class clazz = Person.class;
       Class[] interfaces = clazz.getInterfaces();
       for (Class anInterface : interfaces)
      {
           System.out.println(anInterface);
      }
       System.out.println();
       
        //获取运行时类的父类实现的接口
       Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
       for (Class anInterface : interfaces1)
      {
           System.out.println(anInterface);
      }
  }

   /*
   获取运行时类所在的包
    */
public void Test6()
{
   Class clazz=Person.class;
   Package pack = clazz.getPackage();
   System.out.println(pack);
}

   /*
   获取运行时类声明的注解
    */
   public void Test7()
  {
       Class clazz=Person.class;
       Annotation[] annotations = clazz.getAnnotations();
       for (Annotation annotation : annotations)
      {
           System.out.println(annotation);
      }
  }


   public static void main(String[] args)
  {
       new OtherTest().test1();
       new OtherTest().test2();
       new OtherTest().test3();
       new OtherTest().Test4();
       new OtherTest().Test5();
       new OtherTest().Test6();
       new OtherTest().Test7();
  }
}
posted @ 2022-08-05 17:44  zjw_rp  阅读(24)  评论(0)    收藏  举报