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

Android examples for java.lang.reflect:Method Get

Description

get Boolean Field Set Method

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    public static Method getBooleanFieldSetMethod(Class<?> clazz, Field f) {
        String fn = f.getName();//from w  w w.j a v a2  s .c o  m
        String mn = "set" + fn.substring(0, 1).toUpperCase()
                + fn.substring(1);
        if (isISStart(f.getName())) {
            mn = "set" + fn.substring(2, 3).toUpperCase() + fn.substring(3);
        }
        try {
            return clazz.getDeclaredMethod(mn, f.getType());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static boolean isISStart(String fieldName) {
        if (fieldName == null || fieldName.trim().length() == 0)
            return false;

        return fieldName.startsWith("is")
                && !Character.isLowerCase(fieldName.charAt(2));
    }
}

Related Tutorials