📄 month.java
字号:
package beanutils;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConstructorUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.LazyDynaMap;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.WrapDynaBean;
/**
* Title : Base Dict Class
* Description : here Description is the function of class, here maybe multirows
* @author <a href="mailto:sunpeng@china.freeborders">kevin</a>
* @Version 1.0
*/
/**
* Class description goes here.
* @version 1.0 2005-9-21
* @author kevin
*/
public class Month
{
private int value;
private String name;
private SimpleMonth simple;
private int[] days = {11, 22, 33, 44, 55};
public Month(int v, String n)
{
value = v;
name = n;
simple = new SimpleMonth();
}
/**
* @return Returns the simple.
*/
public SimpleMonth getSimple()
{
return simple;
}
/**
* @param simple The simple to set.
*/
public void setSimple(SimpleMonth simple)
{
this.simple = simple;
}
/**
* Returns the name.
* @return String
*/
public String getName()
{
return name;
}
/**
* Returns the value.
* @return int
*/
public int getValue()
{
return value;
}
/**
* Sets the name.
* @param name The name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* Sets the value.
* @param value The value to set
*/
public void setValue(int value)
{
this.value = value;
}
public void methodTest(String test)
{
System.out.println(">>>>>>>>>..methodTest()===" + test);
}
/**
* @see java.lang.Object#toString()
*/
public String toString()
{
return value + "(" + name + ")";
}
public int[] getDays()
{
return days;
}
public void setDays(int[] is)
{
days = is;
}
public static void getProperty()
{
/*Month month = new Month(1, "Jan");
try
{
System.out.println(BeanUtils.getProperty(month, "name"));
}
catch(Exception e)
{
e.printStackTrace();
}*/
Month month = new Month(1, "Jan");
try
{
System.out.println(PropertyUtils.getIndexedProperty(month, "days", 1));
System.out.println(PropertyUtils.getNestedProperty(month, "simple.name"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void copyProperties()
{
Month month1 = new Month(1, "Jan");
//Month month2 = new Month(2, "Feb");
SimpleMonth month2 = new SimpleMonth();
month2.setName("test--");
month2.setValue(1111);
try
{
BeanUtils.copyProperties(month1, month2);
System.out.println("====>" + month1.getValue());
}
catch(Exception e)
{
}
}
public static void describe()
{
Month month = new Month(1, "Jan");
try
{
Map map = BeanUtils.describe(month);
Set keySet = map.keySet();
for(Iterator iter = keySet.iterator(); iter.hasNext();)
{
Object element = (Object)iter.next();
System.out.println("KeyClass:" + element.getClass().getName());
System.out.println("ValueClass:" + map.get(element).getClass().getName());
System.out.print(element + "\t");
System.out.print(map.get(element));
System.out.println();
}
Month bean = new Month(2, "Feb");
BeanUtils.populate(bean, map);
System.out.println("BeanUtils.populate===>" + bean.getName());
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(InvocationTargetException e)
{
e.printStackTrace();
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
}
}
public static void createObj()
{
Object[] paramters = {new Integer(33), "test"};
try
{
Object obj = ConstructorUtils.invokeConstructor(Month.class, paramters);
Month month = (Month)obj;
System.out.println(BeanUtils.getProperty(month, "name"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void methodObj()
{
Month month = new Month(1, "Jan");
try
{
MethodUtils.invokeMethod(month, "methodTest", "this is test");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void wrapBean()
{
Month month = new Month(1, "dddddd");
DynaBean wrapper = new WrapDynaBean(month);
String name = (String)wrapper.get("name");
System.out.println("wrapBean()===" + name);
}
public static void dynaBean()
{
DynaProperty[] props = new DynaProperty[]{new DynaProperty("address", java.util.Map.class), new DynaProperty("subordinate", Month.class), new DynaProperty("firstName", String.class), new DynaProperty("lastName", String.class)};
BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
try
{
DynaBean employee = dynaClass.newInstance();
employee.set("address", new HashMap());
employee.set("subordinate", new Month(1, "dynaBean"));
employee.set("firstName", "Fred");
employee.set("lastName", "Flintstone");
System.out.println(PropertyUtils.getNestedProperty(employee, "subordinate.name"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void convertUtils()
{
String value = "111";
Integer it = (Integer)ConvertUtils.convert(value, Integer.class);
System.out.println("value==" + it.intValue());
}
public static void putProperty()
{
Month month = new Month(1, "dddddd");
HashMap map = new HashMap();
map.put("value", "99999");
map.put("name", "this is test");
try
{
BeanUtils.populate(month, map);// it will use ConvertUtils for convertings
System.out.println("name==" + month.getValue());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void lazyBean()
{
LazyDynaMap dynaBean1 = new LazyDynaMap();
dynaBean1.set("foo", "bar"); // simple
dynaBean1.set("customer", "title", "Mr"); // mapped
dynaBean1.set("address", 0, "address1"); // indexed
System.out.println(dynaBean1.get("address", 0));
Map myMap = dynaBean1.getMap(); // retrieve the Map
System.out.println(myMap.toString());
dynaBean1.setReturnNull(true);//设为ture。若Bean中没有此字段,返回null
//默认为false。若Bean中没有此字段,自动增加一个:)
System.out.println(dynaBean1.get("aaa"));//此时返回null
//默认为false,允许增删和修改
dynaBean1.setRestricted(true);
//dynaBean1.set("test", "error");//这里会出错!
}
public static void main(String[] args)
{
//describe();
//getProperty();
//copyProperties();
//createObj();
//methodObj();
//wrapBean();
//dynaBean();
//convertUtils();
//putProperty();
lazyBean();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -