Example usage for java.lang.reflect Method isAccessible

List of usage examples for java.lang.reflect Method isAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method isAccessible.

Prototype

@Deprecated(since = "9")
public boolean isAccessible() 

Source Link

Document

Get the value of the accessible flag for this reflected object.

Usage

From source file:co.carlosjimenez.android.currencyalerts.app.MenuTint.java

/**
 * Set the menu to show MenuItem icons in the overflow window.
 *
 * @param menu the menu to force icons to show
 *///from   w  w  w  . j av a2s .c  om
public static void forceMenuIcons(Menu menu) {
    try {
        Class<?> MenuBuilder = menu.getClass();
        Method setOptionalIconsVisible = MenuBuilder.getDeclaredMethod("setOptionalIconsVisible",
                boolean.class);
        if (!setOptionalIconsVisible.isAccessible()) {
            setOptionalIconsVisible.setAccessible(true);
        }
        setOptionalIconsVisible.invoke(menu, true);
    } catch (Exception ignored) {
    }
}

From source file:org.sakaiproject.nakamura.api.lite.StorageClientUtils.java

/**
 * Make the method on the target object accessible and then invoke it.
 * @param target the object with the method to invoke
 * @param methodName the name of the method to invoke
 * @param args the arguments to pass to the invoked method
 * @param argsTypes the types of the arguments being passed to the method
 * @return//from  ww w. j  av  a2s . co m
 */
private static Object safeMethod(Object target, String methodName, Object[] args,
        @SuppressWarnings("rawtypes") Class[] argsTypes) {
    if (target != null) {
        try {
            Method m = target.getClass().getMethod(methodName, argsTypes);
            if (!m.isAccessible()) {
                m.setAccessible(true);
            }
            return m.invoke(target, args);
        } catch (Throwable e) {
            LOGGER.info("Failed to invoke method " + methodName + " " + target, e);
        }
    }
    return null;
}

From source file:org.rhq.bindings.output.TabularWriter.java

private static Object invoke(Object o, Method m) throws IllegalAccessException, InvocationTargetException {
    boolean access = m.isAccessible();
    m.setAccessible(true);/*from  ww w  .j  av  a  2  s .c  o  m*/
    try {
        LazyLoadScenario.setShouldLoad(false);

        return m.invoke(o);
    } catch (Exception e) {
        // That's fine
        return null;
    } finally {
        LazyLoadScenario.setShouldLoad(true);
        m.setAccessible(access);
    }
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Make the given method accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).// w  ww  . ja  v  a2s.  co  m
 * @param method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 */
public static void makeAccessible(Method method) {
    if ((!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
        method.setAccessible(true);
    }
}

From source file:com.helpinput.utils.Utils.java

public static Method accessible(Method method) {
    if (method != null) {
        if (!method.isAccessible())
            method.setAccessible(true);//from w w w  .  j  ava2  s . c om
    }
    return method;
}

From source file:com.helpinput.core.Utils.java

@SuppressWarnings("unchecked")
public static <T extends Member> T setAccess(final Member theMember) {
    if (theMember instanceof Method) {
        Method real = (Method) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);//from w w w . j a va2  s  .  com
    } else if (theMember instanceof Constructor) {
        @SuppressWarnings("rawtypes")
        Constructor real = (Constructor) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);
    } else if (theMember instanceof Field) {
        Field real = (Field) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);
    }
    return (T) theMember;
}

From source file:com.ez.gallery.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }//from  w w w .  j a v a 2 s.  c  o m

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                } catch (InvocationTargetException e) {
                }
            }
        }
    }
}

From source file:com.ag.common.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }//www . j  a  va 2 s  .c  om

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Log.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Log.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:io.cess.core.gallery.permission.EasyPermissions.java

private static void runAnnotatedMethods(Object object, int requestCode) {
    Class clazz = object.getClass();
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute non-void method " + method.getName());
                }/*  w w  w  . j av  a  2s.  c om*/

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    //ILogger.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    //ILogger.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:com.ett.common.util.ReflectUtil.java

/**
 * @param object// w  w  w.  j  a  v a 2 s . co m
 * @param methodName
 * @param params
 * @return
 * @throws NoSuchMethodException
 */
public static Object invokePrivateMethod(Object object, String methodName, Object... params)
        throws NoSuchMethodException {
    Assert.notNull(object);
    Assert.hasText(methodName);
    Class[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }

    Class clazz = object.getClass();
    Method method = null;
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {
            method = superClass.getDeclaredMethod(methodName, types);
            break;
        } catch (NoSuchMethodException e) {
            // ??17,?
        }
    }

    if (method == null)
        throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);

    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object result = null;
    try {
        result = method.invoke(object, params);
    } catch (Exception e) {
        ReflectionUtils.handleReflectionException(e);
    }
    method.setAccessible(accessible);
    return result;
}