📄 genericobjectfactory.java
字号:
/**
* Returns a property definition for the specified tag name.
*
* @param tagName the tag name.
*
* @return the property definition.
*
* @throws ObjectDescriptionException if there is no such tag defined for this object.
*/
public PropertyDefinition getPropertyDefinitionByTagName(final String tagName)
throws ObjectDescriptionException {
for (int i = 0; i < this.propertyDefinitions.length; i++) {
final PropertyDefinition pdef = this.propertyDefinitions[i];
if (pdef.getElementName().equals(tagName)) {
return pdef;
}
}
throw new ObjectDescriptionException(
"This tag is not defined for this kind of object. : " + tagName
);
}
/**
* Returns the constructor definitions.
*
* @return the constructor definitions.
*/
public ConstructorDefinition[] getConstructorDefinitions() {
return this.constructorDefinitions;
}
/**
* Returns the attribute definitions.
*
* @return the attribute definitions.
*/
public AttributeDefinition[] getAttributeDefinitions() {
return this.attributeDefinitions;
}
/**
* Returns the property definitions.
*
* @return the property definitions.
*/
public PropertyDefinition[] getPropertyDefinitions() {
return this.propertyDefinitions;
}
/**
* Returns the property names.
*
* @return the property names.
*/
public String[] getOrderedPropertyNames() {
return this.orderedPropertyNames;
}
/**
* Returns the lookup definitions.
*
* @return the lookup definitions.
*/
public LookupDefinition[] getLookupDefinitions() {
return this.lookupDefinitions;
}
/**
* Returns the value of the specified property.
*
* @param name the property name.
*
* @return the property value.
*/
public Object getProperty(final String name) {
return this.propertyValues.get(name);
}
/**
* Creates an object according to the definition.
*
* @return the object.
*
* @throws ObjectDescriptionException if there is a problem with the object description.
*/
public Object createObject() throws ObjectDescriptionException {
final Class[] cArgs = new Class[this.constructorDefinitions.length];
final Object[] oArgs = new Object[this.constructorDefinitions.length];
for (int i = 0; i < cArgs.length; i++) {
final ConstructorDefinition cDef = this.constructorDefinitions[i];
cArgs[i] = cDef.getType();
if (cDef.isNull()) {
oArgs[i] = null;
}
else {
oArgs[i] = getProperty(cDef.getPropertyName());
}
}
try {
final Constructor constr = this.baseClass.getConstructor(cArgs);
final Object o = constr.newInstance(oArgs);
return o;
}
catch (Exception e) {
throw new ObjectDescriptionException("Ugh! Constructor made a buuuh!", e);
}
}
/**
* Sets a property value.
*
* @param propertyName the property name.
* @param value the property value.
*
* @throws ObjectDescriptionException if there is a problem with the object description.
*/
public void setProperty(final String propertyName, final Object value)
throws ObjectDescriptionException {
final PropertyDescriptor pdesc = getPropertyDescriptor(propertyName);
if (pdesc == null) {
throw new ObjectDescriptionException("Unknown property " + propertyName);
}
if (!isAssignableOrPrimitive(pdesc.getPropertyType(), value.getClass())) {
throw new ObjectDescriptionException(
"Invalid value: " + pdesc.getPropertyType() + " vs. " + value.getClass()
);
}
this.propertyValues.put(propertyName, value);
}
/**
* Returns <code>true</code> if the base type is a primitive or assignable from the value type.
*
* @param baseType the base class.
* @param valueType the value class.
*
* @return A boolean.
*/
private boolean isAssignableOrPrimitive(final Class baseType, final Class valueType) {
if (BasicTypeSupport.isBasicDataType(baseType)) {
return true;
}
// verbose stuff below *should* no longer be needed
return baseType.isAssignableFrom(valueType);
}
/**
* Returns <code>true<code> if the specified property is...
*
* @param propertyName the property name.
*
* @return A boolean.
*/
private boolean isConstructorProperty(final String propertyName) {
for (int i = 0; i < this.constructorDefinitions.length; i++) {
final ConstructorDefinition cDef = this.constructorDefinitions[i];
if (propertyName.equals(cDef.getPropertyName())) {
return true;
}
}
return false;
}
/**
* Writes the properties for the object.
*
* @param object the object.
*
* @throws ObjectDescriptionException if there is a problem.
*/
public void writeObjectProperties(final Object object) throws ObjectDescriptionException {
// this assumes that the order of setting the attributes does not matter.
for (int i = 0; i < this.orderedPropertyNames.length; i++) {
try {
final String name = this.orderedPropertyNames[i];
if (isConstructorProperty(name)) {
continue;
}
final Object value = getProperty(name);
if (value == null) {
// do nothing if value is not defined ...
continue;
}
final PropertyDescriptor pdescr = getPropertyDescriptor(name);
final Method setter = pdescr.getWriteMethod();
setter.invoke(object, new Object[]{value});
}
catch (Exception e) {
throw new ObjectDescriptionException(
"Failed to set properties." + getBaseClass(), e
);
}
}
}
/**
* Reads the properties.
*
* @param object the object.
*
* @throws ObjectDescriptionException if there is a problem.
*/
public void readProperties(final Object object) throws ObjectDescriptionException {
// this assumes that the order of setting the attributes does not matter.
for (int i = 0; i < this.orderedPropertyNames.length; i++) {
try {
final String name = this.orderedPropertyNames[i];
final PropertyDescriptor pdescr = getPropertyDescriptor(name);
if (pdescr == null) {
throw new IllegalStateException("No property defined: " + name);
}
final Method setter = pdescr.getReadMethod();
final Object value = setter.invoke(object, new Object[0]);
if (value == null) {
// do nothing if value is not defined ... or null
continue;
}
setProperty(name, value);
}
catch (Exception e) {
throw new ObjectDescriptionException("Failed to set properties.", e);
}
}
}
/**
* Returns the base class.
*
* @return the base class.
*/
public Class getBaseClass() {
return this.baseClass;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -