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:com.ery.ertc.estorm.util.Methods.java
public static <T> Object call(Class<T> clazz, T instance, String methodName, Class[] types, Object[] args) throws Exception { try {/*w w w . j ava 2 s. c o m*/ Method m = clazz.getMethod(methodName, types); return m.invoke(instance, args); } catch (IllegalArgumentException arge) { LOG.fatal("Constructed invalid call. class=" + clazz.getName() + " method=" + methodName + " types=" + Classes.stringify(types), arge); throw arge; } catch (NoSuchMethodException nsme) { throw new IllegalArgumentException("Can't find method " + methodName + " in " + clazz.getName() + "!", nsme); } catch (InvocationTargetException ite) { // unwrap the underlying exception and rethrow if (ite.getTargetException() != null) { if (ite.getTargetException() instanceof Exception) { throw (Exception) ite.getTargetException(); } else if (ite.getTargetException() instanceof Error) { throw (Error) ite.getTargetException(); } } throw new UndeclaredThrowableException(ite, "Unknown exception invoking " + clazz.getName() + "." + methodName + "()"); } catch (IllegalAccessException iae) { throw new IllegalArgumentException("Denied access calling " + clazz.getName() + "." + methodName + "()", iae); } catch (SecurityException se) { LOG.fatal("SecurityException calling method. class=" + clazz.getName() + " method=" + methodName + " types=" + Classes.stringify(types), se); throw se; } }
From source file:Main.java
public static <T extends Object, I, P extends I> T bind(final T component, final P param, final Class<I> type) { try {// w ww.ja v a 2s. c o m final Method m = findMethod(component, "bind", param, type); m.setAccessible(true); m.invoke(component, param); } catch (final Exception e) { throw new UnsupportedOperationException("Could not bind component", e); } return component; }
From source file:Main.java
public static boolean checkPermission(Context context, String permission) { boolean result = false; if (Build.VERSION.SDK_INT >= 23) { try {/*w w w .j a va 2s .co m*/ Class<?> clazz = Class.forName("android.content.Context"); Method method = clazz.getMethod("checkSelfPermission", String.class); int rest = (Integer) method.invoke(context, permission); if (rest == PackageManager.PERMISSION_GRANTED) { result = true; } else { result = false; } } catch (Exception e) { result = false; } } else { PackageManager pm = context.getPackageManager(); if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) { result = true; } } return result; }
From source file:Main.java
public static void showInternalDebugLog() { try {//from w w w . ja v a 2s. co m Class<?> clz = Class.forName("com.avos.avoscloud.AVOSCloud"); Method startMethod = clz.getDeclaredMethod("showInternalDebugLog", Boolean.TYPE); startMethod.setAccessible(true); startMethod.invoke(null, true); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String getId(Context context) { String id = null;/* w ww. j a va2 s . c o m*/ try { Class aicclass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient"); Method m1 = aicclass.getMethod("getAdvertisingIdInfo", Context.class); Object oInfo = m1.invoke(null, context); Class infoclass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient$Info"); Method m2 = infoclass.getMethod("getId"); id = (String) m2.invoke(oInfo); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } return id; }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.KITKAT) public static boolean isAllowed(Context context, int op) { Log.d(TAG, "api level: " + Build.VERSION.SDK_INT); if (Build.VERSION.SDK_INT < 19) { return true; }//from www . j a v a 2s . co m Log.d(TAG, "op is " + op); String packageName = context.getApplicationContext().getPackageName(); AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); Class<?>[] types = new Class[] { int.class, int.class, String.class }; Object[] args = new Object[] { op, Binder.getCallingUid(), packageName }; try { Method method = aom.getClass().getDeclaredMethod("checkOpNoThrow", types); Object mode = method.invoke(aom, args); Log.d(TAG, "invoke checkOpNoThrow: " + mode); if ((mode instanceof Integer) && ((Integer) mode == AppOpsManager.MODE_ALLOWED)) { Log.d(TAG, "allowed"); return true; } } catch (Exception e) { Log.e(TAG, "invoke error: " + e); e.printStackTrace(); } return false; }
From source file:Main.java
public static Object callMethod(Object obj, Method method, Object... params) { Object result = null;// w w w.java 2s . c o m if (method != null) { try { method.invoke(obj, params); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return result; }
From source file:Main.java
public static void invoke(Method m, Object o, Object[] signature) { if (m == null) return;/*from w w w.j a v a 2 s. c o m*/ try { m.invoke(o, signature); } catch (Exception ignore) { } }
From source file:Main.java
static public Object invokeMethod(Object owner, String methodName, Object... args) throws Exception { Class<?> ownerClass = owner.getClass(); Class<?>[] argsClass = null; if (args != null && args.length > 0) { argsClass = new Class<?>[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); if (argsClass[i] == Integer.class) { argsClass[i] = int.class; } else if (argsClass[i] == Boolean.class) { argsClass[i] = boolean.class; }//w w w .j a v a 2s . c o m } } Method method = ownerClass.getDeclaredMethod(methodName, argsClass); method.setAccessible(true); return method.invoke(owner, args); }
From source file:BareBonesBrowserLaunch.java
public static void openURL(String url) { String osName = System.getProperty("os.name"); try {/*from w w w.j a v a2 s . c o m*/ if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else if (osName.startsWith("Windows")) Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); else { //assume Unix or Linux String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new Exception("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } } catch (Exception e) { JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage()); } }