List of usage examples for java.lang Class getConstructor
@CallerSensitive public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Main.java
/** * Because of a BUG of Android (API 13-), * get signature info by using "getPackageArchiveInfo" of "PackageManager" * always causes "NullPointerException". * Lack of code in method "getPackageArchiveInfo": * if ((flags & GET_SIGNATURES) != 0) * {/*www. j a v a 2s . c om*/ * packageParser.collectCertificates(pkg, 0); * } */ @SuppressWarnings("unchecked") public static PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) { try { Class packageParserClass = Class.forName("android.content.pm.PackageParser"); Class[] innerClasses = packageParserClass.getDeclaredClasses(); Class packageParserPackageClass = null; for (Class innerClass : innerClasses) { if (0 == innerClass.getName().compareTo("android.content.pm.PackageParser$Package")) { packageParserPackageClass = innerClass; break; } } Constructor packageParserConstructor = packageParserClass.getConstructor(String.class); Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class, String.class, DisplayMetrics.class, int.class); Method collectCertificatesMethod = packageParserClass.getDeclaredMethod("collectCertificates", packageParserPackageClass, int.class); Method generatePackageInfoMethod = packageParserClass.getDeclaredMethod("generatePackageInfo", packageParserPackageClass, int[].class, int.class, long.class, long.class); packageParserConstructor.setAccessible(true); parsePackageMethod.setAccessible(true); collectCertificatesMethod.setAccessible(true); generatePackageInfoMethod.setAccessible(true); Object packageParser = packageParserConstructor.newInstance(archiveFilePath); DisplayMetrics displayMetrics = new DisplayMetrics(); displayMetrics.setToDefaults(); final File sourceFile = new File(archiveFilePath); Object pkg = parsePackageMethod.invoke(packageParser, sourceFile, archiveFilePath, displayMetrics, 0); if (pkg == null) { return null; } if ((flags & PackageManager.GET_SIGNATURES) != 0) { collectCertificatesMethod.invoke(packageParser, pkg, 0); } return (PackageInfo) generatePackageInfoMethod.invoke(null, pkg, null, flags, 0, 0); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:azkaban.common.utils.Utils.java
/** * Call the class constructor with the given arguments * //from w w w. j av a 2 s . c o m * @param c The class * @param args The arguments * @return The constructed object */ public static Object callConstructor(Class<?> c, Class<?>[] argTypes, Object[] args) { try { Constructor<?> cons = c.getConstructor(argTypes); return cons.newInstance(args); } catch (InvocationTargetException e) { throw getCause(e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } catch (InstantiationException e) { throw new IllegalStateException(e); } }
From source file:net.buffalo.protocal.util.ClassUtil.java
public static Object convertValue(Object value, Class targetType) { if (value.getClass().equals(targetType)) return value; if (targetType.isPrimitive()) { targetType = getWrapperClass(targetType); }// ww w. ja v a2s. co m if (targetType.isAssignableFrom(value.getClass())) return value; if ((value instanceof String || value instanceof Number) && Number.class.isAssignableFrom(targetType)) { try { Constructor ctor = targetType.getConstructor(new Class[] { String.class }); return ctor.newInstance(new Object[] { value.toString() }); } catch (Exception e) { LOGGER.error("convert type error", e); throw new RuntimeException( "Cannot convert from " + value.getClass().getName() + " to " + targetType, e); } } if (targetType.isArray() && Collection.class.isAssignableFrom(value.getClass())) { Collection collection = (Collection) value; Object array = Array.newInstance(targetType.getComponentType(), collection.size()); int i = 0; for (Iterator iter = collection.iterator(); iter.hasNext();) { Object val = iter.next(); Array.set(array, i++, val); } return array; } if (Collection.class.isAssignableFrom(targetType) && value.getClass().isArray()) { return Arrays.asList((Object[]) value); } throw new IllegalArgumentException( "Cannot convert from " + value.getClass().getName() + " to " + targetType); }
From source file:com.google.gdt.eclipse.designer.mac.BrowserShellMacImplCocoa.java
private static Image createImageFromHandle(long imageHandle, int width, int height) throws Exception { if (imageHandle != 0) { Class<?> nsImageClass = Class.forName("org.eclipse.swt.internal.cocoa.NSImage"); Object handleObject;//w w w. j a va 2 s .co m Class<?> handleClass; if (SystemUtils.OS_ARCH.indexOf("64") != -1) { handleClass = long.class; handleObject = new Long(imageHandle); } else { handleClass = int.class; handleObject = new Integer((int) imageHandle); } Constructor<?> constructor = nsImageClass.getConstructor(handleClass); Object nsImage = constructor.newInstance(handleObject); // Create a temporary image using the captured image's handle Class<?> NSImageClass = Class.forName("org.eclipse.swt.internal.cocoa.NSImage"); Method method = Image.class.getDeclaredMethod("cocoa_new", new Class[] { Device.class, int.class, NSImageClass }); method.setAccessible(true); Image tempImage = (Image) method.invoke(null, new Object[] { Display.getCurrent(), new Integer(SWT.BITMAP), nsImage }); // Create the result image Image image = new Image(Display.getCurrent(), width, height); // Manually copy because the image's data handle isn't available GC gc = new GC(tempImage); gc.copyArea(image, 0, 0); gc.dispose(); // Dispose of the temporary image allocated in the native call tempImage.dispose(); return image; } // prevent failing return new Image(Display.getCurrent(), 1, 1); }
From source file:edu.usc.pgroup.floe.utils.Utils.java
/** * Use reflection to create an instance of the given class. * @param fqdnClassName the fully qualified class name. * @param params types of parameters to query for the constructor. * @return a new instance of the given class. NULL if there was an error * creating the instance.//from ww w.j a v a 2 s . co m */ public static Constructor<?> getConstructor(final String fqdnClassName, final Class<?>... params) { try { Class<?> klass = Class.forName(fqdnClassName); return klass.getConstructor(params); } catch (ClassNotFoundException e) { LOGGER.error("Could not create an instance of {}, Exception:{}", fqdnClassName, e); } catch (NoSuchMethodException e) { LOGGER.error("No such constructor found, Exception:{}", fqdnClassName, e); } return null; }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Converts object value to given type. Converts primitives to their wrappers. * Converts strings to numbers./*from ww w. j a va 2 s.c om*/ * * @param value value to convert * @param type type to convert to * @param <T> type * @return converted value * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException */ public static <T> T convertValue(Object value, Class<T> type) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { Class clazz; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } else { clazz = type; } if (null == value || clazz.isAssignableFrom(value.getClass())) { return (T) value; } Constructor<T> constructor = clazz.getConstructor(new Class[] { String.class }); return constructor.newInstance(value.toString()); }
From source file:com.mobilyzer.util.MeasurementJsonConvertor.java
public static MeasurementTask makeMeasurementTaskFromJson(JSONObject json) throws IllegalArgumentException { try {// ww w. j ava2 s . c om String type = String.valueOf(json.getString("type")); Class taskClass = MeasurementTask.getTaskClassForMeasurement(type); Method getDescMethod = taskClass.getMethod("getDescClass"); // The getDescClassForMeasurement() is static and takes no arguments Class descClass = (Class) getDescMethod.invoke(null, (Object[]) null); MeasurementDesc measurementDesc = (MeasurementDesc) gson.fromJson(json.toString(), descClass); Object cstParam = measurementDesc; Constructor<MeasurementTask> constructor = taskClass.getConstructor(MeasurementDesc.class); return constructor.newInstance(cstParam); } catch (JSONException e) { throw new IllegalArgumentException(e); } catch (SecurityException e) { Logger.w(e.getMessage()); throw new IllegalArgumentException(e); } catch (NoSuchMethodException e) { Logger.w(e.getMessage()); throw new IllegalArgumentException(e); } catch (IllegalArgumentException e) { Logger.w(e.getMessage()); throw new IllegalArgumentException(e); } catch (InstantiationException e) { Logger.w(e.getMessage()); throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { Logger.w(e.getMessage()); throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { Logger.w(e.toString()); throw new IllegalArgumentException(e); } }
From source file:com.austin.base.commons.util.ReflectUtil.java
/** * @param className ??/*from ww w . j a v a 2 s . co m*/ * @param args 17? * @return */ public static Object newInstance(String className, Object[] args) { try { Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i > j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = newoneClass.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception ex) { log.error(ex); return null; } }
From source file:com.open.cas.shiro.util.ReflectionUtils.java
/** * /*from www .ja v a 2s. com*/ * * @param className * ?? * @param args * ? * @return * @throws Exception */ public static Object newInstance(Class clazz, Object[] args) { try { Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = clazz.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }
From source file:com.open.cas.shiro.util.ReflectionUtils.java
public static Object newInstance(String className, Object[] args) { try {/*w w w.jav a 2s.c o m*/ Class newoneClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Constructor cons = newoneClass.getConstructor(argsClass); return cons.newInstance(args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }