reflector.java

来自「基于数据库操作的封装」· Java 代码 · 共 64 行

JAVA
64
字号
package org.shaoye.magic.reflector;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.Enumeration;

import org.shaoye.magic.oracle.Execute;

/**
 * @author zhangt
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Reflector {

	//private Object obj;
	private String className;
	private Hashtable hashtable;

	public Reflector( String _className, Hashtable _hashtable) {
		//obj = _obj;
		className = _className;
		hashtable = _hashtable;
	}

	/**
	 * 
	 * 
	 */

	public Object runReflect() throws Exception, InvocationTargetException {

		Object newObj = Class.forName(className).newInstance();

		Method[] methods = Class.forName(className).getMethods();

		Enumeration hash_names = hashtable.keys();

		String value = null;

		while (hash_names.hasMoreElements()) 
		{
			value = (String)hash_names.nextElement();
			for (int j = 0; j < methods.length; j++) 
			{
				String meth_name = methods[j].getName();
				if (meth_name.startsWith("set")) 
				{
					meth_name = meth_name.substring(3, meth_name.length());
					if (meth_name.equalsIgnoreCase(value)) 
					{
						methods[j].invoke(newObj, new Object[] { hashtable.get(value) });
					}
				}
			}
		}
		return newObj;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?