Example usage for org.springframework.util ClassUtils getConstructorIfAvailable

List of usage examples for org.springframework.util ClassUtils getConstructorIfAvailable

Introduction

In this page you can find the example usage for org.springframework.util ClassUtils getConstructorIfAvailable.

Prototype

@Nullable
public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) 

Source Link

Document

Determine whether the given class has a public constructor with the given signature, and return it if available (else return null ).

Usage

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

private static HttpClient cloneHttpClient(HttpClient sourceClient) throws InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    if (sourceClient == null) {
        return null;
    }/*from w  w w  . j  a va  2s .co m*/

    Class<?> clientType = ClassUtils.getUserClass(sourceClient);
    Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(clientType, HttpParams.class);
    if (constructor != null) {

        HttpClient targetClient = (HttpClient) constructor.newInstance(sourceClient.getParams());
        BeanUtils.copyProperties(sourceClient, targetClient);
        return targetClient;
    } else {
        return new DefaultHttpClient(sourceClient.getParams());
    }

}

From source file:org.openengsb.core.edb.jpa.internal.util.DefaultConverterStep.java

/**
 * Tries to get the object value for a given JPAEntry. To instantiate the type first the static method "valueOf" of
 * the type will be tried. If that didn't work, then the constructor of the object with a string parameter is used.
 * If that didn't work either, the simple string will be set in the entry.
 *//* w ww .  java 2  s .c  om*/
public Object getEntryValue(JPAEntry entry) {
    try {
        Class<?> typeClass = loadClass(entry.getType());
        if (typeClass == null) {
            return entry.getValue();
        }
        Object result = invokeValueOf(typeClass, entry.getValue());
        if (result != null) {
            return result;
        }
        Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(typeClass, String.class);
        if (constructor != null) {
            return constructor.newInstance(entry.getValue());
        }
        LOGGER.debug("DefaultConverterStep didn't find any possibility to convert entry {}. "
                + "The simple string value will be returned", entry);
    } catch (IllegalAccessException e) {
        LOGGER.error("IllegalAccessException when trying to create object of type {}", entry.getType(), e);
    } catch (InvocationTargetException e) {
        LOGGER.error("InvocationTargetException when trying to create object of type {}", entry.getType(), e);
    } catch (IllegalArgumentException e) {
        LOGGER.error("IllegalArgumentException when trying to create object of type {}", entry.getType(), e);
    } catch (InstantiationException e) {
        LOGGER.error("InstantiationException when trying to create object of type {}", entry.getType(), e);
    }
    return entry.getType();
}

From source file:org.springframework.data.neo4j.support.AbstractConstructorEntityInstantiator.java

protected <T> StateBackedCreator<T, STATE> stateTakingConstructorInstantiator(Class<T> type,
        Class<STATE> stateType) {
    @SuppressWarnings("unchecked")
    Class<? extends STATE> stateInterface = (Class<? extends STATE>) stateType.getInterfaces()[0];
    final Constructor<T> constructor = ClassUtils.getConstructorIfAvailable(type, stateInterface);
    if (constructor == null)
        return null;

    log.info("Using " + type + " constructor taking " + stateInterface);
    return new StateBackedCreator<T, STATE>() {
        public T create(STATE n, Class<T> c) throws Exception {
            return constructor.newInstance(n);
        }/*www  .  j a  v a 2  s. c  om*/
    };
}

From source file:org.springframework.security.oauth2.client.test.OAuth2ContextSetup.java

private OAuth2ProtectedResourceDetails creatResource(Object target, OAuth2ContextConfiguration contextLoader) {
    Class<? extends OAuth2ProtectedResourceDetails> type = contextLoader.value();
    if (type == OAuth2ProtectedResourceDetails.class) {
        type = contextLoader.resource();
    }/*ww  w . java 2  s . co m*/
    Constructor<? extends OAuth2ProtectedResourceDetails> constructor = ClassUtils
            .getConstructorIfAvailable(type, TestAccounts.class);
    if (constructor != null && testAccounts != null) {
        return BeanUtils.instantiateClass(constructor, testAccounts);
    }
    constructor = ClassUtils.getConstructorIfAvailable(type, Environment.class);
    if (constructor != null && environment != null) {
        return BeanUtils.instantiateClass(constructor, environment);
    }
    constructor = ClassUtils.getConstructorIfAvailable(type, Object.class);
    if (constructor != null) {
        return BeanUtils.instantiateClass(constructor, target);
    }
    // Fallback to default constructor
    return BeanUtils.instantiate(type);
}

From source file:org.springframework.test.context.TestContextManager.java

/**
 * Attempt to create a copy of the supplied {@code TestContext} using its
 * <em>copy constructor</em>.
 *///from  w  w  w.j ava  2 s .c  o  m
private static TestContext copyTestContext(TestContext testContext) {
    Constructor<? extends TestContext> constructor = ClassUtils
            .getConstructorIfAvailable(testContext.getClass(), testContext.getClass());

    if (constructor != null) {
        try {
            ReflectionUtils.makeAccessible(constructor);
            return constructor.newInstance(testContext);
        } catch (Exception ex) {
            if (logger.isInfoEnabled()) {
                logger.info(String.format("Failed to invoke copy constructor for [%s]; "
                        + "concurrent test execution is therefore likely not supported.", testContext), ex);
            }
        }
    }

    // fallback to original instance
    return testContext;
}