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

public static String getProp(String prop) {
    String output = "";
    try {/*from   w ww  . ja  va  2  s .  co m*/
        Class<?> sp = Class.forName("android.os.SystemProperties");
        Method get = sp.getMethod("get", String.class);
        output = (String) get.invoke(null, prop);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output;
}

From source file:Main.java

public static String getSerialNumber() {
    String serial = null;/*from w w  w  .  j  a va2 s.  c om*/
    try {
        Class<?> c = Class.forName("android.os.SystemProperties");
        Method get = c.getMethod("get", String.class);
        serial = (String) get.invoke(c, "ro.serialno");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return serial;
}

From source file:Main.java

private static String getDeviceId(Activity activity) {
    String deviceId = "";

    try {/*from  w ww . j a  v  a2  s .c  om*/
        Class<?> utils = Class.forName("com.lk.sdk.Utils");
        Method idMethod = utils.getMethod("id", Context.class);
        deviceId = (String) idMethod.invoke(null, activity);
    } catch (Exception e) {

    }

    return deviceId;
}

From source file:Main.java

public static String getSystemProperties(String key) {
    try {/*  w  ww  .java  2  s. c o m*/
        Class osSystem = Class.forName("android.os.SystemProperties");
        Method getInvoke = osSystem.getMethod("get", new Class[] { String.class });
        return (String) getInvoke.invoke(osSystem, new Object[] { key });
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * get method by sizer, it will return the first found method.
 *///from  w  w w  .  j av a 2 s.c  o m
public static String getSizerMethod(Class<?> cls, String[] sizer) {
    for (String s : sizer) {
        try {
            return cls.getMethod(s, new Class<?>[0]).getName() + "()";
        } catch (NoSuchMethodException e) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param type//from w w w. j  a  va 2s.  c  o m
 * @param object
 * @param field
 * @return
 */
public static <T> Object getFieldValue(Class<T> type, T object, Field field) {
    try {
        Method m = type.getMethod(getGetIsPrefix(field) + getFirstLetterUppercased(field.getName()),
                (Class<?>[]) null);
        return m.invoke(object, (Object[]) null);
    } catch (Exception e) {
    }

    return null;
}

From source file:Util.java

@SuppressWarnings("unchecked")
public static <T> T getAnnotationDefault(Class<? extends Annotation> annotationClass, String element)
        throws Exception {
    Method method = annotationClass.getMethod(element, (Class[]) null);
    return ((T) method.getDefaultValue());
}

From source file:Main.java

static <T extends Annotation> T getSuperMethodAnnotation(Class<?> superClass, Method method,
        Class<T> annotationClass) {
    try {/*www .j a  va  2  s  .  c o m*/
        return superClass.getMethod(method.getName(), method.getParameterTypes())
                .getAnnotation(annotationClass);
    } catch (NoSuchMethodException ignored) {
        return null;
    }
}

From source file:Main.java

/**
 * Called internally by installGtkPopupBugWorkaround. Returns a specific
 * GTK style object.//from  w  w  w  .j av  a  2  s .  c  o  m
 * 
 * @param styleFactory
 *            The GTK style factory.
 * @param component
 *            The target component of the style.
 * @param regionName
 *            The name of the target region of the style.
 * @return The GTK style.
 * @throws Exception
 *             When reflection fails.
 */
private static Object getGtkStyle(Object styleFactory, JComponent component, String regionName)
        throws Exception {
    // Create the region object
    Class<?> regionClass = Class.forName("javax.swing.plaf.synth.Region");
    Field field = regionClass.getField(regionName);
    Object region = field.get(regionClass);

    // Get and return the style
    Class<?> styleFactoryClass = styleFactory.getClass();
    Method method = styleFactoryClass.getMethod("getStyle", new Class<?>[] { JComponent.class, regionClass });
    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object style = method.invoke(styleFactory, component, region);
    method.setAccessible(accessible);
    return style;
}

From source file:Main.java

/**
 * @param type/*from   w w w  . ja v a 2 s.c o  m*/
 * @param object
 * @param field
 * @param value
 */
public static <T> void setFieldValue(Class<? extends T> type, T object, Field field, Object value) {
    try {
        Method m = type.getMethod("set" + getFirstLetterUppercased(field.getName()), field.getType());
        m.invoke(object, value);
    } catch (Exception e) {
    }
}