import sun.reflect.FieldAccessor;
import sun.reflect.Reflection;
/**
*
* 表示属性对象
*
* comment by liqiang
*
* @author Kenneth Russell
* @author Nakul Saraiya
*/
public final
class Field extends AccessibleObject implements Member {
//定义此属性的类名
private Class clazz;
private int slot;
//属性名
private String name;
//属性类型
private Class type;
//描述符
private int modifiers;
private volatile FieldAccessor fieldAccessor;
//copy出此对象的原始对象
private Field root;
//与此属性关联的对象实例缓存,如果当前的对象与缓存对象不同则做安全检查
private volatile Class securityCheckTargetClassCache;
//构造函数
Field(Class declaringClass,
String name,
Class type,
int modifiers,
int slot)
{
this.clazz = declaringClass;
this.name = name;
this.type = type;
this.modifiers = modifiers;
this.slot = slot;
}
//通过当前类数据生成新的对象
Field copy() {
//生成新的属性对象
Field res = new Field(clazz, name, type, modifiers, slot);
//新对象指向老对象
res.root = this;
//新对象与老对象共用一个FieldAccessor
res.fieldAccessor = fieldAccessor;
return res;
}
//获得定义此属性的类
public Class getDeclaringClass() {
return clazz;
}
//获得属性名
public String getName() {
return name;
}
//获得属性描述符
public int getModifiers() {
return modifiers;
}
//返回属性类型
public Class getType() {
return type;
}
//判断obj对象是否与当前属性对象相等
public boolean equals(Object obj) {
if (obj != null && obj instanceof Field) {//是属性对象
//转型
Field other = (Field)obj;
//判断定义属性的类,属性名,属性类型
return (getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())
&& (getType() == other.getType());
}
//不等
return false;
}
//返回hashCode
public int hashCode() {
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
}
//返回字符串描述
public String toString() {
int mod = getModifiers();
return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
+ getTypeName(getType()) + " "
+ getTypeName(getDeclaringClass()) + "."
+ getName());
}
/**
* Returns the value of the field represented by this <code>Field</code>, on
* the specified object. The value is automatically wrapped in an
* object if it has a primitive type.
*
* <p>The underlying field's value is obtained as follows:
*
* <p>If the underlying field is a static field, the <code>obj</code> argument
* is ignored; it may be null.
*
* <p>Otherwise, the underlying field is an instance field. If the
* specified <code>obj</code> argument is null, the method throws a
* <code>NullPointerException.</code> If the specified object is not an
* instance of the class or interface declaring the underlying
* field, the method throws an <code>IllegalArgumentException</code>.
*
* <p>If this <code>Field</code> object enforces Java language access control, and
* the underlying field is inaccessible, the method throws an
* <code>IllegalAccessException</code>.
* If the underlying field is static, the class that declared the
* field is initialized if it has not already been initialized.
*
* <p>Otherwise, the value is retrieved from the underlying instance
* or static field. If the field has a primitive type, the value
* is wrapped in an object before being returned, otherwise it is
* returned as is.
*
* <p>If the field is hidden in the type of <code>obj</code>,
* the field's value is obtained according to the preceding rules.
*
* @param obj object from which the represented field's value is
* to be extracted
* @return the value of the represented field in object
* <tt>obj</tt>; primitive values are wrapped in an appropriate
* object before being returned
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof).
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
*/
public Object get(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).get(obj);
}
/**
* Gets the value of a static or instance <code>boolean</code> field.
*
* @param obj the object to extract the <code>boolean</code> value
* from
* @return the value of the <code>boolean</code> field
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>boolean</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public boolean getBoolean(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getBoolean(obj);
}
/**
* Gets the value of a static or instance <code>byte</code> field.
*
* @param obj the object to extract the <code>byte</code> value
* from
* @return the value of the <code>byte</code> field
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>byte</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public byte getByte(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getByte(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>char</code> or of another primitive type convertible to
* type <code>char</code> via a widening conversion.
*
* @param obj the object to extract the <code>char</code> value
* from
* @return the value of the field converted to type <code>char</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>char</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public char getChar(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getChar(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>short</code> or of another primitive type convertible to
* type <code>short</code> via a widening conversion.
*
* @param obj the object to extract the <code>short</code> value
* from
* @return the value of the field converted to type <code>short</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>short</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public short getShort(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getShort(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>int</code> or of another primitive type convertible to
* type <code>int</code> via a widening conversion.
*
* @param obj the object to extract the <code>int</code> value
* from
* @return the value of the field converted to type <code>int</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>int</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public int getInt(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getInt(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>long</code> or of another primitive type convertible to
* type <code>long</code> via a widening conversion.
*
* @param obj the object to extract the <code>long</code> value
* from
* @return the value of the field converted to type <code>long</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>long</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public long getLong(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getLong(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>float</code> or of another primitive type convertible to
* type <code>float</code> via a widening conversion.
*
* @param obj the object to extract the <code>float</code> value
* from
* @return the value of the field converted to type <code>float</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>float</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public float getFloat(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getFloat(obj);
}
/**
* Gets the value of a static or instance field of type
* <code>double</code> or of another primitive type convertible to
* type <code>double</code> via a widening conversion.
*
* @param obj the object to extract the <code>double</code> value
* from
* @return the value of the field converted to type <code>double</code>
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not
* an instance of the class or interface declaring the
* underlying field (or a subclass or implementor
* thereof), or if the field value cannot be
* converted to the type <code>double</code> by a
* widening conversion.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#get
*/
public double getDouble(Object obj)
throws IllegalArgumentException, IllegalAccessException
{
return getFieldAccessor(obj).getDouble(obj);
}
/**
* Sets the field represented by this <code>Field</code> object on the
* specified object argument to the specified new value. The new
* value is automatically unwrapped if the underlying field has a
* primitive type.
*
* <p>The operation proceeds as follows:
*
* <p>If the underlying field is static, the <code>obj</code> argument is
* ignored; it may be null.
*
* <p>Otherwise the underlying field is an instance field. If the
* specified object argument is null, the method throws a
* <code>NullPointerException</code>. If the specified object argument is not
* an instance of the class or interface declaring the underlying
* field, the method throws an <code>IllegalArgumentException</code>.
*
* <p>If this <code>Field</code> object enforces Java language access control, and
* the underlying field is inaccessible, the method throws an
* <code>IllegalAccessException</code>.
*
* <p>If the underlying field is final, the method throws an
* <code>IllegalAccessException</code>.
*
* <p>If the underlying field is of a primitive type, an unwrapping
* conversion is attempted to convert the new value to a value of
* a primitive type. If this attempt fails, the method throws an
* <code>IllegalArgumentException</code>.
*
* <p>If, after possible unwrapping, the new value cannot be
* converted to the type of the underlying field by an identity or
* widening conversion, the method throws an
* <code>IllegalArgumentException</code>.
*
* <p>If the underlying field is static, the class that declared the
* field is initialized if it has not already been initialized.
*
* <p>The field is set to the possibly unwrapped and widened new value.
*
* <p>If the field is hidden in the type of <code>obj</code>,
* the field's value is set according to the preceding rules.
*
* @param obj the object whose field should be modified
* @param value the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
*/
public void set(Object obj, Object value)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).set(obj, value);
}
/**
* Sets the value of a field as a <code>boolean</code> on the specified object.
* This method is equivalent to
* <code>set(obj, zObj)</code>,
* where <code>zObj</code> is a <code>Boolean</code> object and
* <code>zObj.booleanValue() == z</code>.
*
* @param obj the object whose field should be modified
* @param z the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setBoolean(Object obj, boolean z)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setBoolean(obj, z);
}
/**
* Sets the value of a field as a <code>byte</code> on the specified object.
* This method is equivalent to
* <code>set(obj, bObj)</code>,
* where <code>bObj</code> is a <code>Byte</code> object and
* <code>bObj.byteValue() == b</code>.
*
* @param obj the object whose field should be modified
* @param b the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setByte(Object obj, byte b)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setByte(obj, b);
}
/**
* Sets the value of a field as a <code>char</code> on the specified object.
* This method is equivalent to
* <code>set(obj, cObj)</code>,
* where <code>cObj</code> is a <code>Character</code> object and
* <code>cObj.charValue() == c</code>.
*
* @param obj the object whose field should be modified
* @param c the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setChar(Object obj, char c)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setChar(obj, c);
}
/**
* Sets the value of a field as a <code>short</code> on the specified object.
* This method is equivalent to
* <code>set(obj, sObj)</code>,
* where <code>sObj</code> is a <code>Short</code> object and
* <code>sObj.shortValue() == s</code>.
*
* @param obj the object whose field should be modified
* @param s the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setShort(Object obj, short s)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setShort(obj, s);
}
/**
* Sets the value of a field as an <code>int</code> on the specified object.
* This method is equivalent to
* <code>set(obj, iObj)</code>,
* where <code>iObj</code> is a <code>Integer</code> object and
* <code>iObj.intValue() == i</code>.
*
* @param obj the object whose field should be modified
* @param i the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setInt(Object obj, int i)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setInt(obj, i);
}
/**
* Sets the value of a field as a <code>long</code> on the specified object.
* This method is equivalent to
* <code>set(obj, lObj)</code>,
* where <code>lObj</code> is a <code>Long</code> object and
* <code>lObj.longValue() == l</code>.
*
* @param obj the object whose field should be modified
* @param l the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setLong(Object obj, long l)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setLong(obj, l);
}
/**
* Sets the value of a field as a <code>float</code> on the specified object.
* This method is equivalent to
* <code>set(obj, fObj)</code>,
* where <code>fObj</code> is a <code>Float</code> object and
* <code>fObj.floatValue() == f</code>.
*
* @param obj the object whose field should be modified
* @param f the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setFloat(Object obj, float f)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setFloat(obj, f);
}
/**
* Sets the value of a field as a <code>double</code> on the specified object.
* This method is equivalent to
* <code>set(obj, dObj)</code>,
* where <code>dObj</code> is a <code>Double</code> object and
* <code>dObj.doubleValue() == d</code>.
*
* @param obj the object whose field should be modified
* @param d the new value for the field of <code>obj</code>
* being modified
*
* @exception IllegalAccessException if the underlying field
* is inaccessible.
* @exception IllegalArgumentException if the specified object is not an
* instance of the class or interface declaring the underlying
* field (or a subclass or implementor thereof),
* or if an unwrapping conversion fails.
* @exception NullPointerException if the specified object is null
* and the field is an instance field.
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails.
* @see Field#set
*/
public void setDouble(Object obj, double d)
throws IllegalArgumentException, IllegalAccessException
{
getFieldAccessor(obj).setDouble(obj, d);
}
// Convenience routine which performs security checks
private FieldAccessor getFieldAccessor(Object obj)
throws IllegalAccessException
{
doSecurityCheck(obj);
if (fieldAccessor == null) {
acquireFieldAccessor();
}
return fieldAccessor;
}
//如果属性对象是由copy得来则属性对象与生成此对象的对象使用同一FieldAccessor
private void acquireFieldAccessor() {
FieldAccessor tmp = null;
if (root != null) tmp = root.getFieldAccessor();
if (tmp != null) {
fieldAccessor = tmp;
return;
}
//不是由copy得来 创建FieldAccessor对象
tmp = reflectionFactory.newFieldAccessor(this);
setFieldAccessor(tmp);
}
//获取FieldAccessor对象
private FieldAccessor getFieldAccessor() {
return fieldAccessor;
}
//设置FieldAccessor对象
private void setFieldAccessor(FieldAccessor accessor) {
fieldAccessor = accessor;
if (root != null) {
//如果有上级则递归设置
root.setFieldAccessor(accessor);
}
}
//做安全检查
private void doSecurityCheck(Object obj) throws IllegalAccessException {
if (!override) {//做安全检查
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
Class caller = Reflection.getCallerClass(4);
Class targetClass = ((obj == null || !Modifier.isProtected(modifiers))
? clazz
: obj.getClass());
if (securityCheckCache != caller ||
targetClass != securityCheckTargetClassCache) {
Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
securityCheckCache = caller;
securityCheckTargetClassCache = targetClass;
}
}
}
}
//返回类的显示名字,如果不是数组则直接返回类名
//如果是数组,则取出最底层元素的对象,显示为元素名+[]..([]个数为数组维数)
static String getTypeName(Class type) {
if (type.isArray()) {//类代表数组
try {
Class cl = type;
//维数
int dimensions = 0;
//取得最底层的数组元素类型,并计算出维数
while (cl.isArray()) {
dimensions++;
cl = cl.getComponentType();
}
StringBuffer sb = new StringBuffer();
sb.append(cl.getName());
for (int i = 0; i < dimensions; i++) {
sb.append("[]");
}
return sb.toString();
} catch (Throwable e) {
//异常
}
}
//不代表数组则直接返回类名
return type.getName();
}
}