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:com.googlecode.dex2jar.tools.DecryptStringCmd.java

public static AbstractInsnNode tryReplace(InsnList instructions, AbstractInsnNode p, AbstractInsnNode q,
        Method jmethod, Object arg) {
    try {//from   ww w .  java  2 s.c o  m
        String newValue = (String) jmethod.invoke(null, arg);
        LdcInsnNode nLdc = new LdcInsnNode(newValue);
        instructions.insertBefore(p, nLdc);
        instructions.remove(p);
        instructions.remove(q);
        return nLdc.getNext();
    } catch (Exception e) {
        // ignore
    }
    return null;
}

From source file:net.projectmonkey.spring.acl.util.reflect.MethodUtil.java

public static Object invoke(final Method method, final Object target, final Object... args) {
    Object providedArgument = null;
    if (target != null) {
        try {/*from   w  w w.  j a va 2s.c om*/
            providedArgument = method.invoke(target, args);
        } catch (Exception e) {
            throw new AuthenticationServiceException("Exception invoking method " + method, e);
        }
    }
    return providedArgument;
}

From source file:com.microsoft.tfs.client.common.ui.helpers.WorkingSetHelper.java

public static String getLabel(final IWorkingSet workingSet) {
    Check.notNull(workingSet, "workingSet"); //$NON-NLS-1$

    try {/*from  w w  w .j  a  va 2 s . c  o  m*/
        final Method getLabelMethod = workingSet.getClass().getMethod("getLabel", new Class[0]); //$NON-NLS-1$
        final Object labelResult = getLabelMethod.invoke(workingSet, new Object[0]);

        if (labelResult != null && labelResult instanceof String) {
            return (String) labelResult;
        }
    } catch (final Exception e) {
        /* Suppress, Eclipse < 3.1 */
    }

    return workingSet.getName();
}

From source file:jin.collection.util.PropertyUtil.java

public static void setProperty(final Object element, final String toProperty, final Object param) {
    try {/* w  w  w  .  ja  v  a2s. c om*/
        // TODO: sistemare
        Class[] classes = null;
        Object[] params = null;
        if (param != null) {
            classes = new Class[] { param.getClass() };
            params = new Object[] { param };
        }

        final String propertyName = "set" + capitalize(toProperty);
        final Method m = element.getClass().getMethod(propertyName, classes);
        m.invoke(element, params);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static List<String> getDirs(Context context) {
    List<String> dirs = new ArrayList<String>();
    StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    try {/*from w  w w.  ja v  a 2 s. com*/
        Class[] paramClasses = {};
        Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses);
        getVolumePathsMethod.setAccessible(true);
        Object[] params = {};
        Object invoke = getVolumePathsMethod.invoke(storageManager, params);
        for (int i = 0; i < ((String[]) invoke).length; i++) {
            // System.out.println(((String[])invoke)[i]);
            dirs.add(((String[]) invoke)[i]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dirs;
}

From source file:com.android.fastergallery.common.HttpClientFactory.java

/**
 * Creates an HttpClient with the specified userAgent string.
 * //w w w.j a  va 2  s  .c o  m
 * @param userAgent
 *            the userAgent string
 * @return the client
 */
public static HttpClient newHttpClient(String userAgent) {
    // AndroidHttpClient is available on all platform releases,
    // but is hidden until API Level 8
    try {
        Class<?> clazz = Class.forName("android.net.http.AndroidHttpClient");
        Method newInstance = clazz.getMethod("newInstance", String.class);
        Object instance = newInstance.invoke(null, userAgent);

        HttpClient client = (HttpClient) instance;

        // ensure we default to HTTP 1.1
        HttpParams params = client.getParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        // AndroidHttpClient sets these two parameters thusly by default:
        // HttpConnectionParams.setSoTimeout(params, 60 * 1000);
        // HttpConnectionParams.setConnectionTimeout(params, 60 * 1000);

        // however it doesn't set this one...
        ConnManagerParams.setTimeout(params, 60 * 1000);

        return client;
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * see http://habrahabr.ru/post/144547///from w w  w .  ja va 2s. c  o  m
 */
public static BluetoothSocket createRfcommSocket(BluetoothDevice device) {
    BluetoothSocket tmp = null;
    try {
        Class class1 = device.getClass();
        Class aclass[] = new Class[1];
        aclass[0] = Integer.TYPE;
        Method method = class1.getMethod("createRfcommSocket", aclass);
        Object aobj[] = new Object[1];
        aobj[0] = Integer.valueOf(1);

        tmp = (BluetoothSocket) method.invoke(device, aobj);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "createRfcommSocket() failed", e);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "createRfcommSocket() failed", e);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "createRfcommSocket() failed", e);
    }
    return tmp;
}

From source file:com.luna.common.utils.ReflectUtils.java

/**
 * set method value by name.//from   w ww . ja va 2  s.  c  o  m
 * 
 * @param target
 *            Object
 * @param methodName
 *            method name
 * @param methodValue
 *            method value
 * @throws Exception
 *             ex
 */
public static void setMethodValue(Object target, String methodName, Object methodValue)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method method = target.getClass().getDeclaredMethod(methodName, methodValue.getClass());

    method.invoke(target, methodValue);
}

From source file:com.xinlv.test.PortalTestUtil.java

public static void callOnBeginRequest() {
    try {/*  w  w w. j  a v a2 s. c o  m*/
        Method method = RequestCycle.class.getDeclaredMethod("onBeginRequest", (Class<?>[]) null);
        method.setAccessible(true);
        method.invoke(RequestCycle.get(), (Object[]) null);
    } catch (Exception e) {
        throw new UnhandledException(e);
    }

}

From source file:Platform.java

/**
 * Unmaximizes the specified Frame./* ww  w  .j  av a 2 s . c  o m*/
 */
public static void unmaximize(Frame f) {
    if (!isJRE13) {
        try {
            Method m1 = Frame.class.getMethod("getExtendedState", (Class[]) null);
            Method m2 = Frame.class.getMethod("setExtendedState", new Class[] { Integer.TYPE });
            int i = ((Integer) m1.invoke(f, (Object[]) null)).intValue();
            m2.invoke(f, new Object[] { new Integer(i & ~6) });
        } catch (java.lang.reflect.InvocationTargetException ite) {
        } catch (NoSuchMethodException nsme) {
        } catch (IllegalAccessException iae) {
        }
    }
}