Example usage for org.apache.commons.lang3.reflect ConstructorUtils getMatchingAccessibleConstructor

List of usage examples for org.apache.commons.lang3.reflect ConstructorUtils getMatchingAccessibleConstructor

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect ConstructorUtils getMatchingAccessibleConstructor.

Prototype

public static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T> cls,
        final Class<?>... parameterTypes) 

Source Link

Document

Finds an accessible constructor with compatible parameters.

This checks all the constructor and finds one with compatible parameters This requires that every parameter is assignable from the given parameter types.

Usage

From source file:gobblin.util.reflection.GobblinConstructorUtils.java

/**
 * Convenience method on top of {@link ConstructorUtils#invokeConstructor(Class, Object[])} that returns a new
 * instance of the <code>cls</code> based on a constructor priority order. Each {@link List} in the
 * <code>constructorArgs</code> array contains the arguments for a constructor of <code>cls</code>. The first
 * constructor whose signature matches the argument types will be invoked.
 *
 * @param cls the class to be instantiated
 * @param constructorArgs An array of constructor argument list. Order defines the priority of a constructor.
 * @return/*from   ww w  . j a v  a 2 s. co  m*/
 *
 * @throws NoSuchMethodException if no constructor matched was found
 */
@SafeVarargs
public static <T> T invokeFirstConstructor(Class<T> cls, List<Object>... constructorArgs)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {

    for (List<Object> args : constructorArgs) {

        Class<?>[] parameterTypes = new Class[args.size()];
        for (int i = 0; i < args.size(); i++) {
            parameterTypes[i] = args.get(i).getClass();
        }

        if (ConstructorUtils.getMatchingAccessibleConstructor(cls, parameterTypes) != null) {
            return ConstructorUtils.invokeConstructor(cls, args.toArray(new Object[args.size()]));
        }
    }
    throw new NoSuchMethodException("No accessible constructor found");
}

From source file:ch.cyberduck.core.PasswordCallbackFactory.java

public PasswordCallback create(final Controller controller) {
    final String clazz = PreferencesFactory.get().getProperty("factory.passwordcallback.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/*ww  w.  ja  va 2 s  .c o m*/
    try {
        final Class<PasswordCallback> name = (Class<PasswordCallback>) Class.forName(clazz);
        final Constructor<PasswordCallback> constructor = ConstructorUtils
                .getMatchingAccessibleConstructor(name, controller.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", controller.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(controller);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
        return new DisabledPasswordCallback();
    }
}

From source file:ch.cyberduck.core.LocalFactory.java

protected Local create(final String path) {
    final String clazz = PreferencesFactory.get().getProperty("factory.local.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/*from   w  w w .j  a v  a  2 s. c  o m*/
    try {
        final Class<Local> name = (Class<Local>) Class.forName(clazz);
        final Constructor<Local> constructor = ConstructorUtils.getMatchingAccessibleConstructor(name,
                path.getClass());
        return constructor.newInstance(path);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        throw new FactoryException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.LoginCallbackFactory.java

public LoginCallback create(final Controller controller) {
    final String clazz = PreferencesFactory.get().getProperty("factory.logincallback.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/*ww w  .  j a v  a2s  .  c  om*/
    try {
        final Class<LoginCallback> name = (Class<LoginCallback>) Class.forName(clazz);
        final Constructor<LoginCallback> constructor = ConstructorUtils.getMatchingAccessibleConstructor(name,
                controller.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", controller.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(controller);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
        return new DisabledLoginCallback();
    }
}

From source file:ch.cyberduck.core.CertificateStoreFactory.java

public CertificateStore create(final Controller c) {
    final String clazz = PreferencesFactory.get().getProperty("factory.certificatestore.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }//from   w  w w.j av a  2  s.  c o m
    try {
        final Class<CertificateStore> name = (Class<CertificateStore>) Class.forName(clazz);
        final Constructor<CertificateStore> constructor = ConstructorUtils
                .getMatchingAccessibleConstructor(name, c.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", c.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(c);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        throw new FactoryException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.AlertCallbackFactory.java

public AlertCallback create(final Controller controller) {
    final String clazz = PreferencesFactory.get().getProperty("factory.alertcallback.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/*w w  w  . j a va  2  s.c  om*/
    try {
        final Class<AlertCallback> name = (Class<AlertCallback>) Class.forName(clazz);
        final Constructor<AlertCallback> constructor = ConstructorUtils.getMatchingAccessibleConstructor(name,
                controller.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", controller.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(controller);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
        return new DisabledAlertCallback();
    }
}

From source file:ch.cyberduck.core.HostKeyCallbackFactory.java

public HostKeyCallback create(final Controller c, final Protocol protocol) {
    if (Scheme.sftp.equals(protocol.getScheme())) {
        final String clazz = PreferencesFactory.get().getProperty("factory.hostkeycallback.class");
        if (null == clazz) {
            throw new FactoryException(
                    String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
        }//from w  w w .j av  a2 s  .c om
        try {
            final Class<HostKeyCallback> name = (Class<HostKeyCallback>) Class.forName(clazz);
            final Constructor<HostKeyCallback> constructor = ConstructorUtils
                    .getMatchingAccessibleConstructor(name, c.getClass());
            if (null == constructor) {
                log.warn(String.format("No matching constructor for parameter %s", c.getClass()));
                // Call default constructor for disabled implementations
                return name.newInstance();
            }
            return constructor.newInstance(c);
        } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
                | IllegalAccessException e) {
            log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
            return new DisabledHostKeyCallback();
        }
    }
    return new DisabledHostKeyCallback();
}

From source file:ch.cyberduck.core.updater.PeriodicUpdateCheckerFactory.java

public PeriodicUpdateChecker create(final Controller controller) {
    final String clazz = PreferencesFactory.get().getProperty("factory.updater.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }/* w  w w .  j av a 2s .c  o m*/
    try {
        final Class<PeriodicUpdateChecker> name = (Class<PeriodicUpdateChecker>) Class.forName(clazz);
        final Constructor<PeriodicUpdateChecker> constructor = ConstructorUtils
                .getMatchingAccessibleConstructor(name, controller.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", controller.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(controller);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
        return new DisabledPeriodicUpdater();
    }
}

From source file:ch.cyberduck.core.DeserializerFactory.java

public Deserializer create(final T dict) {
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }//from   ww w.j  a v a 2 s . c  om
    try {
        final Class<Deserializer> name = (Class<Deserializer>) Class.forName(clazz);
        final Constructor<Deserializer> constructor = ConstructorUtils.getMatchingAccessibleConstructor(name,
                dict.getClass());
        return constructor.newInstance(dict);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException
            | ClassNotFoundException e) {
        throw new FactoryException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.TransferErrorCallbackControllerFactory.java

public TransferErrorCallback create(final Controller c) {
    final String clazz = preferences.getProperty("factory.transfererrorcallback.class");
    if (null == clazz) {
        throw new FactoryException(
                String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
    }//from  www .j  a  v a2  s . c  o  m
    try {
        final Class<TransferErrorCallback> name = (Class<TransferErrorCallback>) Class.forName(clazz);
        final Constructor<TransferErrorCallback> constructor = ConstructorUtils
                .getMatchingAccessibleConstructor(name, c.getClass());
        if (null == constructor) {
            log.warn(String.format("No matching constructor for parameter %s", c.getClass()));
            // Call default constructor for disabled implementations
            return name.newInstance();
        }
        return constructor.newInstance(c);
    } catch (InstantiationException | InvocationTargetException | ClassNotFoundException
            | IllegalAccessException e) {
        log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
        return new DisabledTransferErrorCallback();
    }
}