📄 beanutils.java
字号:
throw new Exception(
"error.beans.nomethod.setproperty");
}
}
}
//*********************************************************************
// PropertyEditor Support
private static Object getValueFromBeanInfoPropertyEditor(
Class attrClass, String attrName, String attrValue,
Class propertyEditorClass) throws Exception {
try {
PropertyEditor pe = (PropertyEditor) propertyEditorClass.newInstance();
pe.setAsText(attrValue);
return pe.getValue();
}
catch (Exception ex) {
throw new Exception(
"Unable to convert string '" + attrValue + "' to class " +
attrClass.getName() + " for attribute " + attrName +
": " + ex);
}
}
// __begin convertMethod
private static Object convert(String propertyName, String propertyValue,
Class type,
Class propertyEditorClass) throws Exception {
try {
if (propertyValue == null) {
if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
propertyValue = "false";
}
else {
return null;
}
}
if (propertyEditorClass != null) {
return getValueFromBeanInfoPropertyEditor(
type, propertyName, propertyValue, propertyEditorClass);
}
else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
if (propertyValue.equalsIgnoreCase("on") ||
propertyValue.equalsIgnoreCase("true")) {
propertyValue = "true";
}
else {
propertyValue = "false";
}
return new Boolean(propertyValue);
}
else if (type.equals(Byte.class) || type.equals(Byte.TYPE)) {
return new Byte(propertyValue);
}
else if (type.equals(Character.class) || type.equals(Character.TYPE)) {
return propertyValue.length() > 0 ?
new Character(propertyValue.charAt(0)) : null;
}
else if (type.equals(Short.class) || type.equals(Short.TYPE)) {
return new Short(propertyValue);
}
else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
return new Integer(propertyValue);
}
else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
return new Float(propertyValue);
}
else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
return new Long(propertyValue);
}
else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
return new Double(propertyValue);
}
else if (type.equals(String.class)) {
return propertyValue;
}
else if (type.equals(java.io.File.class)) {
return new java.io.File(propertyValue);
}
else if (type.equals(java.util.Date.class)) {
java.util.Date date = com.gzrealmap.oa.commonFunction.
getStringToTime(propertyValue);
return date;
}
if (type.equals(java.sql.Timestamp.class)) {
java.util.Date date = com.gzrealmap.oa.commonFunction.
getStringToTime(propertyValue);
return new Timestamp(date.getTime());
}
else if (type.getName().equals("java.lang.Object")) {
return new Object[] {
propertyValue};
}
else {
return getValueFromPropertyEditorManager(
type, propertyName, propertyValue);
}
}
catch (Exception ex) {
throw new Exception(ex);
}
}
private static Object getValueFromPropertyEditorManager(
Class attrClass, String attrName, String attrValue) throws Exception {
try {
PropertyEditor propEditor =
PropertyEditorManager.findEditor(attrClass);
if (propEditor != null) {
propEditor.setAsText(attrValue);
return propEditor.getValue();
}
else {
throw new IllegalArgumentException(
"Property Editor not registered with the PropertyEditorManager");
}
}
catch (IllegalArgumentException ex) {
throw new Exception(
"Unable to convert string '" + attrValue + "' to class " +
attrClass.getName() + " for attribute " + attrName +
": " + ex);
}
}
// __end toStringMethod
/**
* Create a typed array.
* This is a special case where params are passed through
* the request and the property is indexed.
*/
private static void createTypedArray(String propertyName,
Object bean, Method method,
String[] values, Class t,
Class propertyEditorClass) throws
Exception {
try {
if (propertyEditorClass != null) {
Object[] tmpval = new Integer[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] =
getValueFromBeanInfoPropertyEditor(
t, propertyName, values[i], propertyEditorClass);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Integer.class)) {
Integer[] tmpval = new Integer[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Integer(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Byte.class)) {
Byte[] tmpval = new Byte[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Byte(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Boolean.class)) {
Boolean[] tmpval = new Boolean[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Boolean(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Short.class)) {
Short[] tmpval = new Short[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Short(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Long.class)) {
Long[] tmpval = new Long[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Long(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Double.class)) {
Double[] tmpval = new Double[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Double(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Float.class)) {
Float[] tmpval = new Float[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Float(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(Character.class)) {
Character[] tmpval = new Character[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = new Character(values[i].charAt(0));
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(int.class)) {
int[] tmpval = new int[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Integer.parseInt(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(byte.class)) {
byte[] tmpval = new byte[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Byte.parseByte(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(boolean.class)) {
boolean[] tmpval = new boolean[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = (Boolean.valueOf(values[i])).booleanValue();
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(short.class)) {
short[] tmpval = new short[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Short.parseShort(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(long.class)) {
long[] tmpval = new long[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Long.parseLong(values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(double.class)) {
double[] tmpval = new double[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Double.valueOf(values[i]).doubleValue();
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(float.class)) {
float[] tmpval = new float[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = Float.valueOf(values[i]).floatValue();
}
method.invoke(bean, new Object[] {tmpval});
}
else if (t.equals(char.class)) {
char[] tmpval = new char[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] = values[i].charAt(0);
}
method.invoke(bean, new Object[] {tmpval});
}
else {
Object[] tmpval = new Integer[values.length];
for (int i = 0; i < values.length; i++) {
tmpval[i] =
getValueFromPropertyEditorManager(
t, propertyName, values[i]);
}
method.invoke(bean, new Object[] {tmpval});
}
}
catch (Exception ex) {
throw new Exception("error in invoking method");
}
}
public static void main(String[] args) {
try {
BeanUtils util = BeanUtils.getInstance();
// com.gzrealmap.oa.archive.simpleTemplate.Node node= new com.gzrealmap.oa.archive.simpleTemplate.Node();
// com.gzrealmap.oa.archive.simpleTemplate.NodeForm form= new com.gzrealmap.oa.archive.simpleTemplate.NodeForm();
// form.setName("qq");
// node.setNodeForm(form);
// util.setBeanParameter(node,"nodeForm", form);
// System.out.println(""+node.getNodeForm().getName());
com.gzrealmap.archive.ArchiveFile file = new com.gzrealmap.archive.
ArchiveFile();
// file.setLastModified(null);
util.setBeanParameter(file,"lastModified","2002-02-03");
System.out.println(""+file.getLastModified());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -