本帖最后由 杨冉 于 2013-4-18 02:02 编辑
都发了一天了也没个沙发。。占个位置好了~
1.
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- public class Test {
- public static void main(String[] args) throws Exception {
- Person p = (Person) Class.forName("Person").newInstance();
- HashMap<String, String> m = new HashMap<String, String>();
- m.put("username", "itheima");
- m.put("password", "it123456");
- m.put("birthday", "1998-10-11");
- Util.setProperty(p, "username", m.get("username"));
- Util.setProperty(p, "password", m.get("password"));
- Util.setProperty(p, "birthday", new SimpleDateFormat("yyyy-MM-dd").parse(m.get("birthday")));
- System.out.println("username:" + p.getUsername());
- System.out.println("password:" + p.getPassword());
- System.out.println("birthday:" + p.getBirthday());
- }
- }
- class Util {
- public static void setProperty(Object obj, String propertyName, Object value)
- throws Exception {
- PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
- Method methodSet = pd.getWriteMethod();
- methodSet.invoke(obj, value);
- }
- }
- class Person {
- private String username;
- private String password;
- private Date birthday;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- }
复制代码 2.- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- public class Test {
- public static void main(String[] args) throws Exception {
- Class clazz = Class.forName("java.util.Date");
- System.out.println("-----------------------------------Properties----------------------------");
- Field[] properties = clazz.getDeclaredFields();
- for (Field property : properties) {
- StringBuilder sb = new StringBuilder(Modifier.toString(property.getModifiers()));
- String type = property.getType().toString();
- sb.append(" ").append(type).append(" ").append(property.getName());
- System.out.println(sb);
- }
- System.out.println("-------------------------Methods----------------------------------------");
- Method[] methods = clazz.getMethods();
- for (Method method : methods) {
- String name = method.getName();
- StringBuilder sb = new StringBuilder(name);
- sb.append('(');
- Class[] params = method.getParameterTypes();
- for (Class param : params) {
- sb.append(param.getName());
- sb.append(',');
- }
- if (params != null && params.length != 0)
- sb.deleteCharAt(sb.length() - 1);
- sb.append(')');
- System.out.println(sb);
- }
- System.out.println("------------------------Constructors-----------------------------------");
- Constructor[] constructors = clazz.getConstructors();
- for (Constructor constructor : constructors) {
- String name = constructor.getName();
- StringBuilder sb = new StringBuilder(name);
- sb.append('(');
- Class[] params = constructor.getParameterTypes();
- for (Class param : params) {
- sb.append(param.getName());
- sb.append(',');
- }
- if (params != null && params.length != 0)
- sb.deleteCharAt(sb.length() - 1);
- sb.append(')');
- System.out.println(sb);
- }
- }
- }
复制代码 |