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:net.jselby.pc.bukkit.BukkitLoader.java

public static void addToClasspath(File file) throws Exception {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(ClassLoader.getSystemClassLoader(), new Object[] { file.toURI().toURL() });
}

From source file:com.hortonworks.minicluster.util.ReflectionUtils.java

/**
 *
 * @param clazz//from w  ww. j  a v a 2  s  .com
 * @param name
 * @param arguments
 * @return
 */
public static Method getMethodAndMakeAccessible(Class<?> clazz, String name, Class<?>... arguments) {
    ObjectAssertUtils.assertNotNull(clazz);
    StringAssertUtils.assertNotEmptyAndNoSpaces(name);

    try {
        Method m = org.springframework.util.ReflectionUtils.findMethod(clazz, name, arguments);
        m.setAccessible(true);
        return m;
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {
    /*String cmd = "kill -9 "+pid;
    Process process = null;/*  w  w w  .jav a 2  s .com*/
    DataOutputStream os = null;
    try {
      process = Runtime.getRuntime().exec("su"); 
      os = new DataOutputStream(process.getOutputStream());
      os.writeBytes(cmd + "\n");
      os.writeBytes("exit\n");
      os.flush();
      process.waitFor();
    } catch (Exception e) {
      e.printStackTrace();
    }
    AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);*/

    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

/**
 * set method is accessible/*  w  w w.j a v a 2 s. c o m*/
 * 
 * @param method {@link java.lang.reflect.Method}
 */
public static void makeAccessible(final Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);
    }
}

From source file:Main.java

public static Service createService(Context context, ServiceInfo serviceInfo) throws Exception {
    IBinder token = new Binder();

    Class<?> createServiceDataClass = Class.forName("android.app.ActivityThread$CreateServiceData");
    Constructor<?> constructor = createServiceDataClass.getDeclaredConstructor();
    constructor.setAccessible(true);/*www .  j  av a2  s  .  c o  m*/
    Object createServiceData = constructor.newInstance();

    Field tokenField = createServiceDataClass.getDeclaredField("token");
    tokenField.setAccessible(true);
    tokenField.set(createServiceData, token);

    serviceInfo.applicationInfo.packageName = context.getPackageName();
    Field infoField = createServiceDataClass.getDeclaredField("info");
    infoField.setAccessible(true);
    infoField.set(createServiceData, serviceInfo);

    Class<?> compatibilityClass = Class.forName("android.content.res.CompatibilityInfo");
    Field defaultCompatibilityField = compatibilityClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
    defaultCompatibilityField.setAccessible(true);
    Object defaultCompatibility = defaultCompatibilityField.get(null);
    defaultCompatibilityField.set(createServiceData, defaultCompatibility);

    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
    Object currentActivityThread = currentActivityThreadMethod.invoke(null);

    Method handleCreateServiceMethod = activityThreadClass.getDeclaredMethod("handleCreateService",
            createServiceDataClass);
    handleCreateServiceMethod.setAccessible(true);
    handleCreateServiceMethod.invoke(currentActivityThread, createServiceData);

    Field mServicesField = activityThreadClass.getDeclaredField("mServices");
    mServicesField.setAccessible(true);
    Map mServices = (Map) mServicesField.get(currentActivityThread);
    Service service = (Service) mServices.get(token);
    mServices.remove(token);

    return service;
}

From source file:com.abiquo.model.util.ModelTransformer.java

private static Method setter(final String fieldName, final Class clazz, final Class type) throws Exception {
    String name = "set" + StringUtils.capitalize(fieldName);
    Method method = clazz.getMethod(name, new Class[] { type });

    if (method != null) {
        method.setAccessible(true);
    }/* w  w w  .  j  ava  2 s.com*/
    return method;
}

From source file:Main.java

/**
 * Variant of set() to establish initialValue. Used instead
 * of set() in case user has overridden the set() method.
 *
 * @return the initial value//w w  w . j a  v a2s  . c o m
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 * @throws NoSuchFieldException 
 */
@SuppressWarnings("unchecked")
private static <T extends Object> T setInitialValue(Thread thread, ThreadLocal<T> threadLocal)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException {
    T value = null;
    // value = threadLocal.initialValue();
    Method initialValueMethod = ThreadLocal.class.getDeclaredMethod("initialValue", (Class<?>[]) null);
    initialValueMethod.setAccessible(true);
    value = (T) initialValueMethod.invoke(threadLocal, (Object[]) null);

    Object map = getMap(thread);
    if (map != null) {
        // map.set(this, value);
        Method setMethod = map.getClass().getDeclaredMethod("set",
                new Class<?>[] { ThreadLocal.class, Object.class });
        setMethod.setAccessible(true);
        setMethod.invoke(map, new Object[] { threadLocal, value });
    } else {
        //createMap(t, value);
        createMap(thread, threadLocal, value);
    }
    return value;
}

From source file:jin.collection.util.PropertyUtil.java

static Object getMethodResult(final Object element, final String methodName, final Object param)
        throws Exception {

    Class[] classes = null;//from w  ww  . jav a  2  s.  c  o  m
    Object[] params = null;
    if (param != null) {
        classes = new Class[] { param.getClass() };
        params = new Object[] { param };
    }

    // TODO: sistemare
    final Method m = element.getClass().getMethod(methodName, classes);
    m.setAccessible(true);
    return m.invoke(element, params);
}

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;// ww w  . j a  v a2 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) {
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    Log.i("AppUtil", 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:dyco4j.instrumentation.internals.CLI.java

private static void extendClassPath(final CommandLine cmdLine) throws IOException {
    try {/* w w  w  .  j  a va2  s  .  c o  m*/
        final URLClassLoader _urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        final Class<URLClassLoader> _urlClass = URLClassLoader.class;
        final Method _method = _urlClass.getDeclaredMethod("addURL", URL.class);
        _method.setAccessible(true);
        addEntryToClassPath(_urlClassLoader, _method, cmdLine.getOptionValue(IN_FOLDER_OPTION));
        final String _classpathConfig = cmdLine.getOptionValue(CLASSPATH_CONFIG_OPTION);
        if (_classpathConfig != null) {
            for (final String _s : Files.readAllLines(Paths.get(_classpathConfig))) {
                addEntryToClassPath(_urlClassLoader, _method, _s);
            }
        }
    } catch (final NoSuchMethodException _e) {
        throw new RuntimeException(_e);
    }
}