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
public static int untetherIface(Context context, String iface) { int returnCode = -1; Object connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE); Method untether = null; try {/*from ww w.j ava2 s . c o m*/ untether = connectivityManager.getClass().getMethod("untether", new Class[] { String.class }); } catch (Exception e) { e.printStackTrace(); } try { returnCode = (Integer) untether.invoke(connectivityManager, new Object[] { iface }); } catch (Exception e) { e.printStackTrace(); } return returnCode; }
From source file:com.apporiented.hermesftp.utils.IOUtils.java
/** * Closes a file, socket, stream, writer etc. without throwing exceptions. Any exception is * catched and logged. In contrast to the "Commons IO" method <code>closeQuietly()</code> this * method closes any object that has a <code>close()</code>-method and is not restricted to * readers, writers and streams./* ww w. j a v a 2 s.c o m*/ * * @param o The object to be closed. * @return True, if object could be closed. */ public static boolean closeGracefully(Object o) { boolean result; try { Method closeMethod = BeanUtils.findMethod(o.getClass(), "close", null); closeMethod.invoke(o, (Object[]) null); result = true; } catch (IllegalArgumentException e) { result = false; } catch (IllegalAccessException e) { result = false; } catch (InvocationTargetException e) { result = false; } catch (NullPointerException e) { result = false; } return result; }
From source file:cn.loveapple.client.android.LoveappleHelper.java
private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types) throws Exception { Object out = null;//from w ww. j ava 2s . c om Class c = object instanceof Class ? (Class) object : object.getClass(); if (types != null) { Method method = c.getMethod(methodName, types); out = method.invoke(object, params); } else { Method method = c.getMethod(methodName); out = method.invoke(object); } Log.d(LOG_TAG, object.getClass().getName() + "." + methodName + "() = " + out); return out; }
From source file:com.adaptris.util.SimpleBeanUtil.java
/** * Invoke the setter method on the object. * <p>//from w w w. j a v a2 s. co m * Uses the first match, so overloaded methods may cause unexpected behaviour. Assumes that the {@code String} value can be * converted into its corresponding primitive value (long/double/boolean/string/float/int). If it can't a runtime exception is * probably going to be thrown. * </p> * * @param obj the object. * @param methodName the method name (e.g. {@code setClientID}, case insensitive match). * @param value the value; which will be converted into the appropriate primitive. * @return true if the setter was successfully called; false otherwise. */ public static boolean callSetter(Object obj, String methodName, String value) { boolean result = false; try { Method m = getSetterMethod(obj.getClass(), methodName); Object param = OBJECTIFIERS.get(m.getParameterTypes()[0]).objectify(value); m.invoke(obj, param); result = true; } catch (NullPointerException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return result; }
From source file:com.mmj.app.common.core.lang.BeanUtils.java
public static Map<String, Object> beanToMap(Object entity) { Map<String, Object> parameter = new HashMap<String, Object>(); Field[] fields = entity.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { String fieldName = fields[i].getName(); if (StringUtils.equals("serialVersionUID", fieldName)) { continue; }/* ww w. j av a 2s . c o m*/ Object o = null; String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getMethodName = "get" + firstLetter + fieldName.substring(1); Method getMethod; try { getMethod = entity.getClass().getMethod(getMethodName, new Class[] {}); o = getMethod.invoke(entity, new Object[] {}); } catch (Exception e) { e.printStackTrace(); } if (o != null) { parameter.put(fieldName, o); } } return parameter; }
From source file:Main.java
/** * //from www. ja va 2 s . c om * @param <T> * @param thread * @param threadLocal * @param value * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ private static <T extends Object> void createMap(Thread thread, ThreadLocal<T> threadLocal, T value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method createMapMethod = ThreadLocal.class.getDeclaredMethod("createMap", new Class<?>[] { Thread.class, Object.class }); createMapMethod.setAccessible(true); createMapMethod.invoke(threadLocal, new Object[] { thread, value }); }
From source file:ca.psiphon.tunneledwebview.WebViewProxySettings.java
@SuppressWarnings("rawtypes") private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types) throws Exception { Object out = null;//from w w w .java 2 s.c o m Class c = object instanceof Class ? (Class) object : object.getClass(); if (types != null) { Method method = c.getMethod(methodName, types); out = method.invoke(object, params); } else { Method method = c.getMethod(methodName); out = method.invoke(object); } return out; }
From source file:Main.java
public static void killProcesses(Context context, int pid, String processName) { String cmd = "kill -9 " + pid; String Command = "am force-stop " + processName + "\n"; Process sh = null;/*from w ww . jav a 2s. c om*/ DataOutputStream os = null; try { sh = Runtime.getRuntime().exec("su"); os = new DataOutputStream(sh.getOutputStream()); os.writeBytes(Command + "\n"); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { sh.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid); // L.i(processName); ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); String packageName = null; try { if (processName.indexOf(":") == -1) { packageName = processName; } else { packageName = processName.split(":")[0]; } activityManager.killBackgroundProcesses(packageName); // Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage", String.class); forceStopPackage.setAccessible(true); forceStopPackage.invoke(activityManager, packageName); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java
/** * Exports a JFreeChart to a SVG file./*from ww w . ja v a 2s. c o m*/ * * @param chart * JFreeChart to export * @param bounds * the dimensions of the viewport * @param svgFile * the output file. * @throws IOException * if writing the svgFile fails. */ public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException { try { Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation"); // Get a DOMImplementation and create an XML document Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]); DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]); org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D"); Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class }); Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class }); Object svgGenerator = SG2DConstr.newInstance(document); // draw the chart in the SVG generator chart.draw((Graphics2D) svgGenerator, bounds); // Write svg file OutputStream outputStream = new FileOutputStream(svgFile); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ }); outputStream.flush(); outputStream.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:biz.wolschon.finance.jgnucash.actions.FileBugInBrowserAction.java
/** /*from w ww. j a v a 2s .c om*/ * Open the given URL in the default-browser. * @param url the URL to open * @return false if it did not work */ @SuppressWarnings("unchecked") public static boolean showDocument(final URL url) { if (myJNLPServiceManagerObject == null) { myJNLPServiceManagerObject = getJNLPServiceManagerObject(); } // we cannot use JNLP -> make an educated guess if (myJNLPServiceManagerObject == null) { try { String osName = System.getProperty("os.name"); 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 = { "x-www-browser", "firefox", "iceweasle", "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) { return false; } else { Runtime.getRuntime().exec(new String[] { browser, url.toString() }); return true; } } } } catch (Exception e) { LOGGER.error("Error attempting to launch web browser natively.", e); JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage()); } } if (myJNLPServiceManagerObject != null) { try { Method method = myJNLPServiceManagerObject.getClass().getMethod("showDocument", new Class[] { URL.class }); Boolean resultBoolean = (Boolean) method.invoke(myJNLPServiceManagerObject, new Object[] { url }); return resultBoolean.booleanValue(); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + ex.getLocalizedMessage()); } } return false; }