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 String getSystemProperty(String key, String def) {
    try {//from   ww w. ja  va  2 s .c o m
        final ClassLoader cl = ClassLoader.getSystemClassLoader();
        final Class<?> SystemProperties = cl.loadClass("android.os.SystemProperties");
        final Class<?>[] paramTypes = new Class[] { String.class, String.class };
        final Method get = SystemProperties.getMethod("get", paramTypes);
        final Object[] params = new Object[] { key, def };
        return (String) get.invoke(SystemProperties, params);
    } catch (Exception e) {
        return def;
    }
}

From source file:com.abiquo.model.util.ModelTransformer.java

private static Method getter(final String prefix, final String fieldName, final Class clazz) throws Exception {
    String name = prefix + StringUtils.capitalize(fieldName);
    return clazz.getMethod(name, new Class[0]);
}

From source file:Main.java

public static Method findIsSelectedMethodForField(Field field, Class<?> objectClass)
        throws NoSuchMethodException {
    String methodName = "is" + field.getName().toUpperCase().substring(0, 1) + field.getName().substring(1)
            + "Selected";
    return objectClass.getMethod(methodName, (java.lang.Class[]) null);
}

From source file:RenderingUtils.java

private static Method getMethodDrawStringUnderlineCharAt() {
    try {//from  w w  w . ja  v  a  2 s  .c o  m
        Class clazz = Class.forName(SWING_UTILITIES2_NAME);
        return clazz.getMethod("drawStringUnderlineCharAt", new Class[] { JComponent.class, Graphics.class,
                String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE });
    } catch (ClassNotFoundException e) {
        // returns null
    } catch (SecurityException e) {
        // returns null
    } catch (NoSuchMethodException e) {
        // returns null
    }
    return null;
}

From source file:com.ht.halo.dorado.util.proxy.ProxyBeanUtils.java

public static boolean isProxy(Class<?> cl) {
    boolean b = Enhancer.isEnhanced(cl) || ProxyFactory.isProxyClass(cl);
    if (!b) {/*from   w ww . j ava2  s .  co m*/
        if (!extraEnhancersInited) {
            extraEnhancersInited = true;
            try {
                Class<?> enhancerType = ClassUtils.forName("org.springframework.cglib.proxy.Enhancer");
                springCglibIsEnhancerMethod = enhancerType.getMethod("isEnhanced", new Class[] { Class.class });
            } catch (Exception e) {
                // do nothing
            }
        }

        if (springCglibIsEnhancerMethod != null) {
            try {
                b = (Boolean) springCglibIsEnhancerMethod.invoke(null, new Object[] { cl });
            } catch (Exception e) {
                // do nothing
            }
        }
    }
    return b;
}

From source file:Main.java

public static Method getMethod(final Class<?> targetClass, final String name,
        final Class<?>... parameterTypes) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {/*  w ww .  j ava 2  s  . c o m*/
        return targetClass.getMethod(name, parameterTypes);
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchMethodException e) {
        // ignore
    }
    return null;
}

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

/**
 * Creates an HttpClient with the specified userAgent string.
 * //from  w  w w  .j  a v a 2s  .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

/**
 * Tries to initialize fragment of type clazz by one of methods:
 * init(Bundle), init().//from ww w.  j ava 2 s . co  m
 *
 * @param clazz type of fragment to initialize
 * @param data data to initialize fragment with
 * @param <T> child of Fragment class
 * @return initialized fragment or null
 */
private static <T extends Fragment> T initFragment(Class<T> clazz, Bundle data) {

    final String INIT = "newInstance";

    try {
        Method init;
        Object fragment;

        if (data != null) {
            init = clazz.getMethod(INIT, Bundle.class);
            fragment = init.invoke(null, data);

        } else {
            init = clazz.getMethod(INIT);
            fragment = init.invoke(null);
        }
        return clazz.cast(fragment);

    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | ClassCastException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static long[] getMemInfo() {
    long memInfo[] = new long[4];
    try {//from w w w . ja  v a  2 s. c o  m
        Class<?> procClazz = Class.forName("android.os.Process");
        Class<?> paramTypes[] = new Class[] { String.class, String[].class, long[].class };
        Method readProclines = procClazz.getMethod("readProcLines", paramTypes);
        Object args[] = new Object[3];
        final String[] memInfoFields = new String[] { "MemTotal:", "MemFree:", "Buffers:", "Cached:" };
        long[] memInfoSizes = new long[memInfoFields.length];
        memInfoSizes[0] = 30;
        memInfoSizes[1] = -30;
        args[0] = new String("/proc/meminfo");
        args[1] = memInfoFields;
        args[2] = memInfoSizes;
        if (null != readProclines) {
            readProclines.invoke(null, args);
            for (int i = 0; i < memInfoSizes.length; i++) {
                memInfo[i] = memInfoSizes[i] / 1024;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return memInfo;
}

From source file:Main.java

private static Object invoke(Class<?> cls, Object receiver, String methodname, Object... args)
        throws Exception {
    Method method = null;/*from  ww  w.  j  a  v a  2s .c o  m*/
    if (args == null || args.length == 0) {
        method = cls.getMethod(methodname, new Class[0]);
        method.setAccessible(true);
        return method.invoke(receiver, new Object[0]);
    }
    method = cls.getMethod(methodname, getParameterTypes(args));
    method.setAccessible(true);
    return method.invoke(receiver, args);
}