ITeye博客

JAVA反射小例子

HJXia · 2010-08-10 10:04 · JAVA · 编程语言 · Java · Markdown


package com.xhj.test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestReflect
{
    public static void main(String[] args)
    {
        try
        {
            Class clazz = Class.forName("com.xhj.test.Person");
            Object obj = clazz.newInstance();
            
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields)
            {
                String fieldName = field.getName();
                //System.out.println("fieldName : " + fieldName);
                
                String firstUp = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                //System.out.println("firstUp : " + firstUp);
                
                try
                {
                    Method method = clazz.getMethod("set" + firstUp, new Class[]{field.getType()});
                    if ("String".equals(field.getType().getSimpleName()))
                    {
                        method.invoke(obj, new Object[]{"xhj"});
                    }
                    else if ("int".equals(field.getType().getSimpleName()))
                    {
                        method.invoke(obj, new Object[]{26});
                    }
                    
                    Method getMethod = clazz.getMethod("get" + firstUp, new Class[]{});
                    getMethod.invoke(obj, null);
                }
                catch (NoSuchMethodException e)
                {
                    e.printStackTrace();
                }
                catch (IllegalArgumentException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (InvocationTargetException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (InstantiationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SecurityException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


class Person 
{
    String name;
    int age;
    
    public Person()
    {
        
    }
    
    public String getName()
    {
        System.out.println(name);
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getAge()
    {
        System.out.println(age);
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    } 
}

getFields:获得所有公共字段 getDeclaredFields:获取声明的所有字段,包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。