首页 > 代码库 > Java 反射机制

Java 反射机制


import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/*
 * 测试反射机制,根据类名从而得到其中的信息
 */
public class ClassLoadTest {
    public static void main(String[] args) throws IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String className = "test.son";
            Class getClass = Class.forName(className);
            Object getObject = getClass.newInstance();
            /*
             * 获得类的构造函数及其形参和异常
             */

            System.out.println("=======================================================");
            Constructor[] constructList = getClass.getDeclaredConstructors();
//            Constructor[] constructList = getClass.getConstructors();
            for (int i = 0; i < constructList.length; i++) {
                Constructor<?> constructor = constructList[i];
                System.out.println("构造方法名称" + i + ":" + constructor.getName());
                System.out.println("所在类或接口:" + constructor.getDeclaringClass());
                Class<?>[] constructParamList = constructor.getParameterTypes();
                for (int j = 0; j < constructParamList.length; i++) {
                    System.out.println("构造方法形参 " + i + " 的类型:" + constructParamList[i].getName());
                }
                Class<?>[] getExcepList = constructor.getExceptionTypes();
                for (int j = 0; j < getExcepList.length; j++) {
                    System.out.println("构造方法异常:" + j + "的类型:" + getExcepList[j].getName());
                }
                System.out.println("---------------------------");
            }

            /*
             * 获得对应Class的方法及其形参和异常(方法名、方法所在类或接口、形参类型、异常类型
             */
            System.out.println("=======================================================");
            /*
             * getDeclaredMethods获得Class所表示的全部私有、默认、受保护和共有的方法,但不包括继承地方法(
             * 如果Super类和Sub类在同一个Java源文件中进行编译,显示的结果会包括父类共有的方法) 
             * getMethodshuode 获得Class所表示的类及其父类所有的公共方法
             * getDeclaredConstructors和getConstructors也是一样
             */
            // Method[] methods = c.getDeclaredMethods();
            Method[] methods = getClass.getMethods();
            for (Method m : methods) {
                Annotation[] annotations = m.getAnnotations();
                System.out.println("得到注释"+annotations.toString());
                System.out.println("方法名: " + m.getName());
                System.out.println("该方法所在类或者接口:" + m.getDeclaringClass());
                Class<?>[] pramaterList = m.getParameterTypes();
                for (int i = 0; i < pramaterList.length; i++) {
                    System.out.println("形参 " + i + " 的类型:" + pramaterList[i].getName());
                }
                Class<?>[] getExcepList = m.getExceptionTypes();
                for (int i = 0; i < getExcepList.length; i++) {
                    System.out.println("异常:" + i + "的类型:" + getExcepList[i].getName());
                }
//                m.invoke(getObject, null);//执行方法其中里面涉及到可变参数
                System.out.println("---------------------------");
            }

            /*
             * 获取对应Class的属性(名称、所在接口或类、类型、权限修饰
             */
            System.out.println("=======================================================");
            Field filedList[] = getClass.getDeclaredFields();
            for (int i = 0; i < filedList.length; i++) {
                Field f = filedList[i];
                System.out.println("属性" + i + ":" + f.getName());
                System.out.println("所在接口或类:" + f.getDeclaringClass());
                System.out.println("属性类型:" + f.getType());
                System.out.println("属性修饰为:" + Modifier.toString(f.getModifiers()));
                System.out.println("---------------------------");
            }


    }
}

 





public class son extends father{
        static {
            System.out.println("(son) static code");
        }
    
        public son() {
            System.out.println("(son)构造函数");
        }
        
        private int privateParameter_S;
        public int publicParameter_S;
        protected int protectedParameter_S;
        int defaultParameter_S;
        
        //注释(son)调用公共的方法
        public void PublicMethod_S(){
            System.out.println("(son)调用公共的方法");
        }
        private  void PrivateMethod_S(){
            System.out.println("(son)调用私用的方法");
        }
        protected void ProtectedMethod_S(){
            System.out.println("(son)调用受保护的方法");
        }
        void DeSaultMethod_S(){
            System.out.println("(son)调用默认的方法");
        }
    }
    
    class father{
        private int privateParameter_F;
        public int publicParameter_F;
        protected int protectedParameter_F;
        int defaultParameter_F;
        
        
        public father() {
            super();
            System.out.println("(father)构造函数");
        }
        public void PublicMethod_F(){
            System.out.println("(father)调用公共的方法");
        }
        private  void PrivateMethod_F(){
            System.out.println("(father)调用私用的方法");
        }
        protected void ProtectedMethod_F(){
            System.out.println("(father)调用受保护的方法");
        }
        void DefaultMethod_F(){
            System.out.println("(father)调用默认的方法");
        }
    }




技术分享

 

Java 反射机制