Java Class New Instance newInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)

Here you can find the source of newInstance(Map> providerMap, String provider, Class[] paramsClass, Object[] paramValues)

Description

Creates a new instance of the specified provider looking for the associated class in the configuration map.

License

LGPL

Parameter

Parameter Description
providerMap The configuration map of the providers
provider The provider to instantiate
paramsClass The signature of the constructor to invoke
paramValues The parameters to pass to the constructor

Return

An instance of the specific provider

Declaration

public static <E> E newInstance(Map<String, Class<? extends E>> providerMap, String provider,
        Class[] paramsClass, Object[] paramValues) throws InstantiationException, IllegalArgumentException 

Method Source Code

//package com.java2s;

import java.lang.reflect.Constructor;

import java.util.Map;

public class Main {
    /**//from   ww w  .  j av a2s.  com
     * Creates a new instance of the specified provider looking for the associated class in the
     * configuration map.
     *
     * @param providerMap The configuration map of the providers
     * @param provider The provider to instantiate
     * @param paramsClass The signature of the constructor to invoke
     * @param paramValues The parameters to pass to the constructor
     * @return An instance of the specific provider
     * @exception InstantiationException If any error occurs while instantiating. If the constructor
     * throw an exception then the cause of this will be an InvocationTargetException.
     * @exception IllegalArgumentException If no configuration is found for the specified provider
     */
    public static <E> E newInstance(Map<String, Class<? extends E>> providerMap, String provider,
            Class[] paramsClass, Object[] paramValues) throws InstantiationException, IllegalArgumentException {
        Class<? extends E> clazz = providerMap.get(provider);

        if (clazz == null)
            throw new IllegalArgumentException("No configuration for provider: " + provider);

        E instance = null;
        try {
            Constructor<? extends E> cloudcastConstructor;
            cloudcastConstructor = clazz.getConstructor(paramsClass);

            instance = cloudcastConstructor.newInstance(paramValues);
        } catch (Exception e) {
            InstantiationException ex;
            ex = new InstantiationException("Error instantiating provider " + provider);
            ex.initCause(e);
            throw ex;
        }
        return instance;
    }
}

Related

  1. newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs)
  2. newInstance(final Object obj, final String clazz)
  3. newInstance(final String className, ClassLoader cl)
  4. newInstance(final String className, final Object[] args)
  5. newInstance(final String fullyQualifiedClass)
  6. newInstance(Object bean, String propertyName, Class clazz)
  7. newInstance(ProceedingJoinPoint thisJoinPoint, Class clazz, Object... newArgs)
  8. newInstance(String aclass)
  9. newInstance(String className)