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:org.apache.tajo.storage.OldStorageManager.java
/** * Creates a scanner instance./*from www .j a v a 2s . co m*/ * * @param theClass Concrete class of scanner * @param conf System property * @param taskAttemptId Task id * @param meta Table meta data * @param schema Input schema * @param workDir Working directory * @param <T> * @return The scanner instance */ public static <T> T newAppenderInstance(Class<T> theClass, Configuration conf, TaskAttemptId taskAttemptId, TableMeta meta, Schema schema, Path workDir) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(DEFAULT_APPENDER_PARAMS); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(new Object[] { conf, taskAttemptId, schema, meta, workDir }); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:com.lenovo.tensorhusky.common.utils.ReflectionUtils.java
/** * Create an object for the given class and initialize it from conf * * @param theClass class of which an object is created * @return a new object//from w ww .ja v a2 s. c om */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> theClass) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Creates new instance from the given type, based on given parameter types and args. * * @param type class to reflectively instantiate * @param <T> type of the class * @param parameterTypes used to find the right constructor * @param args passed to constructor * @return new instance/*from ww w . j a v a2 s .c om*/ */ public static <T> T newInstance(Class<T> type, Class[] parameterTypes, Object[] args) { try { Constructor constructor = type.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return (T) constructor.newInstance(args); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:com.ery.server.util.ReflectionUtils.java
@SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> theClass) { T result;//from ww w . j a v a 2 s.c om try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(EMPTY_ARRAY); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:tools.xor.util.ClassUtil.java
/** * Creates a new instance of the given class via the no-arg constructor, * invoking the constructor as a privileged action if it is protected or * private.//from w w w . ja v a 2 s .co m * * @param c given class * @return a new instance of the given class via the no-arg constructor * @throws Exception when creating the instance */ public static Object newInstanceAsPrivileged(final Class<?> c) throws Exception { try { return c.newInstance(); } catch (Exception e) { return AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { try { final Constructor<?> constructor = c.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (Exception e) { throw ClassUtil.wrapRun(e); } } }); } }
From source file:ca.psiphon.tunneledwebview.WebViewProxySettings.java
@SuppressWarnings("rawtypes") private static boolean setWebkitProxyICS(Context ctx, String host, int port) { try {//from w ww . ja v a 2s .co m Class webViewCoreClass = Class.forName("android.webkit.WebViewCore"); Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); if (webViewCoreClass != null && proxyPropertiesClass != null) { Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE, Object.class); Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class); if (m != null && c != null) { m.setAccessible(true); c.setAccessible(true); Object properties = c.newInstance(host, port, null); // android.webkit.WebViewCore.EventHub.PROXY_CHANGED = 193; m.invoke(null, 193, properties); return true; } } } catch (Exception e) { } catch (Error e) { } return false; }
From source file:org.assertj.db.common.AbstractTest.java
/** * Returns an instance of a {@code Change}. * // w w w. java2 s . c o m * @param dataType The type of the data on which is the change. * @param dataName The name of the data on which is the change. * @param changeType The type of the change. * @param rowAtStartPoint The row at start point. * @param rowAtEndPoint The row at end point. * @return An instance. * @throws Exception Exception */ protected static Change getChange(DataType dataType, String dataName, ChangeType changeType, Row rowAtStartPoint, Row rowAtEndPoint) throws Exception { Constructor<Change> constructor = Change.class.getDeclaredConstructor(DataType.class, String.class, ChangeType.class, Row.class, Row.class); constructor.setAccessible(true); return constructor.newInstance(dataType, dataName, changeType, rowAtStartPoint, rowAtEndPoint); }
From source file:com.rockagen.commons.util.ClassUtil.java
/** * Create new instance of specified class and type * /* w ww .j a v a 2 s .c om*/ * @param clazz class * @param accessible accessible * @param <T> t * @return instance */ public static <T> T getInstance(Class<T> clazz, boolean accessible) { if (clazz == null) return null; T t = null; try { Constructor<T> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(accessible); t = constructor.newInstance(); } catch (InstantiationException e) { log.error("{}", e.getMessage(), e); } catch (IllegalAccessException e) { log.error("{}", e.getMessage(), e); } 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 (InvocationTargetException e) { log.error("{}", e.getMessage(), e); } return t; }
From source file:com.psiphon3.psiphonlibrary.WebViewProxySettings.java
@TargetApi(Build.VERSION_CODES.KITKAT) @SuppressWarnings("rawtypes") private static boolean setWebkitProxyKitKat(Context appContext, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try {/*from w w w . ja v a 2 s . c o m*/ Class applicationClass = Class.forName("android.app.Application"); Field loadedApkField = applicationClass.getDeclaredField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkClass = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkClass.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object receiver : ((ArrayMap) receiverMap).keySet()) { Class receiverClass = receiver.getClass(); if (receiverClass.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); final String CLASS_NAME = "android.net.ProxyProperties"; Class proxyPropertiesClass = Class.forName(CLASS_NAME); Constructor constructor = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); onReceiveMethod.invoke(receiver, appContext, intent); } } } return true; } catch (ClassNotFoundException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (NoSuchFieldException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (IllegalAccessException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (IllegalArgumentException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (NoSuchMethodException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (InvocationTargetException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } catch (InstantiationException e) { MyLog.d("Exception setting WebKit proxy on KitKat through ProxyChangeListener: " + e.toString()); } return false; }
From source file:org.apache.pulsar.functions.utils.Utils.java
public static Object createInstance(String userClassName, ClassLoader classLoader) { Class<?> theCls;//from ww w. j a v a2s . c o m try { theCls = Class.forName(userClassName); } catch (ClassNotFoundException cnfe) { try { theCls = Class.forName(userClassName, true, classLoader); } catch (ClassNotFoundException e) { throw new RuntimeException("User class must be in class path", cnfe); } } Object result; try { Constructor<?> meth = theCls.getDeclaredConstructor(); meth.setAccessible(true); result = meth.newInstance(); } catch (InstantiationException ie) { throw new RuntimeException("User class must be concrete", ie); } catch (NoSuchMethodException e) { throw new RuntimeException("User class doesn't have such method", e); } catch (IllegalAccessException e) { throw new RuntimeException("User class must have a no-arg constructor", e); } catch (InvocationTargetException e) { throw new RuntimeException("User class constructor throws exception", e); } return result; }