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:com.kangyonggan.cms.util.Reflections.java

/**
 * ?private/protectedpublic?????JDKSecurityManager
 *//*  ww w.  j a va  2  s . co m*/
public static void makeAccessible(Method method) {
    boolean temp = (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible();
    if (temp) {
        method.setAccessible(true);
    }
}

From source file:com.yanzhenjie.permission.AndPermission.java

/**
 * Parse the request results.//  w  w  w .  j  a v a2 s . c o m
 *
 * @param o            {@link Activity} or {@link android.support.v4.app.Fragment} or
 *                     {@link android.app.Fragment}.
 * @param requestCode  request code.
 * @param permissions  all permissions.
 * @param grantResults results.
 */
private static void callbackAnnotation(@NonNull Object o, int requestCode, @NonNull String[] permissions,
        int[] grantResults) {
    List<String> grantedList = new ArrayList<>();
    List<String> deniedList = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++) {
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
            grantedList.add(permissions[i]);
        else
            deniedList.add(permissions[i]);
    }

    boolean isAllGrant = deniedList.isEmpty();

    Class<? extends Annotation> clazz = isAllGrant ? PermissionYes.class : PermissionNo.class;
    Method[] methods = findMethodForRequestCode(o.getClass(), clazz, requestCode);
    if (methods.length == 0) {
        Log.e(TAG, "Not found the callback method, do you forget @PermissionYes or @permissionNo"
                + " for callback method ? Or you can use PermissionListener.");
    } else
        try {
            for (Method method : methods) {
                if (!method.isAccessible())
                    method.setAccessible(true);
                method.invoke(o, isAllGrant ? grantedList : deniedList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

From source file:$.Reflections.java

/**
     * ?private/protectedpublic?????JDKSecurityManager
     *//*  www. j  a  v  a  2 s  .  co m*/
    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.miandui.utils.runtimePermission.AndPermission.java

/**
 * Parse the request results./*from  w  ww .  j a  v  a  2s. c  o m*/
 *
 * @param o            {@link Activity} or {@link android.support.v4.app.Fragment} or
 *                     {@link android.app.Fragment}.
 * @param requestCode  request code.
 * @param permissions  all permissions.
 * @param grantResults results.
 */
private static void callbackAnnotation(@NonNull Context o, int requestCode, @NonNull String[] permissions,
        int[] grantResults) {
    List<String> grantedList = new ArrayList<>();
    List<String> deniedList = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++) {
        //            if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
        //                grantedList.add(permissions[i]);
        //MIUI
        if (CheckPermission.isGranted(o, permissions[i]))
            grantedList.add(permissions[i]);
        else
            deniedList.add(permissions[i]);
    }

    boolean isAllGrant = deniedList.isEmpty();

    Class<? extends Annotation> clazz = isAllGrant ? PermissionYes.class : PermissionNo.class;
    Method[] methods = findMethodForRequestCode(o.getClass(), clazz, requestCode);
    if (methods.length == 0) {
        Log.e(TAG, "Not found the callback method, do you forget @PermissionYes or @permissionNo"
                + " for callback method ? Or you can use PermissionListener.");
    } else
        try {
            for (Method method : methods) {
                if (!method.isAccessible())
                    method.setAccessible(true);
                method.invoke(o, isAllGrant ? grantedList : deniedList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

From source file:com.cnksi.core.tools.utils.Reflections.java

/**
 * ?private/protectedpublic?????JDKSecurityManager
 *//*from w  w w . ja  v a2 s.  c  o m*/
public static void makeAccessible(Method method) {

    if ((!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
        method.setAccessible(true);
    }
}

From source file:Main.java

/**
 * Gets a method and forces it to be accessible, even if it is not.
 *
 * @param clazz The class from which the method will be got.
 * @param methodName The name of the method.
 * @param parameterTypes The parameter types that the method must match.
 * @return The method, if it is found./*  ww w  .  ja v a2s  . c  om*/
 * @since 2.0.7
 */
public static Method getForcedAccessibleMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
    Method method;
    try {
        method = clazz.getMethod(methodName, parameterTypes);
    } catch (SecurityException e) {
        throw new RuntimeException("Cannot access method '" + methodName + "' in class '" + clazz.getName()
                + "' for security reasons", e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(
                "The method '" + methodName + "' in class '" + clazz.getName() + "' does not exist", e);
    }
    if (!method.isAccessible()) {
        method.setAccessible(true);
    }
    return method;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes,
        Object[] parameters) {//ww  w .j av a  2s.  co  m
    Method method = getDeclaredMethod(clazz, methodName, parameterTypes);
    if (method == null) {
        throw new RuntimeException("Method `" + methodName + "` not found in class `" + clazz.getName() + "`.");
    }
    if (!method.isAccessible()) {
        method.setAccessible(true);
    }
    try {
        return method.invoke(object, parameters);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java

/**
 * Fetches the methods on a class that are annotated with SpringBean. The first time it
 * is called for a particular class it will introspect the class and cache the results.
 * All non-overridden methods are examined, including protected and private methods.
 * If a method is not public an attempt it made to make it accessible - if it fails
 * it is removed from the collection and an error is logged.
 *
 * @param clazz the class on which to look for SpringBean annotated methods
 * @return the collection of methods with the annotation
 *///from  w w  w  . ja va2s  .c o  m
protected static Collection<Method> getMethods(Class<?> clazz) {
    Collection<Method> methods = methodMap.get(clazz);
    if (methods == null) {
        methods = ReflectUtil.getMethods(clazz);
        Iterator<Method> iterator = methods.iterator();

        while (iterator.hasNext()) {
            Method method = iterator.next();
            if (!method.isAnnotationPresent(SpringBean.class)) {
                iterator.remove();
            } else {
                // If the method isn't public, try to make it accessible
                if (!method.isAccessible()) {
                    try {
                        method.setAccessible(true);
                    } catch (SecurityException se) {
                        throw new StripesRuntimeException("Method " + clazz.getName() + "." + method.getName()
                                + "is marked " + "with @SpringBean and is not public. An attempt to call "
                                + "setAccessible(true) resulted in a SecurityException. Please "
                                + "either make the method public or modify your JVM security "
                                + "policy to allow Stripes to setAccessible(true).", se);
                    }
                }

                // Ensure the method has only the one parameter
                if (method.getParameterTypes().length != 1) {
                    throw new StripesRuntimeException(
                            "A method marked with @SpringBean must have exactly one parameter: "
                                    + "the bean to be injected. Method [" + method.toGenericString() + "] has "
                                    + method.getParameterTypes().length + " parameters.");
                }
            }
        }

        methodMap.put(clazz, methods);
    }

    return methods;
}

From source file:ReflectUtil.java

/**
 * <p>Attempts to find an accessible version of the method passed in, where accessible
 * is defined as the method itself being public and the declaring class being public.
 * Mostly useful as a workaround to the situation when
 * {@link PropertyDescriptor#getReadMethod()} and/or
 * {@link java.beans.PropertyDescriptor#getWriteMethod()} returns methods that are not
 * accessible (usually due to public implementations of interface methods in private
 * classes).</p>//from   w  ww . j a  v a  2 s.  c o m
 *
 * <p>Checks the method passed in and if it already meets these criteria it is returned
 * immediately. In general this leads to very little performance overhead</p>
 *
 * <p>If the method does not meet the criteria then the class' interfaces are scanned
 * for a matching method. If one is not found, then the class' superclass hierarchy
 * is searched. Finally, if no matching method can be found the original method is
 * returned.</p>
 *
 * @param m a method that may or may not be accessible
 * @return either an accessible version of the same method, or the method passed in if
 *         an accessible version cannot be found
 */
public static Method findAccessibleMethod(final Method m) {
    // If the passed in method is accessible, then just give it back.
    if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers()))
        return m;
    if (m.isAccessible())
        return m;

    final Class<?> clazz = m.getDeclaringClass();
    final String name = m.getName();
    final Class<?>[] ptypes = m.getParameterTypes();

    // Else, loop through the interfaces for the declaring class, looking for a
    // public version of the method that we can call
    for (Class<?> iface : clazz.getInterfaces()) {
        try {
            Method m2 = iface.getMethod(name, ptypes);
            if (m2.isAccessible())
                return m2;
            if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers()))
                return m2;
        } catch (NoSuchMethodException nsme) {
            /* Not Unexpected. */ }
    }

    // Else loop through the superclasses looking for a public method
    Class<?> c = clazz.getSuperclass();
    while (c != null) {
        try {
            Method m2 = c.getMethod(name, ptypes);
            if (m2.isAccessible())
                return m2;
            if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers()))
                return m2;
        } catch (NoSuchMethodException nsme) {
            /* Not Unexpected. */ }

        c = c.getSuperclass();
    }

    // If we haven't found anything at this point, just give up!
    return m;
}

From source file:org.rhq.scripting.javascript.JavascriptCompletor.java

private static Object invoke(Object o, Method m) throws IllegalAccessException, InvocationTargetException {
    boolean access = m.isAccessible();
    m.setAccessible(true);/*from  www . j  a v a 2 s .c o m*/
    try {
        return m.invoke(o);
    } finally {
        m.setAccessible(access);
    }
}