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.github.dozermapper.core.converters.StringConstructorConverter.java
public Object convert(Class destClass, Object srcObj) { String result = (String) stringConverter.convert(destClass, srcObj); try {/*from w w w .j a v a 2 s . co m*/ Constructor constructor = destClass.getConstructor(String.class); // TODO Check, but not catch return constructor.newInstance(result); } catch (NoSuchMethodException e) { // just return the string return result; } catch (Exception e) { throw new ConversionException(e); } }
From source file:org.malaguna.cmdit.service.CommandRunner.java
/** * Dada una clase de un comando, instancia el mismo apropiadamente * //from w w w.j a v a 2 s . c o m * @param clazz * @return */ protected Command createCommand(Class<? extends Command> clazz) { Command comand = null; //Build a new command using Web Application Context (wac) try { Constructor<?> c = clazz.getConstructor(BeanFactory.class); comand = (Command) c.newInstance(getBeanFactory()); } catch (Exception e) { logger.error("Error creating new command [" + clazz.toString() + "] : " + e.getMessage()); } return comand; }
From source file:com.bskyb.cg.environments.message.MessageFormatFactory.java
private Object newInstance(String logFormat, Class<?>[] paramTypes, Object[] params) { Object obj = null;//from w w w. j av a2 s . co m String classname = logFormat; try { Class<?> cls = classes.get(classname); if (cls == null) { throw new RuntimeException("No class registered under " + logFormat); } Constructor<?> ctor = cls.getConstructor(paramTypes); obj = ctor.newInstance(params); } catch (Exception ex) { ex.printStackTrace(); } return obj; }
From source file:com.comcast.drivethru.utils.Method.java
/** * Get a new instance of the appropriate Apache HTTP request object for this HTTP method and * pass it the given URL.//from ww w. j a v a2s. c o m * * @param url * the URL to pass into the constructor of the request object * * @return a new instance of the appropriate Apache HTTP request object for this HTTP method * * @throws HttpException * if the request object could not be created */ public HttpRequestBase getRequest(String url) throws HttpException { try { Constructor<? extends HttpRequestBase> constructor = type.getConstructor(String.class); return constructor.newInstance(url); } catch (Exception ex) { throw new HttpException("Failed to create an RestRequest", ex); } }
From source file:com.adaptris.core.management.config.ConfigManagerImpl.java
private AdapterRegistryMBean createCustomRegistry() throws CoreException { AdapterRegistryMBean result = null;// w w w.j av a2 s . co m String classname = System.getProperty(ADAPTER_REGISTRY_IMPL); Class[] paramTypes = { BootstrapProperties.class }; Object[] args = { bootstrapProperties }; Class clazz; try { clazz = Class.forName(classname); Constructor cnst = clazz.getDeclaredConstructor(paramTypes); result = (AdapterRegistryMBean) cnst.newInstance(args); } catch (Exception e) { ExceptionHelper.rethrowCoreException(e); } return result; }
From source file:mojo.view.rest.DataController.java
@ResponseBody @RequestMapping(value = "/select-view", method = RequestMethod.GET) public DataPage<?> doFetchView(@RequestBody Select<E> spec, @RequestParam String view) { try {/*from w w w . jav a 2s.c o m*/ DataPage<E> page = service.select(spec); List<Object> views = new ArrayList<Object>(page.getData().size()); Class<?> viewClass = Class.forName(view); for (E entity : page.getData()) { Class<?> entityClass = entity.getClass(); Constructor<?> viewConstructor = viewClass.getConstructor(entityClass); Object obj = viewConstructor.newInstance(entity); views.add(obj); } DataPage<Object> result = new DataPage<Object>(); result.setTotal(page.getTotal()); result.setData(views); return result; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:iristk.util.Record.java
private static Record parseJsonObject(JsonObject json) throws JsonToRecordException { try {//from w w w. jav a2s.c om Record record; if (json.get("class") != null) { Constructor<?> constructor = Class.forName(json.get("class").asString()).getDeclaredConstructor(); constructor.setAccessible(true); record = (Record) constructor.newInstance(null); } else { record = new Record(); } for (String name : json.names()) { if (!name.equals("class")) { JsonValue jvalue = json.get(name); record.put(name, parseJsonValue(jvalue)); } } //System.out.println(json + " " + record); return record; } catch (ClassNotFoundException e) { throw new JsonToRecordException("Class not found: " + e.getMessage()); } catch (InstantiationException e) { throw new JsonToRecordException("Could not create: " + e.getMessage()); } catch (IllegalAccessException e) { throw new JsonToRecordException("Could not access: " + e.getMessage()); } catch (IllegalArgumentException e) { throw new JsonToRecordException("Illegal argument: " + e.getMessage()); } catch (InvocationTargetException e) { throw new JsonToRecordException("Invocation problem: " + e.getMessage()); } catch (NoSuchMethodException e) { throw new JsonToRecordException("No such method: " + e.getMessage()); } catch (SecurityException e) { throw new JsonToRecordException("Securiry problem: " + e.getMessage()); } }
From source file:com.soomla.util.JSONFactory.java
public T create(JSONObject jsonObject, String packageName) { if (jsonObject == null) { // warn/*from w ww. ja v a 2 s . c om*/ return null; } T t = null; try { // SoomlaUtils.LogDebug(TAG, jsonObject.toString()); String className = jsonObject.getString(com.soomla.data.JSONConsts.SOOM_CLASSNAME); Class<? extends T> clazz = (Class<? extends T>) Class.forName(packageName + "." + className); SoomlaUtils.LogDebug(TAG, "creating with: " + packageName + "." + className); if (clazz != null) { final Constructor<? extends T> jsonCtor = clazz.getDeclaredConstructor(JSONObject.class); t = jsonCtor.newInstance(jsonObject); } else { SoomlaUtils.LogError(TAG, "unknown class name:" + className); } } catch (JSONException e) { SoomlaUtils.LogError(TAG, "fromJSONObject JSONException:" + e.getMessage()); } catch (InstantiationException e) { SoomlaUtils.LogError(TAG, "fromJSONObject InstantiationException:" + e.getMessage()); } catch (IllegalAccessException e) { SoomlaUtils.LogError(TAG, "fromJSONObject IllegalAccessException:" + e.getMessage()); } catch (NoSuchMethodException e) { SoomlaUtils.LogError(TAG, "fromJSONObject no JSONObject constructor found:" + e.getMessage()); } catch (InvocationTargetException e) { SoomlaUtils.LogError(TAG, "fromJSONObject InvocationTargetException:" + e.getMessage()); SoomlaUtils.LogError(TAG, "fromJSONObject InvocationTargetException[cause]:" + e.getCause()); } catch (ClassNotFoundException e) { SoomlaUtils.LogError(TAG, "fromJSONObject ClassNotFoundException:" + e.getMessage()); } return t; }
From source file:it.cnr.icar.eric.server.plugin.AbstractPluginManager.java
/** * Creates the instance of the pluginClass *///from w ww . j a va2 s .c o m protected Object createPluginInstance(String pluginClass) throws Exception { Object plugin = null; if (log.isDebugEnabled()) { log.debug("pluginClass = " + pluginClass); } if (pluginClass != null) { Class<?> theClass = Class.forName(pluginClass); //try to invoke zero arg constructor using Reflection, //if this fails then try invoking getInstance() try { Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null); plugin = constructor.newInstance(new Object[0]); } catch (Exception e) { Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null); plugin = factory.invoke((java.lang.Class[]) null, new Object[0]); } } return plugin; }
From source file:com.xpn.xwiki.plugin.charts.plots.TimePlotFactory.java
public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException { Class rendererClass = params.getClass(ChartParams.RENDERER); XYItemRenderer renderer;/*w w w .ja v a2s .c o m*/ if (rendererClass != null) { try { Constructor ctor = rendererClass.getConstructor(new Class[] {}); renderer = (XYItemRenderer) ctor.newInstance(new Object[] {}); } catch (Throwable e) { throw new GenerateException(e); } } else { renderer = new XYLineAndShapeRenderer(); } ChartCustomizer.customizeXYItemRenderer(renderer, params); DateAxis domainAxis = new DateAxis(); ChartCustomizer.customizeDateAxis(domainAxis, params, ChartParams.AXIS_DOMAIN_PREFIX); NumberAxis rangeAxis = new NumberAxis(); rangeAxis.setAutoRangeIncludesZero(false); // override default ChartCustomizer.customizeNumberAxis(rangeAxis, params, ChartParams.AXIS_RANGE_PREFIX); XYDataset dataset = TimeSeriesCollectionFactory.getInstance().create(dataSource, params); XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, renderer); ChartCustomizer.customizeXYPlot(plot, params); return plot; }