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 boolean isMobileEnabled(Context context) {
    try {//from   www.  jav a2 s. c om
        Method e = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled", new Class[0]);
        e.setAccessible(true);
        return ((Boolean) e.invoke(getConnManager(context), new Object[0])).booleanValue();
    } catch (Exception var2) {
        var2.printStackTrace();
        return true;
    }
}

From source file:coral.CoralStart.java

private static void loadSwtJar(String swtPath) {
    if (swtPath == null) {
        String osName = System.getProperty("os.name").toLowerCase();
        String osArch = System.getProperty("os.arch").toLowerCase();

        String swtFileNameOsPart = osName.contains("win") ? "win32"
                : osName.contains("mac") ? "osx"
                        : osName.contains("linux") || osName.contains("nix") ? "linux" : "";

        String swtFileNameArchPart = osArch.contains("64") ? "_x86_64" : "";
        swtPath = "lib/swt-" + swtFileNameOsPart + swtFileNameArchPart + ".jar";
    }//from w w  w  .j a va2 s . c o  m

    try {
        URL swtFileUrl = new URL("file:" + swtPath);

        URLClassLoader classLoader = (URLClassLoader) CoralHead.class.getClassLoader();
        Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
        addUrlMethod.setAccessible(true);

        addUrlMethod.invoke(classLoader, swtFileUrl);
        System.out.println("Added the swt jar to the class path: " + swtPath);
    } catch (Exception e) {
        System.out.println("Unable to add the swt jar to the class path: " + swtPath);
        lastresort("Unable to add the swt jar to the class path: " + swtPath + " ", e);
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean isWindowActive(Window window) {
    if (getJavaVersion() >= 1.4) {
        try {//from  w  ww . j a v  a2 s  .  co m
            Class paramTypes[] = null;
            Object args[] = null;
            Method m = window.getClass().getMethod("isActive", paramTypes);
            Boolean b = (Boolean) m.invoke(window, args);
            return b.booleanValue();
        } catch (Exception ex) {
        }
    }
    return true;
}

From source file:Main.java

/**
 * Get the default charset/*from ww  w .  java  2  s. com*/
 * 
 * @return The default charset
 */
public static final String getDefaultCharsetName() throws Exception {
    String defaultCharset;

    try {
        // Try with jdk 1.5 method, if we are using a 1.5 jdk :)
        Method method = Charset.class.getMethod("defaultCharset", new Class[0]);
        defaultCharset = ((Charset) method.invoke(null, new Object[0])).name();
    } catch (NoSuchMethodException nsme) {
        defaultCharset = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
    } catch (InvocationTargetException ite) {
        defaultCharset = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
    } catch (IllegalAccessException iea) {
        defaultCharset = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
    } catch (RuntimeException e) {
        // fall back to old method
        defaultCharset = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
    }

    return defaultCharset;
}

From source file:net.ceos.project.poi.annotated.core.CellCommentHandler.java

/**
 * Apply a explicit formula value at the Cell.
 * //  www  . j ava 2 s . c  om
 * @param object
 *            the object
 * @param method
 *            the method will be read
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private static Object applyCommentRules(final Object object, final String method)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (StringUtils.isNotBlank(method)) {
        @SuppressWarnings("rawtypes")
        Class[] argTypes = {};

        Method m = object.getClass().getDeclaredMethod(method, argTypes);

        return m.invoke(object, (Object[]) null);
    }
    return true;
}

From source file:org.jdal.annotation.AnnotatedElementAccessor.java

/**
 * Try to set a value on AnnotatedElement.
 * @param element the annotated element.
 * @param value value to set.//w  w w.  j a v  a 2s.c  o  m
 */
public static void setValue(AnnotatedElement element, Object target, Object value) {
    if (element instanceof Field) {
        Field field = (Field) element;
        ReflectionUtils.makeAccessible(field);
        ReflectionUtils.setField(field, target, value);
    } else if (element instanceof Method) {
        Method method = (Method) element;
        try {
            method.invoke(target, new Object[] { value });
        } catch (Exception e) {
            log.error("Cannot set value on method [" + method.toString() + "]");
        }
    }
}

From source file:com.codebase.foundation.classloader.xbean.ClassLoaderUtil.java

/**
 * Releases the specified classloader from the Apache Jakarta Commons Logging class loader cache using reflection.
 * @param classLoader the class loader to release
 *///from   w  ww .java  2s. c  o  m
@SuppressWarnings("all")
public static void releaseCommonsLoggingCache(ClassLoader classLoader) {
    try {
        Class logFactory = classLoader.loadClass("org.apache.commons.logging.LogFactory");
        Method release = logFactory.getMethod("release", new Class[] { ClassLoader.class });
        release.invoke(null, new Object[] { classLoader });
    } catch (Throwable ignored) {
        // there is nothing a user could do about this anyway
    }
}

From source file:Main.java

public static void setDataEnabled(Context context, boolean enabled) {
    try {/*from ww  w  .ja  va2 s  .  c  o  m*/
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Method setMobileDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
        if (null != setMobileDataEnabledMethod) {
            setMobileDataEnabledMethod.invoke(tm, enabled);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
static boolean refreshDeviceCache(BluetoothGatt gatt) {
    try {// ww w  .  jav  a2s .  c om
        Method localMethod = gatt.getClass().getMethod("refresh", new Class[0]);
        if (localMethod != null) {
            return (boolean) (Boolean) localMethod.invoke(gatt, new Object[0]);
        }
    } catch (Exception ignored) {
    }
    return false;
}

From source file:com.hortonworks.streamline.common.util.ReflectionHelper.java

public static <T> T invokeSetter(String propertyName, Object object, Object valueToSet)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String methodName = "set" + StringUtils.capitalize(propertyName);
    Method method = object.getClass().getMethod(methodName, valueToSet.getClass());
    return (T) method.invoke(object, valueToSet);
}