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:org.springframework.beans.AbstractNestablePropertyAccessor.java

/**
 * Create a new accessor, wrapping a new instance of the specified class.
 * @param clazz class to instantiate and wrap
 *//*from w w w  . ja  v  a  2  s . c  o  m*/
protected AbstractNestablePropertyAccessor(Class<?> clazz) {
    registerDefaultEditors();
    setWrappedInstance(BeanUtils.instantiateClass(clazz));
}

From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java

private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
    try {//w w  w  .  j  a va  2  s .co m
        if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            // TODO - only handles 2-dimensional arrays
            if (componentType.isArray()) {
                Object array = Array.newInstance(componentType, 1);
                Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
                return array;
            } else {
                return Array.newInstance(componentType, 0);
            }
        } else if (Collection.class.isAssignableFrom(type)) {
            TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
            return CollectionFactory.createCollection(type,
                    (elementDesc != null ? elementDesc.getType() : null), 16);
        } else if (Map.class.isAssignableFrom(type)) {
            TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
            return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
        } else {
            Constructor<?> ctor = type.getDeclaredConstructor();
            if (Modifier.isPrivate(ctor.getModifiers())) {
                throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
            }
            return BeanUtils.instantiateClass(ctor);
        }
    } catch (Throwable ex) {
        throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
                "Could not instantiate property type [" + type.getName()
                        + "] to auto-grow nested property path",
                ex);
    }
}

From source file:org.springframework.beans.BeanWrapperImpl.java

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

From source file:org.springframework.beans.factory.support.DependencyInjectionAspectSupport.java

/**
 * Subclasses will call this to create an object of the requisite class
 * @param clazz/*  www.  j a  v a 2  s. co m*/
 * @return
 * @throws NoAutowiringConfigurationForClassException
 */
protected Object createAndConfigure(Class clazz) throws NoAutowiringConfigurationForClassException {
    Object o = null;
    String name = (String) managedClassToPrototypeMap.get(clazz);
    if (name != null) {
        o = beanFactory.getBean(name);
    } else {
        // Fall back to trying autowiring         
        o = BeanUtils.instantiateClass(clazz);
        autowireProperties(o);
    }

    if (o == null) {
        throw new NoAutowiringConfigurationForClassException(clazz);
    } else {
        return o;
    }
}

From source file:org.springframework.beans.factory.support.SimpleInstantiationStrategy.java

public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {

    // Don't override the class with CGLIB if no overrides.
    if (beanDefinition.getMethodOverrides().isEmpty()) {
        return BeanUtils.instantiateClass(beanDefinition.getBeanClass());
    } else {/*from   w  ww  . ja v a2 s  .com*/
        // Must generate CGLIB subclass.
        return instantiateWithMethodInjection(beanDefinition, beanName, owner);
    }
}

From source file:org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver.java

/**
 * Locate the {@link NamespaceHandler} for the supplied namespace URI
 * from the configured mappings./*  w  w w.j  a v a2 s.c om*/
 * @param namespaceUri the relevant namespace URI
 * @return the located {@link NamespaceHandler}, or {@code null} if none found
 */
@Override
@Nullable
public NamespaceHandler resolve(String namespaceUri) {
    Map<String, Object> handlerMappings = getHandlerMappings();
    Object handlerOrClassName = handlerMappings.get(namespaceUri);
    if (handlerOrClassName == null) {
        return null;
    } else if (handlerOrClassName instanceof NamespaceHandler) {
        return (NamespaceHandler) handlerOrClassName;
    } else {
        String className = (String) handlerOrClassName;
        try {
            Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
            if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
                throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri
                        + "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
            }
            NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
            namespaceHandler.init();
            handlerMappings.put(namespaceUri, namespaceHandler);
            return namespaceHandler;
        } catch (ClassNotFoundException ex) {
            throw new FatalBeanException("Could not find NamespaceHandler class [" + className
                    + "] for namespace [" + namespaceUri + "]", ex);
        } catch (LinkageError err) {
            throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class ["
                    + className + "] for namespace [" + namespaceUri + "]", err);
        }
    }
}

From source file:org.springframework.boot.actuate.endpoint.annotation.AnnotationEndpointDiscoverer.java

@SuppressWarnings("unchecked")
private boolean isFilterMatch(Class<?> filterClass, EndpointInfo<T> endpointInfo) {
    Class<?> generic = ResolvableType.forClass(EndpointFilter.class, filterClass).resolveGeneric(0);
    if (generic == null || generic.isAssignableFrom(getOperationType())) {
        EndpointFilter<T> filter = (EndpointFilter<T>) BeanUtils.instantiateClass(filterClass);
        return isFilterMatch(filter, endpointInfo);
    }//from   w w  w .jav  a2 s. c  om
    return false;
}

From source file:org.springframework.boot.jdbc.DataSourceBuilder.java

@SuppressWarnings("unchecked")
public T build() {
    Class<? extends DataSource> type = getType();
    DataSource result = BeanUtils.instantiateClass(type);
    maybeGetDriverClassName();/*  ww  w . j  a  va2  s  . c  o m*/
    bind(result);
    return (T) result;
}

From source file:org.springframework.boot.web.client.ClientHttpRequestFactorySupplier.java

@Override
public ClientHttpRequestFactory get() {
    for (Map.Entry<String, String> candidate : REQUEST_FACTORY_CANDIDATES.entrySet()) {
        ClassLoader classLoader = getClass().getClassLoader();
        if (ClassUtils.isPresent(candidate.getKey(), classLoader)) {
            Class<?> factoryClass = ClassUtils.resolveClassName(candidate.getValue(), classLoader);
            return (ClientHttpRequestFactory) BeanUtils.instantiateClass(factoryClass);
        }/*from w  w  w.j  a va 2s .  co m*/
    }
    return new SimpleClientHttpRequestFactory();
}

From source file:org.springframework.boot.web.client.RestTemplateBuilder.java

/**
 * Build a new {@link RestTemplate} instance of the specified type and configure it
 * using this builder./*w  ww.j a  v  a 2 s. co  m*/
 * @param <T> the type of rest template
 * @param restTemplateClass the template type to create
 * @return a configured {@link RestTemplate} instance.
 * @see RestTemplateBuilder#build()
 * @see #configure(RestTemplate)
 */

public <T extends RestTemplate> T build(Class<T> restTemplateClass) {
    return configure(BeanUtils.instantiateClass(restTemplateClass));
}