Returns method if available in class or superclass (recursively), otherwise returns null. - Android java.lang.reflect

Android examples for java.lang.reflect:Method

Description

Returns method if available in class or superclass (recursively), otherwise returns null.

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    /**//  www .ja va 2  s.  co  m
     * Returns method if available in class or superclass (recursively),
     * otherwise returns null.
     */
    public static Method getMethod(Class<?> cls, String methodName,
            Class<?>... parametersType) {
        Class<?> sCls = cls.getSuperclass();
        while (sCls != Object.class) {
            try {
                return sCls.getDeclaredMethod(methodName, parametersType);
            } catch (NoSuchMethodException e) {
                // Just super it again
            }
            sCls = sCls.getSuperclass();
        }
        return null;
        //        throw new RuntimeException("Method not found " + methodName);
    }
}

Related Tutorials