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(Constructor<T> ctor, Object... args) throws BeanInstantiationException 

Source Link

Document

Convenience method to instantiate a class using the given constructor.

Usage

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static void inspectAndSaveAttribute(Class<?> searchElement, Collection<AttributeEntity> attributes) {
    if (searchElement != null) {
        for (Field field : searchElement.getDeclaredFields()) {
            if (field.isAnnotationPresent(SearchAttribute.class)) {
                AttributeEntity attrDef = new AttributeEntity().setName(field.getName())
                        .setType(field.getType());
                String value = field.getAnnotation(SearchAttribute.class).value();
                if (value != null && !value.isEmpty()) {
                    try {
                        Object ovalue = BeanUtils.instantiateClass(field.getType().getConstructor(String.class),
                                value);/*from ww  w .  j  av  a  2  s.c o m*/
                        attrDef.setValue(ovalue);
                    } catch (BeanInstantiationException | NoSuchMethodException | SecurityException e) {
                        LOGGER.trace("Could not construct default value (not much of a problem)", e);
                    }
                }
                attributes.add(attrDef);
            }
        }
        inspectAndSaveAttribute(searchElement.getSuperclass(), attributes);
    } else {
        return;
    }
}

From source file:edu.pitt.dbmi.ccd.anno.access.AccessResourceAssembler.java

/**
 * Instantiate AccessResource with non-default constructor
 *
 * @param access entity//from w  w  w. j  av  a 2s  .c  o  m
 * @return resource
 */
@Override
protected AccessResource instantiateResource(Access access) throws IllegalArgumentException {
    Assert.notNull(access);
    try {
        return BeanUtils.instantiateClass(AccessResource.class.getConstructor(Access.class), access);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new AccessResource();
    }
}

From source file:edu.pitt.dbmi.ccd.anno.group.GroupResourceAssembler.java

/**
 * Instantiate GroupResource with non-default constructor
 *
 * @param group entity//from  w  w  w .j  a v  a 2  s  . c om
 * @return resource
 */
@Override
protected GroupResource instantiateResource(Group group) throws IllegalArgumentException {
    Assert.notNull(group);
    try {
        return BeanUtils.instantiateClass(GroupResource.class.getConstructor(Group.class), group);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new GroupResource();
    }
}

From source file:edu.pitt.dbmi.ccd.anno.vocabulary.VocabularyResourceAssembler.java

/**
 * Instantiate VocabularyResource with non-default constructor
 *
 * @param vocabulary entity//from  www  . j ava2  s .  c om
 * @return resource
 */
@Override
protected VocabularyResource instantiateResource(Vocabulary vocabulary) throws IllegalArgumentException {
    Assert.notNull(vocabulary);
    try {
        return BeanUtils.instantiateClass(VocabularyResource.class.getConstructor(Vocabulary.class),
                vocabulary);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new VocabularyResource();
    }
}

From source file:edu.pitt.dbmi.ccd.anno.user.UserResourceAssembler.java

/**
 * Instantiate UserResource with no-default constructor
 *
 * @param account entity//from w  ww  . java  2s .c  om
 * @return resource
 */
@Override
protected UserResource instantiateResource(UserAccount account) throws IllegalArgumentException {
    Assert.notNull(account);
    try {
        return BeanUtils.instantiateClass(UserResource.class.getConstructor(UserAccount.class), account);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new UserResource();
    }
}

From source file:edu.pitt.dbmi.ccd.anno.data.AnnotationTargetResourceAssembler.java

/**
 * Instantiate AnnotationTargetResource with non-default constructor
 *
 * @param target entity//  w  w w.  j a va 2 s.  co  m
 * @return resource
 */
@Override
protected AnnotationTargetResource instantiateResource(AnnotationTarget target)
        throws IllegalArgumentException {
    Assert.notNull(target);
    try {
        return BeanUtils.instantiateClass(AnnotationTargetResource.class.getConstructor(AnnotationTarget.class),
                target);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new AnnotationTargetResource();
    }
}

From source file:io.gravitee.common.spring.factory.SpringFactoriesLoader.java

@SuppressWarnings("unchecked")
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
        ClassLoader classLoader, Object[] args, Set<String> names) {
    List<T> instances = new ArrayList<>(names.size());
    for (String name : names) {
        try {/*from   ww w .j ava  2 s  .c  o m*/
            Class<?> instanceClass = ClassUtils.forName(name, classLoader);
            Assert.isAssignable(type, instanceClass);
            Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
            T instance = (T) BeanUtils.instantiateClass(constructor, args);
            ((AbstractApplicationContext) applicationContext).getBeanFactory().autowireBean(instance);
            if (instance instanceof ApplicationContextAware) {
                ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
            }
            instances.add(instance);
        } catch (Throwable ex) {
            throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
        }
    }
    return instances;
}

From source file:org.mybatis.spring.config.MapperScannerBeanDefinitionParser.java

/**
 * {@inheritDoc}/*  w  ww .ja v a 2 s . c om*/
 */
@Override
public synchronized BeanDefinition parse(Element element, ParserContext parserContext) {
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(parserContext.getRegistry());
    ClassLoader classLoader = scanner.getResourceLoader().getClassLoader();
    XmlReaderContext readerContext = parserContext.getReaderContext();
    scanner.setResourceLoader(readerContext.getResourceLoader());
    try {
        String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
        if (StringUtils.hasText(annotationClassName)) {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> markerInterface = (Class<? extends Annotation>) classLoader
                    .loadClass(annotationClassName);
            scanner.setAnnotationClass(markerInterface);
        }
        String markerInterfaceClassName = element.getAttribute(ATTRIBUTE_MARKER_INTERFACE);
        if (StringUtils.hasText(markerInterfaceClassName)) {
            Class<?> markerInterface = classLoader.loadClass(markerInterfaceClassName);
            scanner.setMarkerInterface(markerInterface);
        }
        String nameGeneratorClassName = element.getAttribute(ATTRIBUTE_NAME_GENERATOR);
        if (StringUtils.hasText(nameGeneratorClassName)) {
            Class<?> nameGeneratorClass = classLoader.loadClass(nameGeneratorClassName);
            BeanNameGenerator nameGenerator = BeanUtils.instantiateClass(nameGeneratorClass,
                    BeanNameGenerator.class);
            scanner.setBeanNameGenerator(nameGenerator);
        }
    } catch (Exception ex) {
        readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
    }
    String sqlSessionTemplateBeanName = element.getAttribute(ATTRIBUTE_TEMPLATE_REF);
    scanner.setSqlSessionTemplateBeanName(sqlSessionTemplateBeanName);
    String sqlSessionFactoryBeanName = element.getAttribute(ATTRIBUTE_FACTORY_REF);
    scanner.setSqlSessionFactoryBeanName(sqlSessionFactoryBeanName);
    scanner.registerFilters();
    String basePackage = element.getAttribute(ATTRIBUTE_BASE_PACKAGE);
    scanner.scan(StringUtils.tokenizeToStringArray(basePackage,
            ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
    return null;
}

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

private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
        ClassLoader classLoader, Object[] args, Set<String> names) {
    List<T> instances = new ArrayList<T>(names.size());
    for (String name : names) {
        try {//from  w ww.  j  ava 2 s.c  o  m
            Class<?> instanceClass = ClassUtils.forName(name, classLoader);
            Assert.isAssignable(type, instanceClass);
            Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
            T instance = (T) BeanUtils.instantiateClass(constructor, args);
            instances.add(instance);
        } catch (Throwable ex) {
            throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
        }
    }
    return instances;
}

From source file:edu.pitt.dbmi.ccd.anno.vocabulary.attribute.AttributeResourceAssembler.java

/**
 * Instantiate AttributeResource with non-default constructor
 *
 * @param attribute entity/*from w ww  .  j  a  v  a2 s.co  m*/
 * @return resource
 */
@Override
protected AttributeResource instantiateResource(Attribute attribute) {
    Assert.notNull(attribute);
    try {
        return BeanUtils.instantiateClass(AttributeResource.class.getConstructor(Attribute.class), attribute);
    } catch (NoSuchMethodException ex) {
        ex.printStackTrace();
        return new AttributeResource();
    }
}