zoukankan      html  css  js  c++  java
  • Java Reflection(getXXX和getDeclaredXXX)

    package com.sunchao.reflection;
    
    public class Person {
    
        private int age ;
        private String name;
        public String address;
        public static final int X = 0;
        private static final int Y = 0;
        
        public Person() {
            
        }
        
        private Person(String name) {
            this.name = name;
        }
        
        public Person(int age) {
            this.age = age;
        }
        
        public Person(int age, String name) {
            this.age = age;
            this.name = name;
        }
        
        public void setAge(int age) {
            this.age = age;
        }
        
        public int getAge() {
            return this.age;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String getName() {
            return this.name;
        }
        
        @SuppressWarnings("unused")
        private void cry() {
            
        }
        
        public static void eat() {
            
        }
    }
    package com.sunchao.reflection;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    /**
     * Java reflection
    public com.sunchao.reflection.Person()
    public com.sunchao.reflection.Person(int arg0; java.lang.String arg1)
    public com.sunchao.reflection.Person(int arg0)
    <==============================================>
    public void setAge(int arg0)
    public int getAge()
    public static void eat()
    public String getName()
    public void setName(String arg0)
    public final native Class getClass()
    public native int hashCode()
    public boolean equals(Object arg0)
    public String toString()
    public final native void notify()
    public final native void notifyAll()
    public final void wait(long arg0; int arg1)
    public final void wait()
    public final native void wait(long arg0)
    <==============================================>
    public void setAge(int arg0)
    public int getAge()
    private void cry()
    public static void eat()
    public String getName()
    public void setName(String arg0)
    <==============================================>
    public String address
    public static final int X
    <==============================================>
    private int age
    private String name
    public String address
    public static final int X
    private static final int Y
    
    
    
    
     * @author Administrator
     *
     */
    public class Test {
    
        public static void main(String args[]) throws Exception{
            testConstructor();
            testMethods();
            testDeclaredMethods();
            testFields();
            testDeclaredFields();
        }
        
        /**
         * print the public constructor of the class,
         * not all,getDeclaredConstructors() ==> getAll
         * @throws Exception
         */
        public static void testConstructor() throws Exception{
            
            Class<?> clazz = Class.forName("com.sunchao.reflection.Person");
            Constructor<?>[] constructors = clazz.getConstructors();
            for(Constructor<?> c : constructors){
                int modifiers = c.getModifiers();
                String modfifier = Modifier.toString(modifiers);
                String name = c.getName();
                Class<?>[]  parameterTypes = c.getParameterTypes();
                StringBuilder sb = new StringBuilder();
                sb.append("(");
                for(int i = 0; i < parameterTypes.length; i++){
                    if(i != 0){
                        sb.append("; ");
                    }
                    sb.append(parameterTypes[i].getName() + " arg" + i);
                }
                sb.append(")");
                System.out.println(modfifier + " " + name + sb.toString() );
            }
        }
        /**
         * print all the method that the class and superclass
         * interface declares(except the  private)
         * @throws Exception
         */
        public static void testMethods() throws Exception {
            System.out.println("<==============================================>");
            Class<?> clazz = Class.forName("com.sunchao.reflection.Person");
            Method[] allMethods = clazz.getMethods();//includes all the interface ,superclass methods
            for(Method method : allMethods)//(except the private)
            {
                int modifiers = method.getModifiers();
                String modifier = Modifier.toString(modifiers);
                Class<?> returnClass = method.getReturnType();
                String name = method.getName();
                Class<?>[] parameterTypes = method.getParameterTypes();
                StringBuilder sb =  new StringBuilder();
                sb.append("(");
                for(int i = 0; i < parameterTypes.length; i++)
                {
                    if(i != 0)
                    {
                        sb.append("; ");
                    }
                    sb.append(parameterTypes[i].getSimpleName() + " arg" + i);
                }
                sb.append(")");
                System.out.println(modifier + " " + returnClass.getSimpleName() + " " 
                                    + name + sb.toString());
            }
        }
        /**
         * print the all methods that the class declared;
         * @throws Exception
         */
        public static void testDeclaredMethods() throws Exception {
            System.out.println("<==============================================>");
            Class<?> clazz = Class.forName("com.sunchao.reflection.Person");
            Method[] methods = clazz.getDeclaredMethods();
            for(Method method : methods)
            {
                int modifiers = method.getModifiers();
                String modifier = Modifier.toString(modifiers);
                Class<?> returnType = method.getReturnType();
                String name = method.getName();
                Class<?>[] parameterTypes = method.getParameterTypes();
                StringBuilder sb = new StringBuilder();
                sb.append("(");
                for(int i = 0; i < parameterTypes.length; i++)
                {
                    if(i != 0)
                    {
                        sb.append("; ");
                    }
                    sb.append(parameterTypes[i].getSimpleName() + " arg" + i);
                }
                sb.append(")");
                System.out.println(modifier + " " + returnType.getSimpleName() + " "
                                  + name + sb.toString());
            }
        }
        /**
         * print the fields of public(includes the static ,final modifier) 
         *  but the private is pass
         * @throws Exception
         */
        public static void testFields() throws Exception {
            System.out.println("<==============================================>");
            Class<?> clazz = Class.forName("com.sunchao.reflection.Person");
            Field[] fields = clazz.getFields();
            for(Field field : fields)
            {
                int  modifiers = field.getModifiers();
                String modifier = Modifier.toString(modifiers);
                Class<?> fieldType = field.getType();
                String name = field.getName();
                System.out.println(modifier + " " + fieldType.getSimpleName() + 
                                   " "  + name);
            }
        }
        
        /**
         * print the all fields that the class declared
         * @throws Exception
         */
        public static void testDeclaredFields() throws Exception {
            System.out.println("<==============================================>");
            Class<?> clazz = Class.forName("com.sunchao.reflection.Person");
            Field[] fields = clazz.getDeclaredFields();
            for(Field field : fields)
            {
                int modifiers = field.getModifiers();
                String modifier = Modifier.toString(modifiers);
                Class<?> fieldType = field.getType();
                String name = field.getName();
                System.out.println(modifier + " " + fieldType.getSimpleName() + 
                                   " " + name);
            }    
         }
    }
  • 相关阅读:
    结构化思考力
    对象序列化解析
    单元测试的规范
    关于CefSharp的坎坷之路
    构建基于Chromium的应用程序
    C# 面向切面编程(AOP)--监控日志记录方案
    CefSharp 中断点 已达到中断点
    用批处理命令实现激活office2016
    Winform开发中的困境及解决方案
    VUE+Element 前端应用开发框架功能介绍
  • 原文地址:https://www.cnblogs.com/onlysun/p/4530692.html
Copyright © 2011-2022 走看看