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.appassit.common.Utils.java

/**
 * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from opaque to translucent following a call to {@link #convertActivityFromTranslucent(android.app.Activity)} .
 * <p>//  w  w w. j  av  a 2s  .co m
 * 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.
 */
public static void convertActivityToTranslucent(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) {
    }
}

From source file:net.sf.jabref.gui.openoffice.OpenOfficePanel.java

private static void addURL(List<URL> jarList) throws IOException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    try {/*from   w w w . ja va 2  s .c om*/
        Method method = sysclass.getDeclaredMethod("addURL", CLASS_PARAMETERS);
        method.setAccessible(true);
        for (URL anU : jarList) {
            method.invoke(sysloader, anU);
        }
    } catch (SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        LOGGER.error("Could not add URL to system classloader", e);
        throw new IOException("Error, could not add URL to system classloader", e);

    }
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private static final Class loadClass(byte[] _byteArray) throws Exception {
    //override classDefine (as it is protected) and define the class.
    Class myClass = null;//from ww  w.  j a  v a2 s .c  om

    ClassLoader classLoader = getClassLoader();
    Class classLoaderClass = Class.forName("java.lang.ClassLoader");
    Method method = classLoaderClass.getDeclaredMethod("defineClass",
            new Class[] { String.class, byte[].class, int.class, int.class });

    // protected method invocaton
    method.setAccessible(true);

    try {
        Object[] argumentArray = new Object[] { null, _byteArray, new Integer(0),
                new Integer(_byteArray.length) };
        myClass = (Class) method.invoke(classLoader, argumentArray);
    } finally {
        method.setAccessible(false);
    }

    return myClass;
}

From source file:tools.xor.util.ClassUtil.java

/**
 * Invoke the given method as a privileged action, if necessary.
 * @param target the object on which the method needs to be invoked
 * @param method to invoke//  w ww .  ja v  a 2 s.  c om
 * @param args to the method
 * @return result of the invocation
 * @throws InvocationTargetException while invoking the method
 * @throws IllegalAccessException when accessing the meta data
 */
//public static Object invokeMethodAsPrivileged(final Object target, final Method method, final Object[] args) 
public static Object invokeMethodAsPrivileged(final Object target, final Method method, final Object... args)
        throws InvocationTargetException, IllegalAccessException {
    if (Modifier.isPublic(method.getModifiers()))
        if (args == null)
            return method.invoke(target);
        else
            return method.invoke(target, args);
    return AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            method.setAccessible(true);
            Object result = null;
            try {
                if (args == null)
                    result = method.invoke(target);
                else
                    result = method.invoke(target, args);
            } catch (Exception e) {
                throw wrapRun(e);
            }
            return result;
        }
    });
}

From source file:com.smart.utils.ReflectionUtils.java

/**
 * ,private/protected.//from   w  w  w . j a  v  a 2s .co m
 */
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
        final Object[] parameters) throws InvocationTargetException {
    Method method = getDeclaredMethod(object, methodName, parameterTypes);
    if (method == null)
        throw new IllegalArgumentException(
                "Could not find method [" + methodName + "] on target [" + object + "]");
    method.setAccessible(true);
    try {
        return method.invoke(object, parameters);
    } catch (IllegalAccessException e) {
        logger.error("??:{}", e);
    }

    return null;
}

From source file:net.ftb.util.OSUtils.java

private static long getOSMemory(String methodName, String warning) {
    long ram = 0;

    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    Method m;
    try {/*from ww w .j  a v  a  2 s  .c om*/
        m = operatingSystemMXBean.getClass().getDeclaredMethod(methodName);
        m.setAccessible(true);
        Object value = m.invoke(operatingSystemMXBean);
        if (value != null) {
            ram = Long.valueOf(value.toString()) / 1024 / 1024;
        } else {
            Logger.logWarn(warning);
            ram = 1024;
        }
    } catch (Exception e) {
        Logger.logError("Error while getting OS memory info", e);
    }

    return ram;
}

From source file:net.sf.jabref.openoffice.OpenOfficePanel.java

private static void addURL(List<URL> jarList) throws IOException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    try {//w  ww  . j a va  2s . c  o m
        Method method = sysclass.getDeclaredMethod("addURL", CLASS_PARAMETERS);
        method.setAccessible(true);
        for (URL anU : jarList) {
            method.invoke(sysloader, anU);
        }
    } catch (SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        LOGGER.error("Could not add URL to system classloader", e);
        throw new IOException("Error, could not add URL to system classloader");

    }
}

From source file:com.medallia.spider.api.StRenderer.java

/** @return the action method of the given class; throws AssertionError if no such method exists */
private static Method findActionMethod(Class<?> clazz) {
    Method am = ACTION_METHOD_MAP.get(clazz);
    if (am != null)
        return am;

    for (Method m : CollUtils.concat(Arrays.asList(clazz.getMethods()),
            Arrays.asList(clazz.getDeclaredMethods()))) {
        if (m.getName().equals("action")) {
            int modifiers = m.getModifiers();
            if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers))
                continue;
            m.setAccessible(true);
            ACTION_METHOD_MAP.put(clazz, m);
            return m;
        }//from  www  .jav  a2  s.  c om
    }
    throw new AssertionError("No action method found in " + clazz);
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static Object readPseudoPropertyValueNamed0(String propertyName, Object bean) {
    Object value = null;//from w  w w.  j a  v a  2 s. c o  m
    try {
        PropertyDescriptor pd = findPropertyDescriptorFor(bean.getClass(), propertyName);
        // ???????
        if (pd != null) {
            Method reader = pd.getReadMethod();
            if (reader != null) {
                reader.setAccessible(true);
                value = reader.invoke(bean);
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("E-UTIL#0012"), replace));
                }
            }
            // ???????
        } else {
            Field f = bean.getClass().getField(propertyName);
            if (f != null && !Modifier.isStatic(f.getModifiers())) {
                f.setAccessible(true);
                value = f.get(bean);
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("D-UTIL#0019"), replace));
                }
            }
        }
    } catch (SecurityException e) {
        throw new InternalException(Beans.class, "E-UTIL#0010", e);
    } catch (NoSuchFieldException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0011", replace, e);
    } catch (IllegalArgumentException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0012", replace, e);
    } catch (IllegalAccessException e) {
        throw new InternalException(Beans.class, "E-UTIL#0013", e);
    } catch (InvocationTargetException e) {
        Map<String, String> replace = new HashMap<String, String>(2);
        replace.put("class", bean.getClass().getName());
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0014", replace, e);
    }
    return value;
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * ?DeclaredMethod ?.//ww w .j  a v a 2s.  c om
 * 
 * @param targetClass
 *            Class
 * @param ignoreParent
 *            ??,?Method
 * 
 * @return List
 */
public static List<Method> getAccessibleMethods(final Class targetClass, boolean ignoreParent) {
    Assert.notNull(targetClass, "targetClass?");
    List<Method> methods = new ArrayList<Method>();

    Class<?> superClass = targetClass;
    do {
        Method[] result = superClass.getDeclaredMethods();

        if (!ArrayUtils.isEmpty(result)) {

            for (Method method : result) {
                method.setAccessible(true);
            }

            CollectionUtils.addAll(methods, result);
        }

        superClass = superClass.getSuperclass();
    } while (superClass != Object.class && !ignoreParent);

    return methods;
}