Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

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

/**
 * Attempt to find a {@link Method} on the supplied class with the supplied name
 * and parameter types. Searches all superclasses up to <code>Object</code>.
 * <p>Returns <code>null</code> if no {@link Method} can be found.
 *
 * @param clazz      the class to introspect
 * @param name       the name of the method
 * @param paramTypes the parameter types of the method
 *                   (may be <code>null</code> to indicate any signature)
 * @return the Method object, or <code>null</code> if none found
 *//*from  w w w  .  jav  a 2 s.c om*/
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName())
                    && (paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:org.openinfinity.core.util.AspectUtil.java

/**
 * Returns the specified annotation./*from w ww.j  av a2 s .c  o m*/
 * 
 * @param joinPoint Represents the join point.
 * @return 
 * @return LogLevel representing the log level.
 */
public static <T extends Annotation> T getAnnotation(JoinPoint joinPoint, Class<T> requiredAnnotationClass) {
    Method[] methods = joinPoint.getTarget().getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().equals(joinPoint.getSignature().getName())) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                if (requiredAnnotationClass.isAssignableFrom(annotation.getClass())) {
                    return (T) annotation;
                }
            }
        }
    }
    throw new SystemException(new AopConfigException("Annotation not found."));
}

From source file:GenericReflectionTest.java

public static void printMethod(Method m) {
    String name = m.getName();
    System.out.print(Modifier.toString(m.getModifiers()));
    System.out.print(" ");
    printTypes(m.getTypeParameters(), "<", ", ", "> ", true);

    printType(m.getGenericReturnType(), false);
    System.out.print(" ");
    System.out.print(name);//from   www . j  a v  a  2s  . c  o  m
    System.out.print("(");
    printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
    System.out.println(")");
}

From source file:Main.java

/**
 * find all getter and is method and return the value by map.
 */// w w w  .java  2s . c  o m
public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<>();
    for (Method method : bean.getClass().getMethods()) {
        String name = method.getName();
        if (((name.length() > 3 && name.startsWith("get")) || (name.length() > 2 && name.startsWith("is")))
                && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0
                && method.getDeclaringClass() != Object.class) {
            int i = name.startsWith("get") ? 3 : 2;
            String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
            try {
                map.put(key, method.invoke(bean, new Object[0]));
            } catch (Exception e) {
            }
        }
    }
    return map;
}

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  ww  w .j av  a  2s  .com*/
 * 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

/**
 * Checks is the given method from java.lang.Object
 * @param method - method to check//from w  w w. j  a  v a 2s  .  c  o  m
 * @return true if method from java.lang.Object
 */
public static boolean isJavaLangObjectMethod(Method method) {
    Method methods[] = Object.class.getDeclaredMethods();
    for (Method objMethod : methods) {
        if (objMethod.getName().equals(method.getName())) {
            Class<?> methodParameterTypes[] = method.getParameterTypes();
            Class<?> objectMethodParameterTypes[] = objMethod.getParameterTypes();
            if (objectMethodParameterTypes.length == methodParameterTypes.length) {
                boolean matched = true;
                for (int i = 0; i < methodParameterTypes.length; i++) {
                    Class<?> class1 = methodParameterTypes[i];
                    Class<?> class2 = objectMethodParameterTypes[i];
                    if (!class1.equals(class2)) {
                        matched = false;
                        break;
                    }
                }
                if (matched) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static Element beanToXml(Document document, Object obj)
        throws ParserConfigurationException, InvocationTargetException, IllegalAccessException {
    String name = obj.getClass().getName();
    Element rootElement = document.createElement(name);
    Method[] methods = obj.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        String fieldName = analyzeFieldName(methodName);
        Element fieldElement = document.createElement(fieldName);
        if (methodName.startsWith("get") && !"getClass".equals(methodName)) {
            Object value = method.invoke(obj);
            if (value != null && !"".equals(value.toString())) {
                fieldElement.setTextContent(value.toString());
            } else {
                continue;
            }//from  w  w  w. j av a2 s.c o m
        } else {
            continue;
        }
        rootElement.appendChild(fieldElement);
    }
    return rootElement;
}

From source file:com.angstoverseer.util.ReflectionUtil.java

public static Method extractMethod(Object target, String methodName) {
    final Method[] declaredMethods = target.getClass().getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return method;
        }/*  ww  w .j  a  v  a2s  . c om*/
    }

    throw new RuntimeException("Method not found: " + methodName);
}

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 w w  w.  j a va2 s  . co m*/
    }
    return null;
}