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);
            }    
         }
    }
  • 相关阅读:
    数学分析教材编写大纲
    鲁病案号1357324
    数据库-常见函数-分组函数
    Java中的快速输入输出
    数据库-数学函数
    IDEA 常用快捷键 (尚硅谷&#183;宋红康 设置版)——高仿eclipse
    linux下网络死掉了肿么办?(Networking Disabled)
    zookeeper集群搭建
    WMware克隆虚拟机后出现网络无法连接的问题
    Partitioner编程——根据运营商分组统计用户上网流量
  • 原文地址:https://www.cnblogs.com/onlysun/p/4530692.html
Copyright © 2011-2022 走看看