27 ก.ย. 2554

Java Reflection get and set Value

package ball.Invoke;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Locale;

import javax.swing.JOptionPane;

import static java.lang.System.out;
import static java.lang.System.err;

public class InvokeSample {
 private String name;
 private String surename;
 private String age;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getSurename() {
  return surename;
 }

 public void setSurename(String surename) {
  this.surename = surename;
 }

 public String getAge() {
  return age;
 }

 public void setAge(String age) {
  this.age = age;
 }

 @Override
 public String toString() {
  return getName() + " " + getSurename() + " " + getAge();
 }

 public static void main(String[] args) throws SecurityException,
   NoSuchFieldException, IllegalArgumentException,
   IllegalAccessException, InvocationTargetException {
  InvokeSample deet = new InvokeSample();
  deet.setAge("20");
  deet.setName("Boner");
  deet.setSurename("Terry");
  deet.parsetObject(deet);
 }
 private void parsetObject(InvokeSample deet)
   throws IllegalArgumentException, IllegalAccessException,
   InvocationTargetException {
  String[] val = { "Data1", "Data2", "Data3" };
  // get Class
  Class<?> c = deet.getClass();
  // get all methods
  Method[] allMethods = c.getDeclaredMethods();

  // get all Fields
  Field[] allF = c.getDeclaredFields();
  for (Field field : allF) {
   System.out.println(field.getName());
  }
  // set value to set method send String
  int i = 0;
  for (Method m : allMethods) {
   String mname = m.getName();
   if (mname.startsWith("set")
     && (m.getGenericReturnType() != boolean.class)) {
    m.setAccessible(true);
    // set value by set method
    m.invoke(deet, new String(val[i++]));
    System.out.println(("invoking " + mname + "()" + m
      .getReturnType()));
   }
  }
  // get value from get method retruned String
  for (Method m : allMethods) {
   String mname = m.getName();
   if (mname.startsWith("get")
     && (m.getGenericReturnType() != boolean.class)
     || mname.startsWith("toString")) {
    Class<?> ret = m.getReturnType();
    if (ret.getSimpleName().trim().equalsIgnoreCase("String")) {
     System.out
       .println(("invoking " + mname + "() return type " + m
         .getReturnType()));
     m.setAccessible(true);
     // get value from get method
     String value = (String) invokeMethod(deet, m);
     out.format("%s() returned %s %n", mname, value);
    }
   }
  }
 }

 private Object invokeMethod(Object obj, Method m) {
  // Invoke Method
  Object value = null;
  try {
   value = m.invoke(obj);
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  }
  return value;
 }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น