Coverage Report - org.dozer.converters.StringConstructorConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
StringConstructorConverter
80%
8/10
N/A
3.5
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.dozer.converters;
 17  
 
 18  
 import org.apache.commons.beanutils.Converter;
 19  
 
 20  
 import java.lang.reflect.Constructor;
 21  
 
 22  
 /**
 23  
  * Internal class for converting String --> Complex Data Types with a String constructor. Only intended for internal
 24  
  * use.
 25  
  * 
 26  
  * @author tierney.matt
 27  
  */
 28  
 public class StringConstructorConverter implements Converter {
 29  
 
 30  
   private StringConverter stringConverter;
 31  
 
 32  120
   public StringConstructorConverter(DateFormatContainer dateFormatContainer) {
 33  120
     this.stringConverter = new StringConverter(dateFormatContainer);
 34  120
   }
 35  
 
 36  
   public Object convert(Class destClass, Object srcObj) {
 37  120
     String result = (String) stringConverter.convert(destClass, srcObj);
 38  
     try {
 39  120
       Constructor constructor = destClass.getConstructor(String.class); // TODO Check, but not catch
 40  97
       return constructor.newInstance(result);
 41  23
     } catch (NoSuchMethodException e) {
 42  
       // just return the string
 43  23
       return result;
 44  0
     } catch (Exception e) {
 45  0
       throw new ConversionException(e);
 46  
     }
 47  
   }
 48  
 
 49  
 }