1 package charactermanaj.util;
\r
3 import java.awt.Color;
\r
4 import java.beans.BeanInfo;
\r
5 import java.beans.IntrospectionException;
\r
6 import java.beans.Introspector;
\r
7 import java.beans.PropertyDescriptor;
\r
8 import java.lang.reflect.InvocationTargetException;
\r
9 import java.lang.reflect.Method;
\r
10 import java.math.BigDecimal;
\r
11 import java.math.BigInteger;
\r
12 import java.util.HashSet;
\r
13 import java.util.Properties;
\r
14 import java.util.Set;
\r
15 import java.util.logging.Level;
\r
16 import java.util.logging.Logger;
\r
20 * Setter/Getterのペアをもつビーンのプロパティを文字列化してプロパティに設定するか、
\r
21 * プロパティからビーンに値を設定するためのユーテリティクラス.<br>
\r
24 public final class BeanPropertiesUtilities {
\r
26 private static final Logger logger = Logger.getLogger(BeanPropertiesUtilities.class.getName());
\r
28 private BeanPropertiesUtilities() {
\r
29 throw new RuntimeException("utilities class.");
\r
33 * ビーンのSetter/Getterのペアをもつプロパティに対して、Propertiesより該当するプロパティの値を
\r
34 * 読み取り、プロパティに設定します.<br>
\r
35 * Propertiesに該当するプロパティ名が設定されていなければスキップされます.<br>
\r
36 * Propertiesにビーンにないプロパティ名があった場合、それは単に無視されます.<br>
\r
37 * Propertyの値が空文字の場合、Beanのプロパティの型が文字列以外であればnullが設定されます.<br>
\r
38 * (文字列の場合、空文字のまま設定されます.書き込み時、nullは空文字にされるため、文字列についてはnullを表現することはできません。)<br>
\r
39 * @param bean 設定されるビーン
\r
40 * @param props プロパティソース
\r
41 * @return 値の設定を拒否されたプロパティの名前、エラーがなければ空
\r
43 public static Set<String> loadFromProperties(Object bean, Properties props) {
\r
44 if (bean == null || props == null) {
\r
45 throw new IllegalArgumentException();
\r
47 HashSet<String> rejectNames = new HashSet<String>();
\r
49 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
\r
50 for (PropertyDescriptor propDesc : beanInfo
\r
51 .getPropertyDescriptors()) {
\r
52 Class<?> typ = propDesc.getPropertyType();
\r
53 Method mtdReader = propDesc.getReadMethod();
\r
54 Method mtdWriter = propDesc.getWriteMethod();
\r
55 if (mtdReader != null && mtdWriter != null) {
\r
56 // 読み書き双方が可能なもののみ対象とする.
\r
58 String name = propDesc.getName();
\r
60 String strVal = props.getProperty(name);
\r
61 if (strVal == null) {
\r
66 Throwable reject = null;
\r
68 if (String.class.equals(typ)) {
\r
70 } else if (strVal.length() == 0) {
\r
73 if (Boolean.class.equals(typ) || boolean.class.equals(typ)) {
\r
74 val = Boolean.valueOf(strVal);
\r
75 } else if (Integer.class.equals(typ) || int.class.equals(typ)) {
\r
76 val = Integer.valueOf(strVal);
\r
77 } else if (Long.class.equals(typ) || long.class.equals(typ)) {
\r
78 val = Long.valueOf(strVal);
\r
79 } else if (Float.class.equals(typ) || float.class.equals(typ)) {
\r
80 val = Float.valueOf(strVal);
\r
81 } else if (Double.class.equals(typ) || double.class.equals(typ)) {
\r
82 val = Double.valueOf(strVal);
\r
83 } else if (BigInteger.class.equals(typ)) {
\r
84 val = new BigInteger(strVal);
\r
85 } else if (BigDecimal.class.equals(typ)) {
\r
86 val = new BigDecimal(strVal);
\r
87 } else if (Color.class.equals(typ)) {
\r
88 val = Color.decode(strVal);
\r
90 rejectNames.add(name);
\r
91 logger.log(Level.WARNING,
\r
92 "unsupported propery type: " + typ
\r
93 + "/beanClass=" + bean.getClass() + " #" + name);
\r
97 mtdWriter.invoke(bean, val);
\r
100 } catch (InvocationTargetException ex) {
\r
102 } catch (IllegalAccessException ex) {
\r
104 } catch (RuntimeException ex) {
\r
108 if (reject != null) {
\r
109 rejectNames.add(name);
\r
110 logger.log(Level.WARNING, "invalid propery: "
\r
111 + typ + "/beanClass="
\r
112 + bean.getClass() + " #" + name + " /val=" + strVal
\r
117 } catch (IntrospectionException ex) {
\r
118 throw new RuntimeException("bean intorospector failed. :" + bean.getClass(), ex);
\r
120 return rejectNames;
\r
124 * ビーンのSetter/Getterのペアをもつプロパティの各値をPropertiesに文字列として登録します.<br>
\r
125 * nullの場合は空文字が設定されます.<br>
\r
126 * @param bean プロパティに転送する元情報となるビーン
\r
127 * @param props 設定されるプロパティ
\r
129 public static void saveToProperties(Object bean, Properties props) {
\r
130 if (bean == null || props == null) {
\r
131 throw new IllegalArgumentException();
\r
135 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
\r
136 for (PropertyDescriptor propDesc : beanInfo.getPropertyDescriptors()) {
\r
137 Method mtdReader = propDesc.getReadMethod();
\r
138 Method mtdWriter = propDesc.getWriteMethod();
\r
139 if (mtdReader != null && mtdWriter != null) {
\r
140 // 読み書き双方が可能なもののみ対象とする.
\r
142 String name = propDesc.getName();
\r
143 Object val = mtdReader.invoke(bean);
\r
148 } else if (val instanceof String) {
\r
149 strVal = (String) val;
\r
150 } else if (val instanceof Number) {
\r
151 strVal = ((Number) val).toString();
\r
152 } else if (val instanceof Boolean) {
\r
153 strVal = ((Boolean) val).booleanValue() ? "true" : "false";
\r
154 } else if (val instanceof Color) {
\r
155 strVal = "#" + Integer.toHexString(((Color) val).getRGB() & 0xffffff);
\r
157 logger.log(Level.WARNING, "unsupported propery type: "
\r
158 + val.getClass() + "/beanClass="
\r
159 + bean.getClass() + " #" + name);
\r
163 props.setProperty(name, strVal);
\r
167 } catch (IllegalAccessException ex) {
\r
168 throw new RuntimeException("bean property read failed. :" + bean.getClass(), ex);
\r
169 } catch (InvocationTargetException ex) {
\r
170 throw new RuntimeException("bean property read failed. :" + bean.getClass(), ex);
\r
171 } catch (IntrospectionException ex) {
\r
172 throw new RuntimeException("bean intorospector failed. :" + bean.getClass(), ex);
\r