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 getSerialNumber() {
    String serial = null;//  www.  j a  va  2s  .com
    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);
        serial = (String) get.invoke(c, "ro.serialno");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return serial;
}

From source file:Main.java

public static void unpairDevice(BluetoothDevice device) {
    try {/*w  w w  .  ja  v  a  2  s . co  m*/
        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
    } catch (Exception e) {
        Log.d("BlueUtils", e.getMessage());
    }
}

From source file:Main.java

public static Object getValueByMethodName(Object o, String methodName) {
    try {/*from w w  w.  j a  va2 s  .c  o  m*/
        Method method = o.getClass().getMethod(methodName, new Class[] {});
        Object value = method.invoke(o, new Object[] {});
        return value;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static String getDeviceId(Activity activity) {
    String deviceId = "";

    try {// w w w .j a v  a  2  s .com
        Class<?> utils = Class.forName("com.lk.sdk.Utils");
        Method idMethod = utils.getMethod("id", Context.class);
        deviceId = (String) idMethod.invoke(null, activity);
    } catch (Exception e) {

    }

    return deviceId;
}

From source file:Main.java

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

From source file:Main.java

public static ComponentUI getUI(JComponent component) {
    try {//  www .  ja  v  a2 s  . c o m
        Method method = component.getClass().getMethod("getUI", new Class[0]);
        Object o = method.invoke(component, new Object[0]);
        if (o instanceof ComponentUI)
            return (ComponentUI) o;
        else
            return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static boolean has(int key) {
    Method method = getHasMethod(data.getClass());
    try {/*  w w w .  j  a va  2s  . c om*/
        return (Boolean) method.invoke(data, key);
    } catch (Exception e) {
    }

    return false;
}

From source file:GetColor.java

public static void setObjectColor(Object obj, Color color) {
    Class cls = obj.getClass(); //#1

    try {/*from   ww  w  . ja va  2s  .c o m*/
        Method method = cls.getMethod("setColor", //#2
                new Class[] { Color.class });

        method.invoke(obj, new Object[] { color }); //#3
    } catch (NoSuchMethodException ex) { //#4
        throw new IllegalArgumentException(cls.getName() + " does not support" + "method setColor(:Color)");
    } catch (IllegalAccessException ex) { //#5
        throw new IllegalArgumentException(
                "Insufficient access permissions to call" + "setColor(:Color) in class " + cls.getName());
    } catch (InvocationTargetException ex) { //#6
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static Object invokeObjectMethod(Object object, String methodName, Class[] argsClass, Object[] args) {
    Object returnValue = null;/*from w  w w  .java  2  s  .  co  m*/
    try {
        Class<?> c = object.getClass();
        Method method;
        method = c.getMethod(methodName, argsClass);
        returnValue = method.invoke(object, args);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnValue;
}

From source file:Main.java

public static String getSystemProperties(String key) {
    try {//w w w.  j  a va  2s.c  o m
        Class osSystem = Class.forName("android.os.SystemProperties");
        Method getInvoke = osSystem.getMethod("get", new Class[] { String.class });
        return (String) getInvoke.invoke(osSystem, new Object[] { key });
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return "";
}