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
/** * Encodes the given string./*ww w.j a v a2 s . com*/ * Implementation can be used on Android platform also. * @param src - string to encode. * @return encoded string. */ public static String encodeString(String src) { try { try { Class<?> base64class = Class.forName("android.util.Base64"); Method method = base64class.getMethod("encodeToString", new Class[] { byte[].class, int.class }); return (String) method.invoke(null, new Object[] { src.getBytes(), 2 }); } catch (ClassNotFoundException e0) { try { Class<?> base64class = Class.forName("org.apache.commons.codec.binary.Base64"); Method method = base64class.getMethod("encodeBase64String", new Class[] { byte[].class }); return (String) method.invoke(null, new Object[] { src.getBytes() }); } catch (ClassNotFoundException e1) { throw new IllegalStateException("Unable to find class for Base64 encoding."); } } } catch (NoSuchMethodException ex) { throw new IllegalStateException("Unable to find method for Base64 encoding.", ex); } catch (Exception ex) { throw new IllegalStateException("Could not invoke method for Base64 encoding.", ex); } }
From source file:Main.java
public static Object getVariableFromMethod(Activity activity, String method) { Object value = false;//from w ww . j av a 2 s . c o m Class c = activity.getClass(); try { Method m = (Method) c.getDeclaredMethod(method, new Class[] {}); m.setAccessible(true); value = m.invoke(activity, new Object[] {}); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return value; }
From source file:Main.java
public static <T> T getPrivateMethod(Class<?> outerClass, String innerClassName, Object obj, String methodName, Class<?> parameterTypes[], Object parameters[], Class<T> returnType) throws Exception { // Get all inner classes Class<?> innerClasses[] = outerClass.getDeclaredClasses(); // find the inner class that matches the order Class<?> innerClass = null; for (int index = 0; index < innerClasses.length; index++) { if (innerClassName.equals(innerClasses[index].getSimpleName())) { innerClass = innerClasses[index]; }/*from w w w .j a v a2s. co m*/ } T returnValue = null; if (innerClass != null) { Method method = innerClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); returnValue = (T) method.invoke(obj, parameters); } return returnValue; }
From source file:Main.java
public static void callInjectViews(Object activity) { try {//from ww w. j a va2 s .c o m Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector"); Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class); injectViewsMethod.setAccessible(true); injectViewsMethod.invoke(null, activity); } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchMethodException e) { throw new RuntimeException("Could not invoke RoboGuice method!", e); } }
From source file:SampleInvoke.java
public static String append(String firstWord, String secondWord) { String result = null;/*w w w . java 2s. c om*/ Class c = String.class; Class[] parameterTypes = new Class[] { String.class }; Method concatMethod; Object[] arguments = new Object[] { secondWord }; try { concatMethod = c.getMethod("concat", parameterTypes); result = (String) concatMethod.invoke(firstWord, arguments); } catch (NoSuchMethodException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } return result; }
From source file:Main.java
/** * @param type//from w ww .ja v a2s . com * @param object * @param field * @return */ public static <T> Object getFieldValue(Class<T> type, T object, Field field) { try { Method m = type.getMethod(getGetIsPrefix(field) + getFirstLetterUppercased(field.getName()), (Class<?>[]) null); return m.invoke(object, (Object[]) null); } catch (Exception e) { } return null; }
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; }/*from w w w . j a va2 s. c o m*/ try { return method.invoke(receiver, args); } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { //Log.e(TAG, "Exception in invoke", e); } return defaultValue; }
From source file:Main.java
/** * Call a private method of a given class object * /*ww w .j a v a2 s .c o m*/ * @param objectInstance Class object to invoke method on * @param methodName Method name to invoke * @param int1 int Parameter to the method * @return Return value from invoked method * * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException */ public static Object callPrivateMethod(Object objectInstance, String methodName, int int1) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException // NOSONAR { Method method = objectInstance.getClass().getDeclaredMethod(methodName, Integer.TYPE); method.setAccessible(true); return method.invoke(objectInstance, int1); }
From source file:Main.java
/** * Sets the root of the preference hierarchy. * * @param preferenceScreen The root {@link android.preference.PreferenceScreen} of the preference hierarchy. * @return Whether the {@link android.preference.PreferenceScreen} given is different than the previous. *///from w ww .j a va2s . c o m public static boolean setPreferences(@NonNull PreferenceManager manager, PreferenceScreen preferenceScreen) { try { Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class); m.setAccessible(true); return ((Boolean) m.invoke(manager, preferenceScreen)); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Call a private method of a given class object - with no parameters * /*from w w w . j a va2s . c o m*/ * @param objectInstance Class object to invoke method on * @param methodName Method name to invoke * @param inputVal * @return Return value from invoked method * * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ public static Object callPrivateMethod(Object objectInstance, String methodName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException // NOSONAR { Object[] params = null; Method method = objectInstance.getClass().getDeclaredMethod(methodName, (Class[]) null); method.setAccessible(true); return method.invoke(objectInstance, params); }