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:com.angstoverseer.util.ReflectionUtil.java

public static Object getPropertyValue(Object target, String propertyName) {
    try {/* www  . jav a2  s .c  om*/
        final Method getter = extractMethod(target, "get" + StringUtils.capitalize(propertyName));
        getter.setAccessible(true);
        return getter.invoke(target);
    } catch (Exception e) {
        throw new RuntimeException("Could not invoke method.", e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static String getDeviceSerial() {
    String serial = "null";
    try {/* www  .j  a  v a  2s  .  co  m*/
        Class clazz = Class.forName("android.os.Build");
        Class paraTypes = Class.forName("java.lang.String");
        Method method = clazz.getDeclaredMethod("getString", paraTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        serial = (String) method.invoke(new Build(), "ro.serialno");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return serial;
}

From source file:Main.java

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

From source file:Main.java

public static Object invokeMethod(Class<?> methodClass, String methodName, Class<?>[] parameters,
        Object instance, Object... arguments) {
    try {//from w w w .  j  ava2  s. c om
        Method e = methodClass.getDeclaredMethod(methodName, parameters);
        e.setAccessible(true);
        return e.invoke(instance, arguments);
    } catch (Exception var6) {
        var6.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static <T> T getValue(ThreadLocal<T> variable, Thread key) {
    try {//  w w w.jav a 2s  . c om
        Field field = Thread.class.getDeclaredField("threadLocals");
        field.setAccessible(true);
        Object entryMap = field.get(key);
        if (entryMap == null)
            return null;
        Method getEntryMethod = entryMap.getClass().getDeclaredMethod("getEntry", ThreadLocal.class);
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(entryMap, variable);
        if (entry == null)
            return null;
        Field valueField = entry.getClass().getDeclaredField("value");
        valueField.setAccessible(true);

        return (T) valueField.get(entry);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static List<Thread> getThreadsFor(Runnable myRunnable) throws Exception {
    Method getThreads = Thread.class.getDeclaredMethod("getThreads");
    Field target = Thread.class.getDeclaredField("target");
    target.setAccessible(true);// w w w . java  2  s  .c o m
    getThreads.setAccessible(true);
    Thread[] threads = (Thread[]) getThreads.invoke(null);
    List<Thread> result = new ArrayList<Thread>();
    for (Thread thread : threads) {
        Object runnable = target.get(thread);
        if (runnable == myRunnable)
            result.add(thread);
    }
    return result;
}

From source file:Main.java

public static void convertActivityToTranslucent(Activity activity) {
    try {/*  w w  w. j  a va2s.c o m*/
        Class<?>[] classes = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        for (Class clazz : classes) {
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
            }
        }
        Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
                translucentConversionListenerClazz);
        method.setAccessible(true);
        method.invoke(activity, new Object[] { null });
    } catch (Throwable t) {
    }
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName;/*ww  w  .  j a  v  a 2 s.  co  m*/
    try {
        if (!processName.contains(":")) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }
        activityManager.killBackgroundProcesses(packageName);
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Locates a given method anywhere in the class inheritance hierarchy.
 *
 * @param instance an object to search the method into.
 * @param name method name/*from  w w  w .j  a  v  a  2 s . c o  m*/
 * @param parameterTypes method parameter types
 * @return a method object
 * @throws NoSuchMethodException if the method cannot be located
 */
static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
        throws NoSuchMethodException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {
            Method method = clazz.getDeclaredMethod(name, parameterTypes);

            if (!method.isAccessible()) {
                method.setAccessible(true);
            }

            return method;
        } catch (NoSuchMethodException e) {
            // ignore and search next
        }
    }

    throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes)
            + " not found in " + instance.getClass());
}

From source file:Main.java

/**
 * Calling the convertToTranslucent method on platforms before Android 5.0
 */// w  w  w .j  av  a 2 s .c  o m
public static void convertActivityToTranslucentBeforeL(Activity activity) {
    try {
        Class<?>[] classes = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        for (Class<?> clazz : classes) {
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
            }
        }
        Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
                translucentConversionListenerClazz);
        method.setAccessible(true);
        method.invoke(activity, new Object[] { null });
    } catch (Throwable t) {
    }
}