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

/**
 * Returns whether the device is voice-capable (meaning, it can do MMS).
 *//*from  w ww .  ja va  2s  .c om*/
public static boolean isMmsCapable(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null) {
        return false;
    }

    try {
        Class partypes[] = new Class[0];
        Method sIsVoiceCapable = TelephonyManager.class.getMethod("isVoiceCapable", partypes);

        Object arglist[] = new Object[0];
        Object retobj = sIsVoiceCapable.invoke(telephonyManager, arglist);
        return (Boolean) retobj;
    } catch (java.lang.reflect.InvocationTargetException ite) {
        // Failure, must be another device.
        // Assume that it is voice capable.
    } catch (IllegalAccessException iae) {
        // Failure, must be an other device.
        // Assume that it is voice capable.
    } catch (NoSuchMethodException nsme) {
    }
    return true;
}

From source file:Main.java

/**
 * Obtains selected text//from  w  w w .  j  a  v a2  s.c o m
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static String getSelectedText(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getSelectedText",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            if (result == null) {
                return null;
            }
            return (String) result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        Component childComponent = component.getComponent(i);
        if (childComponent instanceof JComponent) {
            return getSelectedText((JComponent) childComponent);
        }
    }

    return null;
}

From source file:Main.java

private static Method executeMethod(Object obj, Method executeMethod) {
    try {//from  ww w  .java2 s.c om
        if (executeMethod != null) {
            if (!executeMethod.isAccessible())
                executeMethod.setAccessible(true);
            executeMethod.invoke(obj, new Object[] {});
        }
    } catch (Exception e) {
        executeMethod = null;
        e.printStackTrace();
    } finally {
        return executeMethod;
    }

}

From source file:Main.java

/**
 * Gets the current cursor position in the component or one of it's
 * subcomponents./* w  ww.j  a va2  s .c om*/
 *
 * @param component the component that is an EditorPane or some of its
 *      subcomponents is an EditorPane
 * @return current caret position
 */
public static int getCaretPosition(Container component) {
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method caretGetter = component.getClass().getMethod("getCaretPosition",
                    new Class[] {});
            Object result = caretGetter.invoke(component, new Object[] {});
            return ((Integer) result).intValue();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            int result = getCaretPosition((javax.swing.JComponent) childComponent);

            if (result >= 0) {
                return result;
            }
        }
    }

    return -1;
}

From source file:org.mongojack.internal.util.JacksonAccessor.java

private static <T> T invoke(Object object, Method method, Class<T> returnType, Object... args) {
    try {/*from   www .j a  v  a  2  s .c  om*/
        return (T) method.invoke(object, args);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Get the cookie of an asset using an undocumented API call that is marked
 * as "no to be used by applications" in its source code
 * //  w ww  .  j  a  va 2  s .  c o  m
 * @see <a href="http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/content/res/AssetManager.java#612">AssetManager.java#612</a>
 * @return the cookie
 */
private static int addAssets(final File APKFILE, final Object ASSETMANAGERINSTANCE) {
    try {
        Method addAssetPath = ASSETMANAGERINSTANCE.getClass().getMethod("addAssetPath",
                new Class[] { String.class });
        return (Integer) addAssetPath.invoke(ASSETMANAGERINSTANCE, APKFILE.getAbsolutePath());
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return -1;
}

From source file:Main.java

public static <T> T callMethod(@NonNull Object obj, @NonNull String name, Object... args) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals(name)) {
                m.setAccessible(true);//from   ww  w .  j  a  v  a2  s .  c  om
                //noinspection unchecked
                return (T) m.invoke(obj, args);
            }
        }
    }
    throw new RuntimeException("no method matching " + name);
}

From source file:com.hangum.tadpole.engine.initialize.JDBCDriverLoader.java

/**
 * Add jar loader// w  w  w.j a  va  2  s . co m
 * 
 * @param jarArray
 * @throws Exception
 */
private static void addJARLoader(Object[] jarArray) throws Exception {
    URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    try {
        Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(systemClassLoader, jarArray);
    } catch (Throwable t) {
        logger.error("jar loader", t);
    }
}

From source file:Main.java

public static <T> T getValue(ThreadLocal<T> variable, Thread key) {
    try {//from  w  ww.  j a v  a2 s  .c  o  m
        Field field = Thread.class.getDeclaredField("threadLocals");
        field.setAccessible(true);
        Object entryMap = field.get(key);
        if (entryMap == null)
            return null;
        Method getEntryMethod = entryMap.getClass().getDeclaredMethod("getEntry", ThreadLocal.class);
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(entryMap, variable);
        if (entry == null)
            return null;
        Field valueField = entry.getClass().getDeclaredField("value");
        valueField.setAccessible(true);

        return (T) valueField.get(entry);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tlabs.eve.api.parser.BaseRule.java

private static final boolean invokeMethod(final Object bean, final Method method, final Object value) {
    try {// w  w w .j av a  2 s  . com
        method.invoke(bean, value);
        return true;
    } catch (InvocationTargetException e) {
        LOG.warn("InvocationTargetException: " + e.getLocalizedMessage(), e);
        return false;
    } catch (IllegalAccessException e) {
        LOG.warn("IllegalAccessException: " + e.getLocalizedMessage(), e);
        return false;
    }
}