Example usage for java.lang.reflect Method setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List parents(Object o) {
    try {// w w w  . j ava 2s .co  m

        Method getParent = o.getClass().getMethod("getParent");
        getParent.setAccessible(true);

        List result = new ArrayList(8);
        try {
            while (true) {
                Object parent = getParent.invoke(o);
                if (parent == null) {
                    return result;
                } else {
                    result.add(parent);
                }
                o = parent;
            }
        } catch (IllegalArgumentException e) {
            return result;
        } catch (IllegalAccessException e) {
            return result;
        } catch (InvocationTargetException e) {
            return result;
        }

    } catch (NoSuchMethodException e) {
        return Collections.EMPTY_LIST;
    }

}

From source file:Main.java

public static <T> T callMethod(@NonNull Object obj, @NonNull String name, Object... args) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals(name)) {
                m.setAccessible(true);
                //noinspection unchecked
                return (T) m.invoke(obj, args);
            }//w ww .  j  ava2s  .c om
        }
    }
    throw new RuntimeException("no method matching " + name);
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List subviews(Object o) {

    try {// w w w. j  a v a  2  s.  co m
        Method getChild = o.getClass().getMethod("getChildAt", int.class);
        getChild.setAccessible(true);
        Method getChildCount = o.getClass().getMethod("getChildCount");
        getChildCount.setAccessible(true);
        List result = new ArrayList(8);
        int childCount = (Integer) getChildCount.invoke(o);
        for (int i = 0; i < childCount; i++) {
            result.add(getChild.invoke(o, i));
        }
        return result;

    } catch (NoSuchMethodException e) {
        return Collections.EMPTY_LIST;
    } catch (IllegalArgumentException e) {
        return Collections.EMPTY_LIST;
    } catch (IllegalAccessException e) {
        return Collections.EMPTY_LIST;
    } catch (InvocationTargetException e) {
        return Collections.EMPTY_LIST;
    }

}

From source file:com.angstoverseer.util.ReflectionUtil.java

public static Object invokeMethod(Object target, String methodName, Object... parameters) {
    try {/*from w  w  w.j  av  a  2s .c o  m*/
        final Method method = extractMethod(target, methodName);
        method.setAccessible(true);
        return method.invoke(target, parameters);
    } catch (Exception e) {
        throw new RuntimeException("Could not invoke method.", e);
    }
}

From source file:Main.java

public static Method getMethod(Class<?> clazz, String methodName) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(methodName)) {
            method.setAccessible(true);
            return method;
        }/*from   ww w. j  av a  2 s .co m*/
    }
    return null;
}

From source file:Main.java

private static LongSparseArray<Drawable.ConstantState> hackPreloadDrawablesV19(Resources res) {
    try {/*from w ww. j  a v  a 2 s.c  o m*/
        Method method = Resources.class.getDeclaredMethod("getPreloadedDrawables");
        method.setAccessible(true);
        return (LongSparseArray<Drawable.ConstantState>) method.invoke(res);
    } catch (NoSuchMethodException e) {
        // ignore
    } catch (IllegalAccessException e) {
        // ignore
    } catch (IllegalArgumentException e) {
        // ignore
    } catch (InvocationTargetException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

/**
 * This method is used to check whether mobile data connection is enabled or disabled.
 * @param appContext// w  ww.  j a  v a2s .  c  o m
 * @return
 */
public static boolean isMobileDataEnables(Context appContext) {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method call able
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        System.err.println("isMobileDataEnables() : " + e);
    }
    return mobileDataEnabled;
}

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];
        }/*  ww w .ja  va  2s. c o  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:coral.CoralStart.java

private static void loadSwtJar(String swtPath) {
    if (swtPath == null) {
        String osName = System.getProperty("os.name").toLowerCase();
        String osArch = System.getProperty("os.arch").toLowerCase();

        String swtFileNameOsPart = osName.contains("win") ? "win32"
                : osName.contains("mac") ? "osx"
                        : osName.contains("linux") || osName.contains("nix") ? "linux" : "";

        String swtFileNameArchPart = osArch.contains("64") ? "_x86_64" : "";
        swtPath = "lib/swt-" + swtFileNameOsPart + swtFileNameArchPart + ".jar";
    }/*from w  ww.j a  v  a 2 s.c  o m*/

    try {
        URL swtFileUrl = new URL("file:" + swtPath);

        URLClassLoader classLoader = (URLClassLoader) CoralHead.class.getClassLoader();
        Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
        addUrlMethod.setAccessible(true);

        addUrlMethod.invoke(classLoader, swtFileUrl);
        System.out.println("Added the swt jar to the class path: " + swtPath);
    } catch (Exception e) {
        System.out.println("Unable to add the swt jar to the class path: " + swtPath);
        lastresort("Unable to add the swt jar to the class path: " + swtPath + " ", e);
        e.printStackTrace();
    }
}

From source file:Main.java

private static ThreadInfo[] dumpAllThreads(ThreadMXBean threadBean) throws SecurityException,
        NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Method method = threadBean.getClass().getDeclaredMethod("dumpAllThreads", boolean.class, boolean.class);
    method.setAccessible(true);
    return (ThreadInfo[]) method.invoke(threadBean, true, true);
}