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:Main.java
private static <T> T newInstance(Context context, Class clazz, Class[] constructorSig, Object... arguments) { try {/*from w ww . ja v a 2 s . com*/ Constructor<?> constructor = clazz.getConstructor(constructorSig); return (T) constructor.newInstance(arguments); } catch (Exception e) { Log.w(LOG_TAG, "Cannot instantiate class: " + clazz.getName(), e); } return null; }
From source file:Main.java
/** * Creates a new timer whose associated thread has the specified name in Java 1.5. * /* w ww . j av a 2 s .c om*/ * @param name the name of the associated thread * */ public static Timer createTimer(String name) { try { Constructor c = Timer.class.getConstructor(new Class[] { String.class }); return (Timer) c.newInstance(new Object[] { name }); } catch (Throwable e) { // In cany case create new Timer return new Timer(); } }
From source file:Main.java
/** * Creates a new empty instance of the provided collection * /* w w w . j a v a2 s . c o m*/ * @param <T> * @param in * @return */ public static <T> Collection<T> createEmpty(Collection<?> in) { Class<?> originalClass = in.getClass(); try { Constructor<?> originalConstructor = originalClass.getConstructor(new Class[0]); return (Collection<T>) originalConstructor.newInstance(new Object[0]); } catch (IllegalArgumentException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } catch (SecurityException e) { } catch (NoSuchMethodException e) { } finally { return null; } }
From source file:Main.java
/** * Returns a filtered copy of the collection <code>coll</code>. * * @param coll the collection to filter. * @param p the predicate to filter by./* w w w . j a v a2s .c o m*/ * @return a new collection. */ /*public static <T> Collection<T> filter(Collection<T> coll, Predicate<T> p){ Collection<T> c2 = newCollection(coll); for(T obj: coll) if(p.apply(obj)) c2.add(obj); return c2; }*/ private static <T> Collection<T> cloneCollection(Collection<T> coll) { try { Class cl = coll.getClass(); Constructor con = cl.getConstructor(new Class[] { Collection.class }); return (Collection<T>) con.newInstance(new Object[] { coll }); } catch (Exception e) { if (coll instanceof List) return new LinkedList<T>(coll); if (coll instanceof Set) return new HashSet<T>(coll); throw new RuntimeException("Cannot handle this collection"); } }
From source file:Main.java
public static void loadContext(android.content.Context context, Field field, Object object) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {/* w w w . j av a2 s .c om*/ field.setAccessible(true); Class fType = (Class) field.getGenericType(); Constructor constructor = fType.getConstructor(android.content.Context.class); if (constructor != null) { field.set(object, constructor.newInstance(context)); } }
From source file:ee.ria.xroad.proxy.clientproxy.HandlerLoader.java
private static Handler instantiate(Class<? extends Handler> handlerClass, HttpClient client) throws Exception { try {//w w w.jav a 2 s . c om Constructor<? extends Handler> constructor = handlerClass.getConstructor(HttpClient.class); return constructor.newInstance(client); } catch (NoSuchMethodException e) { throw new Exception("Handler must have constructor taking " + "1 parameter (" + HttpClient.class + ")", e); } }
From source file:Main.java
public static Object newInstance(String paramString, Object[] paramArrayOfObject, Class<?>[] paramArrayOfClass) { try {//from w w w .ja v a2 s . c o m Constructor e = Class.forName(paramString).getDeclaredConstructor(paramArrayOfClass); e.setAccessible(true); return e.newInstance(paramArrayOfObject); } catch (Exception var4) { var4.printStackTrace(); return null; } }
From source file:Main.java
public static <T extends Object> T newInstance(Class<T> cl, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Constructor<T> constructor = cl.getDeclaredConstructor(new Class[0]); boolean accessible = constructor.isAccessible(); constructor.setAccessible(true);/*from ww w. ja v a 2 s .co m*/ T t = constructor.newInstance(args); constructor.setAccessible(accessible); return t; }
From source file:Main.java
public static Map<String, Object> getProperties(Element paramElement) { HashMap<String, Object> localHashMap = new HashMap<String, Object>(); Element[] arrayOfElement = getChildrenByName(paramElement, "property"); for (int i = 0; i < arrayOfElement.length; ++i) { String str1 = arrayOfElement[i].getAttribute("name"); String str2 = arrayOfElement[i].getAttribute("type"); String str3 = getText(arrayOfElement[i]); try {/*from w w w.j a v a 2 s .c om*/ Class<?> localClass = Class.forName(str2); Constructor<?> localConstructor = localClass.getConstructor(new Class[] { String.class }); Object localObject = localConstructor.newInstance(new Object[] { str3 }); localHashMap.put(str1, localObject); } catch (Exception localException) { System.err.println( "Unable to parse property '" + str1 + "'='" + str3 + "': " + localException.toString()); } } return localHashMap; }
From source file:org.eclipse.virgo.ide.ui.editors.PdeCompatibilityUtil.java
public static IEditorInput createSystemFileEditorInput(File file) { if (isEclipse34) { Class<?> systemFileEditorInputClass; try {//from w w w. j a v a2 s. co m systemFileEditorInputClass = Class .forName("org.eclipse.pde.internal.ui.editor.SystemFileEditorInput"); Constructor<?> constructor = systemFileEditorInputClass.getConstructor(File.class); return (IEditorInput) constructor.newInstance(file); } catch (ClassNotFoundException e) { isEclipse34 = false; } catch (Throwable e) { SpringCore.log(new Status(IStatus.ERROR, ServerIdeUiPlugin.PLUGIN_ID, "Failed to create instance of SystemFileEditorInput")); isEclipse34 = false; } } return null; }