Example usage for org.springframework.beans BeanUtils instantiate

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

Introduction

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

Prototype

@Deprecated
public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException 

Source Link

Document

Convenience method to instantiate a class using its no-arg constructor.

Usage

From source file:org.jdal.vaadin.ui.form.ConfigurableFieldFactory.java

/**
 * Try to find a field. It will tray the four configured maps in order:
 * <ol>//  w  ww  .  ja  v a 2  s.c o m
 *  <li> propertyId to FieldBuilder map.</li>
 *  <li> propertyId to Field map.</li>
 *  <li> propertyClass to FieldBuilder map.</li>
 *  <li> propertyClass to Field map.</li>
 * </ol>
 * @param propertyId the propertyId
 * @param clazz the bean class holding the propertyId
 * @return Field or null if none configured
 */
@SuppressWarnings("unchecked")
protected Field getField(Object propertyId, Class<?> clazz) {
    // try id to builder map
    FieldBuilder builder = idBuilderMap.get(propertyId);
    if (builder != null) {
        if (log.isDebugEnabled())
            log.debug("Found FieldBuilder in idBuilderMap: [" + builder.getClass().getSimpleName() + "]");

        return builder.build(clazz, (String) propertyId);
    }

    // try id to class Map
    Class<? extends Field> fieldClass = idClassMap.get(propertyId);
    if (fieldClass != null) {
        if (log.isDebugEnabled())
            log.debug("Found FieldBuilder in idClassMap: [" + fieldClass.getSimpleName() + "]");
        return BeanUtils.instantiate(fieldClass);
    }

    // try class to builder map
    Class<?> propertyClass = BeanUtils.getPropertyDescriptor(clazz, (String) propertyId).getPropertyType();
    builder = (FieldBuilder) findByClass(propertyClass, classBuilderMap);
    if (builder != null) {
        if (log.isDebugEnabled())
            log.debug("Found FieldBuilder in classBuilderMap: [" + builder.getClass().getSimpleName() + "]");

        return builder.build(clazz, (String) propertyId);
    }

    // try class to field map
    fieldClass = (Class<? extends Field>) findByClass(propertyClass, classFieldMap);
    if (fieldClass != null) {
        if (log.isDebugEnabled())
            log.debug("Found FieldBuilder in classFieldMap: [" + fieldClass.getSimpleName() + "]");

        return BeanUtils.instantiate(fieldClass);
    }

    log.debug("Not found field for propertyId: " + propertyId);

    return null;
}

From source file:com.baculsoft.beanutils.test.TestBeanUtil2.java

@Test
public void testNewInstance() {
    BeanUtils.instantiate(PojoExample.class);
    pojoDescriptor.newInstance();//  w w  w.j  a  va 2s.  co m

    System.out.println("-----------------\nTime to new Instance");
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < 50000; i++) {
        pojoDescriptor.newInstance();
    }
    t1 = System.currentTimeMillis() - t1;

    long t2 = System.currentTimeMillis();
    for (int i = 0; i < 50000; i++) {
        BeanUtils.instantiate(PojoExample.class);
    }
    t2 = System.currentTimeMillis() - t2;

    System.out.println("this class t1:" + t1);
    System.out.println("Spring Beans t2:" + t2);
    System.out.println("------------------------------");

}

From source file:com.github.moscaville.contactsdb.controller.BaseController.java

public List<T> loadItems(int count, int itemsLoaded, T t) {

    List<T> result = new ArrayList<>();
    if (!OFFLINE_TEST) {
        StringBuilder uri = new StringBuilder();
        uri.append(AIRTABLE_ENDPOINT_URL).append(getAirTableName());
        count = count <= 0 || count > 100 ? 100 : count;
        uri.append("?limit=").append(count);
        if (sortColumn != null) {
            uri.append("&sortField=").append(Utility.toHumanName(sortColumn));
        }/*from w w  w.  j  av a2 s  .  co m*/
        RecordCollection recordCollection = restTemplate.getForObject(uri.toString(),
                getRecordCollection().getClass());
        for (Object o : recordCollection.getRecords()) {
            RecordWrapper recordWrapper = (RecordWrapper) o;
            Object r = BeanUtils.instantiate(t.getClass());
            //BeanUtils.copyProperties(recordWrapper, r);
            BeanUtils.copyProperties(recordWrapper.getFields(), r);
            ((AtBaseRecord) r).setId(recordWrapper.getId());
            result.add((T) r);
        }
    }
    return result;
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Object cloneBean(Object bean, Class<?> type) {
    BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type));

    // keep instance for circular and multiple references
    context.setAsVisited(bean);/*w  w w  .  j a va2 s. co  m*/
    alreadyCloned.put(bean, copy.getWrappedInstance());

    PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : beanProperties) {
        String name = propertyDescriptor.getName();
        context.pushPath(name);
        if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) {
            Object value = source.getPropertyValue(name);
            copy.setPropertyValue(name, clone(value));
        }
        context.popPath();
    }
    Object beanCopy = copy.getWrappedInstance();
    source = null;
    copy = null;
    return beanCopy;
}

From source file:org.jdal.vaadin.data.ListBeanContainer.java

private Object newBean() {
    return BeanUtils.instantiate(this.beanClass);
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Collection<?> cloneCollection(Collection<?> collection, Class<?> type) {
    Collection<Object> collectionCopy = (Collection<Object>) BeanUtils.instantiate(type);
    for (Object item : collection) {
        collectionCopy.add(clone(item));
    }/* ww w .j  a va2s .  c o  m*/
    CollectionUtils.filter(collectionCopy, PredicateUtils.notNullPredicate());
    if (collectionCopy.isEmpty()) {
        collectionCopy = null;
    }
    return collectionCopy;
}

From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java

protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {//from   w  w  w. j  a v a 2 s .  c  o  m
            contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
        } catch (ClassNotFoundException ex) {
            throw new IllegalStateException("Unable to create a default ApplicationContext, "
                    + "please specify an ApplicationContextClass", ex);
        }
    }
    return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Map<Object, Object> cloneMap(Map<?, ?> map, Class<?> type) {
    Map<Object, Object> mapCopy = (Map<Object, Object>) BeanUtils.instantiate(type);
    for (Object key : map.keySet()) {
        Object value = map.get(key);
        mapCopy.put(clone(key), clone(value));
    }/* w  w w.j av  a  2 s .c  om*/
    return mapCopy;
}

From source file:com.expedia.seiso.domain.entity.ItemTests.java

private void exerciseEquals(Object item) {
    log.trace("Exercising equals()");

    // The following is legitimate testing, as opposed to simple coverage grabs.
    assertTrue(item.equals(item));/*  w w w  .ja  v a2s . c  om*/

    val copy = BeanUtils.instantiate(item.getClass());
    // FIXME This is true when we don't set anything on the POJO. [WLW]
    assertFalse(item.equals(copy));

    BeanUtils.copyProperties(item, copy);
    assertTrue(item.equals(copy));

    assertFalse(item.equals(null));
    assertFalse(item.equals("dlafkjs"));
}

From source file:org.opentides.web.controller.BaseCrudController.java

/**
 * This is the entry point of a CRUD page which loads the search page.
 * /*from w  w w  . j ava  2 s  .co m*/
 * @param command
 * @param bindingResult
 * @param uiModel
 * @param request
 * @param response
 * @return
 */
@RequestMapping(method = RequestMethod.GET)
public final String searchHtml(@ModelAttribute("searchCommand") T command, BindingResult bindingResult,
        Model uiModel, HttpServletRequest request, HttpServletResponse response) {
    uiModel.addAttribute("formCommand", BeanUtils.instantiate(this.entityBeanType));
    uiModel.addAttribute("searchCommand", command);
    if (!request.getParameterMap().isEmpty()) {
        preSearch(command, bindingResult, uiModel, request, response);
        SearchResults<T> results = search(command, request);
        postSearch(command, results, bindingResult, uiModel, request, response);
        uiModel.addAttribute("results", results);
    } else {
        onLoadSearch(command, bindingResult, uiModel, request, response);
    }
    uiModel.addAttribute("mode", "search");
    uiModel.addAttribute("search", "ot3-search");
    uiModel.addAttribute("form", "ot3-form hide");
    uiModel.addAttribute("view", "ot3-view hide");
    uiModel.addAttribute("add", "ot3-add");
    uiModel.addAttribute("update", "ot3-update");
    uiModel.addAttribute("method", "post");

    return singlePage;
}