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
private static boolean setWebkitProxyICS(Context ctx, String host, int port) throws Exception { // PSIPHON: added support for Android 4.x WebView proxy try {//from w w w . 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; } else return false; } } catch (Exception e) { Log.e("ProxySettings", "Exception setting WebKit proxy through android.net.ProxyProperties: " + e.toString()); } catch (Error e) { Log.e("ProxySettings", "Exception setting WebKit proxy through android.webkit.Network: " + e.toString()); } return false; }
From source file:libepg.common.descriptor.Descriptors.java
public static final Descriptor init(byte[] data) throws InvocationTargetException { try {//w w w . ja v a 2s. co m Object[] args = { data }; Class<?>[] params = { byte[].class }; Constructor<Descriptor> constructor = Descriptor.class.getDeclaredConstructor(params); constructor.setAccessible(true); Descriptor target = constructor.newInstance(args); return target; } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | SecurityException ex) { LOG.fatal(ex); } return null; }
From source file:com.knockturnmc.api.util.ConfigurationUtils.java
/** * Loads a mapped {@link Properties} file and applies the mapping provided by the {@link NamedProperties}. * If the desired file was not found in the datafolder, a default file will be copied from the classpath. * * @param classLoader the classloader to use for the default file * @param filename the filename//from w ww.ja v a 2 s. c om * @param datafolder the datafolder * @param mapping the mapped file * @param <T> the type of the mapped file * @return the loaded configuration mapping */ public static <T extends NamedProperties> T loadConfiguration(ClassLoader classLoader, String filename, File datafolder, Class<? extends T> mapping) { try { File file = getConfigFile(classLoader, filename, datafolder); Constructor<? extends T> constructor = mapping.getDeclaredConstructor(); constructor.setAccessible(true); T properties = constructor.newInstance(); FileInputStream stream = new FileInputStream(file); properties.load(stream); stream.close(); OutputStream fos = new FileOutputStream(file); properties.store(fos, "Configuration for " + filename); fos.close(); return properties; } catch (Exception e) { logger.warn("Failed to load configuration", e); throw new RuntimeException(e); } }
From source file:com.qualogy.qafe.business.integration.adapter.MappingAdapter.java
private static Object createServiceObj(String className, ClassLoader classLoader) { Object result = null;/*from ww w . j a v a 2 s . c om*/ try { if (className == null) throw new UnableToAdaptException("Adapter states null outputclass"); Class<?> clazz = classLoader.loadClass(className); Constructor<?> c = clazz.getConstructor(new Class[0]); c.setAccessible(true); result = c.newInstance(new Object[0]); } catch (InstantiationException e) { throw new UnableToAdaptException( "Cannot instantiate [" + className + "], hint: define a default constructor", e); } catch (IllegalAccessException e) { throw new UnableToAdaptException(e); } catch (Exception e) { throw new UnableToAdaptException(e); } return result; }
From source file:com.dianping.squirrel.client.util.ClassUtils.java
@SuppressWarnings("unchecked") private static <T> Constructor<T> getMatchingDeclaredConstructor(Class<T> clazz, Class<?>[] parameterTypes) { try {/* www. jav a2 s .c o m*/ Constructor<T> ctor = clazz.getConstructor(parameterTypes); try { ctor.setAccessible(true); } catch (SecurityException se) { // do nothing } return ctor; } catch (NoSuchMethodException e) { } int paramSize = parameterTypes.length; Constructor<?>[] ctors = clazz.getDeclaredConstructors(); for (int i = 0, size = ctors.length; i < size; i++) { Class<?>[] ctorParams = ctors[i].getParameterTypes(); int ctorParamSize = ctorParams.length; if (ctorParamSize == paramSize) { boolean match = true; for (int n = 0; n < ctorParamSize; n++) { if (!MethodUtils.isAssignmentCompatible(ctorParams[n], parameterTypes[n])) { match = false; break; } } if (match) { Constructor<?> ctor = getDeclaredConstructor(ctors[i]); if (ctor != null) { return (Constructor<T>) ctor; } } } } return null; }
From source file:org.jumbune.org.apache.hadoop.tools.rumen.HistoryEventEmitter.java
protected static Counters parseCounters(String counters) throws ParseException { if (counters == null) { LOG.warn("HistoryEventEmitters: null counter detected:"); return null; }/*from w ww .j ava 2 s.co m*/ counters = counters.replace("\\.", "\\\\."); counters = counters.replace("\\\\(", "\\("); counters = counters.replace("\\\\)", "\\)"); counters = counters.replace("\\\\[", "\\["); counters = counters.replace("\\\\]", "\\]"); org.apache.hadoop.mapred.Counters depForm = org.apache.hadoop.mapred.Counters .fromEscapedCompactString(counters); try { Constructor<Counters> constructor = Counters.class .getDeclaredConstructor(new Class[] { org.apache.hadoop.mapred.Counters.class }); constructor.setAccessible(true); Counters count = constructor.newInstance(depForm); return count; } catch (InstantiationException e) { LOG.warn("InstantiationException: unable to create instance for Counters." + e); return null; } catch (IllegalAccessException e) { LOG.warn("IllegalAccessException: unable to create instance for Counters." + e); return null; } catch (InvocationTargetException e) { LOG.warn("InvocationTargetException: unable to create instance for Counters." + e); return null; } catch (NoSuchMethodException e) { LOG.warn("NoSuchMethodException: unable to create instance for Counters." + e); return null; } catch (Exception e) { LOG.warn("Exception: unable to create instance for Counters. " + e); return null; } }
From source file:org.apache.tez.engine.runtime.RuntimeUtils.java
private static RuntimeTask createRuntime(TezEngineTaskContext taskContext, Processor processor, Input[] inputs, Output[] outputs) {// w w w .jav a2s . co m try { // TODO Change this to use getNewInstance Class<?> runtimeClazz = Class.forName(taskContext.getRuntimeName()); Constructor<?> ctor = runtimeClazz.getConstructor(TezEngineTaskContext.class, Processor.class, Input[].class, Output[].class); ctor.setAccessible(true); return (RuntimeTask) ctor.newInstance(taskContext, processor, inputs, outputs); } catch (ClassNotFoundException e) { throw new RuntimeException("Unable to load runtimeClass: " + taskContext.getRuntimeName(), e); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.microsoft.tfs.client.common.git.utils.GitHelpers.java
public static Object getInstance(final String pluginName, final String className, final Class<?>[] constructorParameterTypes, final Object[] constructorParameterValues) { Check.notNullOrEmpty(pluginName, "pluginName"); //$NON-NLS-1$ Check.notNullOrEmpty(className, "className"); //$NON-NLS-1$ Check.notNull(constructorParameterTypes, "constructorParameterTypes"); //$NON-NLS-1$ Check.notNull(constructorParameterTypes, "constructorParameterTypes"); //$NON-NLS-1$ final Class<?> operationClass = getClass(pluginName, className); if (operationClass == null) { return null; }//from w ww . j a v a 2s . c om final Constructor<?> constructor; try { constructor = operationClass.getDeclaredConstructor(constructorParameterTypes); constructor.setAccessible(true); } catch (final Exception e) { log.error("Searching for the " + operationClass.getName() + " constructor", e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } final Object operationInstance; try { operationInstance = constructor.newInstance(constructorParameterValues); } catch (final Exception e) { log.error("Creating the " + operationClass.getName() + " object", e); //$NON-NLS-1$ //$NON-NLS-2$ return null; } return operationInstance; }
From source file:jp.furplag.util.commons.ObjectUtils.java
/** * substitute for {@link java.lang.Class#newInstance()}. * * @param type the Class object, return false if null. * @return empty instance of specified {@link java.lang.Class}. * @throws IllegalArgumentException//from www . j a va 2s . com * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException * @throws ClassNotFoundException * @throws NegativeArraySizeException */ @SuppressWarnings("unchecked") public static <T> T newInstance(final Class<T> type) throws InstantiationException { if (type == null) return null; if (type.isArray()) return (T) Array.newInstance(type.getComponentType(), 0); if (Void.class.equals(ClassUtils.primitiveToWrapper(type))) { try { Constructor<Void> c = Void.class.getDeclaredConstructor(); c.setAccessible(true); return (T) c.newInstance(); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } catch (InvocationTargetException e) { } catch (IllegalAccessException e) { } return null; } if (type.isInterface()) { if (!Collection.class.isAssignableFrom(type)) throw new InstantiationException( "could not create instance, the type \"" + type.getName() + "\" is an interface."); if (List.class.isAssignableFrom(type)) return (T) Lists.newArrayList(); if (Map.class.isAssignableFrom(type)) return (T) Maps.newHashMap(); if (Set.class.isAssignableFrom(type)) return (T) Sets.newHashSet(); } if (type.isPrimitive()) { if (boolean.class.equals(type)) return (T) Boolean.FALSE; if (char.class.equals(type)) return (T) Character.valueOf(Character.MIN_VALUE); return (T) NumberUtils.valueOf("0", (Class<? extends Number>) type); } if (ClassUtils.isPrimitiveOrWrapper(type)) return null; if (Modifier.isAbstract(type.getModifiers())) throw new InstantiationException( "could not create instance, the type \"" + type.getName() + "\" is an abstract class."); try { Constructor<?> c = type.getDeclaredConstructor(); c.setAccessible(true); return (T) c.newInstance(); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } catch (InvocationTargetException e) { } catch (IllegalAccessException e) { } throw new InstantiationException("could not create instance, the default constructor of \"" + type.getName() + "()\" is not accessible ( or undefined )."); }
From source file:org.cloudata.core.client.CTableManager.java
public static IOException makeIOException(Exception e) { if (e instanceof CRemoteException) { CRemoteException re = (CRemoteException) e; IOException resultException = null; try {/*from w ww . java 2s . c om*/ if (re.getClassName() == null) { throw re; } Constructor cn = IOException.class.getConstructor(String.class); cn.setAccessible(true); IOException ex = (IOException) cn.newInstance(e.getMessage()); ex.initCause(e); return ex; //resultException = (IOException)Class.forName(re.getClassName()).newInstance(); //resultException.initCause(e); //return resultException; } catch (Exception e1) { e1.printStackTrace(); LOG.error(e1.getMessage()); // return null; return re; } } else if (e instanceof IOException) { return (IOException) e; } else { IOException err = new IOException(e.getMessage()); err.initCause(e); return err; } }