Java basics-obtaining class information through reflection

In the previous section, we introduced the detailed content of the reflection mechanism . In this section, we will introduce the acquisition of class attributes and methods through reflection.

1. Get Field

public class ReflectTest03 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //1.获取整个类
        Class studentClass=Class.forName("cn.yujie.bean.Student");
        String className=studentClass.getName();
        System.out.println(className);
        System.out.println(studentClass.getSimpleName());

        //获取类中的所有public的Field
        Field[] fields=studentClass.getFields();
        System.out.println(fields.length);      //测试数组中只有1个元素
        //测试这个field
        Field f=fields[0];
        //取出field的它的名字
        String fieldName=f.getName();
        System.out.println(fieldName);

        //获取所有的Field
        //获取所有的Field
        Field[] fs=studentClass.getDeclaredFields();
        System.out.println(fs.length);
        System.out.println("-----------------");
        //遍历
        for(Field field :fs){
    
    
            //获取属性的修饰符列表
            //返回的修饰符是一个数字,每个数字是修饰符的代号
            int i=field.getModifiers();
            //可以将这个代号数字转换成字符串
            String ModifierString = Modifier.toString(i);
            System.out.print(ModifierString+" ");
            //获取属性的类型
            Class fieldType=field.getType();
            String fName=fieldType.getSimpleName();
            System.out.print(fName+" ");
            //获取属性的名字
            System.out.println(field.getName()+" ");
            System.out.println();
        }
    }
}

2. Decompile Field

Decompile the attribute Field of a class through reflection mechanism

public class ReflectTest05 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //创建这个是为了拼接字符串
        StringBuilder s=new StringBuilder();
        Class studentClass=Class.forName("cn.yujie.bean.Student");
        s.append(Modifier.toString(studentClass.getModifiers())+" class "+studentClass.getSimpleName()+"{");
        Field[] fields=studentClass.getDeclaredFields();
        s.append(";\n");
        for(Field field:fields){
    
    
            s.append("\t");
            s.append(Modifier.toString(field.getModifiers()));
            s.append(" ");
            s.append(field.getType().getSimpleName());
            s.append(" ");
            s.append(field.getName());
            s.append(";\n");
        }
        s.append("}");
        System.out.println(s);
    }
}

Insert picture description here

3. Access object properties through reflection mechanism

Must master:
how to access the properties of an object through the reflection mechanism,
assign a value to the property set to
obtain the value of the property get

public class ReflectTest07 {
    
    
    public static void main(String[] args) throws Exception{
    
    

        Class studentClass=Class.forName("cn.yujie.bean.Student");
        //使用反射机制,怎么取访问一个对象的属性
        Object obj=studentClass.newInstance();  //obj就是student对象(底层调用无参数构造方法)
        //获取no属性(根据属性的名称获取Field)
        Field noFiled=studentClass.getDeclaredField("no");
        //给obj对象(Student对象)的no属性赋值
        //s.no=111;         //给s对象的no属性赋值111,三要素,对象,属性和值
        //虽然使用了反射机制,但是三要素还是缺一不可
        //注意,反射机制让代码复杂了,但是为了一个灵活,这也是值得的。
        noFiled.set(obj,222);           //给obj对象的no属性赋值222
        //读取属性的值
        //两个要素:获取obj对象no属性的值
        System.out.println(noFiled.get(obj));
        //可以访问私有属性吗
        Field nameField=studentClass.getDeclaredField("name");
        //打破封装(反射机制的缺点:打破封装,可能会给不法分子留下机会)
        nameField.setAccessible(true);
        //给name属性赋值
        nameField.set(obj,"jackson");
        //获取name属性的值
        System.out.println(nameField.get(obj));

    }
}

4. Variable length parameters

Variable length parameter
int... args This is the variable length parameter. The
syntax is: type... (note: it must be 3 dots)

  • 1. The number of parameters required for variable length parameters is 0-n
  • 2. The variable length parameter must be at the last position in the parameter list, and there must be only one variable length parameter
  • 3. Variable length can be seen as an array
public class ArgumentsTest {
    
    
    public static void main(String[] args) {
    
    
      m();
      m(10);
      m(20,40);
      //m("abc");编译错误
      m3("ab","cd","def","de9");
      String [] strs={
    
    "a","b","c"};
      m3(strs);
    }
    public static void m(int... args){
    
    

    }
    //必须在最后一个,且只有1个
//    public static void m2(String... args1,int...args2){
    
    
//
//    }
    public static void m3(String ...args){
    
    
        //可以将可变长度参数当成一个数组来看
        for(int i=0;i<args.length;i++){
    
    
            System.out.println(args[i]);
        }
    }
}

5. Reflection Method (understand)

public class ReflectTest08 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //获取类了
        Class userServiceClass = Class.forName("cn.yujie.service.UserService");
        //获取所有的Method(包括私有的)
        Method[] methods=userServiceClass.getDeclaredMethods();
        System.out.println(methods.length);         //2
        //遍历Method
        for(Method method:methods){
    
    
            //获取修饰符列表
            System.out.println(Modifier.toString(method.getModifiers()));
            //获取方法的返回值类型
            System.out.println(method.getReturnType().getSimpleName());
            //获取方法名
            System.out.println(method.getName());
            //方法的参数列表(一个方法的参数可能有多个)
            Class[] parameterTypes=method.getParameterTypes();
            for(Class parameterType:parameterTypes){
    
    
                System.out.println(parameterType.getSimpleName());
            }
        }

    }
}

6. Decompile (understand)

public class ReflectTest09 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        StringBuilder s=new StringBuilder();
        Class userServiceClass = Class.forName("cn.yujie.service.UserService");
        s.append("public class UserService{ ");
        s.append(Modifier.toString(userServiceClass.getModifiers())+"class "+userServiceClass.getSimpleName()+"{ "+"\n");
        Method[] methods=userServiceClass.getDeclaredMethods();
        for(Method method:methods){
    
    
            s.append("\t");
            s.append(Modifier.toString(method.getModifiers()));
            s.append(" ");
            s.append(method.getReturnType().getSimpleName());
            s.append(" ");
            s.append(method.getName());
            s.append("(");
            //参数列表
            Class[] parameterTypes = method.getParameterTypes();
            for(Class parameterType:parameterTypes){
    
    
                s.append(parameterType.getSimpleName());
                s.append(",");
            }
            s.append("){}\n");
        }
        //删除指定下标位置上的字符
        s.deleteCharAt(s.length()-1);
        s.append("}");
        System.out.println(s);
    }
}

7. The reflection mechanism calls the method

Key point: You must master how to call a method through the reflection mechanism. The reflection mechanism makes the code more versatile. After modifying the configuration file in the future, the created objects will be different and the calling methods will be different, but the java code does not need to be changed. This is the charm of reflection .

public class ReflectTest10 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //通过反射机制调用方法
        Class userServiceClass = Class.forName("cn.yujie.service.UserService");
        //创建对象
        Object obj=userServiceClass.newInstance();
        //java中有重载,java中通过方法名和形参来区分方法
        Method loginMethod=userServiceClass.getDeclaredMethod("login",String.class,String.class);
        //调用方法
        //调用方法有几个要素
        //要素1:对象,要素2:方法名,要素3:参数列表,要素4:返回值
        //反射机制中最重要的一个方法:必须记住
        //四要素
        /*
        loginMethod方法,obj对象
        admin,123是参数列表,retValue是返回值
        * */
        Object retValue=loginMethod.invoke(obj,"admin","123");
    }
}

8. Reflection Constructor

public class ReflectTest11 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        StringBuilder s=new StringBuilder();
        Class vipClass=Class.forName("cn.yujie.bean.Vip");
        s.append(Modifier.toString(vipClass.getModifiers()));
        s.append(" class ");
        s.append(vipClass.getSimpleName());
        s.append("{\n");
        //拼接构造方法
        Constructor[] constructors=vipClass.getDeclaredConstructors();
        for(Constructor constructor:constructors){
    
    
            s.append("\t");
            s.append(Modifier.toString(constructor.getModifiers()));
            s.append(" ");
            s.append(vipClass.getSimpleName());
            s.append("(");
            Class[] parameterTypes=constructor.getParameterTypes();
            for(Class parameterType:parameterTypes){
    
    
                s.append(parameterType.getSimpleName());
                s.append(",");
            }
            if(parameterTypes.length>0) {
    
    
                s.deleteCharAt(s.length() - 1);
            }
            s.append("){}\n");
        }
        s.append("}");
        System.out.println(s);
    }
}

9. The reflection mechanism calls the constructor

public class ReflectTest12 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //使用反射机制怎么创建对象
        Class vipClass=Class.forName("cn.yujie.bean.Vip");
        //调用无参构造方法
        Object obj= vipClass.newInstance();
        //调用有参数构造方法
        //第一步:先获取到有参数构造方法
        Constructor con=vipClass.getDeclaredConstructor(int.class,String.class,String.class,boolean.class);
        //第二步:调用构造方法new对象
        Object newObj=con.newInstance(110,"jackson","1999-10-11",true);
        System.out.println(newObj);
    }
}

Insert picture description here

10. Get the parent class and parent interface

public class ReflectTest13 {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //String举例
        Class stringClass=Class.forName("java.lang.String");
        //获取String的父类
        Class superClass=stringClass.getSuperclass();
        System.out.println(superClass.getSimpleName());
        //获取String类实现的所有接口
        Class[] interfaces=stringClass.getInterfaces();
        for(Class in:interfaces){
    
    
            System.out.println(in.getName());
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/113871560