疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

java类反射例子


 

     java类反射例子

  * 取得参数对象中的公共属性

  package util;

  import java.lang.reflect.Field;

  import java.lang.reflect.Method;

  import test.web.example.PP;

  /**

  *

  * @author peidw

  *

  */

  public class Reflection {

  /**

  * 取得参数对象中的公共属性

  * @param obj

  * @param fieldname

  * @return

  * @throws Exception

  */

  public Object getProperty(Object obj,String fieldname)throws Exception{

  Object result=null;

  Class objClass=obj.getClass();

  Field field=objClass.getField(fieldname);

  result=field.get(obj);

  return result;

  }

  /**

  * 获得某类的静态属性

  * @param className

  * @param fieldName

  * @return

  * @throws Exception

  */

  public Object getStaticProperty(String className,String fieldName)throws Exception{

  Class cls=Class.forName(className);

  Field field=cls.getField(fieldName);

  Object provalue=field.get(cls);

  return provalue;

  }

  /**

  * 获取参数对象的属性值

  * @param obj

  * @param propertyName

  * @return

  * @throws Exception

  */

  public Object getPrivatePropertyValue(Object obj,String propertyName)throws Exception{

  Class cls=obj.getClass();

  Field field=cls.getDeclaredField(propertyName);

  field.setAccessible(true);

  Object retvalue=field.get(obj);

  return retvalue;

  }

  /**

  * 执行某对象的方法

  * @param owner

  * @param methodName

  * @param args

  * @return

  * @throws Exception

  */

  public Object invokeMethod(Object owner,String methodName,Object[] args)throws Exception{

  Class cls=owner.getClass();

  Class[] argclass=new Class[args.length];

  for(int i=0,j=argclass.length;i

  argclass[i]=args[i].getClass();

  }

  Method method=cls.getMethod(methodName,argclass);

  return method.invoke(owner, args);

  }

  /**

  * 执行静态类的方法

  * @param className

  * @param methodName

  * @param args

  * @return

  * @throws Exception

  */

  public Object invokeStaticMethod(String className,String methodName,Object[] args)throws Exception{

  Class cls=Class.forName(className);

  Class[] argclass=new Class[args.length];

  for(int i=0,j=argclass.length;i

  argclass[i]=args[i].getClass();

  }

  Method method=cls.getMethod(methodName,argclass);

  return method.invoke(null, args);

  }

  public Object newInstance(String className,Object[] args)throws Exception{

  Class clss=Class.forName(className);

  Class[] argclass=new Class[args.length];

  for(int i=0,j=argclass.length;i

  argclass[i]=args[i].getClass();

  }

  java.lang.reflect.Constructor cons=clss.getConstructor(argclass);

  return cons.newInstance();

  }

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

  Reflection rl=new Reflection();

  PP p=new PP();

  rl.getPrivatePropertyValue(p,"aname");

  }

  }

  测试类

  package test.web.example;

  import static org.junit.Assert.*;

  import org.junit.Before;

  import org.junit.Test;

  import org.junit.Assert.*;

  import util.Reflection;

  /**

  *

  * @author peidw

  *

  */

  public class ReflectionTest {

  private PP pp=null;

  private Reflection reflection=null;

  @Before

  public void setUp() throws Exception {

  reflection=new Reflection();

  pp=new PP();

  }

  @Test

  public void testGetProperty() throws Exception{

  System.out.println(pp);

  System.out.println(reflection);

  System.out.println(reflection.getProperty(pp, "address"));

  //assertEquals(reflection.getProperty(pp, "address"), null);

  }

  @Test

  public void testGetStaticProperty()throws Exception{

  assertEquals(reflection.getStaticProperty("test.web.example.PP", "count"),"love you");

  }

  @Test

  public void testGetPrivatePropertyValue() throws Exception{

  assertEquals(reflection.getPrivatePropertyValue(pp, "aname"),null);

  }

  @Test

  public void testInvokeMethod() throws Exception{

  assertEquals(reflection.invokeMethod(pp, "setAddress", new Object[]{"合浦西场镇"}),null);

  assertEquals(reflection.getProperty(pp, "address"), "合浦西场镇");

  }

  @Test

  public void testInvokeStaticMethod()throws Exception {

  assertEquals(reflection.invokeStaticMethod("test.web.example.PP", "changeCount",new Object[]{"80"}),true);

  assertEquals(reflection.getStaticProperty("test.web.example.PP", "count"),"80");

  }

  @Test

  public void testNewInstance() throws Exception{

  Object vpp=reflection.newInstance("test.web.example.PP",new Object[]{"peidw","广州"});

  org.junit.Assert.assertNotNull(vpp);

  //assertEquals(reflection.getPrivatePropertyValue(vpp,"aname"),"peidw");

  }

  }