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.jkoolcloud.tnt4j.streams.matchers.StringMatcher.java

private static Method findMatchingMethodAndConvertArgs(Object methodName, String[] arguments,
        Object[] convertedArguments, Method[] methods) {
    for (Method method : methods) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arguments.length + 1) {
            boolean methodMatch = true;
            Class<?>[] parameterTypes = method.getParameterTypes();
            for (int i = 1; i < parameterTypes.length; i++) {
                Class<?> parameterType = parameterTypes[i];
                if (CharSequence.class.isAssignableFrom(parameterType)) {
                    convertedArguments[i - 1] = arguments[i - 1];
                } else {
                    try {
                        if (parameterType.isPrimitive()) {
                            parameterType = ClassUtils.primitiveToWrapper(parameterType);
                        }//w  w  w  . j  av a  2s .co  m
                        Method converterMethod = parameterType.getMethod("valueOf", String.class);
                        convertedArguments[i - 1] = converterMethod.invoke(null, arguments[i - 1]);
                    } catch (Exception e) {
                        methodMatch = false;
                        break;
                    }
                }
            }

            if (methodMatch) {
                return method;
            }
        }
    }
    return null;
}

From source file:ar.com.zauber.commons.spring.web.CommandURLParameterGenerator.java

/**
 * @see #getURLParameter(Class, Set)//from  w  w w  .java2 s  . c  om
 */
public static String getURLParameter(final Class<?> cmdClass, final Map<Class<?>, Object> values) {
    final StringBuilder sb = new StringBuilder("?");
    final Pattern getter = Pattern.compile("^get(.*)$");
    boolean first = true;

    // look for getters
    for (final Method method : cmdClass.getMethods()) {
        final Matcher matcher = getter.matcher(method.getName());
        if (matcher.lookingAt() && matcher.groupCount() == 1 && method.getParameterTypes().length == 0
                && values.containsKey(method.getReturnType())) {
            try {
                cmdClass.getMethod("set" + matcher.group(1), new Class[] { method.getReturnType() });
                if (!first) {
                    sb.append("&");
                } else {
                    first = false;
                }

                sb.append(URLEncoder.encode(matcher.group(1).toLowerCase(), CHARSET));
                sb.append("=");
                sb.append(URLEncoder.encode(values.get(method.getReturnType()).toString(), CHARSET));
            } catch (Exception e) {
                // skip
            }
        }
    }

    return sb.toString();
}

From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java

public static Method findMethodByName(String name) {

    Method out = null;/*from  w  w  w. j  a  v  a  2 s .  c o  m*/
    Method[] method = Constants.class.getMethods();

    for (int i = 0; i < method.length; i++) {
        Method method1 = method[i];
        if (method1.getName().equals(name)) {

            out = method1;
            break;
        }
    }

    return out;
}

From source file:com.hihframework.core.utils.BeanUtils.java

/**
 * java bean??Map//from w  ww  .ja  v a  2s  .com
 * @param bean
 * @param map
 * @author 
 * @since 
 */
public static void beanToMap(Object bean, Map<String, Object> map) {
    if (map == null || bean == null)
        return;
    Method[] methods = bean.getClass().getDeclaredMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        if (!name.startsWith("get") && !name.startsWith("is"))
            continue;
        int position = name.startsWith("get") ? 3 : 2;
        String n = StringHelpers.lowerFirst(name.substring(position));
        try {
            Object val = m.invoke(bean);
            map.put(n, val);
        } catch (Exception e) {
        }
    }
}

From source file:com.bluecloud.ioc.classloader.ClassHandler.java

/**
 * <h3>Class</h3>/*from  w  w  w . j  av a2 s  .co  m*/
 * 
 * @param obj
 *            
 * @param methodName
 *            ??
 * @param args
 *            ?
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void invokeMethod(Object obj, String methodName, Object args)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<? extends Object> clazz = obj.getClass();
    try {
        if (args != null) {
            clazz.getMethod(methodName, args.getClass()).invoke(obj, args);
        }
    } catch (SecurityException e) {
        log.error(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
                method.invoke(obj, args);
            }
        }
    }
}

From source file:jenkins.plugins.git.MethodUtils.java

/**
 * <p>Retrieves a method whether or not it's accessible. If no such method
 * can be found, return {@code null}.</p>
 *
 * @param cls            The class that will be subjected to the method search
 * @param methodName     The method that we wish to call
 * @param parameterTypes Argument class types
 * @return The method//from   w w  w  . ja va  2  s .  com
 */
static Method getMethodImpl(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) {
    Validate.notNull(cls, "Null class not allowed.");
    Validate.notEmpty(methodName, "Null or blank methodName not allowed.");

    // fast path, check if directly declared on the class itself
    for (final Method method : cls.getDeclaredMethods()) {
        if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) {
            return method;
        }
    }
    if (!cls.isInterface()) {
        // ok, now check if directly implemented on a superclass
        // Java 8: note that super-interface implementations trump default methods
        for (Class<?> klass = cls.getSuperclass(); klass != null; klass = klass.getSuperclass()) {
            for (final Method method : klass.getDeclaredMethods()) {
                if (methodName.equals(method.getName())
                        && Arrays.equals(parameterTypes, method.getParameterTypes())) {
                    return method;
                }
            }
        }
    }
    // ok, now we are looking for an interface method... the most specific one
    // in the event that we have two unrelated interfaces both declaring a method of the same name
    // we will give up and say we could not find the method (the logic here is that we are primarily
    // checking for overrides, in the event of a Java 8 default method, that default only
    // applies if there is no conflict from an unrelated interface... thus if there are
    // default methods and they are unrelated then they don't exist... if there are multiple unrelated
    // abstract methods... well they won't count as a non-abstract implementation
    Method res = null;
    for (final Class<?> klass : (List<Class<?>>) ClassUtils.getAllInterfaces(cls)) {
        for (final Method method : klass.getDeclaredMethods()) {
            if (methodName.equals(method.getName())
                    && Arrays.equals(parameterTypes, method.getParameterTypes())) {
                if (res == null) {
                    res = method;
                } else {
                    Class<?> c = res.getDeclaringClass();
                    if (c == klass) {
                        // match, ignore
                    } else if (c.isAssignableFrom(klass)) {
                        // this is a more specific match
                        res = method;
                    } else if (!klass.isAssignableFrom(c)) {
                        // multiple overlapping interfaces declare this method and there is no common ancestor
                        return null;

                    }
                }
            }
        }
    }
    return res;
}

From source file:de.micromata.genome.gwiki.utils.ClassUtils.java

public static boolean hasMethod(Class<?> cls, String method) {
    if (StringUtils.isEmpty(method) == true) {
        return false;
    }/*from   w w w  . j av a  2  s. c  om*/
    for (Method m : cls.getMethods()) {
        if (method.equals(m.getName()) == true) {
            return true;
        }
    }
    return false;
}

From source file:com.curl.orb.servlet.InstanceManagementUtil.java

public static boolean isGetter(Method method) {
    return (method.getName().startsWith("get") && Character.isUpperCase(method.getName().charAt(3))
            && method.getParameterTypes().length == 0 && method.getReturnType() != void.class);
}

From source file:com.curl.orb.servlet.InstanceManagementUtil.java

public static boolean isSetter(Method method) {
    return (method.getName().startsWith("set") && Character.isUpperCase(method.getName().charAt(3))
            && method.getParameterTypes().length == 1 && method.getReturnType() == void.class);
}

From source file:com.hihframework.core.utils.BeanUtils.java

/**
 * ?????/*from  w w w  . j  av a  2 s . c  o m*/
 * @param bean
 * @param name
 * @return
 * @author 
 * @since 
 */
public static String getProperty(Object bean, String name) {
    try {
        Method m = bean.getClass().getMethod("get" + StringHelpers.upperFirst(name));
        if (m != null && Modifier.isPublic(m.getModifiers())) {
            Object val = m.invoke(bean);
            return val == null ? null : val.toString();
        }
    } catch (Exception e) {
    }
    try {
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (Method method : methods) {
            String n = method.getName();
            if (!n.startsWith("get"))
                continue;
            n = n.substring(3);
            if (!name.equalsIgnoreCase(n))
                continue;
            if (!Modifier.isPublic(method.getModifiers()))
                continue;
            Object val = method.invoke(bean);
            return val == null ? null : val.toString();
        }
    } catch (Exception e) {
    }
    return null;
}