List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:edu.umich.robot.soar.OLCommandManager.java
/** * Convert a wme in to a command instance for processing. If the command * returns from this, it is accepted and status is set as such. * //from ww w . java2 s. c om * @param id * @return * @throws SoarCommandError */ public OLCommand newInstance(Identifier id) throws SoarCommandError { String name = id.GetAttribute(); Class<? extends OLCommand> klass = commands.get(name); if (klass != null) { try { Constructor<? extends OLCommand> ctor = klass .getConstructor(new Class<?>[] { Identifier.class, SoarAgent.class }); OLCommand command = ctor.newInstance(id, agent); if (command != null) CommandStatus.ACCEPTED.addStatus(id); logger.debug(agentPrompt + command); return command; } catch (InvocationTargetException e) { if (e.getCause() instanceof SoarCommandError) { SoarCommandError sce = (SoarCommandError) e.getCause(); CommandStatus.ERROR.addStatus(id, sce.getMessage()); logger.error(sce.getMessage()); } return null; } catch (NoSuchMethodException e) { logger.error(e.getMessage()); return null; } catch (IllegalAccessException e) { logger.error(e.getMessage()); return null; } catch (InstantiationException e) { logger.error(e.getMessage()); return null; } } logger.warn("No such command: " + name); return null; }
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 ava 2 s . c o 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:fr.inrialpes.exmo.align.cli.TestGen.java
public void run(String[] args) throws Exception { try {//from w w w. j a v a 2 s . c om CommandLine line = parseCommandLine(args); if (line == null) return; // --help outputfilename = fileName; // likely useless // Here deal with command specific arguments if (line.hasOption('t')) methodName = line.getOptionValue('t'); if (line.hasOption('o')) parameters.setProperty("ontoname", line.getOptionValue('o')); if (line.hasOption('a')) parameters.setProperty("alignname", line.getOptionValue('a')); if (line.hasOption('w')) { dir = line.getOptionValue('w'); parameters.setProperty("outdir", dir); } if (line.hasOption('u')) { url = line.getOptionValue('u'); parameters.setProperty("urlprefix", url); // JE: Danger urlprefix/uriprefix } String[] argList = line.getArgs(); if (argList.length > 0) { fileName = argList[0]; parameters.setProperty("filename", fileName); } else { logger.error("Require the seed ontology filename"); usage(); System.exit(-1); } } catch (ParseException exp) { logger.error(exp.getMessage()); usage(); System.exit(-1); } logger.debug(" >>>> {} from {}", methodName, fileName); if (methodName == null) { // generate one test TestGenerator tg = new TestGenerator(); // It would certainly be better to initialise the generator with parameters tg.setDirPrefix(dir); tg.setURLPrefix(url); if (parameters.getProperty("ontoname") != null) tg.setOntoFilename(parameters.getProperty("ontoname")); if (parameters.getProperty("alignname") != null) tg.setAlignFilename(parameters.getProperty("alignname")); tg.modifyOntology(fileName, (Properties) null, (String) null, parameters); } else { // generate a test set TestSet tset = null; try { Class<?> testSetClass = Class.forName(methodName); Class<?>[] cparams = {}; Constructor<?> testSetConstructor = testSetClass.getConstructor(cparams); Object[] mparams = {}; tset = (TestSet) testSetConstructor.newInstance(mparams); } catch (Exception ex) { logger.error("Cannot create TestSet {}", methodName); logger.error("Caught error", ex); usage(); System.exit(-1); } tset.generate(parameters); } }
From source file:com.legstar.proxy.invoke.ServiceProxy.java
/** * Load the operation proxy named in the configuration or the default one * if none is found.//from w w w . j a va2s. co m * * @param config the current configuration * @return an instance of the operation proxy * @throws ProxyConfigurationException if unable to instantiate the * operation proxy */ private IOperationProxy getOperationProxy(final Map<String, String> config) throws ProxyConfigurationException { try { String operationProxyClassName = config.get(OPERATION_PROXY_CLASS_NAME_PROPERTY); if (operationProxyClassName == null || operationProxyClassName.length() == 0) { operationProxyClassName = DEFAULT_OPERATION_PROXY_CLASS_NAME; } Class<?> clazz = ClassUtil.loadClass(operationProxyClassName); Constructor<?> constructor = clazz.getConstructor(Map.class); return (IOperationProxy) constructor.newInstance(new Object[] { config }); } catch (SecurityException e) { throw new ProxyConfigurationException(e); } catch (IllegalArgumentException e) { throw new ProxyConfigurationException(e); } catch (ClassNotFoundException e) { throw new ProxyConfigurationException(e); } catch (NoSuchMethodException e) { throw new ProxyConfigurationException(e); } catch (InstantiationException e) { throw new ProxyConfigurationException(e); } catch (IllegalAccessException e) { throw new ProxyConfigurationException(e); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof ProxyConfigurationException) { throw (ProxyConfigurationException) e.getTargetException(); } throw new ProxyConfigurationException(e); } }
From source file:com.curl.orb.context.Seasar2ApplicationContext.java
/** * Create new Seasar2ApplicationContext. * /*from ww w. ja v a 2 s .c om*/ * @param context ServletContext * @throws ApplicationContextException */ public Seasar2ApplicationContext(ServletContext context) throws ApplicationContextException { try { Class<?> cls = Class.forName("org.seasar.framework.container.servlet.SingletonS2ContainerInitializer"); Object obj = cls.getConstructor(new Class[0]).newInstance(new Object[0]); if (context.getInitParameter("configPath") != null) { MethodUtils.invokeMethod(obj, "setConfigPath", new Object[] { context.getInitParameter("configPath") }); } MethodUtils.invokeMethod(obj, "setApplication", new Object[] { context }); MethodUtils.invokeMethod(obj, "initialize", new Object[0]); } catch (IllegalArgumentException e) { throw new ApplicationContextException(e); } catch (SecurityException e) { throw new ApplicationContextException(e); } catch (InstantiationException e) { throw new ApplicationContextException(e); } catch (ClassNotFoundException e) { throw new ApplicationContextException(e); } catch (NoSuchMethodException e) { throw new ApplicationContextException(e); } catch (IllegalAccessException e) { throw new ApplicationContextException(e); } catch (InvocationTargetException e) { throw new ApplicationContextException(e); } }
From source file:com.jkoolcloud.tnt4j.utils.Utils.java
/** * Create object instance based on specific class name and given parameters * * @param className//from ww w .ja v a 2 s. c o m * name of the class * @param args * arguments to be passed to the constructor * @param types * list of parameter types * * @return instance of the objects specified by the class name * @throws Exception * if error creating instance */ public static Object createInstance(String className, Object[] args, Class<?>... types) throws Exception { if (className == null) return null; Class<?> classObj = Class.forName(className); Constructor<?> ct = classObj.getConstructor(types); return ct.newInstance(args); }
From source file:com.quinsoft.zeidon.dbhandler.JdbcHandlerUtils.java
@SuppressWarnings("unchecked") public DbHandler getDbHandler() { DbHandler handler = options.getDbHandler(); if (handler != null) return handler; String handlerName = options.getConfigValue(getGroupName(), "DbHandler"); // If the handler name isn't specified we'll try to be smart and determine the default // handler using the connection string. if (StringUtils.isBlank(handlerName)) { String conn = options.getOiSourceUrl(); if (conn.startsWith("jdbc:mysql:")) handlerName = MysqlJdbcHandler.class.getCanonicalName(); else if (conn.startsWith("jdbc:sqlite:")) handlerName = SqliteJdbcHandler.class.getCanonicalName(); else if (conn.startsWith("testsql:")) handlerName = TestSqlHandler.class.getCanonicalName(); else// w w w. j a va 2 s.co m handlerName = JdbcHandler.class.getCanonicalName(); } try { // For now we assume the DBHandler is the JDBC handler. ClassLoader loader = options.getTask().getObjectEngine().getClassLoader(handlerName); Class<? extends JdbcHandler> handlerClass; handlerClass = (Class<? extends JdbcHandler>) loader.loadClass(handlerName); Constructor<? extends JdbcHandler> constructor = handlerClass.getConstructor(handlerConstructorArgs); JdbcHandler jdbcHandler = constructor.newInstance(options.getTask(), options); return jdbcHandler; } catch (Exception e) { throw ZeidonException.wrapException(e).appendMessage("Handler name = ", handlerName); } }
From source file:com.all.ultrapeer.UltrapeerConfig.java
public <T> T getTypedProperty(String property, Class<T> clazz) { try {//from w w w. j av a2 s . c om return clazz.getConstructor(String.class).newInstance(getProperty(property)); } catch (Exception e) { return null; } }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Determine whether the given class has a public constructor with the given signature, * and return it if available (else return {@code null}). * <p>Essentially translates {@code NoSuchMethodException} to {@code null}. * @param clazz the clazz to analyze//from w w w . ja v a 2 s . co m * @param paramTypes the parameter types of the method * @return the constructor, or {@code null} if not found * @see Class#getConstructor */ public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) { Assert.notNull(clazz, "Class must not be null"); try { return clazz.getConstructor(paramTypes); } catch (NoSuchMethodException ex) { return null; } }
From source file:net.dmulloy2.ultimatearena.api.SimpleArenaType.java
@Override public Arena newArena(ArenaZone az) { Class<? extends Arena> clazz = getArena(); Validate.notNull(clazz, "Arena class cannot be null!"); try {//from w ww . jav a2 s.co m Constructor<? extends Arena> 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); } }