Example usage for java.lang Class getMethod

List of usage examples for java.lang Class getMethod

Introduction

In this page you can find the example usage for java.lang Class getMethod.

Prototype

@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Usage

From source file:Main.java

private static Object invoke(Class<?> cls, Object receiver, String methodname, Class<?>[] clsArr,
        Object[] objArr) throws Exception {
    Method method = null;//from w  ww .  java2s. c o m
    if (objArr == null || objArr.length == 0) {
        method = cls.getMethod(methodname, new Class[0]);
        method.setAccessible(true);
        return method.invoke(receiver, new Object[0]);
    }
    method = cls.getMethod(methodname, clsArr);
    method.setAccessible(true);
    return method.invoke(receiver, objArr);
}

From source file:com.dwdesign.tweetings.util.WebViewProxySettings.java

@SuppressWarnings("rawtypes")
private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)
        throws Exception {
    final Class<?> c = object instanceof Class ? (Class) object : object.getClass();

    if (types != null) {
        final Method method = c.getMethod(methodName, types);
        return method.invoke(object, params);
    } else {//from ww w. j  a v  a  2  s  .  co  m
        final Method method = c.getMethod(methodName);
        return method.invoke(object);
    }
}

From source file:com.knowbout.hibernate.HibernateUtil.java

public static synchronized void addDeleteHandler(DeleteHandler<?> deleteHandler) {
    if (deleteHandlers == null) {
        deleteHandlers = new LinkedList<DeleteHandler<?>>();
        EventListeners els = config.getEventListeners();
        DeleteEventListener[] dels = els.getDeleteEventListeners();
        DeleteEventListener[] newDels = new DeleteEventListener[dels.length + 1];
        System.arraycopy(dels, 0, newDels, 1, dels.length);
        newDels[0] = new DeleteEventListener() {

            private static final long serialVersionUID = 0L;

            public void onDelete(DeleteEvent event) throws HibernateException {
                Object deleted = event.getObject();
                for (DeleteHandler<?> dh : deleteHandlers) {
                    Class<?> handledType = (Class) ((ParameterizedType) dh.getClass().getGenericSuperclass())
                            .getActualTypeArguments()[0];
                    if (handledType.isInstance(deleted)) {
                        /* The following should work but won't compile, so I'll invoke through reflection
                        dh.onDelete(handledType.cast(deleted));
                        *///from  w ww .  ja  va2  s .c o  m
                        Class clazz = dh.getClass();
                        try {
                            Method method = clazz.getMethod("onDelete", new Class[] { handledType });
                            method.invoke(dh, new Object[] { deleted });
                        } catch (Exception e) {
                            throw new Error("Bug in HibernateUtil.addDeleteHandler()");
                        }
                    }
                }
            }

        };
    }
    deleteHandlers.add(0, deleteHandler);
}

From source file:com.bluecloud.ioc.classloader.ClassHandler.java

/**
 * <h3>Class</h3>//from   w  w  w  .j a  va2s  .  co  m
 * 
 * @param obj
 *            
 * @param methodName
 *            ??
 * @param args
 *            ?
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void invokeMethod(Object obj, String methodName, Object args)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<? extends Object> clazz = obj.getClass();
    try {
        if (args != null) {
            clazz.getMethod(methodName, args.getClass()).invoke(obj, args);
        }
    } catch (SecurityException e) {
        log.error(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
                method.invoke(obj, args);
            }
        }
    }
}

From source file:Main.java

/**
 * //from ww  w . jav a 2s . c om
 * 
 */
public static <T> boolean equalsAsSet(Collection<T> value1, Collection<T> value2, Class<T> clazz,
        String equalsMethodName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Method m = null;
    try {
        m = clazz.getMethod(equalsMethodName, clazz);
    } catch (NoSuchMethodException e) {
        m = clazz.getMethod(equalsMethodName, Object.class);
    }
    return equalsAsSet(value1, value2, m);
}

From source file:ClassUtils.java

/**
 * Helper for invoking a static method that takes one parameter.
 * /*from  ww w  .  j a v  a 2 s.co m*/
 * @param cl
 *            The class that implements the static method
 * @param methodName
 *            The method name
 * @param param
 *            A parameter
 * @param paramClass
 *            Class of the parameter
 * @throws Throwable
 */
public static Object invokeStaticMethod(Class cl, String methodName, Object param, Class paramClass)
        throws Throwable {
    Method method = cl.getMethod(methodName, new Class[] { paramClass });
    try {
        return method.invoke(null, new Object[] { param });
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}

From source file:com.lemi.mario.download.utils.Proxy.java

/**
 * Return the default proxy port specified by the carrier.
 * //ww  w  .  j a  v a  2s  .  co  m
 * @return The port number to be used with the proxy host or -1 if there is
 *         no proxy for this carrier.
 */
static final public int getDefaultPort() {
    String host = null;
    // use reflection to invoke the method
    try {
        Class clazz = Class.forName("android.os.SystemProperties");
        Method getMethod = clazz.getMethod("get", new Class[] { String.class });
        host = (String) getMethod.invoke(clazz, new Object[] { "net.gprs.http-proxy" });
    } catch (Exception e) {
        e.printStackTrace();
    }
    // String host = SystemProperties.get("net.gprs.http-proxy");
    if (host != null) {
        Uri u = Uri.parse(host);
        return u.getPort();
    } else {
        return -1;
    }
}

From source file:Main.java

public static Bitmap createVideoThumbnail(String filePath) {
    // MediaMetadataRetriever is available on API Level 8
    // but is hidden until API Level 10
    Class<?> clazz = null;
    Object instance = null;//from w  w  w.  j a  v a  2s .  c  o  m
    try {
        clazz = Class.forName("android.media.MediaMetadataRetriever");
        instance = clazz.newInstance();

        Method method = clazz.getMethod("setDataSource", String.class);
        method.invoke(instance, filePath);

        // The method name changes between API Level 9 and 10.
        if (Build.VERSION.SDK_INT <= 9) {
            return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
        } else {
            byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
            if (data != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                if (bitmap != null)
                    return bitmap;
            }
            return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
        }
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } catch (InstantiationException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (InvocationTargetException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (NoSuchMethodException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (IllegalAccessException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } finally {
        try {
            if (instance != null) {
                clazz.getMethod("release").invoke(instance);
            }
        } catch (Exception ignored) {
        }
    }
    return null;
}

From source file:com.lemi.mario.download.utils.Proxy.java

/**
 * Return the default proxy host specified by the carrier.
 * /*from w ww .  j a  v  a  2 s. c o  m*/
 * @return String containing the host name or null if there is no proxy for
 *         this carrier.
 */
static final public String getDefaultHost() {
    String host = null;
    // use reflection to invoke the method
    try {
        Class clazz = Class.forName("android.os.SystemProperties");
        Method getMethod = clazz.getMethod("get", new Class[] { String.class });
        host = (String) getMethod.invoke(clazz, new Object[] { "net.gprs.http-proxy" });
    } catch (Exception e) {
        e.printStackTrace();
    }

    // String host = SystemProperties.get("net.gprs.http-proxy");
    if (host != null) {
        Uri u = Uri.parse(host);
        host = u.getHost();
        return host;
    } else {
        return null;
    }
}

From source file:com.he5ed.lib.cloudprovider.utils.AuthHelper.java

/**
 * Extract user information from the JSONObject
 *
 * @param cloudApi type of cloud account
 * @param jsonObject JSONObject that contain user information
 * @return User//from w  w w.j av a2s . c om
 * @throws JSONException
 */
public static User extractUser(String cloudApi, JSONObject jsonObject) throws JSONException {
    // use reflection for flexibility
    try {
        Class<?> clazz = Class.forName(cloudApi);
        Method extractUser = clazz.getMethod("extractUser", JSONObject.class);
        return (User) extractUser.invoke(null, jsonObject);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        // catch exception throw by class method
        if (e.getCause() instanceof JSONException) {
            throw new JSONException(e.getMessage());
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}