Java Reflection Method Get from Object getMethods(Object instance, String name, Class[] arguments, boolean isPrefix)

Here you can find the source of getMethods(Object instance, String name, Class[] arguments, boolean isPrefix)

Description

Returns setters of given instance with given argumentType

License

Open Source License

Declaration

public static List<Method> getMethods(Object instance, String name, Class<?>[] arguments, boolean isPrefix) 

Method Source Code

//package com.java2s;
/**//from   w ww.  ja v  a2s  . c  om
 * This file is part of GraphJ
 * 
 * Copyright (C) 2009 Nils Meier
 * 
 * GraphJ is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GraphJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GraphJ; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Returns setters of given instance with given argumentType
     * @name regex for matching method-name
     */
    public static List<Method> getMethods(Object instance, String name, Class<?>[] arguments, boolean isPrefix) {

        // loop over methods
        Method[] methods = instance.getClass().getMethods();
        ArrayList<Method> collect = new ArrayList<Method>(methods.length);
        compliance: for (int m = 0; m < methods.length; m++) {
            // here's a method
            Method method = methods[m];
            // check prefix
            if (!method.getName().matches(name))
                continue;
            // check public
            if (!Modifier.isPublic(method.getModifiers()))
                continue;
            // check static
            if (Modifier.isStatic(method.getModifiers()))
                continue;
            // check parameter types
            Class<?>[] ptypes = method.getParameterTypes();
            for (int a = 0; a < ptypes.length; a++) {
                if (a >= arguments.length) {
                    if (!isPrefix)
                        continue compliance;
                    else
                        break;
                }
                if (arguments[a] != null && !ptypes[a].isAssignableFrom(arguments[a]))
                    continue compliance;
            }
            collect.add(method);
        }

        // done
        return collect;
    }

    /**
     * Returns the name of a class without package information
     */
    public static String getName(Class<?> clazz) {
        String result = clazz.getName();
        int dot = result.lastIndexOf('.');
        return dot < 0 ? result : result.substring(dot + 1);
    }
}

Related

  1. getMethodObject(Object object, String method, Object[] parametre)
  2. getMethodResult(final Object element, final String methodName)
  3. getMethodReturn(String className, String methodName, Class[] params, Object[] args, boolean isStatic)
  4. getMethodReturnTypeGeneric(Object source, Method method)
  5. getMethods(Class objectClass, Class annotationClass)
  6. getMethods(Object obj, boolean hasParent)
  7. getMethods(Object obj, int cmpModifier, String prefix)
  8. getMethodsAnnotatedWith(Class ann, Object o)
  9. getMethodsAnnotatedWith(Object target, Class annotation)