Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

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

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:Main.java

public static Class<?> getXMLBeansValueType(Class<?> wrapperType) {
    Class<?> result = wrapperType;
    for (Method method : wrapperType.getMethods()) {
        if (method.getName().startsWith("addNew")) {
            result = method.getReturnType();
            break;
        }//  w  w  w .  ja v  a2s .  com
    }
    return result;
}

From source file:Main.java

public static Method[] getMethods(Class clazz) {
    Method[] ms = methodCacheTable.get(clazz);
    if (null == ms) {
        ms = clazz.getMethods();
        methodCacheTable.put(clazz, ms);
    }//w  w w .  j a  v  a  2 s. com
    return ms;
}

From source file:Main.java

public final static Set<String> getMethods(Class<?> clazz) {
    HashSet<String> methodSet = new HashSet<String>();
    Method[] methodArray = clazz.getMethods();
    for (Method method : methodArray) {
        String methodName = method.getName();
        methodSet.add(methodName);/*from   ww  w  . j  ava2s. c o  m*/
    }
    return methodSet;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>//from w w  w . ja v  a  2  s  .co  m
 * Implementation note:  The returned value is a HashMap.  This is subject
 * to change, so callers should code against the interface Map.
 *
 * @param component the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(JComponent component) {
    Map<Object, Object> retVal = new HashMap<Object, Object>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                Class returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !setExclude.contains(method.getName())) {
                    String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be accessed
            } catch (IllegalAccessException ex) {
            } catch (IllegalArgumentException ex) {
            } catch (InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties of a JComponent. Array properties are not included.
 * <P>//from w ww .  ja  va2  s . co  m
 * Implementation note: The returned value is a HashMap. This is subject to change, so callers should code against the interface Map.
 * 
 * @param component
 *            the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(JComponent component) {
    Map<Object, Object> retVal = new HashMap<>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && //$NON-NLS-1$
                method.getParameterTypes().length == 0) {
            try {
                Class<?> returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[") && //$NON-NLS-1$
                        !setExclude.contains(method.getName())) {
                    String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be accessed
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                //no catch
            }
        }
    }
    return retVal;
}

From source file:Main.java

public static boolean setProperty(Object object, String fieldName, Object fieldValue) {
    Class<?> clazz = object.getClass();
    String mname = "set" + fieldName;
    try {/*ww  w .jav  a2 s .c  o m*/
        for (Method m : clazz.getMethods()) {
            if (m.getName().equalsIgnoreCase(mname)) {
                m.invoke(object, fieldValue);
                return true;
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    return false;
}

From source file:Main.java

private static final Method findMethod(Class<?> clazz, String name, List<Class<?>> actualArgs) {
    for (final Method m : clazz.getMethods()) {
        if (m.getName().equals(name) && callableWith(m.getParameterTypes(), actualArgs)) {
            return m;
        }/*from www .j  a  v  a2s  .  c om*/
    }
    return null;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>/*  w  w w . ja  v a  2s  .c  om*/
 * Implementation note:  The returned value is a HashMap.  This is subject
 * to change, so callers should code against the interface Map.
 * 
 * @param component the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(JComponent component) {
    Map<Object, Object> retVal = new HashMap<>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                Class<?> returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !setExclude.contains(method.getName())) {
                    String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be accessed
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:Main.java

public static List<Method> getMethods(Class<?> clazz, String methodName) {
    List<Method> list = new ArrayList<>();
    Method[] ms = clazz.getMethods();
    if (ms != null) {
        for (int size = ms.length, i = size - 1; i >= 0; i--) {
            if (ms[i].getName().equals(methodName)) {
                list.add(ms[i]);//from   w  ww  .  j  a v  a 2  s.c  om
            }
        }
    }
    return list;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent. Array properties are not included.
 * <P>//from  w ww .ja v a  2 s  .  c o m
 * Implementation note: The returned value is a HashMap. This is subject to
 * change, so callers should code against the interface Map.
 * 
 * @param component
 *            the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(final JComponent component) {
    final Map<Object, Object> retVal = new HashMap<Object, Object>();
    final Class<?> clazz = component.getClass();
    final Method[] methods = clazz.getMethods();
    Object value = null;
    for (final Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                final Class returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !setExclude.contains(method.getName())) {
                    final String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be
                // accessed
            } catch (final IllegalAccessException ex) {
            } catch (final IllegalArgumentException ex) {
            } catch (final InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}