1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of Genjava-Core nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.beans;
33
34 import java.beans.BeanInfo;
35 import java.beans.Introspector;
36 import java.beans.IntrospectionException;
37 import java.beans.PropertyDescriptor;
38 import java.lang.reflect.Method;
39 import java.lang.reflect.InvocationTargetException;
40
41 import com.generationjava.lang.ClassW;
42 import com.generationjava.time.DateW;
43
44 import org.apache.commons.lang.NumberUtils;
45
46 public class BeansW {
47
48 static public Object convert(Object value, Class toClass) {
49 System.err.println("ASKED TO CONVERT: "+value+" which is a "+value.getClass()+" to "+toClass);
50 if(toClass.equals(value.getClass())) {
51 return value;
52 } else
53 if(ClassW.classInstanceOf(toClass, "java.lang.String")) {
54 return value;
55 } else
56 if(ClassW.classInstanceOf(toClass, "java.lang.Integer") ||
57 Integer.TYPE.equals(toClass)
58 ) {
59 return NumberUtils.createInteger(value.toString());
60 } else
61 if(ClassW.classInstanceOf(toClass, "java.lang.Long") ||
62 Long.TYPE.equals(toClass)
63 ) {
64 return NumberUtils.createLong(value.toString());
65 } else
66 if(ClassW.classInstanceOf(toClass, "java.lang.Double") ||
67 Double.TYPE.equals(toClass)
68 ) {
69 return NumberUtils.createDouble(value.toString());
70 } else
71 if(ClassW.classInstanceOf(toClass, "java.lang.Number")) {
72 return NumberUtils.createNumber(value.toString());
73 } else
74 if(ClassW.classInstanceOf(toClass, "java.util.Date")) {
75 return DateW.parseString(value.toString());
76 } else {
77 return value;
78 }
79 }
80
81 static public String beanToString(Object bean) {
82 if(bean == null) {
83 return null;
84 }
85 Class clss = bean.getClass();
86 StringBuffer text = new StringBuffer();
87 try {
88 BeanInfo beaninfo = Introspector.getBeanInfo(clss);
89 PropertyDescriptor[] pd = beaninfo.getPropertyDescriptors();
90 for(int i=0; i<pd.length; i++) {
91 if(pd[i] == null) {
92 continue;
93 }
94 Method method = pd[i].getReadMethod();
95 Class arg = method.getReturnType();
96 if("class".equals(pd[i].getName())) {
97 if(arg == Class.class) {
98 continue;
99 }
100 }
101 Object val = method.invoke(bean,new Object[0]);
102 text.append(pd[i].getName());
103 text.append(": ");
104 text.append(""+val);
105 text.append("\n");
106 }
107 } catch(IntrospectionException ie) {
108 ie.printStackTrace();
109 } catch(IllegalArgumentException iae) {
110 iae.printStackTrace();
111 } catch(IllegalAccessException iae) {
112 iae.printStackTrace();
113 } catch(InvocationTargetException ite) {
114 ite.printStackTrace();
115 }
116
117 return text.toString();
118 }
119
120 }
This page was automatically generated by Maven