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

/**
 * Convert a translucent themed Activity
 * {@link android.R.attr#windowIsTranslucent} back from opaque to
 * translucent following a call to/*from   www  .  j a v a 2  s . c o m*/
 * {@link #convertActivityFromTranslucent(Activity)} .
 * <p/>
 * Calling this allows the Activity behind this one to be seen again. Once
 * all such Activities have been redrawn
 * <p/>
 * This call has no effect on non-translucent activities or on activities
 * with the {@link android.R.attr#windowIsFloating} attribute.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("rawtypes")
public static void convertActivityToTranslucent(Activity activity) {

    try {
        Class[] t = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        Class[] method = t;
        int len$ = t.length;

        for (int i$ = 0; i$ < len$; ++i$) {
            Class<?> clazz = method[i$];
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
                break;
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Method var8 = Activity.class.getDeclaredMethod("convertToTranslucent",
                    translucentConversionListenerClazz, ActivityOptions.class);
            var8.setAccessible(true);
            var8.invoke(activity, new Object[] { null, null });
        } else {
            Method var8 = Activity.class.getDeclaredMethod("convertToTranslucent",
                    translucentConversionListenerClazz);
            var8.setAccessible(true);
            var8.invoke(activity, new Object[] { null });
        }
    } catch (Throwable e) {
    }
}

From source file:jetbrains.exodus.entitystore.EntitySnapshotTests.java

private static Object executeMethod(Object obj, String methodName) {
    try {/*from  w  w  w. j av  a 2 s. c om*/
        final Method method = obj.getClass().getDeclaredMethod(methodName);
        method.setAccessible(true);
        return method.invoke(obj);
    } catch (Throwable t) {
        return t;
    }
}

From source file:Main.java

/**
 * Returns the value in the current thread's copy of this
 * thread-local variable.  If the variable has no value for the
 * current thread, it is first initialized to the value returned
 * by an invocation of the {@link #initialValue} method.
 *
 * @return the current thread's value of this thread-local
 * @throws NoSuchMethodException //  w ww  .j a  v a 2  s.  c o m
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 * @throws NoSuchFieldException 
 */
@SuppressWarnings("unchecked")
public static <T extends Object> T get(Thread thread, ThreadLocal<T> threadLocal)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException {
    Object map = getMap(thread); // ThreadLocalMap
    if (map != null) {
        // calling getEntry returns a ThreadLocal.Entry instance, which is a
        // mapping from a ThreadLocal to an Entry, and an Entry is an 
        // extension of WeakReference.
        // ThreadLocalMap.Entry e = map.getEntry(this);
        Method getEntryMethod = map.getClass().getDeclaredMethod("getEntry", new Class[] { ThreadLocal.class });
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(map, new Object[] { threadLocal }); // ThreadLocalMap.Entry

        if (entry != null) {
            Field valueField = entry.getClass().getDeclaredField("value");
            valueField.setAccessible(true);
            return (T) valueField.get(entry);
        }
    }
    return setInitialValue(thread, threadLocal);
}

From source file:Main.java

public static Object invokeMethod(Class clazz, Object object, String methodName, Class[] cls, Object[] args)
        throws Exception {
    Method method;
    if (null == cls) {
        method = clazz.getDeclaredMethod(methodName);
    } else {//from   ww  w .ja v a  2s .c  om
        method = clazz.getDeclaredMethod(methodName, cls);
    }

    method.setAccessible(true);

    if (null == args) {
        return method.invoke(object);
    } else {
        return method.invoke(object, args);
    }
}

From source file:Main.java

static Method getMethod(final Class<?> clz, final String mtdName) {
    if (clz == null || TextUtils.isEmpty(mtdName)) {
        return null;
    }/*from  ww w. j  av a2 s  .  co  m*/
    Method mtd = null;
    try {
        for (Method m : clz.getDeclaredMethods()) {
            if (m.getName().equals(mtdName)) {
                mtd = m;
                mtd.setAccessible(true);
                break;
            }
        }
    } catch (Exception e) {
        Log.d(TAG, e.getMessage(), e);
    }
    return mtd;
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;/*from ww  w.j a va 2 s .c  o  m*/
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        os.writeBytes(Command + "\n");
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    // L.i(processName);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            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

/**
 * // w w  w . j  a  v  a  2 s  . com
 * @param <T>
 * @param thread
 * @param threadLocal
 * @param value
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private static <T extends Object> void createMap(Thread thread, ThreadLocal<T> threadLocal, T value)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method createMapMethod = ThreadLocal.class.getDeclaredMethod("createMap",
            new Class<?>[] { Thread.class, Object.class });
    createMapMethod.setAccessible(true);
    createMapMethod.invoke(threadLocal, new Object[] { thread, value });
}

From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java

public static Object invoke(Object obj, Method method, Object... args) {
    return CheckedSupplier.of(() -> {
        method.setAccessible(true);
        return method.invoke(obj, args);
    }).get();/*w  w w. j av  a2s.  co  m*/
}

From source file:io.github.seleniumquery.functions.jquery.manipulation.HtmlFunction.java

private static String getHtmlUnitInnerHTML(HtmlUnitWebElement element) {
    WebDriver driver = element.getWrappedDriver();
    // we resort to JavaScript when it is enabled
    if (((HtmlUnitDriver) driver).isJavascriptEnabled()) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        return js.executeScript("return arguments[0].innerHTML", element).toString();
    }//from   ww w.  j  a  v a2s  .  com

    // or use reflection if JS is not enabled
    // (this method is not preferred as it relies on HtmlUnit's internals, which can change without notice)
    try {
        // #HtmlUnit #reflection #hack
        Method getElementMethod = org.openqa.selenium.htmlunit.HtmlUnitWebElement.class
                .getDeclaredMethod("getElement");
        getElementMethod.setAccessible(true);

        HtmlElement he = (HtmlElement) getElementMethod.invoke(element);
        HTMLElement e = (HTMLElement) he.getScriptObject();

        return e.getInnerHTML();
    } catch (Exception e) {
        LOGGER.warn("Unable to get WebElement's innerHTML. Returning empty string.", e);
        return "";
    }
}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

/**
 * Finds a method with the specified name and parameters in the given class and makes it accessible.
 * Note: for performance, store the returned value and avoid calling this repeatedly.
 * <p>//from   www  .  j a v a  2  s.c  om
 * Throws an exception if the method is not found.
 *
 * @param clazz          The class to find the method on.
 * @param methodName     The name of the method to find (used in developer environments, i.e. "getWorldTime").
 * @param methodObfName  The obfuscated name of the method to find (used in obfuscated environments, i.e. "getWorldTime").
 *                       If the name you are looking for is on a class that is never obfuscated, this should be null.
 * @param parameterTypes The parameter types of the method to find.
 * @return The method with the specified name and parameters in the given class.
 */
@Nonnull
public static Method findMethod(@Nonnull Class<?> clazz, @Nonnull String methodName,
        @Nullable String methodObfName, Class<?>... parameterTypes) {
    Preconditions.checkNotNull(clazz);
    Preconditions.checkArgument(StringUtils.isNotEmpty(methodName), "Method name cannot be empty");

    String nameToFind;
    if (methodObfName == null || (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) {
        nameToFind = methodName;
    } else {
        nameToFind = methodObfName;
    }

    try {
        Method m = clazz.getDeclaredMethod(nameToFind, parameterTypes);
        m.setAccessible(true);
        return m;
    } catch (Exception e) {
        throw new UnableToFindMethodException(e);
    }
}