Java反射(三)

属性或者方法为参数化类型的处理

public class User {

    String name;

    Integer age;

    Car car;

    List hobbies;

    List carList;

    Map carMap;

    void addCar(Car car){
        carList.add(car);
    }

    void addHobby(List hobbies){

    }

    void addCar(List cars){

    }
}
public class Car {
    String carNum;
    List carColor;
}
public class Test {

    public static void main(String[] args) {

        Class clz = User.class;

        System.out.println("==========");

        //解析属性
        Field[] fields = clz.getDeclaredFields();
        for (Field field: fields){
            String name = field.getType().getName();
            System.out.println(field+": "+name);
        }
        System.out.println("==========");
        for (Field field: fields){
            String name = field.getType().getClass().getName();
            System.out.println(field+": "+name);
        }
        System.out.println("==========");
        for (Field field: fields){
            String name = field.getGenericType().getTypeName();
            System.out.println(field+": "+name);
        }
        System.out.println("==========");
        for(Field field: fields){
            if(field.getGenericType() instanceof ParameterizedType){
                Type[] types =((ParameterizedType)(field.getGenericType())).getActualTypeArguments();
                for(Type type:types){
                    System.out.println("方法1:"+type+": "+type.getTypeName());
                }
                Type type = ((ParameterizedType)(field.getGenericType())).getRawType();
                System.out.println("方法2:"+type+": "+type.getTypeName());
                System.out.println("==");
            }
        }

        System.out.println("====================华丽丽的分割线=====================");
        //解析方法
        Method[]  methods = clz.getDeclaredMethods();
        for(Method method:methods){
            Parameter[] parameters = method.getParameters();
            for(Parameter parameter:parameters){
                String name = parameter.getType().getName();
                System.out.println(method+": "+name);
            }
        }
        System.out.println("============");
        for(Method method:methods){
            Type[] types = method.getGenericParameterTypes();
            for(Type type : types){
                if(type instanceof ParameterizedType){
                    Type[] types1 = ((ParameterizedType)type).getActualTypeArguments();
                    for(Type type1 :types1){
                        String name = type1.getTypeName();
                        System.out.println(method+": "+name);
                    }
                }
            }
        }
    }
}

运行结果

==========
java.lang.String reflectdemo.User.name: java.lang.String
java.lang.Integer reflectdemo.User.age: java.lang.Integer
reflectdemo.Car reflectdemo.User.car: reflectdemo.Car
java.util.List reflectdemo.User.hobbies: java.util.List
java.util.List reflectdemo.User.carList: java.util.List
java.util.Map reflectdemo.User.carMap: java.util.Map
==========
java.lang.String reflectdemo.User.name: java.lang.Class
java.lang.Integer reflectdemo.User.age: java.lang.Class
reflectdemo.Car reflectdemo.User.car: java.lang.Class
java.util.List reflectdemo.User.hobbies: java.lang.Class
java.util.List reflectdemo.User.carList: java.lang.Class
java.util.Map reflectdemo.User.carMap: java.lang.Class
==========
java.lang.String reflectdemo.User.name: java.lang.String
java.lang.Integer reflectdemo.User.age: java.lang.Integer
reflectdemo.Car reflectdemo.User.car: reflectdemo.Car
java.util.List reflectdemo.User.hobbies: java.util.List
java.util.List reflectdemo.User.carList: java.util.List
java.util.Map reflectdemo.User.carMap: java.util.Map
==========
方法1:class java.lang.String: java.lang.String
方法2:interface java.util.List: java.util.List
==
方法1:class reflectdemo.Car: reflectdemo.Car
方法2:interface java.util.List: java.util.List
==
方法1:class java.lang.String: java.lang.String
方法1:class reflectdemo.Car: reflectdemo.Car
方法2:interface java.util.Map: java.util.Map
==
====================华丽丽的分割线=====================
void reflectdemo.User.addCar(java.util.List): java.util.List
void reflectdemo.User.addCar(reflectdemo.Car): reflectdemo.Car
void reflectdemo.User.addHobby(java.util.List): java.util.List
============
void reflectdemo.User.addCar(java.util.List): reflectdemo.Car
void reflectdemo.User.addHobby(java.util.List): java.lang.String

你可能感兴趣的:(Java反射(三))