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.jtester.utility.ReflectionUtils.java
/** * Creates an instance of the given type * // w w w . j a va 2s . c om * @param <T> * The type of the instance * @param type * The type of the instance * @param bypassAccessibility * If true, no exception is thrown if the parameterless * constructor is not public * @param argumentTypes * The constructor arg types, not null * @param arguments * The constructor args, not null * @return An instance of this type * @throws JTesterException * If an instance could not be created */ @SuppressWarnings("rawtypes") public static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments) { if (type.isMemberClass() && !isStatic(type.getModifiers())) { throw new JTesterException( "Creation of an instance of a non-static innerclass is not possible using reflection. The type " + type.getSimpleName() + " is only known in the context of an instance of the enclosing class " + type.getEnclosingClass().getSimpleName() + ". Declare the innerclass as static to make construction possible."); } try { Constructor<T> constructor = type.getDeclaredConstructor(argumentTypes); if (bypassAccessibility) { constructor.setAccessible(true); } return constructor.newInstance(arguments); } catch (InvocationTargetException e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e.getCause()); } catch (Exception e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e); } }
From source file:com.impetus.kundera.utils.KunderaCoreUtils.java
/** * @param clazz/*from w ww .ja v a 2 s . c o m*/ * @return */ public static Object createNewInstance(Class clazz) { Object target = null; try { Constructor[] constructors = clazz.getDeclaredConstructors(); for (Constructor constructor : constructors) { if ((Modifier.isProtected(constructor.getModifiers()) || Modifier.isPublic(constructor.getModifiers())) && constructor.getParameterTypes().length == 0) { constructor.setAccessible(true); target = constructor.newInstance(); constructor.setAccessible(false); break; } } return target; } catch (InstantiationException iex) { logger.error("Error while creating an instance of {} .", clazz); throw new PersistenceException(iex); } catch (IllegalAccessException iaex) { logger.error("Illegal Access while reading data from {}, Caused by: .", clazz, iaex); throw new PersistenceException(iaex); } catch (Exception e) { logger.error("Error while creating an instance of {}, Caused by: .", clazz, e); throw new PersistenceException(e); } }
From source file:org.bremersee.common.exception.ExceptionRegistry.java
private static Throwable findThrowableByMessageAndCause(final Class<? extends Throwable> clazz, final String message, final Throwable cause) { try {//w w w .ja v a 2 s .c om Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(String.class, Throwable.class); if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(message, cause); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR | InvocationTargetException e) { // NOSONAR if (LOG.isDebugEnabled()) { LOG.debug("Finding throwable by message and cause failed. Class [" + clazz.getName() // NOSONAR + "] has no suitable constructor: " + e.getMessage() // NOSONAR + " (" + e.getClass().getName() + ")."); // NOSONAR } return null; } }
From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java
/** * Make the given constructor accessible, explicitly setting it accessible * if necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active).//w w w . j a va 2s . co m * @param ctor the constructor to make accessible * @see java.lang.reflect.Constructor#setAccessible */ public static void makeAccessible(Constructor<?> ctor) { if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { ctor.setAccessible(true); } }
From source file:org.apache.tajo.storage.AbstractStorageManager.java
/** * create a scanner instance./*from w w w.j a v a 2 s . c om*/ */ public static <T> T newScannerInstance(Class<T> theClass, Configuration conf, Schema schema, TableMeta meta, Fragment fragment) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (meth == null) { meth = theClass.getDeclaredConstructor(DEFAULT_SCANNER_PARAMS); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(new Object[] { conf, schema, meta, fragment }); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.apache.tajo.storage.AbstractStorageManager.java
/** * create a scanner instance.// w w w . ja v a2 s .c om */ public static <T> T newAppenderInstance(Class<T> theClass, Configuration conf, TableMeta meta, Schema schema, Path path) { 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, schema, meta, path }); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:info.guardianproject.netcipher.web.WebkitProxy.java
private static boolean sendProxyChangedIntent(Context ctx, String host, int port) { try {/*from w w w . j a v a 2 s.c o m*/ Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); if (proxyPropertiesClass != null) { Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class); if (c != null) { c.setAccessible(true); Object properties = c.newInstance(host, port, null); Intent intent = new Intent(android.net.Proxy.PROXY_CHANGE_ACTION); intent.putExtra("proxy", (Parcelable) properties); ctx.sendBroadcast(intent); } } } catch (Exception e) { Log.e("ProxySettings", "Exception sending Intent ", e); } catch (Error e) { Log.e("ProxySettings", "Exception sending Intent ", e); } return false; }
From source file:org.cellprofiler.preferences.CellProfilerPreferences.java
/** * Get the preferences factory supplied by the JRE or * provided as a service.//from w w w. j av a 2 s . co m * * @return the default preferences factory. */ static private PreferencesFactory getJREPreferencesFactory() { synchronized (lock) { if (delegatePreferencesFactory == null) { do { /* * First, see if there is a PreferencesFactory * provided as a service. */ final ServiceLoader<PreferencesFactory> pfServiceLoader = ServiceLoader .loadInstalled(PreferencesFactory.class); final Iterator<PreferencesFactory> pfIter = pfServiceLoader.iterator(); if (pfIter.hasNext()) { delegatePreferencesFactory = pfIter.next(); break; } /* * Next, try the WindowsPreferencesFactory if OS is Windows. */ String pfName = (SystemUtils.IS_OS_WINDOWS) ? "java.util.prefs.WindowsPreferencesFactory" : "java.util.prefs.FilePreferencesFactory"; try { Class<?> pfClass = Class.forName("java.util.prefs.WindowsPreferencesFactory", false, null); Class<?> pfFuckYou = Class.forName("java.util.prefs.WindowsPreferences", true, null); Constructor<?>[] pfConstructors = pfClass.getDeclaredConstructors(); for (Constructor<?> c : pfConstructors) { if (c.getParameterTypes().length == 0) { /* * Bad boy - it's package-private AND I CALL IT ANYWAY BAH HA HA HA HA HA HA */ c.setAccessible(true); delegatePreferencesFactory = (PreferencesFactory) c.newInstance(new Object[0]); break; } } break; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * And as a last resort, there's always our headless * preferences factory. */ delegatePreferencesFactory = new HeadlessPreferencesFactory(); } while (false); } } return delegatePreferencesFactory; }
From source file:info.guardianproject.netcipher.web.WebkitProxy.java
@TargetApi(19) private static boolean setKitKatProxy(String appClass, Context appContext, String host, int port) { //Context appContext = webView.getContext().getApplicationContext(); if (host != null) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); }/*from www . j av a 2 s. c o m*/ try { Class applictionCls = Class.forName(appClass); Field loadedApkField = applictionCls.getField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkCls = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkCls.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object rec : ((ArrayMap) receiverMap).keySet()) { Class clazz = rec.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); if (host != null) { /*********** optional, may be need in future *************/ final String CLASS_NAME = "android.net.ProxyProperties"; Class cls = Class.forName(CLASS_NAME); Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); /*********** optional, may be need in future *************/ } onReceiveMethod.invoke(rec, appContext, intent); } } } return true; } catch (ClassNotFoundException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (NoSuchFieldException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (NoSuchMethodException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (InvocationTargetException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (InstantiationException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } return false; }
From source file:info.guardianproject.netcipher.webkit.WebkitProxy.java
@TargetApi(19) private static boolean setKitKatProxy(String appClass, Context appContext, String host, int port) { //Context appContext = webView.getContext().getApplicationContext(); if (host != null) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", Integer.toString(port)); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", Integer.toString(port)); }// w w w . j a v a 2 s.c om try { Class applictionCls = Class.forName(appClass); Field loadedApkField = applictionCls.getField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkCls = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkCls.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object rec : ((ArrayMap) receiverMap).keySet()) { Class clazz = rec.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); if (host != null) { /*********** optional, may be need in future *************/ final String CLASS_NAME = "android.net.ProxyProperties"; Class cls = Class.forName(CLASS_NAME); Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); /*********** optional, may be need in future *************/ } onReceiveMethod.invoke(rec, appContext, intent); } } } return true; } catch (ClassNotFoundException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (NoSuchFieldException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (IllegalAccessException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (NoSuchMethodException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (InvocationTargetException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } catch (InstantiationException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionAsString = sw.toString(); Log.v(TAG, e.getMessage()); Log.v(TAG, exceptionAsString); } return false; }