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

/**
 * Use reflection to access a URL[] getURLs or URL[] getClasspath method so
 * that non-URLClassLoader class loaders, or class loaders that override
 * getURLs to return null or empty, can provide the true classpath info.
 * // w  ww .jav  a 2 s. co m
 * @param cl
 * @return the urls
 */
public static URL[] getClassLoaderURLs(ClassLoader cl) {
    URL[] urls = {};
    try {
        Class returnType = urls.getClass();
        Class[] parameterTypes = {};
        Class clClass = cl.getClass();
        Method getURLs = clClass.getMethod("getURLs", parameterTypes);
        if (returnType.isAssignableFrom(getURLs.getReturnType())) {
            Object[] args = {};
            urls = (URL[]) getURLs.invoke(cl, args);
        }
        if (urls == null || urls.length == 0) {
            Method getCp = clClass.getMethod("getClasspath", parameterTypes);
            if (returnType.isAssignableFrom(getCp.getReturnType())) {
                Object[] args = {};
                urls = (URL[]) getCp.invoke(cl, args);
            }
        }
    } catch (Exception ignore) {
    }
    return urls;
}

From source file:Main.java

public static Object getBeanFromXml(String xml, Class xmlBeanClass) throws Exception {
    Method method = (Method) UNMARSHAL_METHOD_MAP.get(xmlBeanClass.getName());

    if (method == null) {
        method = xmlBeanClass.getMethod("unmarshal", new Class[] { Reader.class });
        UNMARSHAL_METHOD_MAP.put(xmlBeanClass.getName(), method);
    }/*  ww  w .ja  v  a2 s  . c om*/
    StringReader stringReader = new StringReader(xml);

    return method.invoke(null, new Object[] { stringReader });

}

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

/**
 * Closes an HttpClient.// w w  w. j  av  a  2 s .com
 */
public static void close(HttpClient client) {
    // AndroidHttpClient is available on all platform releases,
    // but is hidden until API Level 8
    try {
        Class<?> clazz = client.getClass();
        Method method = clazz.getMethod("close", (Class<?>[]) null);
        method.invoke(client, (Object[]) null);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Creates a list of values extracted from the provided list using the
 * specified value method, keeping the order of the provided list.
 *//*from   w  w  w. j av  a  2  s  .  c  o m*/
@SuppressWarnings("unchecked")
public static <K, T> List<K> createList(List<T> list, String valueMethod) {
    List<K> valueList = new ArrayList<>(list.size());

    if (list.isEmpty()) {
        return valueList;
    }

    Class<?> elementClass = list.iterator().next().getClass();

    Method getValueMethod;

    try {
        getValueMethod = elementClass.getMethod(valueMethod, new Class[0]);
    } catch (Exception e) {
        throw new RuntimeException("Failed to get key method", e);
    }

    for (T element : list) {
        K value;

        try {
            value = (K) getValueMethod.invoke(element, (Object[]) null);
        } catch (Exception e) {
            throw new RuntimeException("Failed to get key", e);
        }

        valueList.add(value);
    }

    return valueList;
}

From source file:Main.java

public static void putInHashMapByGivenAttribute(HashMap map, Object obj, String attribName) {
    try {/*ww  w  .  j a  v  a 2s  .  c om*/
        Class class1 = obj.getClass();
        String methodName = "get" + Character.toUpperCase(attribName.charAt(0)) + attribName.substring(1);
        Method method = class1.getMethod(methodName, new Class[0]);
        Object attributeValue = method.invoke(obj, new Object[0]);
        map.put(attributeValue, obj);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a map with the elements of the collection as values using the
 * specified keyMethod to obtain the key from the elements.
 *//*from w w  w  . j a  va 2 s.c o m*/
@SuppressWarnings("unchecked")
public static <K, T> Map<K, T> createMap(Collection<T> collection, String keyMethod) {
    Map<K, T> map = new HashMap<>(collection.size());

    if (collection.isEmpty()) {
        return map;
    }

    Class<?> elementClass = collection.iterator().next().getClass();

    Method getKeyMethod;

    try {
        getKeyMethod = elementClass.getMethod(keyMethod, new Class[0]);
    } catch (Exception e) {
        throw new RuntimeException("Failed to get key method", e);
    }

    for (T element : collection) {
        K key;
        try {
            key = (K) getKeyMethod.invoke(element, (Object[]) null);
        } catch (Exception e) {
            throw new RuntimeException("Failed to get key", e);
        }

        map.put(key, element);
    }

    return map;
}

From source file:Main.java

/**
 * see http://habrahabr.ru/post/144547///from w w  w  .  jav a 2  s  . 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.codebase.foundation.classloader.xbean.ClassLoaderUtil.java

/**
 * Releases the specified classloader from the Apache Jakarta Commons Logging class loader cache using reflection.
 * @param classLoader the class loader to release
 *//*from   w  w  w  .  j  a v  a  2  s  .  c  om*/
@SuppressWarnings("all")
public static void releaseCommonsLoggingCache(ClassLoader classLoader) {
    try {
        Class logFactory = classLoader.loadClass("org.apache.commons.logging.LogFactory");
        Method release = logFactory.getMethod("release", new Class[] { ClassLoader.class });
        release.invoke(null, new Object[] { classLoader });
    } catch (Throwable ignored) {
        // there is nothing a user could do about this anyway
    }
}

From source file:com.taobao.tddl.interact.rule.ruleimpl.GroovyRule.java

private static Method getMethod(Class<?> c, String name, Class<?>... parameterTypes) {
    try {//w w w  . j  av  a 2  s  . com
        return c.getMethod(name, parameterTypes);
    } catch (SecurityException e) {
        throw new IllegalArgumentException("", e);
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException("" + name, e);
    }
}

From source file:Main.java

public static Method getMethod(Class<?> targetClass, String name, Class<?>... parameterTypes) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {/* w  w w  .j av  a2 s. c om*/
        return targetClass.getMethod(name, parameterTypes);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return null;
}