Example usage for java.lang.reflect Method invoke

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Source Link

Document

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

Usage

From source file:Main.java

public static <T> T runPrivateMethod(Class<?> targetClass, Object obj, String methodName,
        Class<?> paramClasses[], Object params[], Class<T> returnType) throws Exception {
    T returnValue = null;/*from   ww w . jav  a2s.c  o  m*/
    Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
    if (!method.isAccessible())
        method.setAccessible(true);
    returnValue = (T) method.invoke(obj, params);
    return returnValue;
}

From source file:Main.java

/**
 * Calling the convertToTranslucent method on platforms before Android 5.0
 */// w ww  . j a va  2s .  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) {
    }
}

From source file:com.dwdesign.tweetings.util.WebViewProxySettings.java

@SuppressWarnings("rawtypes")
private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)
        throws Exception {
    final Class<?> c = object instanceof Class ? (Class) object : object.getClass();

    if (types != null) {
        final Method method = c.getMethod(methodName, types);
        return method.invoke(object, params);
    } else {//from   www .jav  a  2 s.co  m
        final Method method = c.getMethod(methodName);
        return method.invoke(object);
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

private static Object unmappedValueOfGetter(Object source, String field) {
    Object value = null;//w ww. j a  va2  s.  c  o m
    Method getter = getter(source, field);
    try {
        value = getter.invoke(source, null);
    } catch (java.lang.ClassCastException ex) {
        log.warn("valueOfGetter: No cast available for: " + field);
    } catch (Exception ex) {
        log.warn(ex.toString());
    }
    return value;
}

From source file:Main.java

public static void setActionBarTabsShowAtBottom(ActionBar actionbar, boolean showAtBottom) {
    try {/*from  w w  w . j a  v a  2s.  c  om*/
        Method method = Class.forName("android.app.ActionBar").getMethod("setTabsShowAtBottom",
                new Class[] { boolean.class });
        try {
            method.invoke(actionbar, showAtBottom);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Calling the convertToTranslucent method on platforms before Android 5.0
 *//*from ww w .j a  va 2  s.  co 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) {
    }
}

From source file:net.fabricmc.loom.task.ProcessModsTask.java

public static void addFile(File file, Object object) {
    try {//from w w  w .  ja  v  a  2s.c o m
        URLClassLoader classLoader = (URLClassLoader) object.getClass().getClassLoader();
        Class urlClassLoaderClass = URLClassLoader.class;
        Method method = urlClassLoaderClass.getDeclaredMethod("addURL", URL.class);
        method.setAccessible(true);
        method.invoke(classLoader, file.toURI().toURL());
    } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * This device's SN/*  w ww.  j a  va  2  s.c  o m*/
 *
 * @return SerialNumber
 */
public static String getSerialNumber() {
    String serialNumber = android.os.Build.SERIAL;
    if ((serialNumber == null || serialNumber.length() == 0 || serialNumber.contains("unknown"))) {
        String[] keys = new String[] { "ro.boot.serialno", "ro.serialno" };
        for (String key : keys) {
            try {
                Method systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get",
                        String.class);
                serialNumber = (String) systemProperties_get.invoke(null, key);
                if (serialNumber != null && serialNumber.length() > 0 && !serialNumber.contains("unknown"))
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return serialNumber;
}

From source file:Main.java

public static void callInjectViews(Object activity) {
    try {/*  w w w.  ja va 2  s  .  c  o m*/
        Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
        Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
        injectViewsMethod.setAccessible(true);
        injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException e) {
        propagateRuntimeException(e);
    } catch (NoSuchMethodException e) {
        propagateRuntimeException(e);
    } catch (SecurityException e) {
        propagateRuntimeException(e);
    } catch (IllegalAccessException e) {
        propagateRuntimeException(e);
    } catch (IllegalArgumentException e) {
        propagateRuntimeException(e);
    } catch (InvocationTargetException e) {
        propagateRuntimeException(e);
    }
}

From source file:com.clustercontrol.plugin.ClassUtils.java

/**
 * ??URLCLASSPATH??<br/>/*from ww w  .j av a  2s.  com*/
 * @param ?URL
 * @throws IOException
 */
public static void addURL(URL u) throws IOException {
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    for (URL url : sysLoader.getURLs()) {
        if (url.toString().equals(u.toString())) {
            log.info("URL " + u + " is already in CLASSPATH");
            return;
        }
    }

    Class<URLClassLoader> sysClass = URLClassLoader.class;
    try {
        Method method = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Throwable t) {
        log.warn(t.getMessage(), t);
        throw new IOException("could not add URL " + u + " to CLASSPATH");
    }
}