List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:org.apache.tez.engine.runtime.RuntimeUtils.java
@SuppressWarnings("unchecked") public static <T> T getNewInstance(Class<T> theClass, TezEngineTaskContext context) { T result;// www . j ava 2 s . com try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(CONTEXT_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(context); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.apache.tez.engine.runtime.RuntimeUtils.java
@SuppressWarnings("unchecked") public static <T> T getNewInputInstance(Class<T> theClass, TezEngineTaskContext context, int index) { T result;//from w ww . j a va2 s. c o m try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(CONTEXT_INT_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(context, index); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:com.microsoft.tfs.client.common.git.utils.GitHelpers.java
public static Object getInstance(final String pluginName, final String className, final Class<?>[] constructorParameterTypes, final Object[] constructorParameterValues) { Check.notNullOrEmpty(pluginName, "pluginName"); //$NON-NLS-1$ Check.notNullOrEmpty(className, "className"); //$NON-NLS-1$ Check.notNull(constructorParameterTypes, "constructorParameterTypes"); //$NON-NLS-1$ Check.notNull(constructorParameterTypes, "constructorParameterTypes"); //$NON-NLS-1$ final Class<?> operationClass = getClass(pluginName, className); if (operationClass == null) { return null; }/*from w w w .j ava2 s . c o m*/ final Constructor<?> constructor; try { constructor = operationClass.getDeclaredConstructor(constructorParameterTypes); constructor.setAccessible(true); } catch (final Exception e) { log.error("Searching for the " + operationClass.getName() + " constructor", e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } final Object operationInstance; try { operationInstance = constructor.newInstance(constructorParameterValues); } catch (final Exception e) { log.error("Creating the " + operationClass.getName() + " object", e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } return operationInstance; }
From source file:org.wikipedia.nirvana.statistics.StatisticsFabric.java
@SuppressWarnings("unchecked") static Statistics createReporter(NirvanaWiki wiki, String type, int year) { Statistics ob = null;/* w w w. j ava 2 s . c o m*/ MultiKey multiKey = new MultiKey(type, new Integer(year)); ob = (Statistics) reportersYear.get(multiKey); if (ob == null) { Class cl = reporterClass.get(type); if (cl == null) return null; try { Class params[] = new Class[] { NirvanaWiki.class, String.class, int.class }; Constructor c = cl.getDeclaredConstructor(params); ob = (Statistics) c.newInstance(wiki, type, year); reportersYear.put(multiKey, ob); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); } } // NULL return value will tell about error return ob; }
From source file:com.frostwire.android.gui.UniversalScanner.java
private static Uri nativeScanFile(Context context, String path) { try {//www . j av a 2s.c o m File f = new File(path); Class<?> clazz = Class.forName("android.media.MediaScanner"); Constructor<?> mediaScannerC = clazz.getDeclaredConstructor(Context.class); Object scanner = mediaScannerC.newInstance(context); try { Method setLocaleM = clazz.getDeclaredMethod("setLocale", String.class); setLocaleM.invoke(scanner, Locale.US.toString()); } catch (Throwable e) { e.printStackTrace(); } Field mClientF = clazz.getDeclaredField("mClient"); mClientF.setAccessible(true); Object mClient = mClientF.get(scanner); Method scanSingleFileM = clazz.getDeclaredMethod("scanSingleFile", String.class, String.class, String.class); Uri fileUri = (Uri) scanSingleFileM.invoke(scanner, f.getAbsolutePath(), "external", "data/raw"); int n = context.getContentResolver().delete(fileUri, null, null); if (n > 0) { LOG.debug("Deleted from Files provider: " + fileUri); } Field mNoMediaF = mClient.getClass().getDeclaredField("mNoMedia"); mNoMediaF.setAccessible(true); mNoMediaF.setBoolean(mClient, false); // This is only for HTC (tested only on HTC One M8) try { Field mFileCacheF = clazz.getDeclaredField("mFileCache"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, Object>()); } catch (Throwable e) { // no an HTC, I need some time to refactor this hack } try { Field mFileCacheF = clazz.getDeclaredField("mNoMediaPaths"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, String>()); } catch (Throwable e) { e.printStackTrace(); } Method doScanFileM = mClient.getClass().getDeclaredMethod("doScanFile", String.class, String.class, long.class, long.class, boolean.class, boolean.class, boolean.class); Uri mediaUri = (Uri) doScanFileM.invoke(mClient, f.getAbsolutePath(), null, f.lastModified(), f.length(), false, true, false); Method releaseM = clazz.getDeclaredMethod("release"); releaseM.invoke(scanner); return mediaUri; } catch (Throwable e) { e.printStackTrace(); return null; } }
From source file:org.apache.tajo.ws.rs.resources.outputs.RestOutputFactory.java
private static Map<String, String> load() { Map<String, String> outputClasses = Maps.newHashMap(); Set<Class> restOutputClasses = ClassUtil.findClasses(AbstractStreamingOutput.class, "org.apache.tajo.ws.rs.resources.outputs"); for (Class eachClass : restOutputClasses) { if (eachClass.isInterface() || Modifier.isAbstract(eachClass.getModifiers())) { continue; }/* w w w. ja v a2 s.c o m*/ AbstractStreamingOutput streamingOutput = null; try { streamingOutput = (AbstractStreamingOutput) eachClass .getDeclaredConstructor( new Class[] { NonForwardQueryResultScanner.class, Integer.class, Integer.class }) .newInstance(null, 0, 0); } catch (Exception e) { LOG.warn(eachClass + " cannot instantiate Function class because of " + e.getMessage(), e); continue; } String className = streamingOutput.getClass().getCanonicalName(); String headerType = streamingOutput.getClass().getAnnotation(RestReturnType.class).mimeType(); if (StringUtils.isNotEmpty(headerType)) { outputClasses.put(headerType, className); } } return outputClasses; }
From source file:org.apache.hadoop.util.ReflectionUtils.java
/** Create an object for the given class and initialize it from conf * * @param theClass class of which an object is created * @param conf Configuration// w ww . j av a 2 s .c o m * @return a new object */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> theClass, Configuration conf) { T result; try { Constructor<T> meth = theClass.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } setConf(result, conf); return result; }
From source file:com.oltpbenchmark.util.ClassUtil.java
/** Create an object for the given class and initialize it from conf * * @param theClass class of which an object is created * @param expected the expected parent class or interface * @return a new object//from w w w . j a v a 2 s . c om */ public static <T> T newInstance(Class<?> theClass, Class<T> expected) { T result; try { if (!expected.isAssignableFrom(theClass)) { throw new Exception("Specified class " + theClass.getName() + "" + "does not extend/implement " + expected.getName()); } Class<? extends T> clazz = (Class<? extends T>) theClass; Constructor<? extends T> meth = clazz.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.jfree.data.time.RegularTimePeriod.java
/** * Creates a time period that includes the specified millisecond, assuming * the given time zone.//ww w . ja va2 s.c o m * * @param c the time period class. * @param millisecond the time. * @param zone the time zone. * @param locale the locale. * * @return The time period. */ public static RegularTimePeriod createInstance(Class c, Date millisecond, TimeZone zone, Locale locale) { RegularTimePeriod result = null; try { Constructor constructor = c .getDeclaredConstructor(new Class[] { Date.class, TimeZone.class, Locale.class }); result = (RegularTimePeriod) constructor.newInstance(new Object[] { millisecond, zone, locale }); } catch (Exception e) { // do nothing, so null is returned } return result; }
From source file:org.mule.config.dsl.internal.util.MessageFactoryUtil.java
/** * Returns the message factory associated with given payload type. * * @param payload the payload/*from www .j av a 2 s .com*/ * @param muleContext the mule context * @return the proper message factory */ public static MuleMessageFactory getMessageFactory(Object payload, MuleContext muleContext) { if (!cachedMessageTypes.containsKey(payload.getClass())) { boolean found = false; for (Map.Entry<Class, Class> messageType : messageTypes.entrySet()) { if (messageType.getKey().isAssignableFrom(payload.getClass())) { cachedMessageTypes.put(payload.getClass(), messageType.getValue()); found = true; break; } } if (!found) { cachedMessageTypes.put(payload.getClass(), null); } } Class<? extends MuleMessageFactory> messageFactoryClass = cachedMessageTypes.get(payload.getClass()); if (messageFactoryClass == null) { return new DefaultMuleMessageFactory(muleContext); } try { Constructor<? extends MuleMessageFactory> x = messageFactoryClass .getDeclaredConstructor(MuleContext.class); return x.newInstance(muleContext); } catch (Exception e) { throw new ConfigurationException("Can't create message factory.", e); } }