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.github.dozermapper.core.util.ReflectionUtils.java

private static boolean isNonVoidSetter(Method method, String setterMethodName) {
    return method.getName().equals(setterMethodName) && method.getParameterTypes().length == 1
            && method.getReturnType() != Void.TYPE;
}

From source file:com.autobizlogic.abl.util.BeanUtil.java

/**
 * Get the method with the given name from the given class, provided that it takes one argument
 * of the provided type.// www.jav  a2s.c o m
 * @param cls The class who should have (or inherit) the method
 * @param methodName The name of the method
 * @param argClass If provided, the type of the sole argument to the method. If null, no argument is assumed.
 * @param onlyProtectedAndHigher If true, we will ignore private methods in the superclasses.
 * @return The method if found, otherwise null.
 */
private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass,
        boolean onlyProtectedAndHigher) {

    Method[] allMethods = cls.getDeclaredMethods();
    for (Method meth : allMethods) {
        if (!meth.getName().equals(methodName))
            continue;

        if (onlyProtectedAndHigher) {
            int modifiers = meth.getModifiers();
            if (Modifier.isPrivate(modifiers))
                continue;
        }

        if (argClass != null) {
            Class<?>[] paramTypes = meth.getParameterTypes();
            if (paramTypes.length != 1)
                continue;

            Class<?> genericType = getGenericType(paramTypes[0]);
            if (!genericType.isAssignableFrom(argClass))
                continue;
        }

        // Note that if we're trying to set a value to null, we obviously cannot check the
        // signature for overloading, and therefore we'll return the first method which takes
        // one parameter. I think that's not that unreasonable, but it could conceivably break
        // if someone does funny things with their bean.

        return meth;
    }

    return null;
}

From source file:podd.util.PoddWebappTestUtil.java

public static User createUser(Map<String, Object> map) {
    try {/* w  w  w  . j  ava  2s.co  m*/
        Method[] methods = User.class.getMethods();
        Map<String, Method> methodMap = new HashMap<String, Method>();
        for (Method mtd : methods) {
            methodMap.put(mtd.getName(), mtd);
        }
        User user = new UserImpl();

        for (String key : map.keySet()) {
            String methodName = "set" + key;
            final Method method = methodMap.get(methodName);
            Object value = map.get(key);
            method.invoke(user, value);
        }
        return user;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

private static boolean isAutoboxingSetter(Method method, String setterMethodName, Field field) {
    return method.getName().equals(setterMethodName) && method.getParameterTypes().length == 1
            && canBeAutoboxed(method.getParameterTypes()[0], field.getType());
}

From source file:com.weibo.api.motan.protocol.grpc.GrpcUtil.java

@SuppressWarnings("rawtypes")
public static HashMap<String, MethodDescriptor> getMethodDescriptorByAnnotation(Class<?> clazz,
        String serialization) throws Exception {
    String clazzName = getGrpcClassName(clazz);
    HashMap<String, MethodDescriptor> result = serviceMap.get(clazzName);
    if (result == null) {
        synchronized (serviceMap) {
            if (!serviceMap.containsKey(clazzName)) {
                ServiceDescriptor serviceDesc = getServiceDesc(getGrpcClassName(clazz));
                HashMap<String, MethodDescriptor> methodMap = new HashMap<String, MethodDescriptor>();
                for (MethodDescriptor<?, ?> methodDesc : serviceDesc.getMethods()) {
                    Method interfaceMethod = getMethod(methodDesc.getFullMethodName(), clazz);
                    if (JSON_CODEC.equals(serialization)) {
                        methodDesc = convertJsonDescriptor(methodDesc, interfaceMethod.getParameterTypes()[0],
                                interfaceMethod.getReturnType());
                    }/*from   ww w .  j a v a 2  s  .  com*/
                    methodMap.put(interfaceMethod.getName(), methodDesc);
                }
                serviceMap.put(clazzName, methodMap);
            }
            result = serviceMap.get(clazzName);
        }
    }
    return result;
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

private static String findGetterFieldName(Method m) {
    String name = m.getName();
    if (name.length() > 3 && name.startsWith("get") && Character.isUpperCase(name.charAt(3))) {
        if (name.length() == 4) {
            name = String.valueOf(Character.toLowerCase(name.charAt(3)));
        } else {/*  w w  w  .j a  va  2 s.  c o m*/
            name = String.valueOf(Character.toLowerCase(name.charAt(3))) + name.substring(4);
        }
    } else if (name.length() > 2 && name.startsWith("is") && Character.isUpperCase(name.charAt(2))) {
        if (name.length() == 3) {
            name = String.valueOf(Character.toLowerCase(name.charAt(2)));
        } else {
            name = String.valueOf(Character.toLowerCase(name.charAt(2))) + name.substring(3);
        }
    }
    return name;
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Generate a string representation of an Annotation, as suggested by
 * {@link Annotation#toString()}.</p>
 *
 * @param a the annotation of which a string representation is desired
 * @return the standard string representation of an annotation, not
 * {@code null}/*from   w w  w. jav a  2 s  .c o  m*/
 */
public static String toString(final Annotation a) {
    ToStringBuilder builder = new ToStringBuilder(a, TO_STRING_STYLE);
    for (Method m : a.annotationType().getDeclaredMethods()) {
        if (m.getParameterTypes().length > 0) {
            continue; //wtf?
        }
        try {
            builder.append(m.getName(), m.invoke(a));
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    return builder.build();
}

From source file:com.mani.cucumber.ReflectionUtils.java

/**
 * Finds a method of the given name that will accept a parameter of the
 * given type. If more than one method matches, returns the first such
 * method found.//from   www.j ava 2  s  .  c  o m
 *
 * @param target the object to reflect on
 * @param name the name of the method to search for
 * @param parameterType the type of the parameter to be passed
 * @return the matching method
 * @throws IllegalStateException if no matching method is found
 */
public static Method findMethod(Object target, String name, Class<?> parameterType) {

    for (Method method : target.getClass().getMethods()) {
        if (!method.getName().equals(name)) {
            continue;
        }

        Class<?>[] parameters = method.getParameterTypes();
        if (parameters.length != 1) {
            continue;
        }

        if (parameters[0].isAssignableFrom(parameterType)) {
            return method;
        }
    }

    throw new IllegalStateException(
            "No method '" + name + "(" + parameterType + ") on type " + target.getClass());
}

From source file:ReflectUtil.java

/**
 * <p>A better (more concise) toString method for annotation types that yields a String
 * that should look more like the actual usage of the annotation in a class. The String produced
 * is similar to that produced by calling toString() on the annotation directly, with the
 * following differences:</p>/* w  ww .j ava 2s  . c  o m*/
 *
 * <ul>
 *   <li>Uses the classes simple name instead of it's fully qualified name.</li>
 *   <li>Only outputs attributes that are set to non-default values.</li>
 *
 * <p>If, for some unforseen reason, an exception is thrown within this method it will be
 * caught and the return value will be {@code ann.toString()}.
 *
 * @param ann the annotation to convert to a human readable String
 * @return a human readable String form of the annotation and it's attributes
 */
public static String toString(Annotation ann) {
    try {
        Class<? extends Annotation> type = ann.annotationType();
        StringBuilder builder = new StringBuilder(128);
        builder.append("@");
        builder.append(type.getSimpleName());

        boolean appendedAnyParameters = false;
        Method[] methods = type.getMethods();
        for (Method method : methods) {
            if (!INHERITED_ANNOTATION_METHODS.contains(method.getName())) {
                Object defaultValue = method.getDefaultValue();
                Object actualValue = method.invoke(ann);

                // If we have arrays, they have to be treated a little differently
                Object[] defaultArray = null, actualArray = null;
                if (Object[].class.isAssignableFrom(method.getReturnType())) {
                    defaultArray = (Object[]) defaultValue;
                    actualArray = (Object[]) actualValue;
                }

                // Only print an attribute if it isn't set to the default value
                if ((defaultArray != null && !Arrays.equals(defaultArray, actualArray))
                        || (defaultArray == null && !actualValue.equals(defaultValue))) {

                    if (appendedAnyParameters) {
                        builder.append(", ");
                    } else {
                        builder.append("(");
                    }

                    builder.append(method.getName());
                    builder.append("=");

                    if (actualArray != null) {
                        builder.append(Arrays.toString(actualArray));
                    } else {
                        builder.append(actualValue);
                    }

                    appendedAnyParameters = true;
                }
            }
        }

        if (appendedAnyParameters) {
            builder.append(")");
        }

        return builder.toString();
    } catch (Exception e) {
        return ann.toString();
    }
}

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

protected static void collectMethods(Class<?> cls, Map<String, Method> collected) {
    for (Method m : cls.getDeclaredMethods()) {
        if ((m.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) {
            continue;
        }/* ww  w  .  j a v a  2s  .c  o m*/
        if ((m.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
            continue;
        }
        if (m.getAnnotation(TpsbIgnore.class) != null) {
            continue;
        }
        if (ignoreMethods.contains(m.getName()) == true) {
            continue;
        }
        if (m.getReturnType().isPrimitive() == true) {
            continue;
        }
        String sm = methodToString(m);
        if (collected.containsKey(sm) == true) {
            continue;
        }
        collected.put(sm, m);
    }
    Class<?> scls = cls.getSuperclass();
    if (scls == Object.class) {
        return;
    }
    collectMethods(scls, collected);
}