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
/** * Sets the vm policy./*from w w w. ja v a 2s . c o m*/ * * @param strictMode * the strict mode to work with * @throws ClassNotFoundException * on problem * @throws NoSuchMethodException * on problem * @throws InstantiationException * on problem * @throws IllegalAccessException * on problem * @throws InvocationTargetException * on problem */ private static void setVmPolicy(final Class<?> strictMode) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> vmPolicyType = Class.forName("android.os.StrictMode$VmPolicy"); Method setVmPolicy = strictMode.getMethod("setVmPolicy", vmPolicyType); Class<?> vmPolicyBuilder = Class.forName("android.os.StrictMode$VmPolicy$Builder"); Object policy = buildPolicy(vmPolicyBuilder); setVmPolicy.invoke(strictMode, policy); }
From source file:org.mstc.zmq.json.Decoder.java
static Object getNested(Class<?> nested, byte[] input) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method parseFrom = nested.getMethod("parseFrom", byte[].class); return parseFrom.invoke(null, input); }
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 w w . ja v a 2s.c o m 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) { e.printStackTrace(); } try { sh.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid); Log.i("AppUtil", 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:jp.terasoluna.fw.batch.unit.util.ClassLoaderUtils.java
/** * <pre>/*from w w w . ja v a 2s .c o m*/ * ??????url??????? * ??????? * * ???? * ???????????? * {@link #resetClassLoader()}??????? * </pre> * * @param url ? */ public static void addClassPath(URL url) { Assert.notNull(url); ClassLoader cl = getClassLoader(); URLClassLoader newCl = null; if (cl instanceof URLClassLoader) { @SuppressWarnings("resource") URLClassLoader ucl = (URLClassLoader) cl; newCl = new URLClassLoader(ucl.getURLs(), cl); } else { newCl = new URLClassLoader(null, cl); } Method method = getAddURLMethod(); try { method.invoke(newCl, url); } catch (Exception e) { throw new UTRuntimeException(e); } previousClassLoader = cl; setClassLoader(newCl); }
From source file:ClassUtils.java
/** * Helper for invoking a static method that takes one parameter. * /*from w ww . j ava 2 s .c o m*/ * @param cl * The class that implements the static method * @param methodName * The method name * @param param * A parameter * @param paramClass * Class of the parameter * @throws Throwable */ public static Object invokeStaticMethod(Class cl, String methodName, Object param, Class paramClass) throws Throwable { Method method = cl.getMethod(methodName, new Class[] { paramClass }); try { return method.invoke(null, new Object[] { param }); } catch (InvocationTargetException e) { throw e.getCause(); } }
From source file:Main.java
public static void removeSysKey(Activity context, EditText paramEditText) { if (Build.VERSION.SDK_INT <= 10) { paramEditText.setInputType(0);//from w ww.j av a2s. c o m return; } context.getWindow().setSoftInputMode(3); try { Class[] arrayOfClass = new Class[1]; arrayOfClass[0] = Boolean.TYPE; Method localMethod = EditText.class.getMethod("setShowSoftInputOnFocus", arrayOfClass); localMethod.setAccessible(true); Object[] arrayOfObject = new Object[1]; arrayOfObject[0] = Boolean.valueOf(false); localMethod.invoke(paramEditText, arrayOfObject); return; } catch (Exception localException) { localException.printStackTrace(); } }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:ReflectionMap?</p> * @param target/* w w w. j av a 2s .c om*/ * @return Map<String,Object> * @author wanggq * @since 2009-9-9 */ @SuppressWarnings("unchecked") public static Map<String, Object> getMapFieldData(Object target) { Map<String, Object> map = new HashMap<String, Object>(); Class clazz = target.getClass(); Field[] fields = clazz.getDeclaredFields(); Method[] methods = clazz.getDeclaredMethods(); for (Field field : fields) { String fieldName = field.getName(); if ("messageTypeId".equals(fieldName)) { continue; } String getMethod = "get" + StringUtils.capitalize(fieldName); for (Method method : methods) { if (method.getName().equals(getMethod)) { try { Object ret = method.invoke(target, null); map.put(fieldName, ret); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } return map; }
From source file:com.github.yongchristophertang.engine.java.LoggerProxyHelper.java
static Object addLogger(Logger logger, Method method, Object[] args, Object client) throws Throwable { if (method.getDeclaringClass() == Object.class) { return method.invoke(client, args); }//from w ww . j a v a 2s . co m String formatter = "\n\tAPI: " + method.getName() + "\n\n"; formatter += "\tInput:\n"; for (int i = 0; i < method.getParameterCount(); i++) { formatter += "\t\t" + method.getParameters()[i].getName() + " (" + method.getParameters()[i].getType().getSimpleName() + "): "; if (args[i] == null) { formatter += "NULL"; } else if (args[i] instanceof Iterable) { int cnt = 0; Iterator iter = ((Iterable) args[i]).iterator(); while (iter.hasNext()) { formatter += "\n\t\t\t[" + (++cnt) + "]: " + toPrinterString(iter.next(), false); } } else { formatter += toPrinterString(args[i], false); } formatter += "\n"; } long bf = System.nanoTime(); Object result; try { result = method.invoke(client, args); } catch (InvocationTargetException e) { formatter += "\n\tException: \n\t\t" + e.getTargetException(); formatter += "\n=======================================================================\n"; logger.info(formatter); throw e.getTargetException(); } long af = System.nanoTime(); formatter += "\n\tResponse Time(ms): " + (af - bf) / 1000000 + "\n\n\tOutput:\n"; if (result == null) { formatter += "\t\tNULL\n"; } else if (result instanceof Iterable) { Iterator iter = ((Iterable) result).iterator(); int cnt = 0; while (iter.hasNext()) { formatter += "\t\t[" + (++cnt) + "]: " + toPrinterString(iter.next(), true) + "\n"; } if (cnt == 0) { formatter += "\t\tEmpty Collection []\n"; } } else { formatter += "\t\t" + toPrinterString(result, true) + "\n"; } formatter += "=======================================================================\n"; logger.info(formatter); return result; }
From source file:com.stratuscom.harvester.liaison.VirtualFileSystemConfiguration.java
private static void setManagerClassLoader() { try {//from ww w.jav a2 s. co m Object mgr = VFS.getManager(); Method setter = mgr.getClass().getMethod("setClassLoader", new Class[] { ClassLoader.class }); setter.invoke(mgr, new Object[] { mgr.getClass().getClassLoader() }); } catch (Throwable t) { t.printStackTrace(); } }
From source file:Main.java
/** * Variant of set() to establish initialValue. Used instead * of set() in case user has overridden the set() method. * * @return the initial value/*from w ww . ja v a 2 s .c o m*/ * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws NoSuchFieldException */ @SuppressWarnings("unchecked") private static <T extends Object> T setInitialValue(Thread thread, ThreadLocal<T> threadLocal) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException { T value = null; // value = threadLocal.initialValue(); Method initialValueMethod = ThreadLocal.class.getDeclaredMethod("initialValue", (Class<?>[]) null); initialValueMethod.setAccessible(true); value = (T) initialValueMethod.invoke(threadLocal, (Object[]) null); Object map = getMap(thread); if (map != null) { // map.set(this, value); Method setMethod = map.getClass().getDeclaredMethod("set", new Class<?>[] { ThreadLocal.class, Object.class }); setMethod.setAccessible(true); setMethod.invoke(map, new Object[] { threadLocal, value }); } else { //createMap(t, value); createMap(thread, threadLocal, value); } return value; }