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 boolean setPairingConfirmation(Class btClass, BluetoothDevice device, boolean confirm)
        throws Exception {
    Method setPairiConfirmMethod = btClass.getMethod("setPairingConfirmation", boolean.class);
    Boolean retVal = (Boolean) setPairiConfirmMethod.invoke(device, confirm);
    return retVal.booleanValue();
}

From source file:Main.java

/**
 * @param type/*w  w  w.  jav  a  2 s.co  m*/
 * @param object
 * @param field
 * @param value
 */
public static <T> void setFieldValue(Class<? extends T> type, T object, Field field, Object value) {
    try {
        Method m = type.getMethod("set" + getFirstLetterUppercased(field.getName()), field.getType());
        m.invoke(object, value);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void actionBarSetDisplayHomeAsUpEnabled(Object actionBar, boolean arg) {
    if (actionBar == null)
        return;// w w w. ja v a 2  s .c  o m

    try {
        Method m = actionBar.getClass().getMethod("setDisplayHomeAsUpEnabled", boolean.class);
        m.invoke(actionBar, arg);
    } catch (NoSuchMethodException ignore) {
    } catch (InvocationTargetException ignore) {
    } catch (IllegalAccessException ignore) {
    }
}

From source file:Main.java

/**
 * set page cache/*from  ww  w  . j  a  va  2s  . c om*/
 *
 * @param settings
 * @param capacity
 */
public static void setWebViewPageCache(WebSettings settings, int capacity) {
    try {
        Method setPageCache = settings.getClass().getMethod("setPageCacheCapacity", new Class[] { int.class });
        setPageCache.invoke(settings, new Object[] { capacity });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void addtoClassLoader(URL url) throws IOException, NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> sysclass = URLClassLoader.class;
    Method method = sysclass.getDeclaredMethod("addURL", parameters);
    method.setAccessible(true);/*www.  ja va2 s. c o  m*/
    method.invoke(sysloader, new Object[] { url });
}

From source file:Main.java

public static boolean setProperty(Object object, String fieldName, Object fieldValue) {
    Class<?> clazz = object.getClass();
    String mname = "set" + fieldName;
    try {//from ww  w.j  ava2  s. c o  m
        for (Method m : clazz.getMethods()) {
            if (m.getName().equalsIgnoreCase(mname)) {
                m.invoke(object, fieldValue);
                return true;
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    return false;
}

From source file:Main.java

public static void invokeSet(Object o, String fieldName, Object value) {

    Method method = getSetMethod(o.getClass(), fieldName);

    try {/*from w  ww.  j a  va  2  s  .  c  om*/

        method.invoke(o, new Object[] { value });

    } catch (Exception e) {

        e.printStackTrace();

    }

}

From source file:Main.java

public static Object invokeGet(Object o, String fieldName) {

    Method method = getGetMethod(o.getClass(), fieldName);

    try {/*from  w  w w. j  av a 2  s .  com*/

        return method.invoke(o, new Object[0]);

    } catch (Exception e) {

        e.printStackTrace();

    }

    return null;

}

From source file:Main.java

static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
    try {/*from   w w w  .  j  a v a  2s. c o  m*/
        Method method = btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
        Boolean value = (Boolean) method.invoke(btDevice, new Object[] { str.getBytes() });
        Log.e(TAG, "" + value);
    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, "setPin error++");
        return false;
    }
    return true;
}

From source file:Main.java

public static void setIconEnable(Menu menu, boolean enable) {
    try {/*ww w. j  ava 2  s.  c  o  m*/
        Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
        Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
        m.setAccessible(true);
        m.invoke(menu, enable);

    } catch (Exception e) {
        e.printStackTrace();
    }
}