List of usage examples for java.lang.reflect Constructor setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
A SecurityException is also thrown if this object is a Constructor object for the class Class and flag is true.
From source file:Main.java
public static String getUserAgentString(Context context) { if (userAgent == null) { try {//from w ww. ja v a 2s . c om Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class, WebView.class); constructor.setAccessible(true); try { WebSettings settings = constructor.newInstance(context, null); userAgent = settings.getUserAgentString(); } finally { constructor.setAccessible(false); } } catch (Exception e) { userAgent = new WebView(context).getSettings().getUserAgentString(); } } return userAgent; }
From source file:com.abiquo.model.util.ModelTransformer.java
public static <T> T transform(final Class sourceClass, final Class<T> targetClass, final Object template) throws Exception { Constructor<T> cons = targetClass.getDeclaredConstructor(); cons.setAccessible(true); T instance = cons.newInstance();//from w w w. j ava2 s . co m transform(sourceClass, targetClass, template, instance); return instance; }
From source file:com.hortonworks.minicluster.util.ReflectionUtils.java
/** * * @param className/*from w w w . ja v a2s. c o m*/ * @return */ public static Object newDefaultInstance(String className) { try { Class<?> clazz = Class.forName(className); Constructor<?> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); // Object instance = clazz.newInstance(); Object instance = constructor.newInstance(); return instance; } catch (Exception e) { throw new IllegalStateException( "Failed to create instance of " + className + " using default constructor", e); } }
From source file:Main.java
public static Object newInstance(String paramString, Object[] paramArrayOfObject) { try {/*from w w w .j a v a2 s . c o m*/ Constructor localConstructor = Class.forName(paramString) .getDeclaredConstructor(getArgsClasses(paramArrayOfObject)); localConstructor.setAccessible(true); return localConstructor.newInstance(paramArrayOfObject); } catch (Exception var3) { return null; } }
From source file:com.espertech.esper.util.ConstructorHelper.java
private static Constructor getRegularConstructor(Class clazz, Class parameterTypes[]) { // Try to find the matching constructor try {/* ww w. jav a2s .c o m*/ Constructor ctor = clazz.getConstructor(parameterTypes); try { ctor.setAccessible(true); } catch (SecurityException se) { // No action } return ctor; } catch (NoSuchMethodException e) { // Thats ok } return null; }
From source file:marshalsec.gadgets.C3P0WrapperConnPool.java
public static String makeC3P0UserOverridesString(String codebase, String clazz) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException { ByteArrayOutputStream b = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(b)) { Class<?> refclz = Class.forName("com.mchange.v2.naming.ReferenceIndirector$ReferenceSerialized"); //$NON-NLS-1$ Constructor<?> con = refclz.getDeclaredConstructor(Reference.class, Name.class, Name.class, Hashtable.class); con.setAccessible(true); Reference jndiref = new Reference("Foo", clazz, codebase); Object ref = con.newInstance(jndiref, null, null, null); oos.writeObject(ref);/* w w w .j a v a2 s.c o m*/ } return "HexAsciiSerializedMap:" + Hex.encodeHexString(b.toByteArray()) + ";"; //$NON-NLS-1$ }
From source file:com.hortonworks.minicluster.util.ReflectionUtils.java
/** * // w w w.ja v a2 s . co m * @param className * @return */ @SuppressWarnings("unchecked") public static <T> Constructor<T> getInvocableConstructor(String className, Class<?>... parameterTypes) { try { Class<?> clazz = Class.forName(className); Constructor<T> constructor = (Constructor<T>) clazz.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (Exception e) { throw new IllegalStateException( "Failed to create instance of " + className + " using default constructor", e); } }
From source file:com.linkedin.pinot.core.query.scheduler.QuerySchedulerFactory.java
private static @Nullable QueryScheduler getQuerySchedulerByClassName(String className, Configuration schedulerConfig, QueryExecutor queryExecutor) { try {//from ww w . j a va2s . c om Constructor<?> constructor = Class.forName(className).getDeclaredConstructor(Configuration.class, QueryExecutor.class); constructor.setAccessible(true); return (QueryScheduler) constructor.newInstance(new Object[] { schedulerConfig, queryExecutor }); } catch (Exception e) { LOGGER.error("Failed to instantiate scheduler class by name: {}", className, e); return null; } }
From source file:com.github.dryangkun.hbase.tidx.hive.HBaseTableSnapshotInputFormatUtil.java
/** * Create a bare TableSnapshotRegionSplit. Needed because Writables require a * default-constructed instance to hydrate from the DataInput. * * TODO: remove once HBASE-11555 is fixed. *//*w ww . j a va 2s. co m*/ public static InputSplit createTableSnapshotRegionSplit() { try { assertSupportsTableSnapshots(); } catch (RuntimeException e) { LOG.debug("Probably don't support table snapshots. Returning null instance.", e); return null; } try { Class<? extends InputSplit> resultType = (Class<? extends InputSplit>) Class .forName(TABLESNAPSHOTREGIONSPLIT_CLASS); Constructor<? extends InputSplit> cxtor = resultType.getDeclaredConstructor(new Class[] {}); cxtor.setAccessible(true); return cxtor.newInstance(new Object[] {}); } catch (ClassNotFoundException e) { throw new UnsupportedOperationException("Unable to find " + TABLESNAPSHOTREGIONSPLIT_CLASS, e); } catch (IllegalAccessException e) { throw new UnsupportedOperationException( "Unable to access specified class " + TABLESNAPSHOTREGIONSPLIT_CLASS, e); } catch (InstantiationException e) { throw new UnsupportedOperationException( "Unable to instantiate specified class " + TABLESNAPSHOTREGIONSPLIT_CLASS, e); } catch (InvocationTargetException e) { throw new UnsupportedOperationException( "Constructor threw an exception for " + TABLESNAPSHOTREGIONSPLIT_CLASS, e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException( "Unable to find suitable constructor for class " + TABLESNAPSHOTREGIONSPLIT_CLASS, e); } }
From source file:libepg.epg.section.sdt.ClassGetter.java
public static ServiceDescriptionTableRepeatingPart init(byte[] data) throws Throwable { try {//from w w w .j a va 2s. co m Object[] args = { data }; Class<?>[] params = { byte[].class }; Constructor<ServiceDescriptionTableRepeatingPart> constructor = ServiceDescriptionTableRepeatingPart.class .getDeclaredConstructor(params); constructor.setAccessible(true); ServiceDescriptionTableRepeatingPart target = constructor.newInstance(args); return target; } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | SecurityException ex) { LOG.fatal(ex); } catch (InvocationTargetException ex) { throw ex.getCause(); } return null; }