Example usage for org.springframework.beans BeanUtils instantiateClass

List of usage examples for org.springframework.beans BeanUtils instantiateClass

Introduction

In this page you can find the example usage for org.springframework.beans BeanUtils instantiateClass.

Prototype

public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException 

Source Link

Document

Instantiate a class using its 'primary' constructor (for Kotlin classes, potentially having default arguments declared) or its default constructor (for regular Java classes, expecting a standard no-arg setup).

Usage

From source file:grails.util.GrailsClassUtils.java

/**
 * Returns the value of the specified property and type from an instance of the specified Grails class
 *
 * @param clazz The name of the class which contains the property
 * @param propertyName The property name
 *
 * @return The value of the property or null if none exists
 *///from  w  w w . jav  a 2  s .com
public static Object getPropertyValueOfNewInstance(Class<?> clazz, String propertyName) {
    // validate
    if (clazz == null || !StringUtils.hasText(propertyName)) {
        return null;
    }

    try {
        return getPropertyOrStaticPropertyOrFieldValue(BeanUtils.instantiateClass(clazz), propertyName);
    } catch (BeanInstantiationException e) {
        return null;
    }
}

From source file:it.doqui.index.ecmengine.business.personalization.hibernate.RoutingLocalSessionFactoryBean.java

/**
 * Subclasses can override this method to perform custom initialization
 * of the Configuration instance used for SessionFactory creation.
 * The properties of this RoutingLocalSessionFactoryBean will be applied to
 * the Configuration object that gets returned here.
 * <p>The default implementation creates a new Configuration instance.
 * A custom implementation could prepare the instance in a specific way,
 * or use a custom Configuration subclass.
 * @return the Configuration instance//from w ww .j  av  a  2s  . co m
 * @throws HibernateException in case of Hibernate initialization errors
 * @see org.hibernate.cfg.Configuration#Configuration()
 */
protected Configuration newConfiguration() {
    return (Configuration) BeanUtils.instantiateClass(this.configurationClass);
}

From source file:it.doqui.index.ecmengine.client.webservices.AbstractWebServiceDelegateBase.java

@SuppressWarnings("unchecked")
protected Object convertDTO(Object sourceDTO, Class destDTOClass) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("[" + getClass().getSimpleName() + "::convertDTO] BEGIN");
    }/*from   ww w  .  j a  v a  2 s.  com*/

    Object destDTO = null;
    try {
        if (sourceDTO != null) {
            if (log.isDebugEnabled()) {
                log.debug("[" + getClass().getSimpleName() + "::convertDTO] converting DTO from type "
                        + sourceDTO.getClass().getName() + " to type " + destDTOClass.getName());
            }
            destDTO = BeanUtils.instantiateClass(destDTOClass);
            PropertyDescriptor[] targetpds = BeanUtils.getPropertyDescriptors(destDTOClass);
            PropertyDescriptor sourcepd = null;
            if (log.isDebugEnabled()) {
                log.debug(" found " + targetpds.length + " properties for type " + destDTOClass.getName());
            }

            for (int i = 0; i < targetpds.length; i++) {
                if (targetpds[i].getWriteMethod() != null) {
                    Method writeMethod = targetpds[i].getWriteMethod();
                    sourcepd = BeanUtils.getPropertyDescriptor(sourceDTO.getClass(), targetpds[i].getName());
                    if (sourcepd != null && sourcepd.getReadMethod() != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] found property: "
                                    + targetpds[i].getName());
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] source type: "
                                    + sourcepd.getPropertyType().getName() + ", dest type: "
                                    + targetpds[i].getPropertyType().getName());
                        }
                        Method readMethod = sourcepd.getReadMethod();
                        Object valueObject = null;
                        if (!BeanUtils.isSimpleProperty(targetpds[i].getPropertyType())) {
                            if (sourcepd.getPropertyType().isArray()) {
                                valueObject = convertDTOArray(
                                        (Object[]) readMethod.invoke(sourceDTO, new Object[] {}),
                                        targetpds[i].getPropertyType().getComponentType());
                            } else if (sourcepd.getPropertyType().equals(java.util.Calendar.class)
                                    && targetpds[i].getPropertyType().equals(java.util.Date.class)) {
                                // if java.util.Calendar => convert to java.util.Date
                                valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                                if (valueObject != null) {
                                    valueObject = ((Calendar) valueObject).getTime();
                                }
                            } else if (sourcepd.getPropertyType().equals(java.util.Date.class)
                                    && targetpds[i].getPropertyType().equals(java.util.Calendar.class)) {
                                // if java.util.Date => convert to java.util.Calendar
                                Calendar calendar = Calendar.getInstance();
                                valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                                if (valueObject != null) {
                                    calendar.setTime((Date) valueObject);
                                    valueObject = calendar;
                                }
                            } else {
                                valueObject = convertDTO(readMethod.invoke(sourceDTO, new Object[0]),
                                        targetpds[i].getPropertyType());
                            }
                        } else {
                            valueObject = readMethod.invoke(sourceDTO, new Object[0]);
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] writing value: "
                                    + valueObject);
                        }
                        writeMethod.invoke(destDTO, new Object[] { valueObject });
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("[" + getClass().getSimpleName() + "::convertDTO] skipping property: "
                                    + targetpds[i].getName());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("[" + getClass().getSimpleName() + "::convertDTO] ERROR", e);
        throw e;
    } finally {
        if (log.isDebugEnabled()) {
            log.debug("[" + getClass().getSimpleName() + "::convertDTO] END");
        }
    }
    return destDTO;
}

From source file:net.sf.sail.webapp.spring.impl.CustomContextLoader.java

/**
 * The behaviour of this method is the same as the superclass except for
 * setting of the config locations./*from  w  w  w.ja v a2  s.  c  o m*/
 * 
 * @throws ClassNotFoundException
 * 
 * @see org.springframework.web.context.ContextLoader#createWebApplicationContext(javax.servlet.ServletContext,
 *      org.springframework.context.ApplicationContext)
 */
@Override
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext,
        ApplicationContext parent) throws BeansException {

    Class<?> contextClass = determineContextClass(servletContext);
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Custom context class [" + contextClass.getName()
                + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext webApplicationContext = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(contextClass);
    webApplicationContext.setParent(parent);
    webApplicationContext.setServletContext(servletContext);

    String configClass = servletContext.getInitParameter(CONFIG_CLASS_PARAM);
    if (configClass != null) {
        try {
            SpringConfiguration springConfig = (SpringConfiguration) BeanUtils
                    .instantiateClass(Class.forName(configClass));
            webApplicationContext.setConfigLocations(springConfig.getRootApplicationContextConfigLocations());
        } catch (ClassNotFoundException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(CONFIG_CLASS_PARAM + " <" + configClass + "> not found.", e);
            }
            throw new InvalidParameterException("ClassNotFoundException: " + configClass);
        }
    } else {
        throw new InvalidParameterException("No value defined for the required: " + CONFIG_CLASS_PARAM);
    }

    webApplicationContext.refresh();
    return webApplicationContext;
}

From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java

/**
 * Create new BeanWrapperImpl, wrapping a new instance of the specified class.
 * /* w  w  w  .  j  ava 2 s  .  com*/
 * @param clazz
 *            class to instantiate and wrap
 */
public ExtendedBeanWrapperImpl(Class<?> clazz) {
    registerDefaultEditors();
    setWrappedInstance(BeanUtils.instantiateClass(clazz));
}

From source file:org.alfresco.repo.web.util.AbstractJettyComponent.java

protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
    GenericWebApplicationContext wac = (GenericWebApplicationContext) BeanUtils
            .instantiateClass(GenericWebApplicationContext.class);

    // Assign the best possible id value.
    wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + contextPath);

    wac.setParent(parent);/*from   w  w w  .  j av  a2  s  .c o  m*/
    wac.setServletContext(sc);
    wac.refresh();

    return wac;
}

From source file:org.apdplat.platform.util.ReflectionUtils.java

/**
 *
 * @param <T>/*from  w w w.j  a  v  a  2s . c o m*/
 * @param instance
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T cloneInstance(T instance) {
    Class<T> cls = (Class<T>) instance.getClass();
    T newIns = (T) BeanUtils.instantiateClass(cls);
    BeanUtils.copyProperties(instance, newIns);
    return newIns;
}

From source file:org.capelin.transaction.dao.RecordDao.java

/**
 * Create a new Record with is assigned by setRecordClass()
 * //from  w  w  w.  java2s  .  c om
 * @return CapelinRecord
 */
public CapelinRecord newRecord() {
    return (CapelinRecord) BeanUtils.instantiateClass(recordClass);
}

From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java

/**
 * Returns the value of the specified property and type from an instance of the specified Grails class
 *
 * @param clazz The name of the class which contains the property
 * @param propertyName The property name
 * @param propertyType The property type
 *
 * @return The value of the property or null if none exists
 *///from w  w  w.j a  va 2s . c o  m
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object getPropertyValueOfNewInstance(Class clazz, String propertyName, Class<?> propertyType) {
    // validate
    if (clazz == null || StringUtils.isBlank(propertyName)) {
        return null;
    }

    try {
        return getPropertyOrStaticPropertyOrFieldValue(BeanUtils.instantiateClass(clazz), propertyName);
    } catch (BeanInstantiationException e) {
        return null;
    }
}

From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java

/**
 * Returns the value of the specified property and type from an instance of the specified Grails class
 *
 * @param clazz The name of the class which contains the property
 * @param propertyName The property name
 *
 * @return The value of the property or null if none exists
 *///from  w  w w .j a va 2 s  .  com
public static Object getPropertyValueOfNewInstance(Class<?> clazz, String propertyName) {
    // validate
    if (clazz == null || StringUtils.isBlank(propertyName)) {
        return null;
    }

    try {
        return getPropertyOrStaticPropertyOrFieldValue(BeanUtils.instantiateClass(clazz), propertyName);
    } catch (BeanInstantiationException e) {
        return null;
    }
}