Java反射小结

1、获取指定类型继承的父类以及实现的接口类
  调用方法:类.getSuperclass()获取父类(由于单根继承性,只能获得一个父类)
       类.getInterfaces()获取实现的接口类(获得接口类数组)

                         类.getName(获取类的全名(带包名))

       类.getSimpleName(获取类名不包含包名)

  代码如下:

public static void getSuperClassAndInterfaces(Class clazz){
        System.out.println("该类的父类是:"+clazz.getSuperclass().getSimpleName());//getName和getSimpleName的区别:getname包含类的父类,getSimpleName只有本类名
        System.out.print("该类实现的接口有:");
        Class[] classes=clazz.getInterfaces();
        for (Class class1 : classes) {//遍历获取的接口类的数组
            System.out.print(class1.getSimpleName()+"	");            
        }        
    }    

2、获取类访问修饰符和类返回类型以及获取类名

           现有的修饰符及各位置使用的修饰符列表如下:

   类修饰符(class):public  protected  private 以及默认修饰符(有用friendly作为默认修饰符)

                          abstract  static   final   strict

           接口修饰符(interface):public   protected  private  abstract  static  strict

            构造函数(constructor):public  protected  private

                  成员方法(method):public  protected  private  abstract  static  final  synchronized  native  strict

                         成员属性(field:public protected private  static final transient

       调用方法:类.getModifiers()获取类的修饰数量,返回类型是int

                         Modifier.toString(int类型变量)  获取访问修饰符配合getModifiers()使用

  代码如下:

public static void getClassAndModifiers(Class clazz){
        System.out.print(Modifier.toString(clazz.getModifiers()));
        System.out.print(" class ");
        System.out.println(clazz.getSimpleName());
    }

3、获取所有的成员变量

          调用方法:类.getField()以及getDeclaredField()   单数形式

          类.getFields()以及getDeclaredFields()  复数形式

                            getField获取非私有字段    getDeclaredField可以获取私有字段(推荐使用)

                             Modifier.toString(field.getModifiers())  获取成员变量的修饰符

        field.getType().getSimpleName()    获取成员变量的类型

        field.getName()      获取成员变量名

        因为成员变量可能是多个,所以用复数的方法得出的成员变量为一个数组,再用数组遍历得出所有信息。

     代码如下:         

public static void getFields(Class clazz) {
           Field[] fields = clazz.getDeclaredFields();            
            for (Field field : fields) {
                System.out.println(Modifier.toString(field.getModifiers())+" "
                                   +field.getType().getSimpleName()+" "+field.getName());
            }
            
     }

4.获取所有成员方法

    调用方法:类.getMethod()           类.getDeclaredMethod()

           类.getMethods()          类.getDeclaredMethods()

        获取成员方法的修饰符:Modifier.toString(method.getModifiers()) 
    获取成员方法的返回类型的名称:
method.getReturnType().getSimpleName()
获取方法的名称:method.getName()
获取方法的所有参数:method.getParameterTypes()
 

      代码如下: 

 public static void getMethods(Class clazz) throws Exception{
         //getMethod    getDeclaredMethod   单数
         //getMethods   getDeclaredMethods  复数+s
         Method[] methods=clazz.getMethods();
         Method[] declaredMethods=clazz.getDeclaredMethods();
         System.out.println("打印的是所有的方法,包含父类");
         for (Method method : methods) {
            System.out.println(Modifier.toString(method.getModifiers())+" "+method.getReturnType().getSimpleName()+" "+method.getName());
            System.out.print(method.getName()+"方法的参数有:");
            for (Class class1 : method.getParameterTypes()) {
                System.out.print(class1.getSimpleName()+",");
            }
            System.out.println();
         }
         for (Method method : declaredMethods) {
            System.out.println(Modifier.toString(method.getModifiers())+" "+method.getReturnType().getSimpleName()+" "+method.getName());
            System.out.print(method.getName()+"方法的参数有:");
            for (Class class1 : method.getParameterTypes()) {
                System.out.print(class1.getSimpleName()+",");
            }
            System.out.println();
        }
}

5.获取构造函数

       调用方法:类.getConstructor

       可以利用调用的方法直接实例对象   

     代码如下:

public static void getConstructors(Class clazz) throws Exception{
         Constructor constructor=clazz.getConstructor(String.class,int.class,String.class);
         Object obj = constructor.newInstance("李四",18,"0002");
            Student stu=(Student)obj;
            stu.talk();
     }     

6:注解Annotation

  1.四元注解.@Target({通过ElementType选择作用范围})作用范围、@Documented  在Api的注释@Retention(RetentionPolicy.RUNTIME)确认注释的类型 @Inherited是否可以继承

  2.用@interface修饰且必须是用public修饰的

  3:在注解中如何定义属性
  @Target({ElementType.METHOD,ElementType.TYPE})
  @Documented
  @Retention(RetentionPolicy.RUNTIME) //运行时注解
  @Inherited     

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public @interface StudentAnnotation {
       String studentName();
}
原文地址:https://www.cnblogs.com/shqnl/p/11052231.html