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.isencia.passerelle.message.type.ComplexConverter.java
protected Object convertTokenToContent(Token typedToken, Class targetType) throws MessageException { Complex cVal = ((ComplexToken) typedToken).complexValue(); if (targetType != null && !Complex.class.equals(targetType)) { if (String.class.equals(targetType)) { return cVal.toString(); } else {//w w w .jav a2 s .co m // only take real part try { Constructor c = targetType.getConstructor(new Class[] { String.class }); return c.newInstance(new Object[] { Double.toString(cVal.real) }); } catch (Exception e) { throw new UnsupportedOperationException(); } } } else { // we just return a Complex as default, // if no specific target type is given try { double real = cVal.real; double imag = cVal.imag; return new Complex(real, imag); } catch (ClassCastException e) { throw new UnsupportedOperationException(); } } }
From source file:com.netspective.sparx.navigate.NavigationTrees.java
public NavigationTree createNavigationTree(Class cls) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { if (NavigationTree.class.isAssignableFrom(cls)) { Constructor c = cls.getConstructor(new Class[] { Project.class }); return (NavigationTree) c.newInstance(new Object[] { project }); } else/* w w w . ja v a 2 s . c om*/ throw new RuntimeException("Don't know what to do with with class: " + cls); }
From source file:net.dmulloy2.ultimatearena.api.SimpleArenaType.java
@Override public ArenaConfig newConfig(ArenaZone az) { Class<? extends ArenaConfig> clazz = getArenaConfig(); Validate.notNull(clazz, "ArenaConfig class cannot be null!"); try {/*from w w w .j av a 2s .com*/ Constructor<? extends ArenaConfig> constructor = clazz.getConstructor(ArenaZone.class); return constructor.newInstance(az); } catch (InvocationTargetException ex) { throw new RuntimeException("Failed to create new " + clazz.getName() + " instance.", ex); } catch (Throwable ex) { throw Throwables.propagate(ex); } }
From source file:com.metadave.stow.TestStow.java
@Test public void testGen() throws Exception { InputStream is = this.getClass().getResourceAsStream("/Stow.stg"); File f = File.createTempFile("stow", "test.stg"); String root = f.getParent();//w w w . jav a 2s . c om String classdir = root + File.separator + "stowtest"; String stgPath = f.getAbsolutePath(); String stowStg = org.apache.commons.io.IOUtils.toString(is); org.apache.commons.io.FileUtils.writeStringToFile(f, stowStg); File d = new File(classdir); d.mkdirs(); Stow.generateObjects(stgPath, "stowtest", "Test", classdir); File testSTBean = new File(classdir + File.separator + "TestSTBean.java"); assertTrue(testSTBean.exists()); File testSTAccessor = new File(classdir + File.separator + "TestSTAccessor.java"); assertTrue(testSTAccessor.exists()); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, testSTBean.getAbsolutePath()); compiler.run(null, null, null, testSTAccessor.getAbsolutePath()); File testSTAccessorClass = new File(classdir + File.separator + "TestSTAccessor.class"); assertTrue(testSTAccessorClass.exists()); File testSTBeanClass = new File(classdir + File.separator + "TestSTBean.class"); assertTrue(testSTBeanClass.exists()); URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File(root).toURI().toURL() }); Class<?> cls = Class.forName("stowtest.TestSTBean", true, classLoader); Constructor<?> c = cls.getConstructors()[0]; STGroup stg = new STGroupFile(f.getAbsolutePath()); Object o = c.newInstance(stg); Set<String> methods = new HashSet<String>(); methods.add("addPackage"); methods.add("addBeanClass"); methods.add("addTemplateName"); methods.add("addAccessor"); int count = 0; for (Method m : o.getClass().getMethods()) { if (methods.contains(m.getName())) { count++; } } assertEquals("Look for 8 generated methods", 8, count); }
From source file:org.opendaylight.ovsdb.lib.schema.TableSchema.java
public <E extends TableSchema<E>> E as(Class<E> clazz) { try {// w w w . j a v a 2s . c om Constructor<E> instance = clazz.getConstructor(TableSchema.class); return instance.newInstance(this); } catch (Exception e) { throw new RuntimeException("exception constructing instance of clazz " + clazz, e); } }
From source file:com.adaptris.core.config.DefaultPreProcessorLoader.java
private ConfigPreProcessor createInstance(String classname, KeyValuePairSet config) throws CoreException { ConfigPreProcessor preProcessor = null; log.trace("Loading pre-processor: " + classname); Class<?>[] paramTypes = { KeyValuePairSet.class }; Object[] args = { config };/* www . java 2s .c om*/ try { Class<?> clazz = Class.forName(classname); Constructor<?> cnst = clazz.getDeclaredConstructor(paramTypes); preProcessor = (ConfigPreProcessor) cnst.newInstance(args); } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return preProcessor; }
From source file:com.adaptris.core.config.DefaultPreProcessorLoader.java
private ConfigPreProcessor createInstance(String classname, BootstrapProperties bootstrapProperties) throws CoreException { ConfigPreProcessor preProcessor = null; log.trace("Loading pre-processor: " + classname); Class<?>[] paramTypes = { BootstrapProperties.class }; Object[] args = { bootstrapProperties }; try {//w w w . j a va 2 s. c o m Class<?> clazz = Class.forName(classname); Constructor<?> cnst = clazz.getDeclaredConstructor(paramTypes); preProcessor = (ConfigPreProcessor) cnst.newInstance(args); } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return preProcessor; }
From source file:com.ksc.transform.LegacyErrorUnmarshaller.java
@Override public KscServiceException unmarshall(Node in) throws Exception { XPath xpath = xpath();/* w ww .ja v a 2 s . c om*/ String errorCode = parseErrorCode(in, xpath); String message = asString("Response/Error/Message", in, xpath); String requestId = asString("Response/RequestID", in, xpath); String errorType = asString("Response/Error/Type", in, xpath); if (StringUtils.isBlank(errorCode)) { return null; } Constructor<? extends KscServiceException> constructor = exceptionClass.getConstructor(String.class); KscServiceException ase = constructor.newInstance(message); ase.setErrorCode(errorCode); ase.setRequestId(requestId); if (errorType == null) { ase.setErrorType(ErrorType.Unknown); } else if (errorType.equalsIgnoreCase("server")) { ase.setErrorType(ErrorType.Service); } else if (errorType.equalsIgnoreCase("client")) { ase.setErrorType(ErrorType.Client); } return ase; }
From source file:com.couchbase.sqoop.manager.CouchbaseFactory.java
@Override public ConnManager accept(JobData data) { SqoopOptions options = data.getSqoopOptions(); if (null != options.getConnManagerClassName()) { String className = options.getConnManagerClassName(); ConnManager connManager = null;/*from w ww . java 2 s . c om*/ try { Class<ConnManager> cls = (Class<ConnManager>) Class.forName(className); Constructor<ConnManager> constructor = cls.getDeclaredConstructor(SqoopOptimnbons.class); connManager = constructor.newInstance(options); } catch (Exception e) { System.err.println("problem finding the connection manager for class name :" + className); // Log the stack trace for this exception LOG.debug(e.getMessage(), e); // Print exception message. System.err.println(e.getMessage()); } return connManager; } String connectStr = options.getConnectString(); // java.net.URL follows RFC-2396 literally, which does not allow a ':' // character in the scheme component (section 3.1). JDBC connect strings, // however, commonly have a multi-scheme addressing system. e.g., // jdbc:mysql://...; so we cannot parse the scheme component via URL // objects. Instead, attempt to pull out the scheme as best as we can. // First, see if this is of the form [scheme://hostname-and-etc..] int schemeStopIdx = connectStr.indexOf("//"); if (-1 == schemeStopIdx) { // If no hostname start marker ("//"), then look for the right-most ':' // character. schemeStopIdx = connectStr.lastIndexOf(':'); if (-1 == schemeStopIdx) { // Warn that this is nonstandard. But we should be as permissive // as possible here and let the ConnectionManagers themselves throw // out the connect string if it doesn't make sense to them. LOG.warn("Could not determine scheme component of connect string"); // Use the whole string. schemeStopIdx = connectStr.length(); } } String scheme = connectStr.substring(0, schemeStopIdx); if (null == scheme) { // We don't know if this is a mysql://, hsql://, etc. // Can't do anything with this. LOG.warn("Null scheme associated with connect string."); return null; } LOG.debug("Trying with scheme: " + scheme); if (scheme.equals("http:")) { return new CouchbaseManager(options); } else { return null; } }
From source file:es.urjc.mctwp.bbeans.AbstractBean.java
/** * Return new instance of command identified by clazz * /*from w ww .j a va 2s .c om*/ * @param clase * @return */ protected Command getCommand(Class<?> clazz) { Command command = null; //Build a new command using Web Application Context (wac) try { Constructor<?> c = clazz.getConstructor(BeanFactory.class); command = (Command) c.newInstance(wac); } catch (Exception e) { logger.error("Error creating new command [" + clazz.toString() + "] : " + e.getMessage()); } return command; }