Example usage for java.lang.reflect Method getModifiers

List of usage examples for java.lang.reflect Method getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Method getModifiers.

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:com.gu.management.spring.ManagementUrlDiscoveryService.java

private boolean isRequestableMethod(Method method) {
    Class<?>[] parameterTypes = method.getParameterTypes();

    boolean isPublic = Modifier.isPublic(method.getModifiers());
    boolean isProtected = Modifier.isProtected(method.getModifiers());
    boolean returnsModelAndView = method.getReturnType().equals(ModelAndView.class);
    boolean takesRequestAndResponse = parameterTypes.length >= 2
            && HttpServletRequest.class.isAssignableFrom(parameterTypes[0])
            && HttpServletResponse.class.isAssignableFrom(parameterTypes[1]);

    return (isPublic || isProtected) && (returnsModelAndView || takesRequestAndResponse);
}

From source file:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java

@Override
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
    Method result = null;//from  w  w  w  . j a v  a2 s . c  o m
    final PropertyDescriptor propertyDescriptor = findPropertyDescriptorFor(clazz, propertyName);
    if (propertyDescriptor != null) {
        result = propertyDescriptor.getReadMethod();
    }
    if (result == null) {
        Class<?> current = clazz;
        final String getterName = "get" + StringUtils.capitalize(propertyName);
        while (result == null && !Object.class.equals(current)) {
            try {
                final Method potentialMethod = current.getDeclaredMethod(getterName);
                if (!mustBeStatic || Modifier.isStatic(potentialMethod.getModifiers())) {
                    if (!potentialMethod.isAccessible()) {
                        potentialMethod.setAccessible(true);
                    }
                    result = potentialMethod;
                }
            } catch (NoSuchMethodException ignored) {
            }
            current = current.getSuperclass();
        }
    }
    return result;
}

From source file:net.sf.cocmvc.ConventionalHandlerMapping.java

private boolean isAction(Method method) {
    String name = method.getName();
    return Modifier.isPublic(method.getModifiers())
            && !(name.matches("^(equals|hashCode|toString|clone|notify.*|wait|getClass)") // methods declared in Object
                    || name.matches("^(init|destroy)") // common lifecycle methods
                    || name.matches("^([sg]et(MetaClass|Property)|.*\\$.*|invokeMethod)") // methods of groovy object
                    || findAnnotation(method, NoMapping.class) != null);
}

From source file:com.freetmp.common.util.ClassUtils.java

public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(methodName, "Method name must not be null");
    try {//from ww  w .j a  v  a 2  s .  co  m
        Method method = clazz.getMethod(methodName, args);
        return Modifier.isStatic(method.getModifiers()) ? method : null;
    } catch (NoSuchMethodException ex) {
        return null;
    }
}

From source file:org.apache.axis2.jaxws.description.impl.EndpointInterfaceDescriptionImpl.java

private static Method[] getSEIMethods(Class sei) {
    // Per JSR-181 all methods on the SEI are mapped to operations regardless
    // of whether they include an @WebMethod annotation.  That annotation may
    // be present to customize the mapping, but is not required (p14)
    Method[] seiMethods = sei.getMethods();
    ArrayList methodList = new ArrayList();
    if (sei != null) {
        for (Method method : seiMethods) {

            if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
                continue;
            }//from ww w  .  j  av  a 2  s .  c om
            methodList.add(method);
            if (!Modifier.isPublic(method.getModifiers())) {
                // JSR-181 says methods must be public (p14)
                throw ExceptionFactory.makeWebServiceException(Messages.getMessage("seiMethodsErr"));
            }
            // TODO: other validation per JSR-181
        }

    }
    return (Method[]) methodList.toArray(new Method[methodList.size()]);
    //        return seiMethods;
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Return a public static method of a class.
 * @param clazz the class which defines the method
 * @param methodName the static method name
 * @param args the parameter types to the method
 * @return the static method, or {@code null} if no static method was found
 * @throws IllegalArgumentException if the method name is blank or the clazz is null
 *///from w w  w .  j  a  va2  s  .c o  m
public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    org.springframework.util.Assert.notNull(methodName, "Method name must not be null");
    try {
        Method method = clazz.getMethod(methodName, args);
        return Modifier.isStatic(method.getModifiers()) ? method : null;
    } catch (NoSuchMethodException ex) {
        return null;
    }
}

From source file:org.apache.axis2.transport.http.AbstractAgent.java

private void examineMethods(Method[] aDeclaredMethods) {
    for (int i = 0; i < aDeclaredMethods.length; i++) {
        Method method = aDeclaredMethods[i];

        Class[] parameterTypes = method.getParameterTypes();
        if ((Modifier.isProtected(method.getModifiers()) || Modifier.isPublic(method.getModifiers()))
                && method.getName().startsWith(METHOD_PREFIX) && parameterTypes.length == 2
                && parameterTypes[0].equals(HttpServletRequest.class)
                && parameterTypes[1].equals(HttpServletResponse.class)) {

            String key = method.getName().substring(METHOD_PREFIX.length()).toLowerCase();

            // ensure we don't overwrite existing method with superclass method
            if (!operationCache.containsKey(key)) {
                operationCache.put(key, method);
            }/* w w w  .j a  v  a 2  s. c o  m*/
        }
    }
}

From source file:com.redhat.rhn.frontend.xmlrpc.api.ApiHandler.java

/**
 * Lists all available api calls for the specified namespace
 * @param loggedInUser The current user/* w w  w.j  ava2 s .c om*/
 * @param namespace namespace of interest
 * @return a map containing list of api calls for every namespace
 * @throws ClassNotFoundException if namespace isn't valid
 *
 * @xmlrpc.doc Lists all available api calls for the specified namespace
 * @xmlrpc.param #param("string", "sessionKey")
 * @xmlrpc.param #param("string", "namespace")
 * @xmlrpc.returntype
 *   #struct("method_info")
 *        #prop_desc("string", "name", "method name")
 *        #prop_desc("string", "parameters", "method parameters")
 *        #prop_desc("string", "exceptions", "method exceptions")
 *        #prop_desc("string", "return", "method return type")
 *   #struct_end()
 */
public Map getApiNamespaceCallList(User loggedInUser, String namespace) throws ClassNotFoundException {
    Class<? extends BaseHandler> handlerClass = new HandlerFactory().getHandler(namespace).getClass();
    Map<String, Map<String, Object>> methods = new HashMap<String, Map<String, Object>>();

    for (Method method : handlerClass.getDeclaredMethods()) {

        if (0 != (method.getModifiers() & Modifier.PUBLIC)) {

            Map<String, Object> methodInfo = new HashMap<String, Object>();

            methodInfo.put("name", method.getName());

            List<String> paramList = new ArrayList<String>();
            String paramListString = "";
            for (Type paramType : method.getParameterTypes()) {
                String paramTypeString = getType(paramType, StringUtils.isEmpty(paramListString));
                paramList.add(paramTypeString);
                paramListString += "_" + paramTypeString;
            }
            methodInfo.put("parameters", paramList);

            Set<String> exceptList = new HashSet<String>();
            for (Class<?> exceptClass : method.getExceptionTypes()) {
                exceptList.add(StringUtil.getClassNameNoPackage(exceptClass));
            }
            methodInfo.put("exceptions", exceptList);
            methodInfo.put("return", getType(method.getReturnType(), false));

            String methodName = namespace + "." + methodInfo.get("name") + paramListString;
            methods.put(methodName, methodInfo);
        }
    }
    return methods;
}

From source file:com.agileapes.couteau.context.spring.event.impl.GenericTranslationScheme.java

@Override
public void fillIn(Event originalEvent, ApplicationEvent translated) throws EventTranslationException {
    if (!(translated instanceof GenericApplicationEvent)) {
        return;//from   w ww  .j a  v a 2  s  .c o  m
    }
    GenericApplicationEvent applicationEvent = (GenericApplicationEvent) translated;
    final Enumeration<?> propertyNames = applicationEvent.getPropertyNames();
    while (propertyNames.hasMoreElements()) {
        final String property = (String) propertyNames.nextElement();
        final Object value = applicationEvent.getProperty(property);
        final Method method = ReflectionUtils.findMethod(originalEvent.getClass(),
                "set" + StringUtils.capitalize(property));
        if (method == null || !Modifier.isPublic(method.getModifiers())
                || method.getParameterTypes().length != 1 || !method.getReturnType().equals(void.class)
                || !method.getParameterTypes()[0].isInstance(value)) {
            continue;
        }
        try {
            method.invoke(originalEvent, value);
        } catch (Exception e) {
            throw new EventTranslationException("Failed to call setter on original event", e);
        }
    }
}

From source file:org.jgentleframework.utils.ReflectUtils.java

/**
 * Returns <b>true</b> if given {@link Method} is <b>public, static,
 * final</b>, otherwise returns <b>false</b>.
 * /*from  w ww. jav  a  2 s  . c o m*/
 * @param method
 *            given {@link Method} need to be tested
 * @return true, if checks if is public static final
 */
public static boolean isPublicStaticFinal(Method method) {

    int modifiers = method.getModifiers();
    return (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers));
}