List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Main.java
private static Object getInstance(String name, Class<?> classParas[], Object paras[]) { Object o = null;/*from w w w .j a v a 2 s . c o m*/ try { Class<?> c = Class.forName(name); Constructor<?> con = c.getConstructor(classParas); o = con.newInstance(paras); } catch (Exception ex) { } return o; }
From source file:Main.java
public static void loadContext(android.content.Context context, Field field, Object object) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {//ww w . ja v a2 s . co m 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: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./*from w w w . ja va 2 s . 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 <T> T loadObject(Element e, Class<T> type) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { if (e != null) { String c = e.getAttribute("type"); Class<T> clazz = (Class) Class.forName(c); Constructor<T> con = clazz.getConstructor((Class[]) null); return con.newInstance(); }/*from ww w. j ava2 s . c o m*/ return null; }
From source file:Main.java
public static Object newHandlerInstance(String clzPath, Class<?>[] parameterTypes, Object[] args) { try {//from w w w . j a v a 2s. c o m Class<?> clz = Class.forName(clzPath); Constructor<?> constructor = clz.getConstructor(parameterTypes); return constructor.newInstance(args); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }
From source file:ReflectionUtils.java
public static boolean hasDeclaredConstructor(Class targetClass, Class[] partypes) { Constructor constructor = null; try {/*from ww w . j av a 2 s . c o m*/ constructor = targetClass.getConstructor(partypes); } catch (Exception e) { e.printStackTrace(); } return constructor != null; }
From source file:Main.java
public static List<String> getDexEntries() { try {//from w ww .jav a 2 s . c o m Object app = Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null); Object codePath = Class.forName("android.app.Application").getMethod("getPackageCodePath").invoke(app); Class<?> dexFileClass = Class.forName("dalvik.system.DexFile"); Object dexFile = dexFileClass.getConstructor(String.class).newInstance(codePath); Enumeration<String> entries = (Enumeration<String>) dexFileClass.getMethod("entries").invoke(dexFile); List<String> dexEntries = new LinkedList<>(); while (entries.hasMoreElements()) { String entry = entries.nextElement(); entry = entry.replace('.', '/') + ".class"; dexEntries.add(entry); } dexFileClass.getMethod("close").invoke(dexFile); return dexEntries; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Checks whether a {@code Constructor} object with no parameter types is specified * by the invoked {@code Class} object or not. * * @param clazz the {@code Class} object whose constructors are checked. * @return {@code true} if a {@code Constructor} object with no parameter types is specified. * @throws SecurityException If a security manager, <i>s</i> is present and any of the * following conditions is met:/*from w w w. j a va 2 s . c om*/ * <ul> * <li> invocation of * {@link SecurityManager#checkMemberAccess * s.checkMemberAccess(this, Member.PUBLIC)} denies * access to the constructor * * <li> the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class * </ul> * * @see {@link Class#getConstructor(Class...)} */ public static boolean hasDefaultConstructor(Class<?> clazz) throws SecurityException { Class<?>[] empty = {}; try { clazz.getConstructor(empty); } catch (NoSuchMethodException e) { return false; } return true; }
From source file:Main.java
public static Constructor getConstructor(Class<?> targetClass, Class<?>... parameterTypes) throws NoSuchMethodException { Constructor constructor = targetClass.getConstructor(parameterTypes); constructor.setAccessible(true);/*from www. j ava2s .c om*/ return constructor; }
From source file:Main.java
/** * Creates a new empty instance of the provided collection * // www.j av a 2s .com * @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; } }