List of usage examples for java.lang.reflect Method isAccessible
@Deprecated(since = "9") public boolean isAccessible()
From source file:Main.java
protected static Object getITelephony(final Context context) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IllegalAccessException { final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); final Method getITelephony = tm.getClass().getDeclaredMethod("getITelephony"); if (!getITelephony.isAccessible()) { getITelephony.setAccessible(true); }/*w w w .j a v a 2s.c om*/ return getITelephony.invoke(tm); }
From source file:Main.java
public static Object invokeMethod(Object target, Class clazz, String methodName, Class[] paramTypes, Object[] paramValues) {//from ww w . j ava 2 s . c o m try { Method method = clazz.getDeclaredMethod(methodName, paramTypes); if (!method.isAccessible()) { method.setAccessible(true); } return method.invoke(target, paramValues); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static <T> T runPrivateMethod(Class<?> targetClass, String methodName, Class<?> paramClasses[], Object params[], Class<T> returnType) throws Exception { T returnValue = null;/*from w w w .ja v a 2 s .c o m*/ Method method = targetClass.getDeclaredMethod(methodName, paramClasses); if (!method.isAccessible()) method.setAccessible(true); returnValue = (T) method.invoke(targetClass, params); return returnValue; }
From source file:Main.java
public static <T> T runPrivateMethod(Class<?> targetClass, Object obj, String methodName, Class<?> paramClasses[], Object params[], Class<T> returnType) throws Exception { T returnValue = null;/*from w ww .j a va 2 s . co m*/ Method method = targetClass.getDeclaredMethod(methodName, paramClasses); if (!method.isAccessible()) method.setAccessible(true); returnValue = (T) method.invoke(obj, params); return returnValue; }
From source file:Main.java
private static Method executeMethod(Object obj, Method executeMethod) { try {/* w w w. j ava 2 s. com*/ if (executeMethod != null) { if (!executeMethod.isAccessible()) executeMethod.setAccessible(true); executeMethod.invoke(obj, new Object[] {}); } } catch (Exception e) { executeMethod = null; e.printStackTrace(); } finally { return executeMethod; } }
From source file:Main.java
@SuppressWarnings("unchecked") public static String getDeviceSerial() { String serial = "null"; try {// www . ja v a 2 s .co m Class clazz = Class.forName("android.os.Build"); Class paraTypes = Class.forName("java.lang.String"); Method method = clazz.getDeclaredMethod("getString", paraTypes); if (!method.isAccessible()) { method.setAccessible(true); } serial = (String) method.invoke(new Build(), "ro.serialno"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return serial; }
From source file:Main.java
/** * Locates a given method anywhere in the class inheritance hierarchy. * * @param instance an object to search the method into. * @param name method name//from ww w .j a v a 2 s. c om * @param parameterTypes method parameter types * @return a method object * @throws NoSuchMethodException if the method cannot be located */ static Method findMethod(Object instance, String name, Class<?>... parameterTypes) throws NoSuchMethodException { for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { try { Method method = clazz.getDeclaredMethod(name, parameterTypes); if (!method.isAccessible()) { method.setAccessible(true); } return method; } catch (NoSuchMethodException e) { // ignore and search next } } throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes) + " not found in " + instance.getClass()); }
From source file:Main.java
/** * invoke the leftParameter and get the name property. * it will try the Array.length() and Map.get(), * then it will try get'Name' and is'Name', * last, it will to find the name by invoke field. *///from ww w . j a va2s. c o m public static Object searchProperty(Object leftParameter, String name) throws Exception { Class<?> leftClass = leftParameter.getClass(); Object result; if (leftParameter.getClass().isArray() && "length".equals(name)) { result = Array.getLength(leftParameter); } else if (leftParameter instanceof Map) { result = ((Map<Object, Object>) leftParameter).get(name); } else { try { String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e2) { try { String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e3) { Field field = leftClass.getField(name); result = field.get(leftParameter); } } } return result; }
From source file:Main.java
/** * Allow calling the hidden method {@code makeOptionalFitsSystem()} through reflection on * {@code view}.// ww w . ja va2 s. c om */ public static void makeOptionalFitsSystemWindows(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { try { // We need to use getMethod() for makeOptionalFitsSystemWindows since both View // and ViewGroup implement the method Method method = view.getClass().getMethod("makeOptionalFitsSystemWindows"); if (!method.isAccessible()) { method.setAccessible(true); } method.invoke(view); } catch (NoSuchMethodException e) { Log.d(TAG, "Could not find method makeOptionalFitsSystemWindows. Oh well..."); } catch (InvocationTargetException e) { Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e); } catch (IllegalAccessException e) { Log.d(TAG, "Could not invoke makeOptionalFitsSystemWindows", e); } } }
From source file:Main.java
@SuppressWarnings("unchecked") public static Object searchProperty(Object leftParameter, String name) throws Exception { Class<?> leftClass = leftParameter.getClass(); Object result;//from w w w . j av a 2 s . c o m if (leftParameter.getClass().isArray() && "length".equals(name)) { result = Array.getLength(leftParameter); } else if (leftParameter instanceof Map) { result = ((Map<Object, Object>) leftParameter).get(name); } else { try { String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e2) { try { String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e3) { Field field = leftClass.getField(name); result = field.get(leftParameter); } } } return result; }