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:ReflectUtil.java

/**
 * Fetches all methods of all access types from the supplied class and super
 * classes. Methods that have been overridden in the inheritance hierarchy are
 * only returned once, using the instance lowest down the hierarchy.
 *
 * @param clazz the class to inspect// ww  w .ja v  a  2  s  . c om
 * @return a collection of methods
 */
public static Collection<Method> getMethods(Class<?> clazz) {
    Collection<Method> found = new ArrayList<Method>();
    while (clazz != null) {
        for (Method m1 : clazz.getDeclaredMethods()) {
            boolean overridden = false;

            for (Method m2 : found) {
                if (m2.getName().equals(m1.getName())
                        && Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) {
                    overridden = true;
                    break;
                }
            }

            if (!overridden)
                found.add(m1);
        }

        clazz = clazz.getSuperclass();
    }

    return found;
}

From source file:ml.shifu.shifu.util.ClassUtils.java

@SuppressWarnings("unchecked")
public static List<Method> getAllMethods(Class<?> clazz) {
    if (clazz == null) {
        return Collections.EMPTY_LIST;
    }//from  w ww  .  j  a  v a  2s .c om

    if (clazz.equals(Object.class)) {
        return Collections.EMPTY_LIST;
    }

    List<Method> result = new ArrayList<Method>();
    for (Method method : clazz.getDeclaredMethods()) {
        result.add(method);
    }
    Class<?> tmpClazz = clazz.getSuperclass();
    while (!Object.class.equals(tmpClazz)) {
        result.addAll(getAllMethods(tmpClazz));
        tmpClazz = tmpClazz.getSuperclass();
    }

    return result;
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(name, "Method name must not be null");
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName()) && (paramTypes == null || paramTypes.length == 0
                    || paramTypes[0] == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }//from w ww.j ava 2s.  co m
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.fxc.lib.pmsion.EasyPermissions.java

private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();// w  w w .j  a v a  2  s .c o  m
    }
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute method " + method.getName()
                            + " because it is non-void method and/or has input parameters.");
                }

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Log.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Log.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:com.cisco.ca.cstg.pdi.utils.Util.java

/**
 * This method validates an object and return the id 
 * @param obj//from w  ww  .j  a v a 2 s .c  o  m
 * @return id, if id not available then <blank> 
 */

public static Object nullValidationAndReturnId(Object obj) {
    if (obj != null) {
        try {
            Class<?> childClass = Class.forName(obj.getClass().getName());
            Method[] childMethod = childClass.getDeclaredMethods();

            for (Method childM : childMethod) {
                if ("getid".equalsIgnoreCase(childM.getName())) {
                    return childM.invoke(obj);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error caught while fetching the id of object.", e);
        }
    }
    return null;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Returns all 'visible' methods for the given class. Visible methods are:
 *
 * - own methods (clazz.getDeclaredMethods()) - all public and protected methods from it's inheritance hierarchy
 *
 * @param clazz the Class/*from ww w.j a v  a 2s .  c o  m*/
 * @return set of visible methods for that class
 */
public static Set<Method> getAllVisibleMethods(final Class<?> clazz) {
    Set<Method> allMethods = new HashSet<>();
    allMethods.addAll(Arrays.asList(clazz.getMethods()));
    allMethods.addAll(Arrays.asList(clazz.getDeclaredMethods()));

    for (Object obj : ClassUtils.getAllSuperclasses(clazz)) {
        Class aClass = (Class) obj;
        for (Method method : aClass.getDeclaredMethods()) {
            if (Modifier.isProtected(method.getModifiers())) {
                allMethods.add(method);
            }
        }
    }
    return allMethods;
}

From source file:com.LFPermission.lib.LFPermissions.java

private static void runAnnotatedMethods(@NonNull Object object, int requestCode) {
    Class clazz = object.getClass();
    if (isUsingAndroidAnnotations(object)) {
        clazz = clazz.getSuperclass();/* w  ww.j a  va 2  s.c o m*/
    }

    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(AfterPermissionGranted.class)) {
            // Check for annotated methods with matching request code.
            AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class);
            if (ann.value() == requestCode) {
                // Method must be void so that we can invoke it
                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException("Cannot execute method " + method.getName()
                            + " because it is non-void method and/or " + "has input parameters.");
                }

                try {
                    // Make method accessible if private
                    if (!method.isAccessible()) {
                        method.setAccessible(true);
                    }
                    method.invoke(object);
                } catch (IllegalAccessException e) {
                    Log.e(TAG, "runDefaultMethod:IllegalAccessException", e);
                } catch (InvocationTargetException e) {
                    Log.e(TAG, "runDefaultMethod:InvocationTargetException", e);
                }
            }
        }
    }
}

From source file:IntrospectionUtil.java

public static Method findMethod(Class clazz, String methodName, Class[] args, boolean checkInheritance,
        boolean strictArgs) throws NoSuchMethodException {
    if (clazz == null)
        throw new NoSuchMethodException("No class");
    if (methodName == null || methodName.trim().equals(""))
        throw new NoSuchMethodException("No method name");

    Method method = null;/* ww  w . j a v  a  2 s.c  om*/
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length && method == null; i++) {
        if (methods[i].getName().equals(methodName) && checkParams(methods[i].getParameterTypes(),
                (args == null ? new Class[] {} : args), strictArgs)) {
            method = methods[i];
        }

    }
    if (method != null) {
        return method;
    } else if (checkInheritance)
        return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs);
    else
        throw new NoSuchMethodException("No such method " + methodName + " on class " + clazz.getName());

}

From source file:com.autobizlogic.abl.util.BeanUtil.java

/**
 * Get the method with the given name from the given class, provided that it takes one argument
 * of the provided type./*from   w  w  w.ja va2s.  c  o  m*/
 * @param cls The class who should have (or inherit) the method
 * @param methodName The name of the method
 * @param argClass If provided, the type of the sole argument to the method. If null, no argument is assumed.
 * @param onlyProtectedAndHigher If true, we will ignore private methods in the superclasses.
 * @return The method if found, otherwise null.
 */
private static Method getMethodFromClass(Class<?> cls, String methodName, Class<?> argClass,
        boolean onlyProtectedAndHigher) {

    Method[] allMethods = cls.getDeclaredMethods();
    for (Method meth : allMethods) {
        if (!meth.getName().equals(methodName))
            continue;

        if (onlyProtectedAndHigher) {
            int modifiers = meth.getModifiers();
            if (Modifier.isPrivate(modifiers))
                continue;
        }

        if (argClass != null) {
            Class<?>[] paramTypes = meth.getParameterTypes();
            if (paramTypes.length != 1)
                continue;

            Class<?> genericType = getGenericType(paramTypes[0]);
            if (!genericType.isAssignableFrom(argClass))
                continue;
        }

        // Note that if we're trying to set a value to null, we obviously cannot check the
        // signature for overloading, and therefore we'll return the first method which takes
        // one parameter. I think that's not that unreasonable, but it could conceivably break
        // if someone does funny things with their bean.

        return meth;
    }

    return null;
}

From source file:com.mine.core.util.ReflectUtils.java

/**
 * ????//from w  w w .  j  a v a2  s .com
 *
 * @param clazz
 * @param methodName
 * @return
 */
public static Class<?> getMethodReturnTypeByName(Class<?> clazz, String methodName) {
    Validate.notNull(clazz, "calzz can't be null");
    Validate.notBlank(methodName);
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            return method.getReturnType();
        }
    }
    return null;
}