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.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBReflector.java

/**
 * Returns whether the method given is a getter method we should serialize /
 * deserialize to the service. The method must begin with "get" or "is",
 * have no arguments, belong to a class that declares its table, and not be
 * marked ignored./*from   w w w .  j av a  2  s .  com*/
 */
private static boolean isRelevantGetter(Method m) {
    return (m.getName().startsWith("get") || m.getName().startsWith("is")) && m.getParameterTypes().length == 0
            && isDocumentType(m.getDeclaringClass())
            && !ReflectionUtils.getterOrFieldHasAnnotation(m, DynamoDBIgnore.class);
}

From source file:cn.com.qiqi.order.utils.Reflections.java

/**
 * ?, ?DeclaredMethod,?.//from  ww  w  .ja  v a  2s .c  o  m
 * ?Object?, null.
 * ????
 * 
 * ?. ?Method,?Method.invoke(Object obj, Object... args)
 */
public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
    Validate.notNull(obj, "object can't be null");
    Validate.notBlank(methodName, "methodName can't be blank");

    for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType
            .getSuperclass()) {
        Method[] methods = searchType.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                makeAccessible(method);
                return method;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Map<String, Object> getProperties(Object object, boolean includeSuperClasses, boolean deepCopy) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    if (object == null) {
        return map;
    }//from w  w  w. j a  v a  2 s . co m

    Class<?> objectClass = object.getClass();
    Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 0 && !method.getReturnType().equals(Void.TYPE)) {
            String methodName = method.getName();
            String propertyName = "";
            if (methodName.startsWith("get")) {
                propertyName = methodName.substring(3);
            } else if (methodName.startsWith("is")) {
                propertyName = methodName.substring(2);
            }
            if (propertyName.length() > 0 && Character.isUpperCase(propertyName.charAt(0))) {
                propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);

                Object value = null;
                try {
                    value = method.invoke(object);
                } catch (Exception e) {
                }

                Object value2 = value;

                if (!deepCopy) {
                    map.put(propertyName, value);
                } else {
                    if (isSimpleObject(value)) {
                        map.put(propertyName, value);
                    } else if (value instanceof Map) {
                        Map<String, Object> submap = new HashMap<String, Object>();
                        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                            submap.put(String.valueOf(entry.getKey()),
                                    convertObject(entry.getValue(), includeSuperClasses));
                        }
                        map.put(propertyName, submap);
                    } else if (value instanceof Iterable) {
                        List<Object> sublist = new ArrayList<Object>();
                        for (Object v : (Iterable<?>) object) {
                            sublist.add(convertObject(v, includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else if (value.getClass().isArray()) {
                        List<Object> sublist = new ArrayList<Object>();
                        int length = Array.getLength(value);
                        for (int i = 0; i < length; i++) {
                            sublist.add(convertObject(Array.get(value, i), includeSuperClasses));
                        }
                        map.put(propertyName, sublist);
                    } else {
                        map.put(propertyName, getProperties(value, includeSuperClasses, deepCopy));
                    }
                }
            }
        }
    }

    return map;
}

From source file:$.Reflections.java

/**
     * ?, ?DeclaredMethod,?.//from   w ww.j a  va2  s.co  m
     * ?Object?, null.
     * ????
     * 
     * ?. ?Method,?Method.invoke(Object obj, Object... args)
     */
    public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
        Validate.notNull(obj, "object can't be null");
        Validate.notBlank(methodName, "methodName can't be blank");

        for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType
                .getSuperclass()) {
            Method[] methods = searchType.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    makeAccessible(method);
                    return method;
                }
            }
        }
        return null;
    }

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

/**
 * retrieve candidate setter method/*  w  w  w.j a v  a  2 s.c  o m*/
 *
 * @param clazz
 * @param name
 * @return
 */
@SuppressWarnings({ "rawtypes" })
private static Method getCandidateMethod(Class clazz, String name) {
    for (Method method : clazz.getMethods()) {
        if (name.equals(method.getName()) && method.getParameterTypes().length == 1)
            return method;
    }
    return null;
}

From source file:edu.umich.flowfence.common.QMDescriptor.java

public static QMDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {/*  w  ww .  java 2s.co m*/
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:edu.umich.oasis.common.SodaDescriptor.java

public static SodaDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {/*w  w w.  j a va2 s  .com*/
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static String methodToString(Method m) {
    StringBuilder sb = new StringBuilder();
    String returnType = m.getReturnType().getName();
    // returnType = "def";
    sb.append(returnType).append(" ").append(m.getName()).append("(");
    int pos = 0;/*from w  w w .ja  v  a 2 s. c om*/
    for (Class<?> cls : m.getParameterTypes()) {
        if (pos > 0) {
            sb.append(", ");
        }
        sb.append(cls.getName()).append(" arg" + pos);
        ++pos;
    }
    sb.append(")");
    return sb.toString();
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the setter method of the given getter method or throws an exception if no such method exists
 *
 * @param m getter method for which a matching setter method shall be found
 * @return setter method belonging to the given getter method
 * @throws java.lang.IllegalArgumentException if the given getter Method has no setter method
 *//*from w w w. j  ava  2  s. co  m*/
static Method getSetterFromGetter(Method m) {
    try {
        return m.getDeclaringClass()
                .getMethod(m.getName().startsWith("get") ? m.getName().replaceFirst("g", "s")
                        : m.getName().replaceFirst("is", "set"), m.getReturnType());
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException(format("Method %s has no corresponding setter method", m.getName()));
    }
}

From source file:api.wiki.WikiGenerator.java

private static String methodDisplayName(Method apiMethod) {
    String methodName = apiMethod.getName();
    String parameterTypes = stream(apiMethod.getGenericParameterTypes()).map(WikiGenerator::typeDisplayName)
            .collect(joining(", "));
    return typeDisplayName(apiMethod.getGenericReturnType()) + " " + methodName + "(" + parameterTypes + ")";
}