Here you can find the source of newInstance(Map
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 |
public static <E> E newInstance(Map<String, Class<? extends E>> providerMap, String provider, Class[] paramsClass, Object[] paramValues) throws InstantiationException, IllegalArgumentException
//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; } }