设置多种数据类型,级联对象实例化,级联属性赋值

 

一个简单Java类中的属性类型不仅仅只有String,还会包含有整数、浮点数、日期等,本课程主要讲解如何实现多种数据类型的赋值以及转换处理操作。
一个类可以与其它类发生引用关系,以描述彼此之间的关系,这样的级联结构中就需要考虑对象实例化问题,本课程主要讲解如何在多级VO配置关系时如何通过反射技术实现动态实例化对象操作。
类引用定义之后就会存在有其它引用类型的属性赋值操作,本课程主要讲解多级实例化对象属性内容的获取与其属性设置。
  1 package com.twitter.demo;
  2 
  3 import java.util.Date;
  4 import java.lang.reflect.Field;
  5 import java.lang.reflect.Method;
  6 import java.text.ParseException;
  7 import java.text.SimpleDateFormat;
  8 
  9 class Company{
 10     private String name;
 11     private Date createdate;
 12     public String getName() {
 13         return this.name;
 14     }
 15     public void setName(String name) {
 16         this.name = name;
 17     }
 18     public Date getCreatedate() {
 19         return this.createdate;
 20     }
 21     public void setCreatedate(Date createdate) {
 22         this.createdate = createdate;
 23     }
 24 }
 25 
 26 class Dept{
 27     private String dname;
 28     private String loc;
 29     private Company company;
 30     public String getDname() {
 31         return dname;
 32     }
 33     public void setDname(String dname) {
 34         this.dname = dname;
 35     }
 36     public String getLoc() {
 37         return loc;
 38     }
 39     public void setLoc(String loc) {
 40         this.loc = loc;
 41     }
 42     public Company getCompany() {
 43         return company;
 44     }
 45     public void setCompany(Company company) {
 46         this.company = company;
 47     }
 48 }
 49 
 50 class Emp{
 51     private String ename;
 52     private String job;
 53     private Double salary;
 54     private Integer age;
 55     private Date hiredate;
 56     private Dept dept;
 57     public String getEname() {
 58         return ename;
 59     }
 60     public void setEname(String ename) {
 61         this.ename = ename;
 62     }
 63     public String getJob() {
 64         return job;
 65     }
 66     public void setJob(String job) {
 67         this.job = job;
 68     }
 69     public Double getSalary() {
 70         return salary;
 71     }
 72     public void setSalary(Double salary) {
 73         this.salary = salary;
 74     }
 75     public Integer getAge() {
 76         return age;
 77     }
 78     public void setAge(Integer age) {
 79         this.age = age;
 80     }
 81     public Date getHiredate() {
 82         return hiredate;
 83     }
 84     public void setHiredate(Date hiredate) {
 85         this.hiredate = hiredate;
 86     }
 87     public Dept getDept() {
 88         return dept;
 89     }
 90     public void setDept(Dept dept) {
 91         this.dept = dept;
 92     }
 93 }
 94 
 95 class BeanUtils{
 96     private BeanUtils() {}
 97     public static void setValue(Object obj,String value) {
 98         String results[] = value.split("\\|");
 99         for(int x=0;x<results.length;x++) {
100             String attval[] = results[x].split(":");
101             try {
102                 if(attval[0].contains(".")) {
103                     String temp[] = attval[0].split("\\.");
104                     Object currentObject = obj;
105                     for(int y=0;y<temp.length-1;y++) {
106                         Method getMethod = currentObject.getClass().getDeclaredMethod("get" + StringUtils.initcap(temp[y]));
107                         Object tempObject = getMethod.invoke(currentObject);
108                         if(tempObject == null) {
109                             Field field = currentObject.getClass().getDeclaredField(temp[y]);
110                             Method method = currentObject.getClass().getDeclaredMethod("set"+StringUtils.initcap(temp[y]), field.getType());
111                             Object newObject = field.getType().getDeclaredConstructor().newInstance();
112                             method.invoke(currentObject, newObject);
113                             currentObject = newObject;
114                         }else {
115                             currentObject = tempObject;
116                         }
117                     }
118                 }else {
119                     Field field = obj.getClass().getDeclaredField(attval[0]);
120                     Method setMethod = obj.getClass().getDeclaredMethod("set"+StringUtils.initcap(attval[0]), field.getType());
121                     Object convertValue = BeanUtils.convertAtributeValue(field.getType().getName(), attval[1]);
122                     setMethod.invoke(obj, convertValue);
123                 }
124             } catch (Exception e) {
125                 // TODO: handle exception
126             }
127         }
128     }
129     
130     private static Object convertAtributeValue(String type,String value) {
131         if("long".equals(type)||"java.lang.Long".equals(type)) {
132             return Long.parseLong(value);
133         }else if ("int".equals(type)||"java.lang.Integer".equals(type)) {
134             return Integer.parseInt(value);
135         }else if ("double".equals(type)||"java.lang.Double".equals(type)) {
136             return Double.parseDouble(value);
137         }else if ("java.util.Date".equals(type)) {
138             SimpleDateFormat sdf = null;
139             if(value.matches("\\d{4}-\\d{2}-\\d{2}")) {
140                 sdf = new SimpleDateFormat("yyyy-MM-dd");
141             }else if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}-\\d{2}-\\d{2}")) {
142                 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
143             }else {
144                 return new Date();
145             }
146             try {
147                 return sdf.parse(type);
148             } catch (ParseException e) {
149                 return new Date();
150             }
151         }else {
152             return value;
153         }
154     }
155 }
156 
157 class StringUtils{
158     public static String initcap(String str) {
159         if(str == null || "".equals(str)) {
160             return str;
161         }
162         if(str.length() == 1) {
163             return str.toUpperCase();
164         }else {
165             return str.substring(0,1).toUpperCase()+str.substring(1);
166         }
167     }
168 }
169 
170 class ClassInstanceFactory{
171     private ClassInstanceFactory() {}
172     public static <T> T create(Class<?> clazz,String value) {
173         try {
174             Object obj = clazz.getDeclaredConstructor().newInstance();
175             BeanUtils.setValue(obj, value);
176             return (T) obj;
177         } catch (Exception e) {
178             return null;
179         }
180         
181     }
182 }
183 
184 public class JavaReflectDemo {
185     public static void main(String[] args) throws Exception{
186         String value = "ename:Smith|job:Clerk|salary:8960.00|age:30|hiredate:2003-10-03|"
187                 +"dept.dname:财务部|dept.company.name:twitter|"
188                 +"dept.company.createdate:2001-11-11";
189         Emp emp = ClassInstanceFactory.create(Emp.class, value);
190         System.out.println(emp);
191         System.out.println(emp.getDept());
192         System.out.println(emp.getDept().getCompany());
193     }
194 }

 

点赞