1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.properties.internal.converter

File ConvertUtilsConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
15
3
1
103
49
7
0.47
5
3
2.33

Classes

Class Line # Actions
ConvertUtilsConverter 46 15 0% 7 2
0.916666791.7%
 

Contributing tests

This file is covered by 1284 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.properties.internal.converter;
21   
22    import java.lang.reflect.ParameterizedType;
23    import java.lang.reflect.Type;
24   
25    import javax.inject.Singleton;
26   
27    import org.apache.commons.beanutils.BeanUtilsBean;
28    import org.apache.commons.beanutils.ConversionException;
29    import org.apache.commons.beanutils.ConvertUtils;
30    import org.apache.commons.lang3.reflect.TypeUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.phase.Initializable;
33    import org.xwiki.properties.converter.Converter;
34   
35    /**
36    * {@link ConvertUtils}-based converter.
37    * <p>
38    * It's the default {@link Converter}, the one used when no other Converter could be found by
39    * {@link org.xwiki.properties.ConverterManager}.
40    *
41    * @version $Id: 4622a7b04737615fd399c6fb0a28616ecbeb5c2c $
42    * @since 2.0M2
43    */
44    @Component
45    @Singleton
 
46    public class ConvertUtilsConverter implements Converter, Initializable
47    {
48    /**
49    * By default {@link ConvertUtils#convert(Object, Class)} does not throw any exceptions for failed conversions.
50    * Instead it will return some default value corresponding to the target type. We must override this behavior in
51    * order to get the desired functionality.
52    */
 
53  1479 toggle @Override
54    public void initialize()
55    {
56  1479 BeanUtilsBean.getInstance().getConvertUtils().register(true, false, 0);
57    }
58   
59    /**
60    * @param <T> the type in which the provided value has to be converted
61    * @param targetType the type in which the provided value has to be converted
62    * @param sourceValue the value to convert
63    * @return the converted value
64    */
 
65  21854 toggle private <T> T convert(Class<T> targetType, Object sourceValue)
66    {
67  21854 T result;
68   
69    // We can't use Class#cast(Object) because ConvertUtils#convert always return Object form of the targetType even
70    // if targetType is a primitive. When using casting syntax Object form is implicitly converter to proper
71    // primitive type.
72  21854 try {
73  21854 result = (T) ConvertUtils.convert(sourceValue, targetType);
74    } catch (ConversionException ex) {
75  1 throw new org.xwiki.properties.converter.ConversionException("Error while performing type conversion", ex);
76    }
77   
78    // BeanUtils converters will return the passed value if no converter has been found. Thus we need to check
79    // that the returned value is compatible with the expected type and raise a ConversionException if not.
80  21758 if (!TypeUtils.isAssignable(targetType, result.getClass())) {
81  16090 throw new org.xwiki.properties.converter.ConversionException(
82    String.format("Failed to find a Converter to convert from [%s] to [%s]",
83    sourceValue.getClass().getName(), targetType.getName()));
84    }
85   
86  5668 return result;
87    }
88   
 
89  21855 toggle @Override
90    public Object convert(Type targetType, Object sourceValue)
91    {
92  21855 Class<?> clazz;
93  21855 if (targetType instanceof Class) {
94  5766 clazz = (Class<?>) targetType;
95  16088 } else if (targetType instanceof ParameterizedType) {
96  16088 clazz = (Class<?>) ((ParameterizedType) targetType).getRawType();
97    } else {
98  0 throw new org.xwiki.properties.converter.ConversionException("Unknown type [" + targetType + "]");
99    }
100   
101  21855 return convert(clazz, sourceValue);
102    }
103    }