List of usage examples for java.lang Class newInstance
@CallerSensitive @Deprecated(since = "9") public T newInstance() throws InstantiationException, IllegalAccessException
From source file:net.hydromatic.optiq.impl.jdbc.JdbcSchema.java
/** * Creates a JdbcSchema, taking credentials from a map. * * @param parentSchema Parent schema//from ww w .j a v a 2s . c o m * @param name Name * @param operand Map of property/value pairs * @return A JdbcSchema */ public static JdbcSchema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) { DataSource dataSource; try { final String dataSourceName = (String) operand.get("dataSource"); if (dataSourceName != null) { final Class<?> clazz = Class.forName((String) dataSourceName); dataSource = (DataSource) clazz.newInstance(); } else { final String jdbcUrl = (String) operand.get("jdbcUrl"); final String jdbcDriver = (String) operand.get("jdbcDriver"); final String jdbcUser = (String) operand.get("jdbcUser"); final String jdbcPassword = (String) operand.get("jdbcPassword"); dataSource = dataSource(jdbcUrl, jdbcDriver, jdbcUser, jdbcPassword); } } catch (Exception e) { throw new RuntimeException("Error while reading dataSource", e); } String jdbcCatalog = (String) operand.get("jdbcCatalog"); String jdbcSchema = (String) operand.get("jdbcSchema"); final SqlDialect dialect = JdbcSchema.createDialect(dataSource); return new JdbcSchema(parentSchema, name, dataSource, dialect, jdbcCatalog, jdbcSchema); }
From source file:com.netflix.astyanax.thrift.ThriftUtils.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T getThriftObjectFromProperties(Class<T> clazz, Properties props) throws Exception { org.apache.thrift.TBase entity = (org.apache.thrift.TBase) clazz.newInstance(); return (T) populateObjectFromProperties(entity, props); }
From source file:Resources.java
/** * Creates an instance of a class//from w ww . ja v a2s . co m * * @param clazz - the class to create * @return An instance of the class * @throws InstantiationException If the class cannot be instantiaed * @throws IllegalAccessException If the class is not public, or other access problems arise */ public static Object instantiate(Class clazz) throws InstantiationException, IllegalAccessException { try { return clazz.newInstance(); } catch (Exception e) { // Try alternative...theoretically should fail for the exact same // reason, but in case of a weird security manager, this will help // some cases. //return clazz.newInstance(); return clazz.newInstance(); } }
From source file:tools.xor.util.ClassUtil.java
/** * Creates a new instance of the given class via the no-arg constructor, * invoking the constructor as a privileged action if it is protected or * private./* ww w .j a v a2s .c o m*/ * * @param c given class * @return a new instance of the given class via the no-arg constructor * @throws Exception when creating the instance */ public static Object newInstanceAsPrivileged(final Class<?> c) throws Exception { try { return c.newInstance(); } catch (Exception e) { return AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { try { final Constructor<?> constructor = c.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (Exception e) { throw ClassUtil.wrapRun(e); } } }); } }
From source file:com.opengamma.language.external.ExternalFunctionHandler.java
private static Object tryGetInstance(final Class<?> clazz) { try {//from w w w . j av a 2 s . c o m return clazz.newInstance(); } catch (Throwable t) { return null; } }
From source file:com.datatorrent.stram.StringCodecs.java
public static void loadDefaultConverters() { LOG.debug("Loading default converters for BeanUtils"); ConvertUtils.register(new Converter() { @Override/*from ww w . j a va2 s. co m*/ @SuppressWarnings("unchecked") public Object convert(Class type, Object value) { if (value == null) { return null; } for (Class<?> clazz = value.getClass(); clazz != null; clazz = clazz.getSuperclass()) { Class<? extends StringCodec> codec = codecs.get(clazz); if (codec == null) { continue; } StringCodec instance; try { instance = codec.newInstance(); } catch (IllegalAccessException ex) { throw new RuntimeException( "Internal Error - it's impossible for this exception to be thrown!", ex); } catch (InstantiationException ex) { throw new RuntimeException( "Internal Error - it's impossible for this exception to be thrown!", ex); } return instance.toString(value); } return value.toString(); } }, String.class); ConvertUtils.register(new Converter() { @Override public Object convert(Class type, Object value) { return value == null ? null : URI.create(value.toString()); } }, URI.class); }
From source file:org.bremersee.common.spring.autoconfigure.ObjectMapperAutoConfiguration.java
private static boolean addObjectMapperModule(final List<Module> moduleList, final String className) { try {/*from ww w . j av a 2 s.c o m*/ LOG.info("Trying to add module'" + className + "' to the Jackson Object Mapper."); @SuppressWarnings("unchecked") Class<? extends Module> cls = (Class<? extends Module>) Class.forName(className); moduleList.add(cls.newInstance()); LOG.info("Module'" + className + "' was successfully added to the Jackson Object Mapper."); // NOSONAR return true; } catch (ClassNotFoundException e) { // NOSONAR LOG.warn("Module'" + className + "' wasn't added to the Jackson Object Mapper: " + className + " was not found."); } catch (InstantiationException | IllegalAccessException e) { LOG.warn("Module'" + className + "' wasn't added to the Jackson Object Mapper.", e); } return false; }
From source file:net.big_oh.common.jdbc.JdbcDriverProxy.java
private static final List<? extends JDBCEventListener> buildListenersListFromPropertiesFile() { Properties props = PropertyUtil.loadProperties(JdbcDriverProxy.class.getSimpleName().toLowerCase()); List<JDBCEventListener> listeners = new ArrayList<JDBCEventListener>(); String[] listenerClassNames = props.getProperty(JDBCEventListener.class.getSimpleName() + "s", "") .split(","); for (String listenerClassName : listenerClassNames) { if (!StringUtils.isBlank(listenerClassName)) { try { Class<?> listenerClass = Class.forName(listenerClassName); JDBCEventListener listener = (JDBCEventListener) listenerClass.newInstance(); listeners.add(listener); logger.info("Registered a new JDBC event listener: " + listenerClass.toString()); } catch (ClassNotFoundException cnfe) { logger.error("Failed to find a JDBC event listener class named '" + listenerClassName + "'", cnfe);//from w w w . j ava2s.com } catch (InstantiationException e) { logger.error( "Failed to instantiate a JDBC event listener class named '" + listenerClassName + "'", e); } catch (IllegalAccessException e) { logger.error(e); } } } return listeners; }
From source file:org.openmrs.module.openhmis.cashier.api.ReceiptNumberGeneratorFactory.java
private static IReceiptNumberGenerator createGeneratorInstance() { Class<? super IReceiptNumberGenerator> cls = null; try {/*from w w w . j a v a 2 s .com*/ cls = FactoryImpl.INSTANCE.getGeneratorClass(); if (cls == null) { return null; } generator = (IReceiptNumberGenerator) cls.newInstance(); return generator; } catch (ClassNotFoundException classEx) { LOG.warn("Attempt to load unknown receipt number generator type", classEx); throw new APIException("Could not locate receipt number generator class.", classEx); } catch (InstantiationException instantiationEx) { throw new APIException("Could not instantiate the '" + cls.getClass().getName() + "' class.", instantiationEx); } catch (IllegalAccessException accessEx) { throw new APIException("Could not access the '" + cls.getClass().getName() + "' class.", accessEx); } }
From source file:com.taobao.android.builder.tools.groovy.ClosureFactory.java
public static Closure<?> buildClosure(String... strings) throws IOException { Closure<?> closure = null;/*from w w w . j a v a 2s . c o m*/ // Create a method returning a closure StringBuilder sb = new StringBuilder("def closure() { { script -> "); sb.append(StringUtils.join(strings, "\n")); sb.append(" } }"); // Create an anonymous class for the method GroovyClassLoader loader = new GroovyClassLoader(); Class<?> groovyClass = loader.parseClass(sb.toString()); try { // Create an instance of the class GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); // Invoke the object's method and thus obtain the closure closure = (Closure<?>) groovyObject.invokeMethod("closure", null); } catch (Throwable e) { throw new RuntimeException(e); } finally { loader.close(); } return closure; }