Example usage for org.apache.commons.beanutils ConstructorUtils getAccessibleConstructor

List of usage examples for org.apache.commons.beanutils ConstructorUtils getAccessibleConstructor

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConstructorUtils getAccessibleConstructor.

Prototype

public static Constructor getAccessibleConstructor(Class klass, Class[] parameterTypes) 

Source Link

Document

Returns a constructor given a class and signature.

Usage

From source file:com.comcast.cereal.convert.MapCerealizer.java

private static boolean isInstantiable(Class<?> clazz) {
    boolean abs = Modifier.isAbstract(clazz.getModifiers());
    boolean hasConstuctor = ConstructorUtils.getAccessibleConstructor(clazz, new Class[0]) != null;
    return !clazz.isInterface() && !abs && hasConstuctor;
}

From source file:nl.ucan.navigate.NestedPath.java

private void growCollection(Object instance, String prop, int positionalIdx) throws NoSuchMethodException,
        IntrospectionException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Object propInstance = pub.getProperty(instance, prop);
    if (propInstance != null) {
        if (propInstance.getClass().isArray()) {
            Object[] elementData = (Object[]) propInstance;
            int oldCapacity = elementData.length;
            int minCapacity = positionalIdx + 1;
            if (minCapacity > oldCapacity) {
                Object oldData[] = elementData;
                int newCapacity = (oldCapacity * 3) / 2 + 1;
                if (newCapacity < minCapacity)
                    newCapacity = minCapacity;
                elementData = Arrays.copyOf(elementData, newCapacity);
                pub.setProperty(instance, prop, elementData);
            }/* ww w  .j  av a  2 s  .  c om*/
            if (elementData[positionalIdx] == null) {
                Class clasz = elementData.getClass().getComponentType();
                Constructor constructor = ConstructorUtils.getAccessibleConstructor(clasz, new Class[] {});
                elementData[positionalIdx] = constructor.newInstance();
            }

        }
    }
}

From source file:nl.ucan.navigate.NestedPath.java

private static Object createInstanceOfCollection(Class clasz, String prop) throws IntrospectionException,
        IllegalAccessException, InvocationTargetException, InstantiationException {
    Class type = getCollectionReturnType(prop, clasz);
    Constructor constructor = ConstructorUtils.getAccessibleConstructor(type, new Class[] {});
    return constructor.newInstance();
}

From source file:org.apache.struts.chain.ComposableRequestProcessor.java

/**
 * <p> Set and cache ActionContext class. </p><p> If there is a custom
 * class provided and if it uses our "preferred" constructor, cache a
 * reference to that constructor rather than looking it up every time.
 * </p>/*w  w  w . j  a v a2 s  . c  om*/
 *
 * @param actionContextClass The ActionContext class to process
 */
private void setActionContextClass(Class actionContextClass) {
    this.actionContextClass = actionContextClass;

    if (actionContextClass != null) {
        this.servletActionContextConstructor = ConstructorUtils.getAccessibleConstructor(actionContextClass,
                SERVLET_ACTION_CONTEXT_CTOR_SIGNATURE);
    } else {
        this.servletActionContextConstructor = null;
    }
}

From source file:org.codehaus.mojo.pomtools.console.screens.custom.ListDependenciesScreen.java

private ConsoleScreen createEditorScreen(String className, int index) {
    try {/*from  w  w  w.  jav  a  2  s . co m*/
        Constructor con = ConstructorUtils.getAccessibleConstructor(Class.forName(className),
                LIST_EDITOR_SIGNATURE);

        return (ConsoleScreen) con.newInstance(new Object[] { dependencies, new Integer(index) });
    } catch (ClassNotFoundException e) {
        throw new ModelReflectionException(e);
    } catch (IllegalArgumentException e) {
        throw new ModelReflectionException(e);
    } catch (InstantiationException e) {
        throw new ModelReflectionException(e);
    } catch (IllegalAccessException e) {
        throw new ModelReflectionException(e);
    } catch (InvocationTargetException e) {
        throw new ModelReflectionException(e);
    }
}

From source file:org.codehaus.mojo.pomtools.helpers.ModelHelper.java

public static Constructor getConstructor(Class clazz, Class[] signature) {
    Constructor con = ConstructorUtils.getAccessibleConstructor(clazz, signature);

    if (con == null) {
        throw new ModelReflectionException("Unable to locate the constructor for: " + clazz.getName()
                + " with the signature " + String.valueOf(signature));
    }/*from  w ww .  ja  v  a 2 s .  com*/

    return con;
}

From source file:org.force66.beantester.utils.InstantiationUtils.java

public static Constructor<?> findPublicConstructor(Class<?> klass) {
    Validate.notNull(klass, "Null class not allowed.");
    Constructor<?> nullConstructor = ConstructorUtils.getAccessibleConstructor(klass, new Class<?>[0]);
    if (nullConstructor != null) {
        return nullConstructor;
    }/*  ww  w. j  a v a 2s. c o  m*/

    Constructor<?>[] constructorArray = klass.getConstructors();
    for (Constructor<?> constructor : constructorArray) {
        if (Modifier.isPublic(constructor.getModifiers())) {
            return constructor;
        }
    }

    return null;
}

From source file:org.geoserver.catalog.impl.ModificationProxyCloner.java

/**
 * Best effort object cloning utility, tries different lightweight strategies, then falls back
 * on copy by XStream serialization (we use that one as we have a number of hooks to avoid deep
 * copying the catalog, and re-attaching to it, in there)
 * //from  www . j av a  2  s  .c  om
 * @param source
 * @return
 */
static <T> T clone(T source) {
    // null?
    if (source == null) {
        return null;
    }

    // already a modification proxy?
    if (ModificationProxy.handler(source) != null) {
        return source;
    }

    // is it a catalog info?
    if (source instanceof CatalogInfo) {
        // mumble... shouldn't we wrap this one in a modification proxy object?
        return (T) ModificationProxy.create(source, getDeepestCatalogInfoInterface((CatalogInfo) source));
    }

    // if a known immutable?
    if (source instanceof String || source instanceof Byte || source instanceof Short
            || source instanceof Integer || source instanceof Float || source instanceof Double
            || source instanceof BigInteger || source instanceof BigDecimal) {
        return (T) source;
    }

    // is it cloneable?
    try {
        if (source instanceof Cloneable) {
            // methodutils does not seem to work against "clone()"...
            // return (T) MethodUtils.invokeExactMethod(source, "clone", null, null);
            Method method = source.getClass().getDeclaredMethod("clone");
            if (Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0) {
                return (T) method.invoke(source);
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.FINE,
                "Source object is cloneable, yet it does not have a public no argument method 'clone'", e);
    }

    // does it have a copy constructor?
    Constructor copyConstructor = ConstructorUtils.getAccessibleConstructor(source.getClass(),
            source.getClass());
    if (copyConstructor != null) {
        try {
            return (T) copyConstructor.newInstance(source);
        } catch (Exception e) {
            LOGGER.log(Level.FINE, "Source has a copy constructor, but it failed, skipping to XStream", e);
        }
    }

    if (source instanceof Serializable) {
        return (T) SerializationUtils.clone((Serializable) source);
    } else {
        XStreamPersister persister = XSTREAM_PERSISTER_FACTORY.createXMLPersister();
        XStream xs = persister.getXStream();
        String xml = xs.toXML(source);
        T copy = (T) xs.fromXML(xml);
        return copy;
    }
}