RSS

Daily Archives: 12/03/2009

Java Reflection Sample II

DumpMethods.java

package com.test.reflection;

import java.lang.reflect.Method;

public class DumpMethods {

    public static void main(String args[]) throws Exception{

        Class<?> classType = Class.forName(args[0]);

        Method [] methods = classType.getDeclaredMethods();

        for(int i=0;i<methods.length;i++){

            System.out.println(methods[i].toString());

        }

    }

}

InvokeTester.java

package com.test.reflection;

import java.lang.reflect.Method;

public class InvokeTester {

    public int add(int param1, int param2){

        return param1 + param2;

    }          

    public String echo(String msg){

        return "echo: " + msg;

    }

    public static void main(String[] args) throws Exception{

        Class<?> classType = InvokeTester.class;

        Object invokeTester = classType.newInstance();

        Method addMethod = classType.getMethod("add", new Class[]{int.class, int.class});

        Object result = addMethod.invoke(invokeTester, new Object[]{new Integer(100), new Integer(200)});

        System.out.println((Integer)result);

        Method echoMethod = classType.getMethod("echo", new Class[]{String.class});

        result = echoMethod.invoke(invokeTester, new Object[]{"Hello"});

        System.out.println((String)result);

    }

}

ReflectTest.java

package com.test.reflection;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class ReflectTest {

    public Object copy(Object object) throws Exception {

        Class<?> classType = object.getClass();

        System.out.println("Class: " + classType.getName());

        Object objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});

        Field fields[] = classType.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {

            Field field = fields[i];

            String fieldName = field.getName();

            String firstLetter = fieldName.substring(0, 1).toUpperCase();

            String getMethodName = "get" + firstLetter + fieldName.substring(1);

            String setMethodName = "set" + firstLetter + fieldName.substring(1);

            Method getMethod = classType.getMethod(getMethodName,  new Class[] {});

            Method setMethod = classType.getMethod(setMethodName,  new Class[] { field.getType() });

            Object value = getMethod.invoke(object, new Object[] {});

            System.out.println(fieldName + ":" + value);

            setMethod.invoke(objectCopy, new Object[] { value });

        }

        return objectCopy;

    }

 

    public static void main(String[] args) throws Exception {

        Customer customer = new Customer("Tom", 21);

        customer.setId(new Long(1));

        Customer customerCopy = (Customer) new ReflectTest().copy(customer);

        System.out.println("Copy information:" + customerCopy.getId() + " "

            + customerCopy.getName() + " " + customerCopy.getAge());

    }

}

class Customer {

    private Long id;

    private String name;

    private int age;

 

    public Customer() {

    }

    public Customer(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

    public Long getId() {

        return id;

    }

    public void setId(Long id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

 
Leave a comment

Posted by on 12/03/2009 in JAVA