List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.xinlv.test.PortalTestUtil.java
public static void callOnBeginRequest() { try {//from w ww . j a va 2 s.co m Method method = RequestCycle.class.getDeclaredMethod("onBeginRequest", (Class<?>[]) null); method.setAccessible(true); method.invoke(RequestCycle.get(), (Object[]) null); } catch (Exception e) { throw new UnhandledException(e); } }
From source file:Main.java
private static void setBitmap(BitmapDrawable drawable, Bitmap bitmap) { try {/* ww w . j a v a 2 s.c om*/ Method method = getMethod(BitmapDrawable.class, "setBitmap", new Class[] { Bitmap.class }); if (method != null) { method.setAccessible(true); method.invoke(drawable, bitmap); } } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
private static Object invoke(Class<?> cls, Object receiver, String methodname, Object... args) throws Exception { Method method = null; if (args == null || args.length == 0) { method = cls.getMethod(methodname, new Class[0]); method.setAccessible(true); return method.invoke(receiver, new Object[0]); }/*from w w w. j a v a 2 s . c om*/ method = cls.getMethod(methodname, getParameterTypes(args)); method.setAccessible(true); return method.invoke(receiver, args); }
From source file:Main.java
/** * Inflates a preference hierarchy from XML. If a preference hierarchy is * given, the new preference hierarchies will be merged in. * * @param context The context of the resource. * @param resId The resource ID of the XML to inflate. * @param rootPreferences Optional existing hierarchy to merge the new * hierarchies into. * @return The root hierarchy (if one was not provided, the new hierarchy's * root)./*from w w w .j a va 2s .c o m*/ */ public static PreferenceScreen inflateFromResource(@NonNull PreferenceManager manager, Context context, int resId, PreferenceScreen rootPreferences) { try { Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class); m.setAccessible(true); return (PreferenceScreen) m.invoke(manager, context, resId, rootPreferences); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
private static Object invoke(Class<?> cls, Object receiver, String methodname, Class<?>[] clsArr, Object[] objArr) throws Exception { Method method = null; if (objArr == null || objArr.length == 0) { method = cls.getMethod(methodname, new Class[0]); method.setAccessible(true); return method.invoke(receiver, new Object[0]); }/* w w w .j av a 2 s .c o m*/ method = cls.getMethod(methodname, clsArr); method.setAccessible(true); return method.invoke(receiver, objArr); }
From source file:Main.java
/** * check for getter method and return the value.{@link #getGetter(Object, String)}. *///from w w w. j a va 2 s. c o m public static Object getProperty(Object bean, String property) { if (bean == null || TextUtils.isEmpty(property)) { return null; } try { Method getter = getGetter(bean, property); if (getter != null) { if (!getter.isAccessible()) { getter.setAccessible(true); } return getter.invoke(bean, new Object[0]); } return null; } catch (Exception e) { return null; } }
From source file:Main.java
/** * Enable/Disable mobile data./*w w w. ja v a 2 s . c om*/ * @param context * @param enabled */ public static void setMobileDataEnabled(Context context, boolean enabled) { try { final ConnectivityManager conman = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final Class conmanClass = Class.forName(conman.getClass().getName()); final Field connectivityManagerField = conmanClass.getDeclaredField("mService"); connectivityManagerField.setAccessible(true); final Object connectivityManager = connectivityManagerField.get(conman); final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName()); final Method setMobileDataEnabledMethod = connectivityManagerClass .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); // Method call for enabling mobile data.. setMobileDataEnabledMethod.invoke(connectivityManager, enabled); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Called internally by installGtkPopupBugWorkaround. Returns a specific * GTK style object.//from w w w . j a v a 2s .c o m * * @param styleFactory * The GTK style factory. * @param component * The target component of the style. * @param regionName * The name of the target region of the style. * @return The GTK style. * @throws Exception * When reflection fails. */ private static Object getGtkStyle(Object styleFactory, JComponent component, String regionName) throws Exception { // Create the region object Class<?> regionClass = Class.forName("javax.swing.plaf.synth.Region"); Field field = regionClass.getField(regionName); Object region = field.get(regionClass); // Get and return the style Class<?> styleFactoryClass = styleFactory.getClass(); Method method = styleFactoryClass.getMethod("getStyle", new Class<?>[] { JComponent.class, regionClass }); boolean accessible = method.isAccessible(); method.setAccessible(true); Object style = method.invoke(styleFactory, component, region); method.setAccessible(accessible); return style; }
From source file:Main.java
/** * Applies stirng to View by Reflection/*from www. jav a 2 s . c om*/ * * @param context Context * @param view View to apply to. * @param attrs Attributes of Layout */ public static void applyResourceStringToView(Context context, View view, AttributeSet attrs) { if (attrs != null) { Resources res = context.getResources(); for (int i = 0; i < attrs.getAttributeCount(); i++) { int resId = attrs.getAttributeResourceValue(i, 0); if (resId > 0) { String resType = res.getResourceTypeName(resId); if (resType.equals("string")) { String attrName = attrs.getAttributeName(i); try { Method method = view.getClass().getMethod("set" + capitalize(attrName), CharSequence.class); method.setAccessible(true); method.invoke(view, res.getString(resId)); } catch (NoSuchMethodException e) { } catch (Exception e) { } } } } } }
From source file:com.npower.dm.util.BeanHelper.java
/** * private/protected//w w w .j av a 2 s. c o m */ static public Object invokePrivateMethod(Object object, String methodName, Object[] params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { assert object != null; assert StringUtils.isNotEmpty(methodName); Class<?>[] types = new Class[params.length]; for (int i = 0; i < params.length; i++) { types[i] = params[i].getClass(); } Method method = object.getClass().getDeclaredMethod(methodName, types); method.setAccessible(true); return method.invoke(object, params); }