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.mawujun.util.AnnotationUtils.java

/**
 * <p>Generate a hash code for the given annotation using the algorithm
 * presented in the {@link Annotation#hashCode()} API docs.</p>
 *
 * @param a the Annotation for a hash code calculation is desired, not
 * {@code null}/* w w w .jav a  2s . co m*/
 * @return the calculated hash code
 * @throws RuntimeException if an {@code Exception} is encountered during
 * annotation member access
 * @throws IllegalStateException if an annotation method invocation returns
 * {@code null}
 */
public static int hashCode(Annotation a) {
    int result = 0;
    Class<? extends Annotation> type = a.annotationType();
    for (Method m : type.getDeclaredMethods()) {
        try {
            Object value = m.invoke(a);
            if (value == null) {
                throw new IllegalStateException(String.format("Annotation method %s returned null", m));
            }
            result += hashMember(m.getName(), value);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    return result;
}

From source file:com.sun.faces.el.impl.BeanInfoManager.java

/**
 * If the given class is public and has a Method that declares the
 * same name and arguments as the given method, then that method is
 * returned.  Otherwise the superclass and interfaces are searched
 * recursively./*from   w  w w.j  a v  a2s . c o  m*/
 */
static Method getPublicMethod(Class pClass, Method pMethod) {
    // See if this is a public class declaring the method
    if (Modifier.isPublic(pClass.getModifiers())) {
        try {
            Method m;
            try {
                m = pClass.getDeclaredMethod(pMethod.getName(), pMethod.getParameterTypes());
            } catch (java.security.AccessControlException ex) {
                // kludge to accommodate J2EE RI's default settings
                // TODO: see if we can simply replace
                //       getDeclaredMethod() with getMethod() ...?
                m = pClass.getMethod(pMethod.getName(), pMethod.getParameterTypes());
            }
            if (Modifier.isPublic(m.getModifiers())) {
                return m;
            }
        } catch (NoSuchMethodException exc) {
        }
    }

    // Search the interfaces
    {
        Class[] interfaces = pClass.getInterfaces();
        if (interfaces != null) {
            for (int i = 0; i < interfaces.length; i++) {
                Method m = getPublicMethod(interfaces[i], pMethod);
                if (m != null) {
                    return m;
                }
            }
        }
    }

    // Search the superclass
    {
        Class superclass = pClass.getSuperclass();
        if (superclass != null) {
            Method m = getPublicMethod(superclass, pMethod);
            if (m != null) {
                return m;
            }
        }
    }

    return null;
}

From source file:ch.ralscha.extdirectspring.util.MethodInfo.java

/**
 * Find a method that is annotated with a specific annotation. Starts with the method
 * and goes up to the superclasses of the class.
 *
 * @param method the starting method/*from  w  ww. j a v a2s.  co  m*/
 * @param annotation the annotation to look for
 * @return the method if there is a annotated method, else null
 */
public static Method findMethodWithAnnotation(Method method, Class<? extends Annotation> annotation) {
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    Class<?> cl = method.getDeclaringClass();
    while (cl != null && cl != Object.class) {
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            if (equivalentMethod.isAnnotationPresent(annotation)) {
                return equivalentMethod;
            }
        } catch (NoSuchMethodException e) {
            // do nothing here
        }
        cl = cl.getSuperclass();
    }

    return null;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private final static List findMutatorMethods(Class _class) {
    List methodList = new ArrayList();

    Enhancer.getMethods(_class, null, methodList);

    Predicate mutatorPredicate = new Predicate() {
        public boolean evaluate(Object _object) {
            Method method = (Method) _object;

            boolean startsWithSet = (method.getName().startsWith("set") == true);
            boolean returnTypeIsVoid = ("void".equals(method.getReturnType().getName()) == true);
            boolean parameterTypeCountIsOne = (method.getParameterTypes().length == 1);
            boolean isPublic = (Modifier.isPublic(method.getModifiers()) == true);

            return startsWithSet && returnTypeIsVoid && parameterTypeCountIsOne && isPublic;
        }//from ww  w  .  java 2  s  . c o m
    };

    CollectionUtils.filter(methodList, mutatorPredicate);

    return methodList;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private final static List findOtherPublicMethods(Class _class) {
    List methodList = new ArrayList();

    Enhancer.getMethods(_class, null, methodList);

    Predicate mutatorPredicate = new Predicate() {
        public boolean evaluate(Object _object) {
            Method method = (Method) _object;

            boolean startsWithSet = (method.getName().startsWith("set") == true);
            boolean returnTypeIsVoid = ("void".equals(method.getReturnType().getName()) == true);
            boolean parameterTypeCountIsOne = (method.getParameterTypes().length == 1);
            boolean isPublic = (Modifier.isPublic(method.getModifiers()) == true);

            return ((startsWithSet && returnTypeIsVoid && parameterTypeCountIsOne) == false) && isPublic;
        }//from w  w  w . j  a v a 2  s  . com
    };

    CollectionUtils.filter(methodList, mutatorPredicate);

    return methodList;
}

From source file:Main.java

private static void setOnClickListener(final Object object, final View view, final Method method) {
    if (null == view)
        return;/*from  w ww.  java  2s  . co m*/
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Class<?>[] classes = method.getParameterTypes();
                if (null == classes || 0 == classes.length) {
                    method.invoke(object);
                } else {
                    method.invoke(object, view);
                }
            } catch (Exception e) {
                throw new RuntimeException(
                        "setOnClickListener fail,method is " + method.getName() + " \n" + e.getMessage());
            }
        }
    });
}

From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java

public static RPCAPIInfo getAPIInfo(String realPath, Method method) {
    RPCAPIInfo apiInfo = new RPCAPIInfo();

    ClassInfo classInfo = RPCAPIDocletUtil.getClassInfo(method.getDeclaringClass().getName());
    MethodInfo methodInfo = null;/*  www  . j av  a2s. c  o m*/

    if (classInfo != null) {
        methodInfo = classInfo.getMethodInfo(method.getName());
    }

    if (methodInfo != null) {

        apiInfo.setComment(methodInfo.getComment());
    }

    Annotation[][] parameterAnnotations = method.getParameterAnnotations();

    Security securityAnnotation = method.getAnnotation(Security.class);
    ResourceMapping resourceMapping = method.getAnnotation(ResourceMapping.class);

    Object returnType = null;

    returnType = buildTypeStructure(method.getReturnType(), method.getGenericReturnType(), null);

    boolean checkCSRF = false;
    if (securityAnnotation != null) {
        checkCSRF = securityAnnotation.checkCSRF();
    }

    apiInfo.setPattern(realPath.replaceAll("///", "/"));
    apiInfo.setCheckCSRF(checkCSRF);
    apiInfo.setResponseMimes(StringUtils.arrayToDelimitedString(resourceMapping.produces(), ","));
    apiInfo.setHttpMethod(StringUtils.arrayToDelimitedString(resourceMapping.method(), ","));

    List<RPCAPIInfo.Parameter> parameters = new ArrayList<RPCAPIInfo.Parameter>();

    RPCAPIInfo.Parameter parameter = null;
    LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] paramNames = nameDiscoverer.getParameterNames(method);
    int i = 0;

    Class<?>[] parameterTypes = method.getParameterTypes();

    Type[] genericParameterTypes = method.getGenericParameterTypes();

    Class<?> paramType = null;

    Type genericType;

    for (int k = 0; k < parameterTypes.length; k++) {

        paramType = parameterTypes[k];
        genericType = genericParameterTypes[k];

        Annotation[] pAnnotations = parameterAnnotations[i];

        if (HttpServletRequest.class.isAssignableFrom(paramType)
                || HttpServletResponse.class.isAssignableFrom(paramType)
                || ErrorContext.class.isAssignableFrom(paramType)) {
            continue;
        } // end if

        String realParamName = paramNames[k];

        if (pAnnotations.length == 0) {

            parameter = apiInfo.new Parameter();
            parameter.setName(realParamName);
            parameter.setType(buildTypeStructure(paramType, genericType, null));
            parameters.add(parameter);

            setParameterComment(parameter, realParamName, methodInfo);

            continue;
        } // end if

        for (Annotation annotation : pAnnotations) {
            parameter = apiInfo.new Parameter();
            setParameterComment(parameter, realParamName, methodInfo);

            if (annotation instanceof RequestParam) {
                RequestParam requestParam = (RequestParam) annotation;
                parameter.setName(requestParam.name());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                if (!StringUtil.isBlank(requestParam.defaultValue())) {
                    parameter.setDefaultValue(requestParam.defaultValue().trim());
                }
                parameters.add(parameter);
            } else if (annotation instanceof PathParam) {
                PathParam pathParam = (PathParam) annotation;
                parameter.setName(pathParam.name());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                if (!StringUtil.isBlank(pathParam.defaultValue())) {
                    parameter.setDefaultValue(pathParam.defaultValue().trim());
                }
                parameters.add(parameter);
            } else if (annotation instanceof JsonParam) {
                JsonParam pathParam = (JsonParam) annotation;
                parameter.setName(pathParam.value());
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                parameters.add(parameter);
            } else if (annotation instanceof RequestParams) {
                parameter.setName(realParamName);
                parameter.setType(buildTypeStructure(paramType, genericType, null));
                parameters.add(parameter);
            } else if (annotation instanceof File) {
                File file = (File) annotation;
                parameter.setName(file.value());
                parameter.setType("");
                parameters.add(parameter);
            } // end if
        } // end for
        i++;
    } // end for
    apiInfo.setParmeters(parameters);
    apiInfo.setReturnType(returnType);
    return apiInfo;
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

private static <A extends Annotation> A searchOnInterfaces(final Method method, final Class<A> annotationType,
        final Class<?>[] ifcs) {
    A annotation = null;/*from  w  w w. j av  a 2  s .co m*/
    for (final Class<?> iface : ifcs) {
        if (isInterfaceWithAnnotatedMethods(iface)) {
            try {
                final Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
                annotation = getAnnotation(equivalentMethod, annotationType);
            } catch (final NoSuchMethodException ex) {
                // Skip this interface - it doesn't have the method...
            }
            if (annotation != null) {
                break;
            }
        }
    }
    return annotation;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Determine whether the given method is a "hashCode" method.
 * /*ww w  . ja va 2s.com*/
 * @see java.lang.Object#hashCode
 */
public static boolean isHashCodeMethod(final Method method) {
    return method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Determine whether the given method is a "toString" method.
 * //  www.j  av a 2  s  .  com
 * @see java.lang.Object#toString()
 */
public static boolean isToStringMethod(final Method method) {
    return method != null && method.getName().equals("toString") && method.getParameterTypes().length == 0;
}