📄 localebeanutilsbean.java
字号:
* @param name The property name
* @param propName The Simple name of target property
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
protected Class definePropertyType(Object target, String name, String propName)
throws IllegalAccessException, InvocationTargetException {
Class type = null; // Java type of target property
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean) target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null) {
return null; // Skip this property setter
}
type = dynaProperty.getType();
}
else {
PropertyDescriptor descriptor = null;
try {
descriptor =
getPropertyUtils().getPropertyDescriptor(target, name);
if (descriptor == null) {
return null; // Skip this property setter
}
}
catch (NoSuchMethodException e) {
return null; // Skip this property setter
}
if (descriptor instanceof MappedPropertyDescriptor) {
type = ((MappedPropertyDescriptor) descriptor).
getMappedPropertyType();
}
else if (descriptor instanceof IndexedPropertyDescriptor) {
type = ((IndexedPropertyDescriptor) descriptor).
getIndexedPropertyType();
}
else {
type = descriptor.getPropertyType();
}
}
return type;
}
/**
* Convert the specified value to the required type using the
* specified convertion pattern.
*
* @param type The Java type of target property
* @param index The indexed subscript value (if any)
* @param value The value to be converted
* @param pattern The convertion pattern
*/
protected Object convert(Class type, int index, Object value, String pattern) {
if (log.isTraceEnabled()) {
log.trace("Converting value '" + value + "' to type:" + type);
}
Object newValue = null;
if (type.isArray() && (index < 0)) { // Scalar value into array
if (value instanceof String) {
String values[] = new String[1];
values[0] = (String) value;
newValue = getLocaleConvertUtils().convert((String[]) values, type, pattern);
}
else if (value instanceof String[]) {
newValue = getLocaleConvertUtils().convert((String[]) value, type, pattern);
}
else {
newValue = value;
}
}
else if (type.isArray()) { // Indexed value into array
if (value instanceof String) {
newValue = getLocaleConvertUtils().convert((String) value,
type.getComponentType(), pattern);
}
else if (value instanceof String[]) {
newValue = getLocaleConvertUtils().convert(((String[]) value)[0],
type.getComponentType(), pattern);
}
else {
newValue = value;
}
}
else { // Value into scalar
if (value instanceof String) {
newValue = getLocaleConvertUtils().convert((String) value, type, pattern);
}
else if (value instanceof String[]) {
newValue = getLocaleConvertUtils().convert(((String[]) value)[0],
type, pattern);
}
else {
newValue = value;
}
}
return newValue;
}
/**
* Convert the specified value to the required type.
*
* @param type The Java type of target property
* @param index The indexed subscript value (if any)
* @param value The value to be converted
*/
protected Object convert(Class type, int index, Object value) {
Object newValue = null;
if (type.isArray() && (index < 0)) { // Scalar value into array
if (value instanceof String) {
String values[] = new String[1];
values[0] = (String) value;
newValue = ConvertUtils.convert((String[]) values, type);
}
else if (value instanceof String[]) {
newValue = ConvertUtils.convert((String[]) value, type);
}
else {
newValue = value;
}
}
else if (type.isArray()) { // Indexed value into array
if (value instanceof String) {
newValue = ConvertUtils.convert((String) value,
type.getComponentType());
}
else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[]) value)[0],
type.getComponentType());
}
else {
newValue = value;
}
}
else { // Value into scalar
if (value instanceof String) {
newValue = ConvertUtils.convert((String) value, type);
}
else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[]) value)[0],
type);
}
else {
newValue = value;
}
}
return newValue;
}
/**
* Invoke the setter method.
*
* @param target The bean
* @param propName The Simple name of target property
* @param key The Mapped key value (if any)
* @param index The indexed subscript value (if any)
* @param newValue The value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
protected void invokeSetter(Object target, String propName, String key, int index, Object newValue)
throws IllegalAccessException, InvocationTargetException {
try {
if (index >= 0) {
getPropertyUtils().setIndexedProperty(target, propName,
index, newValue);
}
else if (key != null) {
getPropertyUtils().setMappedProperty(target, propName,
key, newValue);
}
else {
getPropertyUtils().setProperty(target, propName, newValue);
}
}
catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
}
/**
* Resolve any nested expression to get the actual target bean.
*
* @param bean The bean
* @param name The property name
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
protected Descriptor calculate(Object bean, String name)
throws IllegalAccessException, InvocationTargetException {
String propName = null; // Simple name of target property
int index = -1; // Indexed subscript value (if any)
String key = null; // Mapped key value (if any)
Object target = bean;
int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM);
if (delim >= 0) {
try {
target =
getPropertyUtils().getProperty(bean, name.substring(0, delim));
}
catch (NoSuchMethodException e) {
return null; // Skip this property setter
}
name = name.substring(delim + 1);
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
}
// Calculate the property name, index, and key values
propName = name;
int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
if (i >= 0) {
int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
try {
index =
Integer.parseInt(propName.substring(i + 1, k));
}
catch (NumberFormatException e) {
;
}
propName = propName.substring(0, i);
}
int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
if (j >= 0) {
int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
try {
key = propName.substring(j + 1, k);
}
catch (IndexOutOfBoundsException e) {
;
}
propName = propName.substring(0, j);
}
return new Descriptor(target, name, propName, key, index);
}
protected class Descriptor {
private int index = -1; // Indexed subscript value (if any)
private String name;
private String propName; // Simple name of target property
private String key; // Mapped key value (if any)
private Object target;
public Descriptor(Object target, String name, String propName, String key, int index) {
setTarget(target);
setName(name);
setPropName(propName);
setKey(key);
setIndex(index);
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPropName() {
return propName;
}
public void setPropName(String propName) {
this.propName = propName;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -