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

/**
 * Invoke the specified {@link Method} against the supplied target object with the
 * supplied arguments. The target object can be <code>null</code> when invoking a
 * static {@link Method}.//  w  w  w  .  j av  a2s . co  m
 *
 * @param method the method to invoke
 * @param target the target object to invoke the method on
 * @param args   the invocation arguments (may be <code>null</code>)
 * @return the invocation result, if any
 */
public static Object invokeMethod(Method method, Object target, Object... args) {
    try {
        return method.invoke(target, args);
    } catch (Exception ex) {
        Log.d(TAG, "invokeMethod method= " + method, ex);
    }
    return null;
}

From source file:Main.java

/** Attempts to set the camera's flash mode. Returns true if successful, false if the Android API doesn't support setting flash modes.
 */// w  w w. ja va  2  s. co m
public static boolean setFlashMode(Camera camera, String mode) {
    Camera.Parameters params = camera.getParameters();
    try {
        Method flashModeMethod = params.getClass().getMethod("setFlashMode", String.class);
        flashModeMethod.invoke(params, mode);
        camera.setParameters(params);
        return true;
    } catch (Exception ignored) {
        return false;
    }
}

From source file:Main.java

public static AssetManager createAssetManager(String dexPath) {
    try {/*from   w  w w . j  av a  2s.  c  o m*/
        AssetManager assetManager = AssetManager.class.newInstance();
        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
        addAssetPath.invoke(assetManager, dexPath);
        return assetManager;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean getBoolean(int key) {
    Method method = getBooleanMethod(data.getClass());
    try {//ww w  .  java2 s.co m
        return (Boolean) method.invoke(data, key);
    } catch (Exception e) {
    }

    return false;
}

From source file:Main.java

public static Context getContext() {
    try {/*from w w  w.  j a  va 2s .com*/
        Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
        Method method = activityThreadClass.getMethod("currentApplication");
        return (Application) method.invoke(null, (Object[]) null);
    } catch (Exception e) {
    }

    return null;
}

From source file:Main.java

public static boolean setPin(BluetoothDevice device, String str) throws Exception {
    Method setPinMethod = device.getClass().getMethod("setPin", new Class[] { byte[].class });
    return (Boolean) setPinMethod.invoke(device, new Object[] { str.getBytes() });
}

From source file:Main.java

private static Object invokeGetter(Method getter, Object object) {
    try {/*from www  .  j  a  va  2  s.c o  m*/
        getter.setAccessible(true);
        return getter.invoke(object, new Object[0]);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException(e);
    }
}

From source file:TestSample.java

public static void run(Class which, String[] args) {
    TestSuite suite = null;//from  ww w  . j  a  v a2s.  com
    if (args.length != 0) {
        try {
            java.lang.reflect.Constructor ctor;
            ctor = which.getConstructor(new Class[] { String.class });
            suite = new TestSuite();
            for (int i = 0; i < args.length; i++) {
                suite.addTest((TestCase) ctor.newInstance(new Object[] { args[i] }));
            }
        } catch (Exception e) {
            System.err.println("Unable to instantiate " + which.getName() + ": " + e.getMessage());
            System.exit(1);
        }

    } else {
        try {
            Method suite_method = which.getMethod("suite", new Class[0]);
            suite = (TestSuite) suite_method.invoke(null, null);
        } catch (Exception e) {
            suite = new TestSuite(which);
        }
    }
    junit.textui.TestRunner.run(suite);
}

From source file:Main.java

public static boolean cancelPairingUserInput(BluetoothDevice device) throws Exception {
    Method cancelPairingUserInputMethod = device.getClass().getMethod("cancelPairingUserInput");
    return (Boolean) cancelPairingUserInputMethod.invoke(device, true);
}

From source file:Main.java

public static Object containsClassAttributeInCollection(Collection in, String fieldName, Object value)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (value instanceof Boolean || value == 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$

    while (it.hasNext()) {
        Object obj = it.next();/*from  w  w  w  .ja  va  2 s. c o m*/
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        if (value != null && value2 != null && value.equals(value2))
            return obj;
    }
    return null;
}