Example usage for java.lang Class getDeclaredMethods

List of usage examples for java.lang Class getDeclaredMethods

Introduction

In this page you can find the example usage for java.lang Class getDeclaredMethods.

Prototype

@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

Usage

From source file:org.jboss.spring.support.SpringInjectionSupport.java

protected Method[] getAllMethods(Object bean) {
    Class<?> beanClass = bean.getClass();
    Set<Method> methods = new TreeSet<Method>(METHOD_COMPARATOR);
    while (beanClass != Object.class) {
        methods.addAll(Arrays.asList(beanClass.getDeclaredMethods()));
        beanClass = beanClass.getSuperclass();
    }/*from w  w w  .  ja va  2 s . c o m*/
    return methods.toArray(new Method[methods.size()]);
}

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/*from w w w.  j  a  v  a2 s . c o m*/
 * @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.haulmont.cuba.core.global.View.java

protected List<String> getInterfaceProperties(Class<?> intf) {
    List<String> result = new ArrayList<>();
    for (Method method : intf.getDeclaredMethods()) {
        if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
            result.add(StringUtils.uncapitalize(method.getName().substring(3)));
        }/*from  w  ww.j  a v a2s  .c o m*/
    }
    return result;
}

From source file:org.apache.tapestry.enhance.DefaultComponentClassEnhancer.java

/**
 *  Searches the class for abstract methods, returning the first found.
 *  Records non-abstract methods in the implementedMethods set.
 * // w w w  . j  a va2  s.  co  m
 **/

private Method checkForAbstractMethods(Class current, Set implementedMethods) {
    boolean debug = LOG.isDebugEnabled();

    if (debug)
        LOG.debug("Searching for abstract methods in " + current);

    Method[] methods = current.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];

        if (debug)
            LOG.debug("Checking " + m);

        boolean isAbstract = Modifier.isAbstract(m.getModifiers());

        MethodSignature s = new MethodSignature(m);

        if (isAbstract) {
            if (implementedMethods.contains(s))
                continue;

            return m;
        }

        implementedMethods.add(s);
    }

    return null;
}

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

/**
 * Convenience method for finding the first annotated method in a given class.
 * //from   w  w w  .  j a  v  a  2s  .  c  o  m
 * @param clazz            the class to reflect
 * @param annotationClasses   the annotations to look for
 * @return               the first method annotated with the provided list
 */
@SuppressWarnings("unchecked")
public Method findAnnotatedMethod(Class<?> clazz, Class<? extends Annotation>... annotationClasses) {
    for (Method method : clazz.getDeclaredMethods()) {
        for (Class<? extends Annotation> annotationClass : annotationClasses) {
            if (AnnotationUtils.findAnnotation(method, annotationClass) != null) {
                return method;
            }
        }
    }
    return null;
}

From source file:com.github.wnameless.spring.routing.RoutingPathResolver.java

private List<Method> getMethodsListWithAnnotation(final Class<?> cls,
          final Class<? extends Annotation> annotationCls) {
      Method[] allMethods = cls.getDeclaredMethods();
      List<Method> annotatedMethods = new ArrayList<Method>();
      for (Method method : allMethods) {
          if (method.getAnnotation(annotationCls) != null) {
              annotatedMethods.add(method);
          }/* w ww . j  a va 2s  . c  o m*/
      }
      return annotatedMethods;
  }

From source file:GenericClass.java

public List<MethodDef> getMethods() {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
        Method[] methods = current.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.isBridge() && !method.isSynthetic()) {
                MethodDef def = new MethodDef(method);
                if (!founds.contains(def))
                    founds.add(def);/*from   w w w  . j  a  v  a  2  s.c  om*/
            }
        }
        current = current.getSuperclass();
    }
    return founds;
}

From source file:GenericClass.java

/**
 * Search for all methods having that name, no matter which parameter they
 * take.//ww  w  .  j  ava  2  s .c om
 * 
 * @param name
 *          The name of the methods
 * @return A list of {@link MethodDef}, ordered with methods from current
 *         class first, then method from superclass and so on.
 */
public List<MethodDef> findAllMethods(String name) {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
        Method[] methods = current.getDeclaredMethods();
        for (Method method : methods) {
            if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) {
                MethodDef def = new MethodDef(method);
                if (!founds.contains(def))
                    founds.add(def);
            }
        }
        current = current.getSuperclass();
    }
    return founds;
}

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

/**
 * Does the given class or one of its superclasses at least have one or more
 * methods with the supplied name (with any argument types)?
 * Includes non-public methods.//from   w  w w .j a  v a  2s . c  o m
 * @param clazz   the clazz to check
 * @param methodName the name of the method
 * @return whether there is at least one method with the given name
 */
public static boolean hasAtLeastOneMethodWithName(Class<?> clazz, String methodName) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    org.springframework.util.Assert.notNull(methodName, "Method name must not be null");
    Method[] declaredMethods = clazz.getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return true;
        }
    }
    Class<?>[] ifcs = clazz.getInterfaces();
    for (Class<?> ifc : ifcs) {
        if (hasAtLeastOneMethodWithName(ifc, methodName)) {
            return true;
        }
    }
    return (clazz.getSuperclass() != null && hasAtLeastOneMethodWithName(clazz.getSuperclass(), methodName));
}

From source file:com.vityuk.ginger.DefaultLocalization.java

private <T extends Localizable> T createLocalizableInstance(Class<T> localizable) {
    Method[] methods = localizable.getDeclaredMethods();

    List<Callback> callbacks = Lists.newArrayListWithCapacity(methods.length + 1);
    final Map<Method, Integer> method2CallbackIndex = Maps.newHashMapWithExpectedSize(methods.length);

    callbacks.add(NoOp.INSTANCE);/*ww  w . ja v  a  2 s . com*/
    for (Method localizableMethod : methods) {
        callbacks.add(createCallback(localizableMethod));
        method2CallbackIndex.put(localizableMethod, callbacks.size() - 1);
    }

    CallbackFilter callbackFilter = new CallbackFilter() {
        @Override
        public int accept(Method method) {
            Integer index = method2CallbackIndex.get(method);
            return index == null ? 0 : index;
        }
    };

    T instance = createProxy(localizable, callbackFilter, callbacks);
    return instance;
}