Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;

import android.view.View;

import java.lang.reflect.Method;

public class Main {
    static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
        Method finalMethod = null;
        while (objectClass != null && finalMethod == null) {
            for (Method method : objectClass.getDeclaredMethods()) {
                if (method.getName().equals(fieldName)) {
                    Class<?>[] paramsType = method.getParameterTypes();
                    if (paramsType.length == 0) {
                        finalMethod = method;
                        break;
                    } else if (paramsType.length == 1) {
                        if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                            finalMethod = method;
                            break;
                        }
                    }

                }
            }
            if (finalMethod == null) {
                objectClass = objectClass.getSuperclass();
            }
        }
        if (finalMethod == null) {
            throw new NoSuchFieldException(fieldName);
        }
        return finalMethod;
    }
}