property.java
字号:
package piy;
import java.lang.reflect.*;
import java.io.Serializable;
/**
* Each getter/setter match in a class can have a Property class modelling it. This class
* provides information on a particular property a class possesses, and provides standard
* methods for invoking the methods using low-level reflection.
* @author David Vivash
* @version 1.0, 5/12/00
*/
public class Property implements Serializable
{
private Object holder;
private String name;
private Class type;
private boolean converted = false;
/**
* Construct a property for the specific object, where the property has the particular
* name and type. The property must be obtainable using the structure <code>
* type = holder.get"Name"() </code> and settable using <code> holder.set"Name"(type) </code>
* @param holder the Object which contains the property
* @param name the name of the property
* @param type the type of the property
*/
public Property(Object holder, String name, Class type) {
this.holder = holder;
this.name = name;
setType(type);
}
/**
* Get the name of the property.
* @return the name of the property, as provided in the constructor for this object
*/
public String getName() { return name; }
/**
* Get the holder holding this property.
* @return the object holding the property
*/
public Object getHolder() { return holder; }
/**
* Retrieves the value of the property by invoking the get"Name"() method. The returned
* Object should be castable to an object of the type specified in the constructor for this
* class.
* @return the return value of calling get"Name"() on the holder object
*/
public Object getValue() {
try {
return holder.getClass().getMethod("get" + name, new Class[0]).invoke(holder, null);
} catch (Exception e) {
System.err.println("Unable to get property " + name);
e.printStackTrace();
}
return null;
}
/**
* Sets the property to a new value. The type of the input variable shold be castable to
* the type of the property as specified in this constructor. This method effectively
* invokes holder.set"Name"(value).
* @param value the value to set the property to
*/
public void setValue(Object value) {
boolean success = false;
try {
holder.getClass().getMethod("set" + name, new Class[] { getType() }).invoke(holder, new Object[] { value });
success = true;
} catch (Exception e) {
//don't do anything yet - we'll try again
}
if (!success) { //try again with a general Object instead, sometimes works...
try{
holder.getClass().getMethod("set" + name, new Class[] { Object.class }).invoke(holder, new Object[] { value });
} catch (Exception e) {
System.err.println("Unable to set property " + name + " on holder " + holder + " with type " + getType());
e.printStackTrace();
}
}
}
/**
* This method should only be used by primitive type support classes, to set the
* type of the property when it has been incorrectly set to a non-primitive
* wrapper class type.
* @param type the new primitive type to set the property type to
*/
public void setType(Class type) {
if (type == null) return;
if (type.isPrimitive()) {
if (type == int.class) this.type = Integer.class;
if (type == boolean.class) this.type = Boolean.class;
if (type == byte.class) this.type = Byte.class;
if (type == char.class) this.type = Character.class;
if (type == short.class) this.type = Short.class;
if (type == long.class) this.type = Long.class;
if (type == float.class) this.type = Float.class;
if (type == double.class) this.type = Double.class;
if (type == void.class) this.type = Void.class;
converted = true;
} else {
this.type = type;
converted = false;
}
}
/**
* Get the type of the property.
* @return the type of the property, as provided in the constructor for this object
*/
public Class getType() {
if (converted) {
if (type == Integer.class) return int.class;
if (type == Boolean.class) return boolean.class;
if (type == Byte.class) return byte.class;
if (type == Character.class) return char.class;
if (type == Short.class) return short.class;
if (type == Long.class) return long.class;
if (type == Float.class) return float.class;
if (type == Double.class) return double.class;
if (type == Void.class) return void.class;
}
//else
return type;
}
public String toString() {
return "["+holder+", "+name+", "+type+"]";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -