Package com.codiform.moo.translator

Source Code of com.codiform.moo.translator.DefaultObjectTargetFactory

package com.codiform.moo.translator;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.codiform.moo.TranslationInitializationException;

/**
* The default target factory for properties annotated with @Property, this simply
* instantiates whatever class was defined in the destination method or field.
*/
public class DefaultObjectTargetFactory implements TranslationTargetFactory {
 
  @Override
  public <T> T getTranslationTargetInstance( Object source, Class<T> targetType ) {
    try {
      Constructor<T> constructor = targetType.getDeclaredConstructor();
      constructor.setAccessible( true );
      return constructor.newInstance();
    } catch ( NoSuchMethodException exception ) {
      throw new TranslationInitializationException( "No no-argument constructor in class " + targetType.getName(), exception );
    } catch ( InstantiationException exception ) {
      throw new TranslationInitializationException( String.format( "Error while instantiating %s", targetType ), exception );
    } catch ( IllegalAccessException exception ) {
      throw new TranslationInitializationException( String.format( "Not allowed to instantiate %s", targetType ), exception );
    } catch ( IllegalArgumentException exception ) {
      throw new TranslationInitializationException( String.format( "Error while instantiating %s", targetType ), exception );
    } catch ( InvocationTargetException exception ) {
      throw new TranslationInitializationException( String.format( "Error thrown by constructor of %s", targetType ), exception );
    }
  }

 
}
TOP

Related Classes of com.codiform.moo.translator.DefaultObjectTargetFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.