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

📄 reflectionutils.java

📁 Bean Scripting Framework (BSF)为在java应用中使用脚本语言
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	  }	}	return null;  }  public static Bean getField (Object target, String fieldName)	  throws IllegalArgumentException, IllegalAccessException {	// This is to handle how we do static fields.	Class targetClass = (target instanceof Class)						? (Class) target						: target.getClass ();	try {	  Field f = targetClass.getField (fieldName);	  Class fieldType = f.getType ();	  // Get the value and return it.	  Object value = f.get (target);	  return new Bean (fieldType, value);	} catch (NoSuchFieldException e) {	  throw new IllegalArgumentException ("field '" + fieldName + "' is " +										  "unknown for '" + target + "'");	}  }  //////////////////////////////////////////////////////////////////////////  /**   * Get a property of a bean.   *   * @param target    the object whose prop is to be gotten   * @param propName  name of the property to set   * @param index     index to get (if property is indexed)   *   * @exception IntrospectionException if unable to introspect   * @exception IllegalArgumentException if problems with args: if the   *            property is unknown, or if the property is given an index   *            when its not, or if the property is not writeable, or if   *            the given value cannot be assigned to the it (type mismatch).   * @exception IllegalAccessException if read method is not accessible   * @exception InvocationTargetException if read method excepts   */  public static Bean getProperty (Object target, String propName,								  Integer index)	   throws IntrospectionException, IllegalArgumentException,			  IllegalAccessException, InvocationTargetException {	// find the property descriptor	BeanInfo bi = Introspector.getBeanInfo (target.getClass ());	PropertyDescriptor pd = (PropertyDescriptor)	  findFeatureByName ("property", propName, bi.getPropertyDescriptors ());	if (pd == null) {	  throw new IllegalArgumentException ("property '" + propName + "' is " +										  "unknown for '" + target + "'");	}	// get read method and type of property	Method rm;	Class propType;	if (index != null) {	  // if index != null, then property is indexed - pd better be so too	  if (!(pd instanceof IndexedPropertyDescriptor)) {		throw new IllegalArgumentException ("attempt to get non-indexed " +											"property '" + propName +											"' as being indexed");	  }	  IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;	  rm = ipd.getIndexedReadMethod ();	  propType = ipd.getIndexedPropertyType ();	} else {	  rm = pd.getReadMethod ();	  propType = pd.getPropertyType ();	}	if (rm == null) {	  throw new IllegalArgumentException ("property '" + propName +										  "' is not readable");	}	// now get the value	Object propVal = null;	if (index != null) {	  propVal = rm.invoke (target, new Object[] {index});	} else {	  propVal = rm.invoke (target, null);	}	return new Bean (propType, propVal);  }  public static void setField (Object target, String fieldName, Bean value,							   TypeConvertorRegistry tcr)	  throws IllegalArgumentException, IllegalAccessException {	// This is to handle how we do static fields.	Class targetClass = (target instanceof Class)						? (Class) target						: target.getClass ();	try {	  Field f = targetClass.getField (fieldName);	  Class fieldType = f.getType ();	  // type convert the value if necessary	  Object fieldVal = null;	  boolean okeydokey = true;	  if (fieldType.isAssignableFrom (value.type)) {		fieldVal = value.value;	  } else if (tcr != null) {		TypeConvertor cvtor = tcr.lookup (value.type, fieldType);		if (cvtor != null) {		  fieldVal = cvtor.convert (value.type, fieldType, value.value);		} else {		  okeydokey = false;		}	  } else {		okeydokey = false;	  }	  if (!okeydokey) {		throw new IllegalArgumentException ("unable to assign '" + value.value +											"' to field '" + fieldName + "'");	  }	  // now set the value	  f.set (target, fieldVal);	} catch (NoSuchFieldException e) {	  throw new IllegalArgumentException ("field '" + fieldName + "' is " +										  "unknown for '" + target + "'");	}  }  //////////////////////////////////////////////////////////////////////////  /**   * Set a property of a bean to a given value.   *   * @param target    the object whose prop is to be set   * @param propName  name of the property to set   * @param index     index to set (if property is indexed)   * @param value     the property value   * @param valueType the type of the above (needed when its null)   * @param tcr       type convertor registry to use to convert value type to   *                  property type if necessary   *   * @exception IntrospectionException if unable to introspect   * @exception IllegalArgumentException if problems with args: if the   *            property is unknown, or if the property is given an index   *            when its not, or if the property is not writeable, or if   *            the given value cannot be assigned to the it (type mismatch).   * @exception IllegalAccessException if write method is not accessible   * @exception InvocationTargetException if write method excepts   */  public static void setProperty (Object target, String propName,								  Integer index, Object value,								  Class valueType, TypeConvertorRegistry tcr)	   throws IntrospectionException, IllegalArgumentException,			  IllegalAccessException, InvocationTargetException {	// find the property descriptor	BeanInfo bi = Introspector.getBeanInfo (target.getClass ());	PropertyDescriptor pd = (PropertyDescriptor)	  findFeatureByName ("property", propName, bi.getPropertyDescriptors ());	if (pd == null) {	  throw new IllegalArgumentException ("property '" + propName + "' is " +										  "unknown for '" + target + "'");	}	// get write method and type of property	Method wm;	Class propType;	if (index != null) {	  // if index != null, then property is indexed - pd better be so too	  if (!(pd instanceof IndexedPropertyDescriptor)) {		throw new IllegalArgumentException ("attempt to set non-indexed " +											"property '" + propName +											"' as being indexed");	  }	  IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;	  wm = ipd.getIndexedWriteMethod ();	  propType = ipd.getIndexedPropertyType ();	} else {	  wm = pd.getWriteMethod ();	  propType = pd.getPropertyType ();	}	if (wm == null) {	  throw new IllegalArgumentException ("property '" + propName +										  "' is not writeable");	}	// type convert the value if necessary	Object propVal = null;	boolean okeydokey = true;	if (propType.isAssignableFrom (valueType)) {	  propVal = value;	} else if (tcr != null) {	  TypeConvertor cvtor = tcr.lookup (valueType, propType);	  if (cvtor != null) {		propVal = cvtor.convert (valueType, propType, value);	  } else {		okeydokey = false;	  }	} else {	  okeydokey = false;	}	if (!okeydokey) {	  throw new IllegalArgumentException ("unable to assign '" + value +										  "' to property '" + propName + "'");	}	// now set the value	if (index != null) {	  wm.invoke (target, new Object[] {index, propVal});	} else {	  wm.invoke (target, new Object[] {propVal});	}  }}

⌨️ 快捷键说明

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