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 Collection extractField(Collection in, String fieldName, Class type) throws SecurityException,
        NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (type == Boolean.class || type == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$
    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();//from w w w .  j  av a  2 s.c  o m
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        out.add(value2);

    }
    return out;
}

From source file:BrowserLaunch.java

public static void launch(String url) {
    try {/*from   w  ww. j  a  v a  2 s  .  c o  m*/
        if (isMacos) {
            Class fileMgr = Class.forName("com.apple.eio.FileManager");
            Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });

            openURL.invoke(null, new Object[] { url });
        } else if (isWindows)
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        else {
            if (BROWSER == null)
                throw new Exception("Could not find web browser");
            else
                Runtime.getRuntime().exec(new String[] { BROWSER, url });
        }
    } catch (Exception e) {
        System.out.println(
                "Unable to detect/startup your browser... please go to " + url + " for futher instructions");
    }
}

From source file:Main.java

public static Object safelyInvokeMethod(Method method, Object receiver, Object... args) {
    try {//ww w .j  a  v  a2s  . c  o  m
        return method.invoke(receiver, args);
    } catch (IllegalArgumentException e) {
        Log.e("Safe invoke fail", "Invalid args", e);
    } catch (IllegalAccessException e) {
        Log.e("Safe invoke fail", "Invalid access", e);
    } catch (InvocationTargetException e) {
        Log.e("Safe invoke fail", "Invalid target", e);
    }

    return null;
}

From source file:Main.java

public static void scrollByOffset(ListView v, int off) {
    //v.smoothScrollByOffset(pos);
    try {//from  w  ww  . j ava 2  s .  com
        Method m = v.getClass().getMethod("smoothScrollByOffset", new Class[] { int.class });
        m.invoke(v, off);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void scrollToPosition(ListView v, int pos) {
    //v.smoothScrollToPosition(pos);
    try {/*from ww  w . j av a  2 s  . c  om*/
        Method m = v.getClass().getMethod("smoothScrollToPosition", new Class[] { int.class });
        m.invoke(v, pos);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void putInHashMapByGivenAttribute(HashMap map, Object obj, String attribName) {
    try {//www. ja v a  2 s  .  c om
        Class class1 = obj.getClass();
        String methodName = "get" + Character.toUpperCase(attribName.charAt(0)) + attribName.substring(1);
        Method method = class1.getMethod(methodName, new Class[0]);
        Object attributeValue = method.invoke(obj, new Object[0]);
        map.put(attributeValue, obj);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void invokeObjMethod(Object ob, String methodName, Runnable value) {

    if (ob == null) {
        return;//from  www  .  j a v  a 2 s.  c o m
    }
    try {
        Method method = ob.getClass().getMethod(methodName, Runnable.class);
        method.invoke(ob, value);
    } catch (Exception e) {
        Log.d(TAG, "invokeObjMethod error", e);
    }
}

From source file:Main.java

public static Collection invokeForEach(Collection in, String methodName, Object[] args)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();

    Class[] argsClasses = new Class[args.length];
    for (int i = 0; i < args.length; i++)
        argsClasses[i] = args[i].getClass();

    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();//w ww.  j  a  va2s. c  om

        Method m = obj.getClass().getMethod(methodName, argsClasses);
        Object value2 = m.invoke(obj, args);
        out.add(value2);

    }
    return out;
}

From source file:Main.java

public static boolean setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm)
        throws Exception {
    Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
    return (boolean) setPairingConfirmation.invoke(device, isConfirm);
}

From source file:Main.java

public static void invokeObjMethod(Object ob, String methodName, CharSequence value) {

    if (ob == null) {
        return;//  w  w  w. jav a  2  s . co  m
    }
    try {
        Method method = ob.getClass().getMethod(methodName, CharSequence.class);
        method.invoke(ob, value);
    } catch (Exception e) {
        Log.d(TAG, "invokeObjMethod error", e);
    }
}