get Boolean Field Get Method - Android java.lang.reflect

Android examples for java.lang.reflect:Method Get

Description

get Boolean Field Get Method

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static Method getBooleanFieldGetMethod(Class<?> clazz,
            String fieldName) {/*from  ww  w . j av a  2s  . c  o m*/
        String mn = "is" + fieldName.substring(0, 1).toUpperCase()
                + fieldName.substring(1);
        if (isISStart(fieldName)) {
            mn = fieldName;
        }
        try {
            return clazz.getDeclaredMethod(mn);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static boolean isISStart(String fieldName) {
        if (fieldName == null || fieldName.trim().length() == 0)
            return false;
        //is?????is????????????? ?? isAdmin
        return fieldName.startsWith("is")
                && !Character.isLowerCase(fieldName.charAt(2));
    }
}

Related Tutorials