List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:me.piebridge.android.preference.PreferenceFragment.java
/** * return XmlPullParser/* w w w .ja v a2 s . com*/ * @param xml compiled XML encoded in base64 * @return XmlPullParser */ public static XmlPullParser getParser(String xml) { try { byte[] data = Base64.decode(xml, Base64.DEFAULT); // XmlBlock block = new XmlBlock(LAYOUT.getBytes("UTF-8")); Class<?> clazz = Class.forName("android.content.res.XmlBlock"); Constructor<?> constructor = clazz.getDeclaredConstructor(byte[].class); constructor.setAccessible(true); Object block = constructor.newInstance(data); // XmlPullParser parser = block.newParser(); Method method = clazz.getDeclaredMethod("newParser"); method.setAccessible(true); return (XmlPullParser) method.invoke(block); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (java.lang.InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
From source file:me.piebridge.android.preference.PreferenceFragment.java
@SuppressWarnings("unchecked") public static <T> T callConstructor(Class<T> returnType, Class<?>[] parameterTypes, Object[] args) { try {// w w w . ja v a 2s . c om Constructor<?> constructor = returnType.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return (T) constructor.newInstance(args); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (java.lang.InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
From source file:org.hibernate.internal.util.ReflectHelper.java
/** * Retrieve the default (no arg) constructor from the given class. * * @param clazz The class for which to retrieve the default ctor. * @return The default constructor./* w w w .j av a 2 s. c om*/ * @throws PropertyNotFoundException Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???) */ public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException { if (isAbstractClass(clazz)) { return null; } try { Constructor<T> constructor = clazz.getDeclaredConstructor(NO_PARAM_SIGNATURE); constructor.setAccessible(true); return constructor; } catch (NoSuchMethodException nme) { throw new PropertyNotFoundException( "Object class [" + clazz.getName() + "] must declare a default (no-argument) constructor"); } }
From source file:org.projectforge.common.BeanHelper.java
public static Object newInstance(final Class<?> clazz, final Class<?>[] paramTypes, final Object... params) { Constructor<?> constructor = null; try {/* w ww . ja va2 s. c o m*/ constructor = clazz.getDeclaredConstructor(paramTypes); } catch (final SecurityException ex) { logInstantiationException(ex, clazz); } catch (final NoSuchMethodException ex) { logInstantiationException(ex, clazz); } constructor.setAccessible(true); try { return constructor.newInstance(params); } catch (final IllegalArgumentException ex) { logInstantiationException(ex, clazz); } catch (final InstantiationException ex) { logInstantiationException(ex, clazz); } catch (final IllegalAccessException ex) { logInstantiationException(ex, clazz); } catch (final InvocationTargetException ex) { logInstantiationException(ex, clazz); } return null; }
From source file:com.rockagen.commons.util.ClassUtil.java
/** * Create new instance of specified class and type * /* w w w .j a va2 s. c o m*/ * @param clazz class * @param accessible accessible * @param parameterTypes parameter types * @param paramValue param value * @param <T> t * @return instance */ public static <T> T getInstance(Class<T> clazz, boolean accessible, Class<?>[] parameterTypes, Object[] paramValue) { if (clazz == null) return null; T t = null; try { if (parameterTypes != null && paramValue != null) { Constructor<T> constructor = clazz.getDeclaredConstructor(parameterTypes); Object[] obj = new Object[parameterTypes.length]; System.arraycopy(paramValue, 0, obj, 0, parameterTypes.length); constructor.setAccessible(accessible); t = constructor.newInstance(obj); } } catch (SecurityException e) { log.error("{}", e.getMessage(), e); } catch (NoSuchMethodException e) { log.error("{}", e.getMessage(), e); } catch (IllegalArgumentException e) { log.error("{}", e.getMessage(), e); } catch (InstantiationException e) { log.error("{}", e.getMessage(), e); } catch (IllegalAccessException e) { log.error("{}", e.getMessage(), e); } catch (InvocationTargetException e) { log.error("{}", e.getMessage(), e); } return t; }
From source file:cascading.util.Util.java
public static Object createProtectedObject(Class type, Object[] parameters, Class[] parameterTypes) { try {/*from w w w. jav a2s . c o m*/ Constructor constructor = type.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor.newInstance(parameters); } catch (Exception exception) { exception.printStackTrace(); throw new FlowException("unable to instantiate type: " + type.getName(), exception); } }
From source file:Main.java
public static Constructor<?> findConstructorExact(Class<?> clazz, Class<?>... parameterTypes) { StringBuilder sb = new StringBuilder(clazz.getName()); sb.append(getParametersString(parameterTypes)); sb.append("#exact"); String fullConstructorName = sb.toString(); if (constructorCache.containsKey(fullConstructorName)) { Constructor<?> constructor = constructorCache.get(fullConstructorName); if (constructor == null) throw new NoSuchMethodError(fullConstructorName); return constructor; }// w w w . j a v a2 s . c om try { Constructor<?> constructor = clazz.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); constructorCache.put(fullConstructorName, constructor); return constructor; } catch (NoSuchMethodException e) { constructorCache.put(fullConstructorName, null); throw new NoSuchMethodError(fullConstructorName); } }
From source file:org.mrgeo.utils.HadoopUtils.java
private static void loadJobContextClass() { try {//w ww.j a va2 s.c o m final Class<?> jc = Class.forName("org.apache.hadoop.mapreduce.JobContext"); final Class<?>[] argTypes = { Configuration.class, JobID.class }; jobContext = jc.getDeclaredConstructor(argTypes); return; } catch (final ClassNotFoundException e) { // TaskAttemptContext is not a class, could be Hadoop 2.0 } catch (final SecurityException e) { // Exception, we'll try Hadoop 2.0 just in case... } catch (final NoSuchMethodException e) { // Exception, we'll try Hadoop 2.0 just in case... } try { final Class<?> jci = Class.forName("org.apache.hadoop.mapreduce.task.JobContextImpl"); final Class<?>[] argTypes = { Configuration.class, JobID.class }; jobContext = jci.getDeclaredConstructor(argTypes); return; } catch (final ClassNotFoundException e) { } catch (final SecurityException e) { } catch (final NoSuchMethodException e) { } log.error("ERROR! Can not find a JobContext implementation class!"); jobContext = null; }
From source file:org.mrgeo.utils.HadoopUtils.java
private static void loadTaskAttemptClass() { try {/* ww w . j a va 2 s. c om*/ final Class<?> tac = Class.forName("org.apache.hadoop.mapreduce.TaskAttemptContext"); final Class<?>[] argTypes = { Configuration.class, TaskAttemptID.class }; taskAttempt = tac.getDeclaredConstructor(argTypes); return; } catch (final ClassNotFoundException e) { // TaskAttemptContext is not a class, could be Hadoop 2.0 } catch (final SecurityException e) { // Exception, we'll try Hadoop 2.0 just in case... } catch (final NoSuchMethodException e) { // Exception, we'll try Hadoop 2.0 just in case... } try { // Class<?> taci = Class.forName("org.apache.hadoop.mapreduce.TaskAttemptContextImpl"); final Class<?> taci = Class.forName("org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl"); final Class<?>[] argTypes = { Configuration.class, TaskAttemptID.class }; taskAttempt = taci.getDeclaredConstructor(argTypes); return; } catch (final ClassNotFoundException e) { } catch (final SecurityException e) { } catch (final NoSuchMethodException e) { } log.error("ERROR! Can not find a TaskAttempt implementation class!"); taskAttempt = null; }
From source file:net.minecraftforge.common.util.EnumHelper.java
private static Object getConstructorAccessor(Class<?> enumClass, Class<?>[] additionalParameterTypes) throws Exception { Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2]; parameterTypes[0] = String.class; parameterTypes[1] = int.class; System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length); return newConstructorAccessor.invoke(reflectionFactory, enumClass.getDeclaredConstructor(parameterTypes)); }