List of usage examples for java.lang.reflect Method invoke
@CallerSensitive @ForceInline @HotSpotIntrinsicCandidate public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
From source file:Main.java
private static synchronized BluetoothSocket getRfcommSocketByReflection(BluetoothDevice device) throws Exception { if (device == null) return null; Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); return (BluetoothSocket) m.invoke(device, 1); }
From source file:Main.java
public static Object invoke(final Object receiver, final Object defaultValue, final Method method, final Object... args) { if (method == null) { return defaultValue; }/* www . ja v a2 s .c om*/ try { return method.invoke(receiver, args); } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { Log.e(TAG, "Exception in invoke", e); } return defaultValue; }
From source file:ExceptionUtils.java
/** * Set the cause of the Exception. Will detect if this is not allowed. * @param onObject// www . java 2s . c o m * @param cause */ public static void setCause(Throwable onObject, Throwable cause) { if (causesAllowed) { try { Method method = onObject.getClass().getMethod("initCause", new Class[] { Throwable.class }); method.invoke(onObject, new Object[] { cause }); } catch (RuntimeException e) { throw e; } catch (Exception e) { causesAllowed = false; } } }
From source file:Main.java
public static void forceStopPackage(Context ctx, String packageName) { ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); Method method; try {/* w w w. j a v a 2 s. c om*/ method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class); method.invoke(am, packageName); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException 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(); } }
From source file:URLUtilities.java
/** * Calls <code>java.net.URLEncoder.encode(String, String)</code> via * reflection, if we are running on JRE 1.4 or later, otherwise reverts to * the deprecated <code>URLEncoder.encode(String)</code> method. * * @param s the string to encode./*from w ww. j a va 2 s .c o m*/ * @param encoding the encoding. * * @return The encoded string. * * @since 1.0.6 */ public static String encode(String s, String encoding) { Class c = URLEncoder.class; String result = null; try { Method m = c.getDeclaredMethod("encode", STRING_ARGS_2); try { result = (String) m.invoke(null, new Object[] { s, encoding }); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchMethodException e) { // we're running on JRE 1.3.1 so this is the best we have... result = URLEncoder.encode(s); } return result; }
From source file:oz.hadoop.yarn.api.utils.ConfigUtils.java
/** * Will dynamically add configuration directory to the classpath. * /* w w w .j a v a 2 s .co m*/ * @param configurationDirectoryPath */ public static void addToClasspath(File configurationDirectoryPath) { Assert.notNull(configurationDirectoryPath, "'configurationDirectoryPath' must not be null"); Assert.isTrue(configurationDirectoryPath.exists(), "'configurationDirectoryPath' must exist"); Assert.isTrue(configurationDirectoryPath.isDirectory(), "'configurationDirectoryPath' must be a directory"); URL configUrl = null; try { configUrl = new URL("file:" + configurationDirectoryPath.getAbsolutePath() + "/"); } catch (Exception e) { throw new IllegalArgumentException("Failed to construct URL for " + configurationDirectoryPath, e); } URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Method addUrlMethod = ReflectionUtils.getMethodAndMakeAccessible(URLClassLoader.class, "addURL", URL.class); try { addUrlMethod.invoke(cl, configUrl); } catch (Exception e) { throw new IllegalStateException("Failed to add URL: " + configUrl + " to the classpath", e); } }
From source file:com.android.fastergallery.common.HttpClientFactory.java
/** * Closes an HttpClient.// w w w . java 2 s .co m */ public static void close(HttpClient client) { // AndroidHttpClient is available on all platform releases, // but is hidden until API Level 8 try { Class<?> clazz = client.getClass(); Method method = clazz.getMethod("close", (Class<?>[]) null); method.invoke(client, (Object[]) null); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:Main.java
private static String getSystemProperty(String key, String def) { try {// w ww .j a v a2 s . com final ClassLoader cl = ClassLoader.getSystemClassLoader(); final Class<?> SystemProperties = cl.loadClass("android.os.SystemProperties"); final Class<?>[] paramTypes = new Class[] { String.class, String.class }; final Method get = SystemProperties.getMethod("get", paramTypes); final Object[] params = new Object[] { key, def }; return (String) get.invoke(SystemProperties, params); } catch (Exception e) { return def; } }
From source file:com.yahoo.sql4d.sql4ddriver.BaseMapper.java
/** * More granular(sets the property of a bean based on a json key value * pair).//w w w. j a va 2 s . c o m * * @param bean * @param key * @param value * @throws NoSuchMethodException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ public static void applyKVToBean(Object bean, String key, Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method getterMethod = bean.getClass().getMethod(Util.getterMethodName(key)); Method setterMethod = bean.getClass().getMethod(Util.setterMethodName(key), getterMethod.getReturnType()); setterMethod.invoke(bean, value); }
From source file:Main.java
public static Application getApp() { if (myApp == null) { try {//from w ww . j a v a 2 s . com final Class<?> activityThreadClass = Class.forName("android.app.ActivityThread"); final Method method = activityThreadClass.getMethod("currentApplication"); myApp = (Application) method.invoke(null, (Object[]) null); } catch (Exception e) { // handle exception } } // below gives me /mnt/sdcard/Android/data/com.hyperionics.pdfxTest/cache/tmpPdfx // File file = new File(app.getApplicationContext().getExternalCacheDir(), "tmpPdfx"); return myApp; }