View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean.conversion;
12  
13  import java.lang.reflect.Method;
14  
15  import org.csveed.bean.BeanProperty;
16  
17  /**
18   * The Class BeanWrapper.
19   */
20  public class BeanWrapper {
21  
22      /** The default converters. */
23      private DefaultConverters defaultConverters;
24  
25      /** The bean. */
26      private Object bean;
27  
28      /**
29       * Instantiates a new bean wrapper.
30       *
31       * @param defaultConverters
32       *            the default converters
33       * @param bean
34       *            the bean
35       */
36      public BeanWrapper(DefaultConverters defaultConverters, Object bean) {
37          this.defaultConverters = defaultConverters;
38          this.bean = bean;
39      }
40  
41      /**
42       * Gets the property.
43       *
44       * @param property
45       *            the property
46       *
47       * @return the property
48       *
49       * @throws ConversionException
50       *             the conversion exception
51       */
52      public String getProperty(BeanProperty property) throws ConversionException {
53          Method readMethod = property.getPropertyDescriptor().getReadMethod();
54          Converter converter = getConverter(property);
55          if (converter == null) {
56              throw new NoConverterFoundException("No Converter found for", getPropertyType(property));
57          }
58          try {
59              Object value = readMethod.invoke(bean);
60              return converter.toString(value);
61          } catch (Exception err) {
62              throw new BeanPropertyConversionException("Problem converting", converter.infoOnType(), err);
63          }
64      }
65  
66      /**
67       * Sets the property.
68       *
69       * @param property
70       *            the property
71       * @param value
72       *            the value
73       *
74       * @throws ConversionException
75       *             the conversion exception
76       */
77      public void setProperty(BeanProperty property, String value) throws ConversionException {
78          Method writeMethod = property.getPropertyDescriptor().getWriteMethod();
79          Converter converter = getConverter(property);
80          if (converter == null) {
81              throw new NoConverterFoundException("No Converter found for", getPropertyType(property));
82          }
83          try {
84              writeMethod.invoke(bean, value == null || value.equals("") ? null : converter.fromString(value));
85          } catch (Exception err) {
86              throw new BeanPropertyConversionException("Problem converting", converter.infoOnType(), err);
87          }
88      }
89  
90      /**
91       * Gets the converter.
92       *
93       * @param property
94       *            the property
95       *
96       * @return the converter
97       */
98      protected Converter getConverter(BeanProperty property) {
99          if (property.getConverter() != null) {
100             return property.getConverter();
101         }
102         return defaultConverters.getConverter(getPropertyType(property));
103     }
104 
105     /**
106      * Gets the property type.
107      *
108      * @param property
109      *            the property
110      *
111      * @return the property type
112      */
113     protected Class getPropertyType(BeanProperty property) {
114         return property.getPropertyDescriptor().getPropertyType();
115     }
116 
117 }