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:io.albraga.namus.OverlaypdfUI.java

private static void load() throws Exception {
    String[] args = new String[3];
    args[0] = "input.pdf";
    args[1] = "overlay.pdf";
    args[2] = "output.pdf";
    URL[] classLoaderUrls = new URL[] { new URL("file:///pdfbox-tools-2.0.4") };
    URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
    Class<?> overlaypdfClass = urlClassLoader.loadClass("org.apache.pdfbox.tools.OverlayPDF");
    Method method = overlaypdfClass.getMethod("main", new Class[] { args.getClass() });
    method.invoke(null, new Object[] { args });
}

From source file:Main.java

public static void setBackIcon(ActionBar actionbar, Drawable backIcon) {
    try {//from   w w  w .j av a  2 s  .co  m
        Method method = Class.forName("android.app.ActionBar").getMethod("setBackButtonDrawable",
                new Class[] { Drawable.class });
        try {
            method.invoke(actionbar, backIcon);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setActionModeHeaderHidden(ActionBar actionbar, boolean hidden) {
    try {//from  www. j a v  a 2  s  .  c  o m
        Method method = Class.forName("android.app.ActionBar").getMethod("setActionModeHeaderHidden",
                new Class[] { boolean.class });
        try {
            method.invoke(actionbar, hidden);
        } 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:com.networknt.info.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);/*w  w  w.  j  av a  2s .  com*/
    method.invoke(classLoader, url);
}

From source file:de.unentscheidbar.validation.internal.Methods.java

public static Object invokeQuietly(Method method, Object target, Object... params) {

    try {/*from ww  w. ja va 2  s  .  c  o  m*/
        return method.invoke(target, params);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Method not accessible");
    } catch (InvocationTargetException e) {
        /*
         * Eek! Not our fault.
         */
        throw Throwables.propagate(e);
    }
}

From source file:Main.java

public static Object invokeMethod(Object target, Class clazz, String methodName, Class[] paramTypes,
        Object[] paramValues) {//from   ww  w  .j  a v  a2s.  c  o  m
    try {
        Method method = clazz.getDeclaredMethod(methodName, paramTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        return method.invoke(target, paramValues);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * opens the URL in a browser.//from  w  ww  .j  a  v  a2  s . c o  m
 * 
 * @param parent the parent component
 * @param url the URL to open
 * @param showDialog whether to display a dialog in case of an error or just
 *          print the error to the console
 */
public static void openURL(Component parent, String url, boolean showDialog) {
    String osName = System.getProperty("os.name");
    try {
        // Mac OS
        if (osName.startsWith("Mac OS")) {
            Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
            Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
            openURL.invoke(null, new Object[] { url });
        }
        // Windows
        else if (osName.startsWith("Windows")) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        }
        // assume Unix or Linux
        else {
            String browser = null;
            for (int count = 0; count < LINUX_BROWSERS.length && browser == null; count++) {
                // look for binaries and take first that's available
                if (Runtime.getRuntime().exec(new String[] { "which", LINUX_BROWSERS[count] }).waitFor() == 0) {
                    browser = LINUX_BROWSERS[count];
                    break;
                }
            }
            if (browser == null) {
                throw new Exception("Could not find web browser");
            } else {
                Runtime.getRuntime().exec(new String[] { browser, url });
            }
        }
    } catch (Exception e) {
        String errMsg = "Error attempting to launch web browser:\n" + e.getMessage();

        if (showDialog) {
            JOptionPane.showMessageDialog(parent, errMsg);
        } else {
            System.err.println(errMsg);
        }
    }
}

From source file:Main.java

/**
 * Invoke a method./*from w w w .j  a v  a 2  s . c  o  m*/
 *
 * @param component the component to invoke the method on
 * @param method the method to invoke
 * @param type the property type
 * @param value the property value
 * @throws ReflectiveOperationException if an error occurs
 */
private static void invoke(final Object component, final Method method, final Class<?> type, final String value)
        throws ReflectiveOperationException {
    Object obj = valueOf(type, value);
    method.invoke(component, obj);
}

From source file:Main.java

public static void setActionBarViewCollapsable(ActionBar actionbar, boolean collapsable) {
    try {/*w ww.j av a  2 s.co m*/
        Method method = Class.forName("android.app.ActionBar").getMethod("setActionBarViewCollapsable",
                new Class[] { boolean.class });
        try {
            method.invoke(actionbar, collapsable);
        } 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

public static void killProcesses(Context context, int pid, String processName) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName;/*from   www  .  j  ava 2  s . co  m*/
    try {
        if (!processName.contains(":")) {
            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();
    }
}