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:com.jinmibao.common.util.BeanUtil.java

/**
 * ? ????? ?/* w  w w  . ja  v a 2 s .  c  o  m*/
 * 
 * @param source
 * @param target
 */
public static void copyPropertiesIfNull(Object source, Object target) {
    if (source == null || target == null) {
        throw new RuntimeException("Source Or Target Can't Be Null");
    }
    Method[] methods = target.getClass().getDeclaredMethods();
    List<String> notNullPropertyList = new ArrayList<String>();
    for (Method method : methods) {
        if (!method.getName().startsWith("get")) {
            continue;
        }
        try {
            Object methodResult = method.invoke(target);
            if (methodResult != null) {
                if (method.getReturnType().getName().equals("java.lang.String")
                        && StringUtil.isBlank((String) methodResult)) {
                    continue;
                }
                notNullPropertyList.add(BeanUtils.findPropertyForMethod(method).getName());
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }
    BeanUtils.copyProperties(source, target, ListUtil.list2Array(notNullPropertyList));
}

From source file:Main.java

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

From source file:Main.java

public static Map<String, Object> getProperties(Object bean) {
    Map<String, Object> map = new HashMap<String, Object>();
    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) {
            }//from  w  w  w .  j  a  va  2s.c  o  m
        }
    }
    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  .  java 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;
}

From source file:Main.java

static Method getMethod(final Class<?> clz, final String mtdName) {
    if (clz == null || TextUtils.isEmpty(mtdName)) {
        return null;
    }//  w  w w.j  a  va2s  . c o  m
    Method mtd = null;
    try {
        for (Method m : clz.getDeclaredMethods()) {
            if (m.getName().equals(mtdName)) {
                mtd = m;
                mtd.setAccessible(true);
                break;
            }
        }
    } catch (Exception e) {
        Log.d(TAG, e.getMessage(), e);
    }
    return mtd;
}

From source file:Main.java

public static boolean declaredEquals(Class<?> clazz) {
    for (Method declaredMethod : clazz.getDeclaredMethods()) {
        if (EQUALS_METHOD.equals(declaredMethod.getName())
                && Arrays.equals(declaredMethod.getParameterTypes(), EQUALS_PARAMETERS)) {
            return true;
        }//from  w  ww . ja va 2  s  .  c o m
    }
    return false;
}

From source file:Main.java

static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Method finalMethod = null;// w  w  w  .jav  a  2  s. c om
    while (objectClass != null && finalMethod == null) {
        for (Method method : objectClass.getDeclaredMethods()) {
            if (method.getName().equals(fieldName)) {
                Class<?>[] paramsType = method.getParameterTypes();
                if (paramsType.length == 0) {
                    finalMethod = method;
                    break;
                } else if (paramsType.length == 1) {
                    if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                        finalMethod = method;
                        break;
                    }
                }

            }
        }
        if (finalMethod == null) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (finalMethod == null) {
        throw new NoSuchFieldException(fieldName);
    }
    return finalMethod;
}

From source file:Main.java

private static Object dontShowPasswords(Object aReturnValue, Method aMethod) {
    Object result = aReturnValue;
    Matcher matcher = PASSWORD_PATTERN.matcher(aMethod.getName());
    if (matcher.find()) {
        result = HIDDEN_PASSWORD_VALUE;// w  w  w  . j a v a2s. c  om
    }
    return result;
}

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 ava  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(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 String getQualifiedMethodName(Method method) {
    return (new StringBuilder()).append(method.getDeclaringClass().getName()).append(".")
            .append(method.getName()).toString();
}