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 String getSystemProperties(String property) {
    try {/*from w  w w  .  j  a  va  2 s  .  c om*/
        Method getStringMethod = Build.class.getDeclaredMethod("getString", String.class);
        getStringMethod.setAccessible(true);
        return (String) getStringMethod.invoke(null, property);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

static public boolean setPinConfig(Class btClass, BluetoothDevice btDevice, boolean config) throws Exception {

    Method removeBondMethod = btClass.getDeclaredMethod("setPairingConfirmation",
            new Class[] { boolean.class });
    Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[] { config });
    return true;/*from  w w  w  .  java2s  . c  o  m*/
}

From source file:Main.java

public static Object invokeMethod(Method method, Object receiver, Object... args) {
    try {//from w w  w.  j  a va  2  s  .c  om
        return method.invoke(receiver, args);
    } catch (IllegalArgumentException var4) {
        ;
    } catch (IllegalAccessException var5) {
        ;
    } catch (InvocationTargetException var6) {
        ;
    }

    return null;
}

From source file:Main.java

public static void setActionModeHeaderHidden(final ActionBar actionbar, final boolean hidden) {
    try {//from ww w  .j  a  va2  s . co m
        final Method method = ActionBar.class.getMethod("setActionModeHeaderHidden",
                new Class[] { boolean.class });
        method.invoke(actionbar, hidden);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Retrieves a system property//from  ww  w . ja va2 s.  c om
 * @param key the property key
 * @param def the value to be returned if the key could not be resolved
 */
public static String getSystemProperty(String key, String def) {
    try {
        Method getString = Build.class.getDeclaredMethod("getString", String.class);
        getString.setAccessible(true);
        return getString.invoke(null, key).toString();
    } catch (Exception ex) {
        return def;
    }
}

From source file:cf.spring.servicebroker.AccessorUtils.java

/**
 * Invokes the method of the specified object with some method arguments.
 *//*  w  ww  .j  a v  a  2s  .c  o  m*/
public static Object invokeMethod(Object object, Method method, Object... args) throws Throwable {
    try {
        return method.invoke(object, args);
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}

From source file:com.angstoverseer.util.ReflectionUtil.java

public static Object invokeMethod(Object target, String methodName, Object... parameters) {
    try {//from  www  .  ja va2s  . c o  m
        final Method method = extractMethod(target, methodName);
        method.setAccessible(true);
        return method.invoke(target, parameters);
    } catch (Exception e) {
        throw new RuntimeException("Could not invoke method.", e);
    }
}

From source file:Main.java

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

From source file:Main.java

public static String getTreadStackTrace(Thread t) {
    if (java14) {
        return "";
    }/*from ww w  .  j a v a  2s.  com*/
    try {
        // Java 1.5 thread.getStackTrace();
        Method m = t.getClass().getMethod("getStackTrace", null);

        StackTraceElement[] trace = (StackTraceElement[]) m.invoke(t, null);
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < trace.length; i++) {
            b.append("\n\tat ").append(trace[i]);
        }
        return b.toString();
    } catch (Throwable e) {
        java14 = true;
        return "";
    }
}

From source file:BrowserLauncher.java

public static void openURL(String url) {
    int os = PlatformDetector.detect();

    try {//from   w  w  w .j a va2  s .c o m
        if (os == PlatformDetector.MACOS) {
            Class macUtils = Class.forName("com.apple.mrj.MRJFileUtils");
            Method openURL = macUtils.getDeclaredMethod("openURL", new Class[] { String.class });
            openURL.invoke(null, new Object[] { url });
        } else if (os == PlatformDetector.WINDOWS)
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        else { // assume Unix or Linux
            String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
            String browser = null;
            for (int count = 0; count < browsers.length && browser == null; count++)
                if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
                    browser = browsers[count];
            if (browser == null)
                throw new Exception("Could not find web browser.");
            else
                Runtime.getRuntime().exec(new String[] { browser, url });
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
    }
}