List of usage examples for java.lang.reflect Constructor newInstance
@CallerSensitive @ForceInline public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
From source file:com.open.cas.shiro.util.ReflectionUtils.java
public static Object newInstance(Class clazz, Object[] args, Class[] argsClass) { try {// w w w. java 2s. co m Constructor cons = clazz.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }
From source file:com.austin.base.commons.util.ReflectUtil.java
/** * @param className ??// www . j a v a2 s.c o m * @param args 17? * @return */ public static Object newInstance(String className, Object[] args) { try { Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i > j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = newoneClass.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception ex) { log.error(ex); return null; } }
From source file:org.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java
/** * Creates target bean of targetClass type copying source properties to ones * with the same name at target. Beans and collections are copied as * detached objects to allow lazy initialization. * /* www. ja v a 2 s.c o m*/ * @param source * Source Bean to copy * @param targetClass * Type of target bean to generate * @param converters * List of conveters of child bean properties * @return Target bean of targetClass type with source data converted * @throws Exception */ public static <TargetBean extends AbstractBean> TargetBean convert(AbstractBean source, Class<TargetBean> targetClass, IBeanConverter[] converters) throws Exception { // If source bean is null return null target bean if (source == null) { return null; } // Create target bean using target class default constructor Constructor<TargetBean> constructor = targetClass.getConstructor(new Class[] {}); TargetBean target = constructor.newInstance(new Object[] {}); // Obtain source fields (including inherited fields) Field[] sourceFields = getBeanFields(source); // Call class methods to copy simple properties, bean properties and // collection properties ConverterUtils.copyPlainFields(source, target, sourceFields); ConverterUtils.convertBeanFields(source, target, converters, sourceFields); ConverterUtils.convertBeanCollectionFields(source, target, converters, sourceFields); return target; }
From source file:com.open.cas.shiro.util.ReflectionUtils.java
/** * //ww w .j a va 2s.com * * @param className * ?? * @param args * ? * @return * @throws Exception */ public static Object newInstance(Class clazz, Object[] args) { try { Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = clazz.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }
From source file:io.apiman.manager.api.micro.ManagerApiMicroServiceCdiFactory.java
/** * Creates a custom component from a loaded class. * @param componentType//ww w . ja v a 2 s . c om * @param componentClass * @param configProperties */ @SuppressWarnings("unchecked") private static <T> T createCustomComponent(Class<T> componentType, Class<?> componentClass, Map<String, String> configProperties) throws Exception { if (componentClass == null) { throw new IllegalArgumentException("Invalid component spec (class not found)."); //$NON-NLS-1$ } try { Constructor<?> constructor = componentClass.getConstructor(Map.class); return (T) constructor.newInstance(configProperties); } catch (Exception e) { } return (T) componentClass.getConstructor().newInstance(); }
From source file:com.open.cas.shiro.util.ReflectionUtils.java
public static Object newInstance(String className, Object[] args) { try {//from ww w . j a va2s . c o m Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = newoneClass.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }
From source file:de.betterform.xml.config.Config.java
/** * Instantiates and defines the singleton instance. * * @param stream/*w w w .j a v a2s. c om*/ * InputStream from where the configuration will be read. * @throws XFormsConfigException * If the configuration could not be loaded. */ private static void initSingleton(InputStream stream) throws XFormsConfigException { //gets the concrete config class name from a system property //using DefaultConfig if the property is not set String configClassName = System.getProperty(Config.class.getName(), DefaultConfig.class.getName()); try { //uses reflection to get the constructor //(the constructor must have public visibility) Class classRef = Class.forName(configClassName, true, Config.class.getClassLoader()); Constructor construct = classRef.getConstructor(new Class[] { InputStream.class }); //initializes the singleton invoking the constructor SINGLETON = (Config) construct.newInstance(new Object[] { stream }); } catch (Exception e) { throw new XFormsConfigException(e); } }
From source file:edu.uga.cs.fluxbuster.db.DBInterfaceFactory.java
/** * Creates a database interface.//from w w w .ja v a 2s .c om * * @return the database interface */ @SuppressWarnings("rawtypes") public static DBInterface loadDBInterface() { DBInterface retval = null; try { DBInterfaceFactory.init(); Constructor[] constructors = dbifaceClass.getConstructors(); Constructor con = null; for (Constructor constructor : constructors) { Class[] paramtypes = constructor.getParameterTypes(); if (paramtypes.length == 1 && paramtypes[0].isInstance(DBInterfaceFactory.connectionPool)) { con = constructor; } } Object obj = con.newInstance(DBInterfaceFactory.connectionPool); retval = (DBInterface) obj; } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error loading db interface.", e); } } return retval; }
From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java
private static Object createValueLabel(Constructor<?> actualConstructor, Object[] arguments) throws Exception { Object valueLabel = actualConstructor.newInstance(arguments); // set text //from ww w.j a v a2 s . co m { String rendererName; if (arguments[0] == null) { rendererName = "null"; } else { rendererName = CodeUtils.getShortClass(arguments[0].getClass().getName()); } String text = "ValueLabel(" + rendererName + ")"; setValueLabelText(valueLabel, text); } // done return valueLabel; }
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 ww w. ja va 2s . com*/ 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()); } }