Java反射类实例

来源:互联网 发布:怎么关注淘宝店 编辑:程序博客网 时间:2024/06/07 11:57
 

一、Java反射机制定义:
Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反射机制。

Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。

二、Java反射类的实例:
下面用一个实例来实现Java的反射类,其中:

Example.java是反射类操作的类结构,包含私有属性,共有属性,公共方法,静态属性,静态方法;

Reflection.java是反射功能实现的封装类;

ReflectionTest.java是测试类。

1、Example.java
package com.test;

public class Example {

 // 私有属性
 private String privateProperty;
 
 // 公共属性
 public String publicProperty;

 // 公共方法
 public void setProperty(String str) {
  publicProperty = str;
 }

 // 静态属性
 public static String staticProperty;

 // 静态方法
 static public boolean SMethod(String str) {
  staticProperty = str;
  return true;
 }

}


2、Reflection.java
package com.test;

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

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 < j; 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 < j; i++) {
   argclass[i] = args[i].getClass();
  }
  Method method = cls.getMethod(methodName, argclass);
  return method.invoke(null, args);
 }

 /**
  * 实例化一个类对象的方法
  *
  * @param className
  * @param methodName
  * @param args
  * @return
  * @throws Exception
  */
 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 < j; 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 {
 }
}

3、ReflectionTest.java
package com.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;

public class ReflectionTest {

 private Example ex;
 private Reflection reflection;

 @Before
 public void setUp() throws Exception {
  reflection = new Reflection();
  ex = new Example();
 }

 @Test
 // 获取属性测试
 public void testGetProperty() throws Exception {

  // 私有属性无法这样获取
  // assertEquals(reflection.getProperty(ex, "privateProperty"),
  // null);

  // 公共属性
  assertEquals(reflection.getProperty(ex, "staticProperty"), null);
 }

 @Test
 // 获取静态属性测试
 public void testGetStaticProperty() throws Exception {
  assertEquals(reflection.getStaticProperty("com.test.Example",
    "staticProperty"), null);
 }

 @Test
 // 获取私有属性值测试
 public void testGetPrivatePropertyValue() throws Exception {
  assertEquals(reflection.getPrivatePropertyValue(ex, "privateProperty"),
    null);
 }

 @Test
 // 调用方法测试
 public void testInvokeMethod() throws Exception {

  assertEquals(reflection.invokeMethod(ex, "setProperty",
    new Object[] { "abcd" }), null);
  assertEquals(reflection.getProperty(ex, "publicProperty"), "abcd");

 }

 @Test
 // 调用静态方法测试
 public void testInvokeStaticMethod() throws Exception {

  assertEquals(reflection.invokeStaticMethod("com.test.Example",
    "SMethod", new Object[] { "joe" }), true);
  assertEquals(reflection.getStaticProperty("com.test.Example",
    "staticProperty"), "joe");

 }

 @Test
 // 新实例测试
 public void testNewInstance() throws Exception {

  Object obj = reflection
    .newInstance("com.test.Example", new Object[] {});
  org.junit.Assert.assertNotNull(obj);
 }

}

 

原创粉丝点击