⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arguments.java

📁 During your career as a Software Designer/Programmer you will probably encounter the need to use Obj
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package dev.easyref.util;/** * <p>Title: Easy Reflection</p> * <p>Description: This project demonstrate the easier usage of Reflection</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: N/A</p> * @author Doron Barak * @version 1.0 */import java.lang.reflect.*;import java.text.*;import java.util.*;class Argument {	protected Object array;	protected Argument(Class objClass) {		verifyClass(objClass);	}	protected void verifyClass(Class objClass) {		if ((array == null) || (!objClass.equals(Array.get(array, 0).getClass()))) {			array = Array.newInstance(objClass, 1);		}	}	protected Object getData(boolean nullAsClass) {		Object result = Array.get(array, 0);		if (!nullAsClass && (result instanceof Class)) {			result = null;		}		return result;	}}public class Arguments extends Vector {	protected boolean nullAsClass;	protected boolean methodError;	protected boolean showExceptions;	protected SimpleDateFormat sdfTranslator;	protected final static String[][] booleanStrings = new String[][] {		new String[] {"true", "false"},		new String[] {"yes", "no"},		new String[] {"1", "0"}	};	public Arguments() {		super();		nullAsClass = false;		displayExceptions();		forceEmpty();		setDateFormat("dd/MM/yyyy HH:mm:ss");	}	public Arguments(Object collectee) {		this();		collectData(collectee);	}	public void setDateFormat(String dateFormat) {		sdfTranslator = new SimpleDateFormat(dateFormat);	}	public void displayExceptions() {		showExceptions = true;	}	public void hideExceptions() {		showExceptions = false;	}	public void forceEmpty() {		methodError = false;		removeAllElements();		trimToSize();	}	public void collectData(Object dataObj) {		Object data = null;		Class c = dataObj.getClass();		Field[] fList = c.getDeclaredFields();		boolean af = false;		try {			for (int i = 0; i < fList.length; i++) {				Field f = fList[i];				af = f.isAccessible();				if (!af) {					f.setAccessible(true);				}				data = f.get(dataObj);				if (!af) {					f.setAccessible(false);				}				if (data != null) {					addElement(data);				} else {					addNull(f.getType());				}			}		} catch (Exception ex) {			forceEmpty();			ex.printStackTrace();		}	}	public Class[] getArgumentsTypes() {		Class[] result = null;		if (!isEmpty()) {			result = new Class[size()];			int count = 0;			Class classType = null;			Enumeration enum = elements();			Object obj = null;			Argument arg = null;			while (enum.hasMoreElements()) {				obj = enum.nextElement();				try {					classType = obj.getClass();					if (obj instanceof Argument) {						arg = (Argument)obj;						obj = arg.array;						if (arg.getData(nullAsClass) == null) {							classType = (Class)Array.get(obj, 0);						} else {							classType = obj.getClass().getComponentType();						}					}				} catch (Exception ex) {				}				result[count++] = classType;				classType = null;			}		}		return result;	}	public String toString() {		Object[] array = getArguments();		Object obj = null;		if (array == null) {			return "[NULL]";		} else {			int length = Array.getLength(array);			int lastItem = length - 1;			StringBuffer sb = new StringBuffer("[");			for (int i = 0; i < length; i++) {				obj = Array.get(array, i);				if (obj != null) {					sb.append(obj);				} else {					sb.append("[NULL]");				}				if (i < lastItem) {					sb.append(", ");				}			}			sb.append("]");			return sb.toString();		}	}	public Object[] getArguments() {		Object[] result = null;		if (!isEmpty()) {			result = new Object[size()];			int count = 0;			Object classData = null;			Enumeration enum = elements();			Object obj = null;			while (enum.hasMoreElements()) {				obj = enum.nextElement();				try {					classData = unWrapObject(obj);				} catch (Exception ex) {				}				result[count++] = classData;				classData = null;			}		}		return result;	}	public long[] getLongArray() {		long[] result = new long[size()];		try {			int i = 0;			Enumeration eId = elements();			Object xObj = null;			while (eId.hasMoreElements()) {				xObj = unWrapObject(eId.nextElement());				if (xObj instanceof Number) {					result[i++] = ((Number)xObj).longValue();				} else if (xObj instanceof String) {					result[i++] = Long.parseLong((String)xObj);				}			}		} catch (Exception ex) {			result = null;		}		return result;	}	public Object getInstance(String objClassName) {		Exception ex = null;		Object result = null;		Object[] args = null;		Class[] argTypes = null;		try {			args = getArguments();			argTypes = getArgumentsTypes();			Class objClass = Class.forName(objClassName);			Constructor con = objClass.getDeclaredConstructor(argTypes);			boolean ia = con.isAccessible();			if (!ia) {				con.setAccessible(true);			}			result = con.newInstance(args);			con.setAccessible(ia);		} catch (Exception ex0) {			ex = ex0;			methodError = true;			if (showExceptions) {				ex.printStackTrace();			}		} finally {			if (ex != null) {				// Report failure..				methodError = true;				String pType = null;				System.err.println("ERROR: Constructor not found for class [" + objClassName + "] {");				System.err.print("\t" + objClassName.substring(objClassName.lastIndexOf(".") + 1) + " (");				for (int i = 0; i < argTypes.length; i++) {					pType = argTypes[i].getName();					pType = pType.substring(pType.lastIndexOf(".") + 1);					System.err.print(pType + " " + "p" + i);					if (i < argTypes.length - 1) {						System.err.print(", ");					}				}				System.err.println(");\r\n}");				if (showExceptions) {					ex.printStackTrace();				}			}			return result;		}	}	public Method getMethod(Object obj, String methodName) {		return getMethod(obj.getClass(), methodName);	}	public Method getMethod(Class objClass, String methodName) {		Exception ex = null;		Method result = null;		Class[] argTypes = null;		try {			argTypes = getArgumentsTypes();			// Get the method directly declared by the instance's class..			result = objClass.getDeclaredMethod(methodName, argTypes);		} catch (NoSuchMethodException nmsex) {			try {				// Get the method inherited by the instance's class from its parents..				result = objClass.getMethod(methodName, argTypes);			} catch (NoSuchMethodException nmsex0) {				ex = nmsex0;			} catch (Exception ex0) {				ex = ex0;			}		} catch (Exception ex1) {			ex = ex1;		} finally {			if (ex != null) {				// Report failure..				methodError = true;				String pType = null;				System.err.println("ERROR: Method not found for class [" + objClass.getName() + "] {");				System.err.print("\t" + methodName + "(");				if (argTypes != null) {					for (int i = 0; i < argTypes.length; i++) {						pType = argTypes[i].getName();						pType = pType.substring(pType.lastIndexOf(".") + 1);						System.err.print(pType + " " + "p" + i);						if (i < argTypes.length - 1) {							System.err.print(", ");						}					}				}				System.err.println(");\r\n}");				if (showExceptions) {					ex.printStackTrace();				}			}			return result;		}	}	public Object runMethod(Object obj, String methodName) {		return runMethod(obj, getMethod(obj, methodName));	}	public boolean hadMethodError() {		return methodError;	}	public Object runMethod(Object obj, Method method) {

⌨️ 快捷键说明

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